Main Page | Class Hierarchy | Data Structures | File List | Data Fields | Globals

peerlistpump.cpp

Go to the documentation of this file.
00001 /*
00002  * peerlistpump.cpp,v 1.2 2003/12/01 16:24:23 mattid Exp
00003  *
00004  * COBAIN - Communications API for EPOC Environments
00005  * Copyright (C) 2002-2003 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 "peerlistpump.h"
00026 #include "clientsession.h"
00027 
00028 CPeerListPump::CPeerListPump(TProtocol aProtocol, RCobainClientSession &aClientSession)
00029     : CActive(CActive::EPriorityStandard),
00030       iProtocol(aProtocol),
00031       iClientSession(aClientSession),
00032       iOperationStatus(ENone),
00033       iNextPeerOrdinal(-1)
00034 {
00035     CActiveScheduler::Add(this);
00036 }
00037 
00038 CPeerListPump::~CPeerListPump()
00039 {
00040     // must not delete
00041     iListener = NULL;
00042 
00043     Deque();
00044 }
00045 
00046 void CPeerListPump::DiscoverPeers(TUint aServiceClassID, MNetworkDriverListener *aListener)
00047 {
00048     iServiceClassID = aServiceClassID;
00049     iListener = aListener;
00050 
00051     iOperationStatus = ECountPeers;
00052     iClientSession.DiscoverPeers(iProtocol, aServiceClassID, &iPeerCountBuffer, iStatus);
00053     SetActive();
00054 }
00055 
00056 void CPeerListPump::AddPeerToListL()
00057 {
00058     // see if we need to allocate the peer list
00059     if( iPeerList == NULL ) {
00060         iPeerList = new (ELeave) TPeerList();
00061     }
00062 
00063     TPeerData data = iPeerDataBuffer();
00064     CNetworkPeer *peer = CNetworkPeer::NewL(iClientSession, 
00065                                             iProtocol,
00066                                             data.iDeviceName, 
00067                                             data.iDeviceAddress,
00068                                             data.iServiceID,
00069                                             data.iPort);
00070     iPeerList->Append(peer);
00071 }
00072 
00073 void CPeerListPump::RunL()
00074 {
00075     TInt ret = iStatus.Int();
00076     if( ret != KErrNone ) {
00077         iListener->PeerListRetrievalFailed(ret);
00078         return;
00079     }
00080 
00081     switch( iOperationStatus ) {
00082         case ECountPeers:
00083             iNumPeers = iPeerCountBuffer();
00084             if( iNumPeers == 0 ) {
00085                 // return empty list
00086                 TPeerList *list = new (ELeave) TPeerList();
00087                 iListener->PeerListComplete(list);
00088             } else {
00089                 iNextPeerOrdinal = 0;
00090                 iOperationStatus = EFetchPeer;
00091                 iClientSession.FetchPeerData(iNextPeerOrdinal, &iPeerDataBuffer, iStatus);
00092                 SetActive();
00093             }
00094             break;
00095         case EFetchPeer:
00096             // add the fetched peer to peer list
00097             AddPeerToListL();
00098 
00099             // if peers left to fetch, get the next one
00100             iNextPeerOrdinal++;
00101             if( iNextPeerOrdinal < iNumPeers ) {
00102                 iOperationStatus = EFetchPeer;
00103                 iClientSession.FetchPeerData(iNextPeerOrdinal, &iPeerDataBuffer, iStatus);
00104                 SetActive();
00105             } else {
00106                 // all peers fetched; return the list
00107                 iListener->PeerListComplete(iPeerList);
00108 
00109                 // forget about the list; client must release it
00110                 iPeerList = NULL;
00111                 iNextPeerOrdinal = -1;
00112                 iOperationStatus = ENone;
00113             }
00114 
00115             break;
00116     }
00117 }
00118 
00119 void CPeerListPump::DoCancel()
00120 {
00121     // return empty list
00122     TPeerList *list = new (ELeave) TPeerList();
00123     iListener->PeerListComplete(list);
00124 }

Generated on Mon Dec 8 10:26:07 2003 for CobainAPIImplementation by doxygen 1.3.5