Searcher.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 "Searcher.h"
00025 #include "UIs/Searching/SearchModel.h"
00026 #include "Gnutella/LocalPeer.h"
00027 #include "Gnutella/Packets/QueryHits.h"
00028 #include "Gnutella/Packets/Query.h"
00029 #include "Gnutella/Packets/VendorMessages/QueryStatusRequest.h"
00030 #include "Gnutella/Packets/VendorMessages/QueryStatusResponse.h"
00031 #include "Gnutella/PacketProcessing/PacketProcessor.h"
00032 
00033 using Gnutella::LocalPeer;
00034 using Gnutella::Packets::QueryHits;
00035 using Gnutella::Packets::VendorMessages::QueryStatusRequest;
00036 using Gnutella::Packets::VendorMessages::QueryStatusResponse;
00037 using Gnutella::Searching::Searcher;
00038 using Gnutella::PacketProcessing::PacketProcessor;
00039 using UIs::Searching::SearchModel;
00040 using UIs::Searching::SearchResult;
00041 
00042 class Gnutella::Searching::Searcher::SearcherPrivate {
00043 public:
00044     LocalPeer                       *localPeer;
00045     PacketProcessor                 *packetProcessor;
00046     QHash <QUuid, SearchModel*>     searches;
00047 
00048     SearcherPrivate() : localPeer (0), packetProcessor (0), searches() {}
00049 
00050 private:
00051     REFERENCE_OBJECT (SearcherPrivate)
00052 };
00053 
00054 Searcher::Searcher (LocalPeer *localPeer)
00055  :  p()
00056 {
00057     p = new SearcherPrivate;
00058     p->localPeer = localPeer;
00059     p->packetProcessor = localPeer->packetProcessor();
00060 }
00061 
00062 Searcher::~Searcher()
00063 {
00064     delete p;
00065 }
00066 
00067 void Searcher::startSearch (SearchModel *searchModel, const QString &searchString)
00068 {
00069     Gnutella::Packets::Query  query(searchString);
00070     // We will use leaf-guidance for our own searches:
00071     query.setMinimumSpeed (0x9000); // bit 15 and bit 12 set
00072 
00073     p->searches.insert(query.descriptorId(), searchModel);
00074     p->packetProcessor->sendPacket (query);
00075 }
00076 
00077 void Searcher::stopSearch (SearchModel *searchModel)
00078 {
00079     QUuid descriptorId = p->searches.key (searchModel);
00080     stopSearch (descriptorId);
00081 }
00082 
00083 void Searcher::stopSearch (const QUuid &descriptorId)
00084 {
00085     p->searches.remove (descriptorId);
00086 
00087     QueryStatusResponse response (0xFFFF); // \todo Don't use magic values!
00088     response.setDescriptorId (descriptorId);
00089     response.setTtl (0); // \todo any trouble with the ttl == 0?
00090     p->packetProcessor->sendPacket (response);
00091 }
00092 
00093 void Searcher::processQueryHits (const QueryHits &queryHits)
00094 {
00095     SearchModel *searchModel = p->searches.value (queryHits.descriptorId());
00096     if (searchModel == 0)
00097         return; // The QueryHits were not for us!
00098 //  qDebug() << queryHits.numberOfHits();
00099     for (int i=0; i<queryHits.numberOfHits(); i++) {
00100         QueryHits::Result result = queryHits.result (i);
00101         searchModel->addItem (new SearchResult (result.fileName(), result.fileExtension(), result.sizeOfFile(), queryHits.speed(), queryHits.ipAddress().toString(), queryHits.serventId()));
00102     }
00103 }
00104 
00105 void Searcher::processQueryStatusRequest (const QueryStatusRequest &request)
00106 {
00107     SearchModel *searchModel = p->searches.value (request.descriptorId());
00108     if (searchModel == 0)
00109         return; // The QueryStatusRequest was not for us!
00110 
00111     QueryStatusResponse response (searchModel->rowCount());
00112     response.setDescriptorId (request.descriptorId());
00113     p->packetProcessor->sendPacket (response);
00114 }