Upstream tarball 9988
[amule.git] / src / SearchFile.cpp
blob76ea5b545bd6114815cc0485cae4f3627d600958
1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2003-2008 aMule Team ( admin@amule.org / http://www.amule.org )
5 // Copyright (c) 2002-2008 Merkur ( devs@emule-project.net / http://www.emule-project.net )
6 //
7 // Any parts of this program derived from the xMule, lMule or eMule project,
8 // or contributed by third-party developers are copyrighted by their
9 // respective authors.
11 // This program is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation; either version 2 of the License, or
14 // (at your option) any later version.
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU General Public License
22 // along with this program; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include "SearchFile.h" // Interface declarations.
28 #include <tags/FileTags.h>
30 #include "amule.h" // Needed for theApp
31 #include "CanceledFileList.h"
32 #include "MemFile.h" // Needed for CMemFile
33 #include "Preferences.h" // Needed for thePrefs
34 #include "GuiEvents.h"
35 #include "Logger.h"
36 #include "PartFile.h" // Needed for CPartFile::CanAddSource
37 #include "DownloadQueue.h" // Needed for CDownloadQueue
38 #include "KnownFileList.h" // Needed for CKnownFileList
40 CSearchFile::CSearchFile(const CMemFile& data, bool optUTF8, wxUIntPtr searchID, uint32_t serverIP, uint16_t serverPort, const wxString& directory, bool kademlia)
41 : m_parent(NULL),
42 m_showChildren(false),
43 m_searchID(searchID),
44 m_sourceCount(0),
45 m_completeSourceCount(0),
46 m_kademlia(kademlia),
47 m_downloadStatus(NEW),
48 m_directory(directory),
49 m_clientServerIP(serverIP),
50 m_clientServerPort(serverPort),
51 m_kadPublishInfo(0)
53 m_abyFileHash = data.ReadHash();
54 SetDownloadStatus();
55 m_clientID = data.ReadUInt32();
56 m_clientPort = data.ReadUInt16();
58 if (!m_clientID || !m_clientPort || !IsGoodIP(m_clientID, thePrefs::FilterLanIPs())) {
59 m_clientID = 0;
60 m_clientPort = 0;
63 uint32 tagcount = data.ReadUInt32();
64 for (unsigned int i = 0; i < tagcount; ++i) {
65 CTag tag(data, optUTF8);
66 switch (tag.GetNameID()) {
67 case FT_FILENAME:
68 SetFileName(CPath(tag.GetStr()));
69 break;
70 case FT_FILESIZE:
71 SetFileSize(tag.GetInt());
72 break;
73 case FT_FILESIZE_HI:
74 SetFileSize((((uint64)tag.GetInt()) << 32) + GetFileSize());
75 break;
76 case FT_FILERATING:
77 m_iUserRating = (tag.GetInt() & 0xF) / 3;
78 break;
79 case FT_SOURCES:
80 m_sourceCount = tag.GetInt();
81 break;
82 case FT_COMPLETE_SOURCES:
83 m_completeSourceCount = tag.GetInt();
84 break;
85 default:
86 AddTagUnique(tag);
90 if (!GetFileName().IsOk()) {
91 throw CInvalidPacket(wxT("No filename in search result"));
96 CSearchFile::CSearchFile(const CSearchFile& other)
97 : CAbstractFile(other),
98 m_parent(other.m_parent),
99 m_showChildren(other.m_showChildren),
100 m_searchID(other.m_searchID),
101 m_sourceCount(other.m_sourceCount),
102 m_completeSourceCount(other.m_completeSourceCount),
103 m_kademlia(other.m_kademlia),
104 m_downloadStatus(other.m_downloadStatus),
105 m_directory(other.m_directory),
106 m_clients(other.m_clients),
107 m_clientID(other.m_clientID),
108 m_clientPort(other.m_clientPort),
109 m_clientServerIP(other.m_clientServerIP),
110 m_clientServerPort(other.m_clientServerPort),
111 m_kadPublishInfo(other.m_kadPublishInfo)
113 for (size_t i = 0; i < other.m_children.size(); ++i) {
114 m_children.push_back(new CSearchFile(*other.m_children.at(i)));
119 CSearchFile::~CSearchFile()
121 for (size_t i = 0; i < m_children.size(); ++i) {
122 delete m_children.at(i);
127 void CSearchFile::AddClient(const ClientStruct& client)
129 for (std::list<ClientStruct>::const_iterator it = m_clients.begin(); it != m_clients.end(); ++it) {
130 if (client.m_ip == it->m_ip && client.m_port == it->m_port) return;
132 m_clients.push_back(client);
135 void CSearchFile::MergeResults(const CSearchFile& other)
137 // Sources
138 if (m_kademlia) {
139 m_sourceCount = std::max(m_sourceCount, other.m_sourceCount);
140 m_completeSourceCount = std::max(m_completeSourceCount, other.m_completeSourceCount);
141 } else {
142 m_sourceCount += other.m_sourceCount;
143 m_completeSourceCount += other.m_completeSourceCount;
146 // Publish info
147 if (m_kadPublishInfo == 0) {
148 if (other.m_kadPublishInfo != 0) {
149 m_kadPublishInfo = other.m_kadPublishInfo;
151 } else {
152 if (other.m_kadPublishInfo != 0) {
153 m_kadPublishInfo =
154 std::max(m_kadPublishInfo & 0xFF000000, other.m_kadPublishInfo & 0xFF000000) |
155 std::max(m_kadPublishInfo & 0x00FF0000, other.m_kadPublishInfo & 0x00FF0000) |
156 (((m_kadPublishInfo & 0x0000FFFF) + (other.m_kadPublishInfo & 0x0000FFFF)) >> 1);
160 // Rating
161 if (m_iUserRating != 0) {
162 if (other.m_iUserRating != 0) {
163 m_iUserRating = (m_iUserRating + other.m_iUserRating) / 2;
165 } else {
166 if (other.m_iUserRating != 0) {
167 m_iUserRating = other.m_iUserRating;
171 // copy possible available sources from new result
172 if (other.GetClientID() && other.GetClientPort()) {
173 // pre-filter sources which would be dropped by CPartFile::AddSources
174 if (CPartFile::CanAddSource(other.GetClientID(), other.GetClientPort(), other.GetClientServerIP(), other.GetClientServerPort())) {
175 CSearchFile::ClientStruct client(other.GetClientID(), other.GetClientPort(), other.GetClientServerIP(), other.GetClientServerPort());
176 AddClient(client);
182 void CSearchFile::AddChild(CSearchFile* file)
184 wxCHECK_RET(file, wxT("Not a valid child!"));
185 wxCHECK_RET(!file->GetParent(), wxT("Search-result can only be child of one other result"));
186 wxCHECK_RET(!file->HasChildren(), wxT("Result already has children, cannot become child."));
187 wxCHECK_RET(!GetParent(), wxT("A child cannot have children of its own"));
188 wxCHECK_RET(GetFileHash() == file->GetFileHash(), wxT("Mismatching child/parent hashes"));
189 wxCHECK_RET(GetFileSize() == file->GetFileSize(), wxT("Mismatching child/parent sizes"));
191 // If no children exists, then we add the current item.
192 if (GetChildren().empty()) {
193 // Merging duplicate names instead of adding a new one
194 if (file->GetFileName() == GetFileName()) {
195 AddDebugLogLineM( false, logSearch, CFormat(wxT("Merged results for '%s'")) % GetFileName());
196 MergeResults(*file);
197 delete file;
198 return;
199 } else {
200 // The first child will always be the first result we received.
201 AddDebugLogLineM(false, logSearch, CFormat(wxT("Created initial child for result '%s'")) % GetFileName());
202 m_children.push_back(new CSearchFile(*this));
203 m_children.back()->m_parent = this;
207 file->m_parent = this;
209 for (size_t i = 0; i < m_children.size(); ++i) {
210 CSearchFile* other = m_children.at(i);
211 // Merge duplicate filenames
212 if (other->GetFileName() == file->GetFileName()) {
213 other->MergeResults(*file);
214 UpdateParent();
215 delete file;
216 return;
220 // New unique child.
221 m_children.push_back(file);
222 UpdateParent();
224 if (ShowChildren()) {
225 Notify_Search_Add_Result(file);
230 void CSearchFile::UpdateParent()
232 wxCHECK_RET(!m_parent, wxT("UpdateParent called on child item"));
234 uint32_t sourceCount = 0; // ed2k: sum of all sources, kad: the max sources found
235 uint32_t completeSourceCount = 0; // ed2k: sum of all sources, kad: the max sources found
236 uint32_t differentNames = 0; // max known different names
237 uint32_t publishersKnown = 0; // max publishers known
238 uint32_t trustValue = 0; // average trust value
239 unsigned publishInfoTags = 0;
240 unsigned ratingCount = 0;
241 unsigned ratingTotal = 0;
242 CSearchResultList::const_iterator best = m_children.begin();
243 for (CSearchResultList::const_iterator it = m_children.begin(); it != m_children.end(); ++it) {
244 const CSearchFile* child = *it;
246 // Locate the most common name
247 if (child->GetSourceCount() > (*best)->GetSourceCount()) {
248 best = it;
251 // Sources
252 if (m_kademlia) {
253 sourceCount = std::max(sourceCount, child->m_sourceCount);
254 completeSourceCount = std::max(completeSourceCount, child->m_completeSourceCount);
255 } else {
256 sourceCount += child->m_sourceCount;
257 completeSourceCount += child->m_completeSourceCount;
260 // Publish info
261 if (child->GetKadPublishInfo() != 0) {
262 differentNames = std::max(differentNames, (child->GetKadPublishInfo() & 0xFF000000) >> 24);
263 publishersKnown = std::max(publishersKnown, (child->GetKadPublishInfo() & 0x00FF0000) >> 16);
264 trustValue += child->GetKadPublishInfo() & 0x0000FFFF;
265 publishInfoTags++;
268 // Rating
269 if (child->HasRating()) {
270 ratingCount++;
271 ratingTotal += child->UserRating();
274 // Available sources
275 if (child->GetClientID() && child->GetClientPort()) {
276 CSearchFile::ClientStruct client(child->GetClientID(), child->GetClientPort(), child->GetClientServerIP(), child->GetClientServerPort());
277 AddClient(client);
279 for (std::list<ClientStruct>::const_iterator cit = child->m_clients.begin(); cit != child->m_clients.end(); ++cit) {
280 AddClient(*cit);
284 m_sourceCount = sourceCount;
285 m_completeSourceCount = completeSourceCount;
287 if (publishInfoTags > 0) {
288 m_kadPublishInfo = ((differentNames & 0x000000FF) << 24) | ((publishersKnown & 0x000000FF) << 16) | ((trustValue / publishInfoTags) & 0x0000FFFF);
289 } else {
290 m_kadPublishInfo = 0;
293 if (ratingCount > 0) {
294 m_iUserRating = ratingTotal / ratingCount;
295 } else {
296 m_iUserRating = 0;
299 SetFileName((*best)->GetFileName());
302 void CSearchFile::SetDownloadStatus()
304 bool isPart = theApp->downloadqueue->GetFileByID(m_abyFileHash) != NULL;
305 bool isKnown = theApp->knownfiles->FindKnownFileByID(m_abyFileHash) != NULL;
306 bool isCanceled = theApp->canceledfiles->IsCanceledFile(m_abyFileHash);
308 if (isCanceled && isPart) {
309 m_downloadStatus = QUEUEDCANCELED;
310 } else if (isCanceled) {
311 m_downloadStatus = CANCELED;
312 } else if (isPart) {
313 m_downloadStatus = QUEUED;
314 } else if (isKnown) {
315 m_downloadStatus = DOWNLOADED;
316 } else {
317 m_downloadStatus = NEW;
319 // Update status of children too
320 for (CSearchResultList::iterator it = m_children.begin(); it != m_children.end(); it++) {
321 Notify_Search_Update_Sources(*it);
325 // File_checked_for_headers