Upstream tarball 9882
[amule.git] / src / SearchFile.cpp
blobfa4db5fc9c86e26642991a80613768b7ac187316
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 "MemFile.h" // Needed for CMemFile
31 #include "Preferences.h" // Needed for thePrefs
32 #include "GuiEvents.h"
33 #include "Logger.h"
34 #include "PartFile.h" // Needed for CPartFile::CanAddSource
36 CSearchFile::CSearchFile(const CMemFile& data, bool optUTF8, wxUIntPtr searchID, uint32_t serverIP, uint16_t serverPort, const wxString& directory, bool kademlia)
37 : m_parent(NULL),
38 m_showChildren(false),
39 m_searchID(searchID),
40 m_sourceCount(0),
41 m_completeSourceCount(0),
42 m_kademlia(kademlia),
43 m_directory(directory),
44 m_clientServerIP(serverIP),
45 m_clientServerPort(serverPort),
46 m_kadPublishInfo(0)
48 m_abyFileHash = data.ReadHash();
49 m_clientID = data.ReadUInt32();
50 m_clientPort = data.ReadUInt16();
52 if ((!m_clientID) || (!m_clientPort)) {
53 m_clientID = m_clientPort = 0;
54 } else if (!IsGoodIP(m_clientID, thePrefs::FilterLanIPs())) {
55 m_clientID = m_clientPort = 0;
59 uint32 tagcount = data.ReadUInt32();
60 for (unsigned int i = 0; i < tagcount; ++i) {
61 CTag tag(data, optUTF8);
62 switch (tag.GetNameID()) {
63 case FT_FILENAME:
64 SetFileName(CPath(tag.GetStr()));
65 break;
66 case FT_FILESIZE:
67 SetFileSize(tag.GetInt());
68 break;
69 case FT_FILESIZE_HI:
70 SetFileSize((((uint64)tag.GetInt()) << 32) + GetFileSize());
71 break;
72 case FT_FILERATING:
73 m_iUserRating = (tag.GetInt() & 0xF) / 3;
74 break;
75 case FT_SOURCES:
76 m_sourceCount = tag.GetInt();
77 break;
78 case FT_COMPLETE_SOURCES:
79 m_completeSourceCount = tag.GetInt();
80 break;
81 default:
82 AddTagUnique(tag);
86 if (!GetFileName().IsOk()) {
87 throw CInvalidPacket(wxT("No filename in search result"));
92 CSearchFile::CSearchFile(const CSearchFile& other)
93 : CAbstractFile(other),
94 m_parent(other.m_parent),
95 m_showChildren(other.m_showChildren),
96 m_searchID(other.m_searchID),
97 m_sourceCount(other.m_sourceCount),
98 m_completeSourceCount(other.m_completeSourceCount),
99 m_kademlia(other.m_kademlia),
100 m_directory(other.m_directory),
101 m_clients(other.m_clients),
102 m_clientID(other.m_clientID),
103 m_clientPort(other.m_clientPort),
104 m_clientServerIP(other.m_clientServerIP),
105 m_clientServerPort(other.m_clientServerPort),
106 m_kadPublishInfo(other.m_kadPublishInfo)
108 for (size_t i = 0; i < other.m_children.size(); ++i) {
109 m_children.push_back(new CSearchFile(*other.m_children.at(i)));
114 CSearchFile::~CSearchFile()
116 for (size_t i = 0; i < m_children.size(); ++i) {
117 delete m_children.at(i);
122 void CSearchFile::AddClient(const ClientStruct& client)
124 for (std::list<ClientStruct>::const_iterator it = m_clients.begin(); it != m_clients.end(); ++it) {
125 if (client.m_ip == it->m_ip && client.m_port == it->m_port) return;
127 m_clients.push_back(client);
130 void CSearchFile::MergeResults(const CSearchFile& other)
132 // Sources
133 if (m_kademlia) {
134 m_sourceCount = std::max(m_sourceCount, other.m_sourceCount);
135 m_completeSourceCount = std::max(m_completeSourceCount, other.m_completeSourceCount);
136 } else {
137 m_sourceCount += other.m_sourceCount;
138 m_completeSourceCount += other.m_completeSourceCount;
141 // Publish info
142 if (m_kadPublishInfo == 0) {
143 if (other.m_kadPublishInfo != 0) {
144 m_kadPublishInfo = other.m_kadPublishInfo;
146 } else {
147 if (other.m_kadPublishInfo != 0) {
148 m_kadPublishInfo =
149 std::max(m_kadPublishInfo & 0xFF000000, other.m_kadPublishInfo & 0xFF000000) |
150 std::max(m_kadPublishInfo & 0x00FF0000, other.m_kadPublishInfo & 0x00FF0000) |
151 (((m_kadPublishInfo & 0x0000FFFF) + (other.m_kadPublishInfo & 0x0000FFFF)) >> 1);
155 // Rating
156 if (m_iUserRating != 0) {
157 if (other.m_iUserRating != 0) {
158 m_iUserRating = (m_iUserRating + other.m_iUserRating) / 2;
160 } else {
161 if (other.m_iUserRating != 0) {
162 m_iUserRating = other.m_iUserRating;
166 // copy possible available sources from new result
167 if (other.GetClientID() && other.GetClientPort()) {
168 // pre-filter sources which would be dropped by CPartFile::AddSources
169 if (CPartFile::CanAddSource(other.GetClientID(), other.GetClientPort(), other.GetClientServerIP(), other.GetClientServerPort())) {
170 CSearchFile::ClientStruct client(other.GetClientID(), other.GetClientPort(), other.GetClientServerIP(), other.GetClientServerPort());
171 AddClient(client);
177 void CSearchFile::AddChild(CSearchFile* file)
179 wxCHECK_RET(file, wxT("Not a valid child!"));
180 wxCHECK_RET(!file->GetParent(), wxT("Search-result can only be child of one other result"));
181 wxCHECK_RET(!file->HasChildren(), wxT("Result already has children, cannot become child."));
182 wxCHECK_RET(!GetParent(), wxT("A child cannot have children of its own"));
183 wxCHECK_RET(GetFileHash() == file->GetFileHash(), wxT("Mismatching child/parent hashes"));
184 wxCHECK_RET(GetFileSize() == file->GetFileSize(), wxT("Mismatching child/parent sizes"));
186 // If no children exists, then we add the current item.
187 if (GetChildren().empty()) {
188 // Merging duplicate names instead of adding a new one
189 if (file->GetFileName() == GetFileName()) {
190 AddDebugLogLineM( false, logSearch, CFormat(wxT("Merged results for '%s'")) % GetFileName());
191 MergeResults(*file);
192 delete file;
193 return;
194 } else {
195 // The first child will always be the first result we received.
196 AddDebugLogLineM(false, logSearch, CFormat(wxT("Created initial child for result '%s'")) % GetFileName());
197 m_children.push_back(new CSearchFile(*this));
198 m_children.back()->m_parent = this;
202 file->m_parent = this;
204 for (size_t i = 0; i < m_children.size(); ++i) {
205 CSearchFile* other = m_children.at(i);
206 // Merge duplicate filenames
207 if (other->GetFileName() == file->GetFileName()) {
208 other->MergeResults(*file);
209 UpdateParent();
210 delete file;
211 return;
215 // New unique child.
216 m_children.push_back(file);
217 UpdateParent();
219 if (ShowChildren()) {
220 Notify_Search_Add_Result(file);
225 void CSearchFile::UpdateParent()
227 wxCHECK_RET(!m_parent, wxT("UpdateParent called on child item"));
229 uint32_t sourceCount = 0; // ed2k: sum of all sources, kad: the max sources found
230 uint32_t completeSourceCount = 0; // ed2k: sum of all sources, kad: the max sources found
231 uint32_t differentNames = 0; // max known different names
232 uint32_t publishersKnown = 0; // max publishers known
233 uint32_t trustValue = 0; // average trust value
234 unsigned publishInfoTags = 0;
235 unsigned ratingCount = 0;
236 unsigned ratingTotal = 0;
237 CSearchResultList::const_iterator best = m_children.begin();
238 for (CSearchResultList::const_iterator it = m_children.begin(); it != m_children.end(); ++it) {
239 const CSearchFile* child = *it;
241 // Locate the most common name
242 if (child->GetSourceCount() > (*best)->GetSourceCount()) {
243 best = it;
246 // Sources
247 if (m_kademlia) {
248 sourceCount = std::max(sourceCount, child->m_sourceCount);
249 completeSourceCount = std::max(completeSourceCount, child->m_completeSourceCount);
250 } else {
251 sourceCount += child->m_sourceCount;
252 completeSourceCount += child->m_completeSourceCount;
255 // Publish info
256 if (child->GetKadPublishInfo() != 0) {
257 differentNames = std::max(differentNames, (child->GetKadPublishInfo() & 0xFF000000) >> 24);
258 publishersKnown = std::max(publishersKnown, (child->GetKadPublishInfo() & 0x00FF0000) >> 16);
259 trustValue += child->GetKadPublishInfo() & 0x0000FFFF;
260 publishInfoTags++;
263 // Rating
264 if (child->HasRating()) {
265 ratingCount++;
266 ratingTotal += child->UserRating();
269 // Available sources
270 if (child->GetClientID() && child->GetClientPort()) {
271 CSearchFile::ClientStruct client(child->GetClientID(), child->GetClientPort(), child->GetClientServerIP(), child->GetClientServerPort());
272 AddClient(client);
274 for (std::list<ClientStruct>::const_iterator cit = child->m_clients.begin(); cit != child->m_clients.end(); ++cit) {
275 AddClient(*cit);
279 m_sourceCount = sourceCount;
280 m_completeSourceCount = completeSourceCount;
282 if (publishInfoTags > 0) {
283 m_kadPublishInfo = ((differentNames & 0x000000FF) << 24) | ((publishersKnown & 0x000000FF) << 16) | ((trustValue / publishInfoTags) & 0x0000FFFF);
284 } else {
285 m_kadPublishInfo = 0;
288 if (ratingCount > 0) {
289 m_iUserRating = ratingTotal / ratingCount;
290 } else {
291 m_iUserRating = 0;
294 SetFileName((*best)->GetFileName());
296 // File_checked_for_headers