DynamicSearcher.cpp

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2005-2007 by Peter Dimov.
00004 
00005 This file is part of Calitko (http://www.calitko.org).
00006 
00007 Calitko is free software; you can redistribute it and/or modify
00008 it under the terms of the GNU General Public License as published by
00009 the Free Software Foundation; either version 2 of the License, or
00010 (at your option) any later version.
00011 
00012 Calitko 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
00015 GNU General Public License for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Calitko; if not, write to the Free Software
00019 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020 
00021 */
00022 
00023 #include "Qt.h"
00024 #include "DynamicSearcher.h"
00025 #include "DynamicSearch.h"
00026 #include "Imports.cpp"
00027 
00028 namespace Gnutella{
00029 namespace PacketProcessing{
00030 namespace DynamicSearching{
00031 
00032 class DynamicSearcherPrivate
00033 {
00034     REFERENCE_OBJECT (DynamicSearcherPrivate)
00035 
00036 public:
00037     typedef QList <PacketSession *>         Sessions;
00038     typedef QHash <QUuid, DynamicSearch *>  Searches;
00039 
00040     Sessions        sessions;
00041     Searches        searches;
00042     PacketProcessor *packetProcessor;
00043 
00044     DynamicSearcherPrivate() :  sessions(), searches(), packetProcessor (0) {}
00045 };
00046 
00047 } // namespace Gnutella
00048 } // namespace PacketProcessing
00049 } // namespace DynamicSearching
00050 
00051 DynamicSearcher::DynamicSearcher (PacketProcessor *packetProcessor)
00052  :  p (new DynamicSearcherPrivate)
00053 {
00054     p->packetProcessor = packetProcessor;
00055 }
00056 
00057 DynamicSearcher::~DynamicSearcher()
00058 {
00059     delete p;
00060 }
00061 
00062 void DynamicSearcher::addSession (PacketSession *session)
00063 {
00064     // Only neighboring peers will be queried using dynamic searching:
00065     if (session->nodeInfo().type == TypePeer) {
00066         p->sessions.append (session);
00067         foreach (DynamicSearch *search, p->searches) {
00068             search->addSession (session);
00069         }
00070     }
00071 }
00072 
00073 void DynamicSearcher::removeSession (PacketSession *session)
00074 {
00075     // Remove only if the session was added at all (not a peer session?):
00076     if (p->sessions.removeAll (session) > 0) {
00077         foreach (DynamicSearch *search, p->searches) {
00078             search->removeSession (session);
00079         }
00080     }
00081 }
00082 
00083 void DynamicSearcher::startSearch (const Query &query)
00084 {
00085     DynamicSearch *dynamicSearch = new DynamicSearch (query, p->sessions);
00086 
00087     connect (dynamicSearch, SIGNAL (searchStopped (const Query &)),
00088              this, SLOT (searchStopped (const Query &)));
00089 
00090     connect (dynamicSearch, SIGNAL (statusRequest (const QueryStatusRequest &)),
00091              this, SLOT (statusRequest (const QueryStatusRequest &)));
00092 
00093     p->searches.insert (query.descriptorId(), dynamicSearch);
00094     dynamicSearch->startSearch();
00095 }
00096 
00097 void DynamicSearcher::countHits (const QueryHits &queryHits)
00098 {
00099     DynamicSearch *search = p->searches.value (queryHits.descriptorId());
00100     if (search != 0)
00101         search->countHits (queryHits);
00102 }
00103 
00104 void DynamicSearcher::countHits (const QueryStatusResponse &response)
00105 {
00106     DynamicSearch *search = p->searches.value (response.descriptorId());
00107     if (search != 0)
00108         search->countHits (response);
00109 }
00110 
00111 void DynamicSearcher::stopSearch (const Query &query)
00112 {
00113     DynamicSearch *search = p->searches.value (query.descriptorId());
00114     if (search != 0)
00115         search->stopSearch();
00116 }
00117 
00118 void DynamicSearcher::searchStopped (const Query &query)
00119 {
00120     DynamicSearch *search = p->searches.take (query.descriptorId());
00121     Q_ASSERT (search != 0);
00122     search->deleteLater();
00123 }
00124 
00125 void DynamicSearcher::statusRequest (const QueryStatusRequest &request)
00126 {
00127     p->packetProcessor->sendPacket (request);
00128 }