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 "LocalSearch.h" 00025 #include "FileInfo.h" 00026 #include "FileIndexer.h" 00027 #include "Gnutella/LocalPeer.h" 00028 #include "Gnutella/Packets/Query.h" 00029 #include "Gnutella/Packets/QueryHits.h" 00030 #include "Gnutella/PacketProcessing/PacketProcessor.h" 00031 00032 using Gnutella::LocalPeer; 00033 using Gnutella::Searching::LocalSearch; 00034 using Gnutella::Searching::FileIndexer; 00035 using Gnutella::Searching::FileInfo; 00036 using Gnutella::Packets::Packet; 00037 using Gnutella::Packets::Query; 00038 using Gnutella::Packets::QueryHits; 00039 using Gnutella::PacketProcessing::PacketProcessor; 00040 //using Gnutella::PacketProcessing::QueryRoutingFilter; 00041 00042 // \todo use the port from the LocalPeer. 00043 #define INCOMMING_CONNECTION_PORT 4404 00044 #define SPEED 80 00045 00046 LocalSearch::LocalSearch (LocalPeer *localPeer, PacketProcessor *packetProcessor) 00047 : p() 00048 { 00049 p.packetProcessor = packetProcessor; 00050 p.localPeer = localPeer; 00051 p.fileIndexer = 0; 00052 } 00053 00054 LocalSearch::~LocalSearch() 00055 { 00056 delete p.fileIndexer; 00057 } 00058 00059 void LocalSearch::performSearch (const ConnectionId, const Packet &packet) const 00060 { 00061 const Query &query = Query::castFrom (packet); 00062 QList<FileInfo*> foundMatches; 00063 if (p.fileIndexer) 00064 foundMatches = p.fileIndexer->match (query.searchCriteria()); 00065 00066 qDebug() << query.searchCriteria(); 00067 00068 foreach (FileInfo* fi, foundMatches) { 00069 qDebug() << fi->fileName(); 00070 } 00071 00072 if (!foundMatches.isEmpty()) { 00073 QueryHits *queryHits = generateQueryHits (query, foundMatches); // \todo check packet size 00074 00075 // \todo p.packetProcessor->sendPacket (connId, *queryHits); 00076 delete queryHits; 00077 } 00078 } 00079 00080 QueryHits* LocalSearch::generateQueryHits (const Query &query, QList<FileInfo*> foundMatches) const 00081 { 00082 QueryHits *queryHits = new QueryHits (); 00083 00084 queryHits->setDescriptorId (query.descriptorId()); 00085 queryHits->setTtl (query.hops()+1); 00086 queryHits->setHops (0); 00087 00088 queryHits->setPort (static_cast <quint16> (INCOMMING_CONNECTION_PORT)); 00089 queryHits->setIpAddress (p.localPeer->serverIpAddress()); 00090 queryHits->setSpeed (static_cast <quint32> (SPEED)); 00091 00092 foreach (FileInfo* fileInfo, foundMatches) { 00093 QueryHits::Result result; 00094 result.fileIndex = 0; // \todo keep an index in fileInfo?? 00095 result.fileSize = fileInfo->size(); 00096 result.fullFileName = QFileInfo (fileInfo->fileName()).fileName(); 00097 // \todo initialize result.resultData 00098 00099 queryHits->appendResult (result); 00100 } 00101 00102 //queryHits->setQueryHitsData (QByteArray()); 00103 queryHits->setServentId (QUuid (QString ("Dirapet"))); 00104 00105 return queryHits; 00106 }