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.5 2004/01/08 13:20:22 mattid Exp
00003  *
00004  * COBAIN - Communications API for EPOC Environments
00005  * Copyright (C) 2002-2004 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 #include "clientservercommon.h"
00028 
00029 CPeerListPump::CPeerListPump(TProtocol aProtocol, RCobainClientSession &aClientSession)
00030     : CActive(CActive::EPriorityStandard),
00031       iProtocol(aProtocol),
00032       iClientSession(aClientSession),
00033       iOperationStatus(ENone),
00034       iNextPeerOrdinal(-1)
00035 {
00036     CActiveScheduler::Add(this);
00037 }
00038 
00039 CPeerListPump::~CPeerListPump()
00040 {
00041     // must not delete
00042     iListener = NULL;
00043 
00044     Deque();
00045 }
00046 
00047 void CPeerListPump::DiscoverPeers(TUint aServiceClassID, MNetworkDriverListener *aListener)
00048 {
00049     iServiceClassID = aServiceClassID;
00050     iListener = aListener;
00051 
00052     iOperationStatus = ECountPeers;
00053     iClientSession.DiscoverPeers(iProtocol, aServiceClassID, &iPeerCountBuffer, iStatus);
00054     SetActive();
00055 }
00056 
00057 void CPeerListPump::AddPeerToListL()
00058 {
00059     // see if we need to allocate the peer list
00060     if( iPeerList == NULL ) {
00061         iPeerList = new (ELeave) TPeerList();
00062     }
00063 
00064     TPeerData data = iPeerDataBuffer();
00065 
00066     CNetworkPeer *peer = CNetworkPeer::NewL(iClientSession, 
00067                                             iProtocol,
00068                                             data);
00069 
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                 // start fetching peers
00090                 iNextPeerOrdinal = 0;
00091                 iOperationStatus = EFetchPeer;
00092                 iClientSession.FetchPeerData(iNextPeerOrdinal, &iPeerDataBuffer, iStatus);
00093                 SetActive();
00094             }
00095             break;
00096         case EFetchPeer:
00097             // add the fetched peer to peer list
00098             AddPeerToListL();
00099 
00100             // if peers left to fetch, get the next one
00101             iNextPeerOrdinal++;
00102             if( iNextPeerOrdinal < iNumPeers ) {
00103                 iOperationStatus = EFetchPeer;
00104                 iClientSession.FetchPeerData(iNextPeerOrdinal, &iPeerDataBuffer, iStatus);
00105                 SetActive();
00106             } else {
00107                 // all peers fetched; return the list
00108                 iListener->PeerListComplete(iPeerList);
00109 
00110                 // forget about the list; client must release it
00111                 iPeerList = NULL;
00112                 iNextPeerOrdinal = -1;
00113                 iOperationStatus = ENone;
00114             }
00115 
00116             break;
00117     }
00118 }
00119 
00120 void CPeerListPump::DoCancel()
00121 {
00122     Log::Print(_L("CPeerListPump::DoCancel()"));
00123 
00124     // return empty list
00125     TPeerList *list = new (ELeave) TPeerList();
00126     iListener->PeerListComplete(list);
00127 }

Generated on Tue Jan 13 15:47:07 2004 for CobainAPIImplementation by doxygen 1.3.5