00001 /* 00002 * ConnectionListener.cpp,v 1.1 2003/12/08 07:46:56 mattid Exp 00003 * 00004 * COBAIN - Communications API for EPOC Environments 00005 * Copyright (C) 2002 Matti Dahlbom, Matti Kokkola 00006 * 00007 * This library is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2.1 of the License, or (at your option) any later version. 00011 * 00012 * This library is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with this library; if not, write to the Free Software 00019 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00020 * 00021 * Contact: Matti Dahlbom <matti@777-team.org> 00022 * Matti Kokkola <matti.kokkola@iki.fi> 00023 */ 00024 00025 #include "ConnectionListener.h" 00026 #include "clientsession.h" 00027 00028 // 00029 // the listen process is as follows: 00030 // 00031 // 1. user calls driver->Listen() 00032 // 2. driver launches con listener AO 00033 // 3. AO waits asynchronously for notification about available 00034 // incoming connection 00035 // 4. when there is a new inbound connection, the server completes the 00036 // existing call with the id of the new connection 00037 // 00038 00039 CConnectionListener::CConnectionListener(RCobainClientSession &aClientSession, 00040 MNetworkDriver *aDriver) 00041 : CActive(CActive::EPriorityStandard), 00042 iClientSession(aClientSession), 00043 iDriver(aDriver) 00044 { 00045 CActiveScheduler::Add(this); 00046 } 00047 00048 CConnectionListener::~CConnectionListener() 00049 { 00050 CCobainLayer::Log(_L("~CConnectionListener()")); 00051 Cancel(); 00052 Deque(); 00053 CCobainLayer::Log(_L("~CConnectionListener() exiting..")); 00054 } 00055 00056 // actual call to start listening for a connection 00057 void CConnectionListener::DoListen() 00058 { 00059 // asynchronous call that is completed when there is a new 00060 // inbound connection available on the server 00061 iClientSession.GetIncomingSocketL(iStatus); 00062 00063 // start waiting 00064 SetActive(); 00065 } 00066 00067 void CConnectionListener::Listen(MConnectionListener *aListener) 00068 { 00069 CCobainLayer::Log(_L("CConnectionListener::Listen()")); 00070 00071 // register application callback 00072 iConnectionListener = aListener; 00073 00074 DoListen(); 00075 } 00076 00077 void CConnectionListener::RunL() 00078 { 00079 TBuf<128> logbuf; 00080 00081 CCobainLayer::Log(_L("CConnectionListener::RunL()")); 00082 00083 // get the socket id from the request status of the completed call 00084 TInt id = iStatus.Int(); 00085 00086 // check for error 00087 if( id < 0 ) { 00088 logbuf.Format(_L("CConnectionListener::RunL(): error (%d)!"), id); 00089 CCobainLayer::Log(logbuf); 00090 00091 //##TODO## need to do something 00092 Cancel(); 00093 return; 00094 } 00095 00096 logbuf.Format(_L("sock id: %d"), id); 00097 CCobainLayer::Log(logbuf); 00098 00099 // construct a socket object and return it 00100 RCobainSocket *socket = new (ELeave) RCobainSocket(iClientSession, id); 00101 00102 // call application callback 00103 iConnectionListener->Accept(socket, iDriver); 00104 00105 // start listening for the next connection 00106 DoListen(); 00107 } 00108 00109 void CConnectionListener::DoCancel() 00110 { 00111 CCobainLayer::Log(_L("CConnectionListener::DoCancel()")); 00112 00113 // call server to stop listening 00114 iClientSession.StopListening(EBluetooth, 0); 00115 }
1.3.5