Searcher.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
00071 query.setMinimumSpeed (0x9000);
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);
00088 response.setDescriptorId (descriptorId);
00089 response.setTtl (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;
00098
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;
00110
00111 QueryStatusResponse response (searchModel->rowCount());
00112 response.setDescriptorId (request.descriptorId());
00113 p->packetProcessor->sendPacket (response);
00114 }