Fixed some build warnings in release mode
[amule.git] / src / SharedFileList.cpp
blob9151eb624d88e587b204858474ecfa3c74d04747
1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2003-2011 aMule Team ( admin@amule.org / http://www.amule.org )
5 // Copyright (c) 2002-2011 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 "SharedFileList.h" // Interface declarations // Do_not_auto_remove
28 #include <protocol/Protocols.h>
29 #include <protocol/kad/Constants.h>
30 #include <tags/FileTags.h>
32 #include <wx/utils.h>
34 #include "Packet.h" // Needed for CPacket
35 #include "MemFile.h" // Needed for CMemFile
36 #include "ServerConnect.h" // Needed for CServerConnect
37 #include "KnownFileList.h" // Needed for CKnownFileList
38 #include "ThreadTasks.h" // Needed for CThreadScheduler and CHasherTask
39 #include "Preferences.h" // Needed for thePrefs
40 #include "DownloadQueue.h" // Needed for CDownloadQueue
41 #include "amule.h" // Needed for theApp
42 #include "PartFile.h" // Needed for PartFile
43 #include "Server.h" // Needed for CServer
44 #include "Statistics.h" // Needed for theStats
45 #include "Logger.h"
46 #include <common/Format.h>
47 #include <common/FileFunctions.h>
48 #include "GuiEvents.h" // Needed for Notify_*
49 #include "SHAHashSet.h" // Needed for CAICHHash
52 #include "kademlia/kademlia/Kademlia.h"
53 #include "kademlia/kademlia/Search.h"
54 #include "ClientList.h"
56 typedef std::deque<CKnownFile*> KnownFileArray;
58 ///////////////////////////////////////////////////////////////////////////////
59 // CPublishKeyword
61 class CPublishKeyword
63 public:
64 CPublishKeyword(const wxString& rstrKeyword)
66 m_strKeyword = rstrKeyword;
67 // min. keyword char is allowed to be < 3 in some cases (see also 'CSearchManager::getWords')
68 //ASSERT( rstrKeyword.GetLength() >= 3 );
69 wxASSERT( !rstrKeyword.IsEmpty() );
70 KadGetKeywordHash(rstrKeyword, &m_nKadID);
71 SetNextPublishTime(0);
72 SetPublishedCount(0);
75 const Kademlia::CUInt128& GetKadID() const { return m_nKadID; }
76 const wxString& GetKeyword() const { return m_strKeyword; }
77 int GetRefCount() const { return m_aFiles.size(); }
78 const KnownFileArray& GetReferences() const { return m_aFiles; }
80 uint32 GetNextPublishTime() const { return m_tNextPublishTime; }
81 void SetNextPublishTime(uint32 tNextPublishTime) { m_tNextPublishTime = tNextPublishTime; }
83 uint32 GetPublishedCount() const { return m_uPublishedCount; }
84 void SetPublishedCount(uint32 uPublishedCount) { m_uPublishedCount = uPublishedCount; }
85 void IncPublishedCount() { m_uPublishedCount++; }
87 bool AddRef(CKnownFile* pFile) {
88 if (std::find(m_aFiles.begin(), m_aFiles.end(), pFile) != m_aFiles.end()) {
89 wxFAIL;
90 return false;
92 m_aFiles.push_back(pFile);
93 return true;
96 int RemoveRef(CKnownFile* pFile) {
97 KnownFileArray::iterator it = std::find(m_aFiles.begin(), m_aFiles.end(), pFile);
98 if (it != m_aFiles.end()) {
99 m_aFiles.erase(it);
101 return m_aFiles.size();
104 void RemoveAllReferences() {
105 m_aFiles.clear();
108 void RotateReferences(unsigned iRotateSize) {
109 wxCHECK_RET(m_aFiles.size(), wxT("RotateReferences: Rotating empty array"));
111 unsigned shift = (iRotateSize % m_aFiles.size());
112 std::rotate(m_aFiles.begin(), m_aFiles.begin() + shift, m_aFiles.end());
115 protected:
116 wxString m_strKeyword;
117 Kademlia::CUInt128 m_nKadID;
118 uint32 m_tNextPublishTime;
119 uint32 m_uPublishedCount;
120 KnownFileArray m_aFiles;
124 ///////////////////////////////////////////////////////////////////////////////
125 // CPublishKeywordList
127 class CPublishKeywordList
129 public:
130 CPublishKeywordList();
131 ~CPublishKeywordList();
133 void AddKeyword(const wxString& keyword, CKnownFile *file);
134 void AddKeywords(CKnownFile* pFile);
135 void RemoveKeyword(const wxString& keyword, CKnownFile *file);
136 void RemoveKeywords(CKnownFile* pFile);
137 void RemoveAllKeywords();
139 void RemoveAllKeywordReferences();
140 void PurgeUnreferencedKeywords();
142 int GetCount() const { return m_lstKeywords.size(); }
144 CPublishKeyword* GetNextKeyword();
145 void ResetNextKeyword();
147 uint32 GetNextPublishTime() const { return m_tNextPublishKeywordTime; }
148 void SetNextPublishTime(uint32 tNextPublishKeywordTime) { m_tNextPublishKeywordTime = tNextPublishKeywordTime; }
150 protected:
151 // can't use a CMap - too many disadvantages in processing the 'list'
152 //CTypedPtrMap<CMapStringToPtr, CString, CPublishKeyword*> m_lstKeywords;
153 typedef std::list<CPublishKeyword*> CKeyWordList;
154 CKeyWordList m_lstKeywords;
155 CKeyWordList::iterator m_posNextKeyword;
156 uint32 m_tNextPublishKeywordTime;
158 CPublishKeyword* FindKeyword(const wxString& rstrKeyword, CKeyWordList::iterator* ppos = NULL);
161 CPublishKeywordList::CPublishKeywordList()
163 ResetNextKeyword();
164 SetNextPublishTime(0);
167 CPublishKeywordList::~CPublishKeywordList()
169 RemoveAllKeywords();
172 CPublishKeyword* CPublishKeywordList::GetNextKeyword()
174 if (m_posNextKeyword == m_lstKeywords.end()) {
175 m_posNextKeyword = m_lstKeywords.begin();
176 if (m_posNextKeyword == m_lstKeywords.end()) {
177 return NULL;
180 return *m_posNextKeyword++;
183 void CPublishKeywordList::ResetNextKeyword()
185 m_posNextKeyword = m_lstKeywords.begin();
188 CPublishKeyword* CPublishKeywordList::FindKeyword(const wxString& rstrKeyword, CKeyWordList::iterator* ppos)
190 CKeyWordList::iterator it = m_lstKeywords.begin();
191 for (; it != m_lstKeywords.end(); ++it) {
192 CPublishKeyword* pPubKw = *it;
193 if (pPubKw->GetKeyword() == rstrKeyword) {
194 if (ppos) {
195 (*ppos) = it;
198 return pPubKw;
202 return NULL;
205 void CPublishKeywordList::AddKeyword(const wxString& keyword, CKnownFile *file)
207 CPublishKeyword* pubKw = FindKeyword(keyword);
208 if (pubKw == NULL) {
209 pubKw = new CPublishKeyword(keyword);
210 m_lstKeywords.push_back(pubKw);
211 SetNextPublishTime(0);
213 pubKw->AddRef(file);
216 void CPublishKeywordList::AddKeywords(CKnownFile* pFile)
218 const Kademlia::WordList& wordlist = pFile->GetKadKeywords();
220 Kademlia::WordList::const_iterator it;
221 for (it = wordlist.begin(); it != wordlist.end(); ++it) {
222 AddKeyword(*it, pFile);
226 void CPublishKeywordList::RemoveKeyword(const wxString& keyword, CKnownFile *file)
228 CKeyWordList::iterator pos;
229 CPublishKeyword* pubKw = FindKeyword(keyword, &pos);
230 if (pubKw != NULL) {
231 if (pubKw->RemoveRef(file) == 0) {
232 if (pos == m_posNextKeyword) {
233 ++m_posNextKeyword;
235 m_lstKeywords.erase(pos);
236 delete pubKw;
237 SetNextPublishTime(0);
242 void CPublishKeywordList::RemoveKeywords(CKnownFile* pFile)
244 const Kademlia::WordList& wordlist = pFile->GetKadKeywords();
245 Kademlia::WordList::const_iterator it;
246 for (it = wordlist.begin(); it != wordlist.end(); ++it) {
247 RemoveKeyword(*it, pFile);
252 void CPublishKeywordList::RemoveAllKeywords()
254 DeleteContents(m_lstKeywords);
255 ResetNextKeyword();
256 SetNextPublishTime(0);
260 void CPublishKeywordList::RemoveAllKeywordReferences()
262 CKeyWordList::iterator it = m_lstKeywords.begin();
263 for (; it != m_lstKeywords.end(); ++it) {
264 (*it)->RemoveAllReferences();
269 void CPublishKeywordList::PurgeUnreferencedKeywords()
271 CKeyWordList::iterator it = m_lstKeywords.begin();
272 while (it != m_lstKeywords.end()) {
273 CPublishKeyword* pPubKw = *it;
274 if (pPubKw->GetRefCount() == 0) {
275 if (it == m_posNextKeyword) {
276 ++m_posNextKeyword;
278 m_lstKeywords.erase(it++);
279 delete pPubKw;
280 SetNextPublishTime(0);
281 } else {
282 ++it;
288 CSharedFileList::CSharedFileList(CKnownFileList* in_filelist){
289 filelist = in_filelist;
290 reloading = false;
291 m_lastPublishED2K = 0;
292 m_lastPublishED2KFlag = true;
293 /* Kad Stuff */
294 m_keywords = new CPublishKeywordList;
295 m_currFileSrc = 0;
296 m_currFileNotes = 0;
297 m_lastPublishKadSrc = 0;
298 m_lastPublishKadNotes = 0;
299 m_currFileKey = 0;
303 CSharedFileList::~CSharedFileList()
305 delete m_keywords;
309 void CSharedFileList::FindSharedFiles()
311 /* Abort loading if we are shutting down. */
312 if(theApp->IsOnShutDown()) {
313 return;
316 // Clear statistics.
317 theStats::ClearSharedFilesInfo();
319 // Reload shareddir.dat
320 theApp->glob_prefs->ReloadSharedFolders();
323 wxMutexLocker lock(list_mut);
324 m_Files_map.clear();
327 // All part files are automatically shared.
328 for ( uint32 i = 0; i < theApp->downloadqueue->GetFileCount(); ++i ) {
329 CPartFile* file = theApp->downloadqueue->GetFileByIndex( i );
331 if ( file->GetStatus(true) == PS_READY ) {
332 AddLogLineNS(CFormat(_("Adding file %s to shares"))
333 % file->GetFullName().GetPrintable());
334 AddFile(file);
338 // Create a list of all shared paths and weed out duplicates.
339 std::list<CPath> sharedPaths;
341 // Global incoming dir and all category incoming directories are automatically shared.
342 sharedPaths.push_back(thePrefs::GetIncomingDir());
343 for (unsigned int i = 1;i < theApp->glob_prefs->GetCatCount(); ++i) {
344 sharedPaths.push_back(theApp->glob_prefs->GetCatPath(i));
347 const thePrefs::PathList& shared = theApp->glob_prefs->shareddir_list;
348 sharedPaths.insert(sharedPaths.end(), shared.begin(), shared.end());
350 sharedPaths.sort();
351 sharedPaths.unique();
353 filelist->PrepareIndex();
354 unsigned addedFiles = 0;
355 std::list<CPath>::iterator it = sharedPaths.begin();
356 for (; it != sharedPaths.end(); ++it) {
357 addedFiles += AddFilesFromDirectory(*it);
359 filelist->ReleaseIndex();
361 if (addedFiles == 0) {
362 AddLogLineN(CFormat(wxPLURAL("Found %i known shared file", "Found %i known shared files", GetCount())) % GetCount());
364 // Make sure the AICH-hashes are up to date.
365 CThreadScheduler::AddTask(new CAICHSyncTask());
366 } else {
367 // New files, AICH thread will be run at the end of the hashing thread.
368 AddLogLineN(CFormat(wxPLURAL("Found %i known shared file, %i unknown", "Found %i known shared files, %i unknown", GetCount())) % GetCount() % addedFiles);
373 // Checks if the dir a is the same as b. If they are, then logs the message and returns true.
374 bool CheckDirectory(const wxString& a, const CPath& b)
376 if (CPath(a).IsSameDir(b)) {
377 AddLogLineC(CFormat( _("ERROR: Attempted to share %s") ) % a);
379 return true;
382 return false;
386 unsigned CSharedFileList::AddFilesFromDirectory(const CPath& directory)
388 // Do not allow these folders to be shared:
389 // - The .aMule folder
390 // - The Temp folder
391 // - The users home-dir
392 if (CheckDirectory(wxGetHomeDir(), directory)) {
393 return 0;
394 } else if (CheckDirectory(theApp->ConfigDir, directory)) {
395 return 0;
396 } else if (CheckDirectory(thePrefs::GetTempDir().GetRaw(), directory)) {
397 return 0;
400 if (!directory.DirExists()) {
401 AddLogLineNS(CFormat(_("Shared directory not found, skipping: %s"))
402 % directory.GetPrintable());
404 return 0;
407 CDirIterator::FileType searchFor = CDirIterator::FileNoHidden;
408 if (thePrefs::ShareHiddenFiles()) {
409 searchFor = CDirIterator::File;
412 unsigned knownFiles = 0;
413 unsigned addedFiles = 0;
415 CDirIterator SharedDir(directory);
417 CPath fname = SharedDir.GetFirstFile(searchFor);
418 while (fname.IsOk()) {
419 CPath fullPath = directory.JoinPaths(fname);
421 if (!fullPath.FileExists()) {
422 AddDebugLogLineN(logKnownFiles,
423 CFormat(wxT("Shared file does not exist (possibly a broken link): %s")) % fullPath);
425 fname = SharedDir.GetNextFile();
426 continue;
429 AddDebugLogLineN(logKnownFiles,
430 CFormat(wxT("Found shared file: %s")) % fullPath);
432 time_t fdate = CPath::GetModificationTime(fullPath);
433 sint64 fsize = fullPath.GetFileSize();
435 // This will also catch files with too strict permissions.
436 if ((fdate == (time_t)-1) || (fsize == wxInvalidOffset)) {
437 AddDebugLogLineN(logKnownFiles,
438 CFormat(wxT("Failed to retrive modification time or size for '%s', skipping.")) % fullPath);
440 fname = SharedDir.GetNextFile();
441 continue;
445 CKnownFile* toadd = filelist->FindKnownFile(fname, fdate, fsize);
446 if (toadd) {
447 knownFiles++;
448 if (AddFile(toadd)) {
449 AddDebugLogLineN(logKnownFiles,
450 CFormat(wxT("Added known file '%s' to shares"))
451 % fname);
453 toadd->SetFilePath(directory);
454 } else {
455 AddDebugLogLineN(logKnownFiles,
456 CFormat(wxT("File already shared, skipping: %s"))
457 % fname);
459 } else {
460 //not in knownfilelist - start adding thread to hash file
461 AddDebugLogLineN(logKnownFiles,
462 CFormat(wxT("Hashing new unknown shared file '%s'")) % fname);
464 if (CThreadScheduler::AddTask(new CHashingTask(directory, fname))) {
465 addedFiles++;
469 fname = SharedDir.GetNextFile();
472 if ((addedFiles == 0) && (knownFiles == 0)) {
473 AddLogLineNS(CFormat(_("No shareable files found in directory: %s"))
474 % directory.GetPrintable());
477 return addedFiles;
481 bool CSharedFileList::AddFile(CKnownFile* pFile)
483 wxASSERT(pFile->GetHashCount() == pFile->GetED2KPartHashCount());
485 wxMutexLocker lock(list_mut);
487 CKnownFileMap::value_type entry(pFile->GetFileHash(), pFile);
488 if (m_Files_map.insert(entry).second) {
489 /* Keywords to publish on Kad */
490 m_keywords->AddKeywords(pFile);
491 theStats::AddSharedFile(pFile->GetFileSize());
492 return true;
494 return false;
498 void CSharedFileList::SafeAddKFile(CKnownFile* toadd, bool bOnlyAdd)
500 // TODO: Check if the file is already known - only with another date
502 if (AddFile(toadd)) {
503 Notify_SharedFilesShowFile(toadd);
506 if (!bOnlyAdd && theApp->IsConnectedED2K()) {
507 // Publishing of files is not anymore handled here.
508 // Instead, the timer does it by itself.
509 m_lastPublishED2KFlag = true;
514 // removes first occurrence of 'toremove' in 'list'
515 void CSharedFileList::RemoveFile(CKnownFile* toremove){
516 Notify_SharedFilesRemoveFile(toremove);
517 wxMutexLocker lock(list_mut);
518 if (m_Files_map.erase(toremove->GetFileHash()) > 0) {
519 theStats::RemoveSharedFile(toremove->GetFileSize());
521 /* This file keywords must not be published to kad anymore */
522 m_keywords->RemoveKeywords(toremove);
526 void CSharedFileList::Reload()
528 // Madcat - Disable reloading if reloading already in progress.
529 // Kry - Fixed to let non-english language users use the 'Reload' button :P
530 // deltaHF - removed the old ugly button and changed the code to use the new small one
531 // Kry - bah, let's use a var.
532 if (!reloading) {
533 reloading = true;
534 Notify_SharedFilesRemoveAllItems();
536 /* All Kad keywords must be removed */
537 m_keywords->RemoveAllKeywordReferences();
539 /* Public identifiers must be erased as they might be invalid now */
540 m_PublicSharedDirNames.clear();
542 FindSharedFiles();
544 /* And now the unreferenced keywords must be removed also */
545 m_keywords->PurgeUnreferencedKeywords();
547 Notify_SharedFilesShowFileList();
549 reloading = false;
554 const CKnownFile *CSharedFileList::GetFileByIndex(unsigned int index) const
556 wxMutexLocker lock(list_mut);
557 if ( index >= m_Files_map.size() ) {
558 return NULL;
560 CKnownFileMap::const_iterator pos = m_Files_map.begin();
561 std::advance(pos, index);
562 return pos->second;
566 CKnownFile* CSharedFileList::GetFileByID(const CMD4Hash& filehash)
568 wxMutexLocker lock(list_mut);
569 CKnownFileMap::iterator it = m_Files_map.find(filehash);
571 if ( it != m_Files_map.end() ) {
572 return it->second;
573 } else {
574 return NULL;
578 short CSharedFileList::GetFilePriorityByID(const CMD4Hash& filehash)
580 CKnownFile* tocheck = GetFileByID(filehash);
581 if (tocheck)
582 return tocheck->GetUpPriority();
583 else
584 return -10; // file doesn't exist
588 void CSharedFileList::CopyFileList(std::vector<CKnownFile*>& out_list) const
590 wxMutexLocker lock(list_mut);
592 out_list.reserve(m_Files_map.size());
593 for (
594 CKnownFileMap::const_iterator it = m_Files_map.begin();
595 it != m_Files_map.end();
596 ++it
598 out_list.push_back(it->second);
604 void CSharedFileList::UpdateItem(CKnownFile* toupdate)
606 Notify_SharedFilesUpdateItem(toupdate);
610 void CSharedFileList::GetSharedFilesByDirectory(const wxString& directory,
611 CKnownFilePtrList& list)
613 wxMutexLocker lock(list_mut);
615 const CPath dir = CPath(directory);
616 for (CKnownFileMap::iterator pos = m_Files_map.begin();
617 pos != m_Files_map.end(); ++pos ) {
618 CKnownFile *cur_file = pos->second;
620 if (dir.IsSameDir(cur_file->GetFilePath())) {
621 continue;
624 list.push_back(cur_file);
628 /* ---------------- Network ----------------- */
630 void CSharedFileList::ClearED2KPublishInfo(){
631 CKnownFile* cur_file;
632 m_lastPublishED2KFlag = true;
633 wxMutexLocker lock(list_mut);
634 for (CKnownFileMap::iterator pos = m_Files_map.begin(); pos != m_Files_map.end(); ++pos ) {
635 cur_file = pos->second;
636 cur_file->SetPublishedED2K(false);
640 void CSharedFileList::ClearKadSourcePublishInfo()
642 wxMutexLocker lock(list_mut);
643 CKnownFile* cur_file;
644 for (CKnownFileMap::iterator pos = m_Files_map.begin(); pos != m_Files_map.end(); ++pos ) {
645 cur_file = pos->second;
646 cur_file->SetLastPublishTimeKadSrc(0,0);
650 void CSharedFileList::RepublishFile(CKnownFile* pFile)
652 CServer* server = theApp->serverconnect->GetCurrentServer();
653 if (server && (server->GetTCPFlags() & SRV_TCPFLG_COMPRESSION)) {
654 m_lastPublishED2KFlag = true;
655 pFile->SetPublishedED2K(false); // FIXME: this creates a wrong 'No' for the ed2k shared info in the listview until the file is shared again.
659 uint8 GetRealPrio(uint8 in)
661 switch(in) {
662 case 4 : return 0;
663 case 0 : return 1;
664 case 1 : return 2;
665 case 2 : return 3;
666 case 3 : return 4;
668 return 0;
671 bool SortFunc( const CKnownFile* fileA, const CKnownFile* fileB )
673 return GetRealPrio(fileA->GetUpPriority()) < GetRealPrio(fileB->GetUpPriority());
676 void CSharedFileList::SendListToServer(){
677 std::vector<CKnownFile*> SortedList;
680 wxMutexLocker lock(list_mut);
682 if (m_Files_map.empty() || !theApp->IsConnectedED2K() ) {
683 return;
686 // Getting a sorted list of the non-published files.
687 SortedList.reserve( m_Files_map.size() );
689 CKnownFileMap::iterator it = m_Files_map.begin();
690 for ( ; it != m_Files_map.end(); ++it ) {
691 if (!it->second->GetPublishedED2K()) {
692 SortedList.push_back( it->second );
697 std::sort( SortedList.begin(), SortedList.end(), SortFunc );
699 // Limits for the server.
701 CServer* server = theApp->serverconnect->GetCurrentServer();
703 uint32 limit = server ? server->GetSoftFiles() : 0;
704 if( limit == 0 || limit > 200 ) {
705 limit = 200;
708 if( (uint32)SortedList.size() < limit ) {
709 limit = SortedList.size();
710 if (limit == 0) {
711 m_lastPublishED2KFlag = false;
712 return;
716 CMemFile files;
718 // Files sent.
719 files.WriteUInt32(limit);
721 uint16 count = 0;
722 // Add to packet
723 std::vector<CKnownFile*>::iterator sorted_it = SortedList.begin();
724 for ( ; (sorted_it != SortedList.end()) && (count < limit); ++sorted_it ) {
725 CKnownFile* file = *sorted_it;
726 if (!file->IsLargeFile() || (server && server->SupportsLargeFilesTCP())) {
727 file->CreateOfferedFilePacket(&files, server, NULL);
729 file->SetPublishedED2K(true);
730 ++count;
733 wxASSERT(count == limit);
735 CPacket* packet = new CPacket(files, OP_EDONKEYPROT, OP_OFFERFILES);
736 // compress packet
737 // - this kind of data is highly compressable (N * (1 MD4 and at least 3 string meta data tags and 1 integer meta data tag))
738 // - the min. amount of data needed for one published file is ~100 bytes
739 // - this function is called once when connecting to a server and when a file becomes shareable - so, it's called rarely.
740 // - if the compressed size is still >= the original size, we send the uncompressed packet
741 // therefor we always try to compress the packet
742 if (server->GetTCPFlags() & SRV_TCPFLG_COMPRESSION){
743 packet->PackPacket();
746 theStats::AddUpOverheadServer(packet->GetPacketSize());
747 theApp->serverconnect->SendPacket(packet,true);
751 void CSharedFileList::Process()
753 Publish();
754 if( !m_lastPublishED2KFlag || ( ::GetTickCount() - m_lastPublishED2K < ED2KREPUBLISHTIME ) ) {
755 return;
757 SendListToServer();
758 m_lastPublishED2K = ::GetTickCount();
761 void CSharedFileList::Publish()
763 // Variables to save cpu.
764 unsigned int tNow = time(NULL);
765 bool IsFirewalled = theApp->IsFirewalled();
767 if( Kademlia::CKademlia::IsConnected() && ( !IsFirewalled || ( IsFirewalled && theApp->clientlist->GetBuddyStatus() == Connected)) && GetCount() && Kademlia::CKademlia::GetPublish()) {
768 //We are connected to Kad. We are either open or have a buddy. And Kad is ready to start publishing.
770 if( Kademlia::CKademlia::GetTotalStoreKey() < KADEMLIATOTALSTOREKEY) {
772 //We are not at the max simultaneous keyword publishes
773 if (tNow >= m_keywords->GetNextPublishTime()) {
775 //Enough time has passed since last keyword publish
777 //Get the next keyword which has to be (re)-published
778 CPublishKeyword* pPubKw = m_keywords->GetNextKeyword();
779 if (pPubKw) {
781 //We have the next keyword to check if it can be published
783 //Debug check to make sure things are going well.
784 wxASSERT( pPubKw->GetRefCount() != 0 );
786 if (tNow >= pPubKw->GetNextPublishTime()) {
787 //This keyword can be published.
788 Kademlia::CSearch* pSearch = Kademlia::CSearchManager::PrepareLookup(Kademlia::CSearch::STOREKEYWORD, false, pPubKw->GetKadID());
789 if (pSearch) {
790 //pSearch was created. Which means no search was already being done with this HashID.
791 //This also means that it was checked to see if network load wasn't a factor.
793 //This sets the filename into the search object so we can show it in the gui.
794 pSearch->SetFileName(pPubKw->GetKeyword());
796 //Add all file IDs which relate to the current keyword to be published
797 const KnownFileArray& aFiles = pPubKw->GetReferences();
798 uint32 count = 0;
799 for (unsigned int f = 0; f < aFiles.size(); ++f) {
801 //Only publish complete files as someone else should have the full file to publish these keywords.
802 //As a side effect, this may help reduce people finding incomplete files in the network.
803 if( !aFiles[f]->IsPartFile() ) {
804 count++;
805 pSearch->AddFileID(Kademlia::CUInt128(aFiles[f]->GetFileHash().GetHash()));
806 if( count > 150 ) {
807 //We only publish up to 150 files per keyword publish then rotate the list.
808 pPubKw->RotateReferences(f);
809 break;
814 if( count ) {
815 //Start our keyword publish
816 pPubKw->SetNextPublishTime(tNow+(KADEMLIAREPUBLISHTIMEK));
817 pPubKw->IncPublishedCount();
818 Kademlia::CSearchManager::StartSearch(pSearch);
819 } else {
820 //There were no valid files to publish with this keyword.
821 delete pSearch;
826 m_keywords->SetNextPublishTime(KADEMLIAPUBLISHTIME+tNow);
830 if( Kademlia::CKademlia::GetTotalStoreSrc() < KADEMLIATOTALSTORESRC) {
831 if(tNow >= m_lastPublishKadSrc) {
832 if(m_currFileSrc > GetCount()) {
833 m_currFileSrc = 0;
835 CKnownFile* pCurKnownFile = const_cast<CKnownFile*>(GetFileByIndex(m_currFileSrc));
836 if(pCurKnownFile) {
837 if(pCurKnownFile->PublishSrc()) {
838 Kademlia::CUInt128 kadFileID;
839 kadFileID.SetValueBE(pCurKnownFile->GetFileHash().GetHash());
840 if(Kademlia::CSearchManager::PrepareLookup(Kademlia::CSearch::STOREFILE, true, kadFileID )==NULL) {
841 pCurKnownFile->SetLastPublishTimeKadSrc(0,0);
845 m_currFileSrc++;
847 // even if we did not publish a source, reset the timer so that this list is processed
848 // only every KADEMLIAPUBLISHTIME seconds.
849 m_lastPublishKadSrc = KADEMLIAPUBLISHTIME+tNow;
853 if( Kademlia::CKademlia::GetTotalStoreNotes() < KADEMLIATOTALSTORENOTES) {
854 if(tNow >= m_lastPublishKadNotes) {
855 if(m_currFileNotes > GetCount()) {
856 m_currFileNotes = 0;
858 CKnownFile* pCurKnownFile = const_cast<CKnownFile*>(GetFileByIndex(m_currFileNotes));
859 if(pCurKnownFile) {
860 if(pCurKnownFile->PublishNotes()) {
861 Kademlia::CUInt128 kadFileID;
862 kadFileID.SetValueBE(pCurKnownFile->GetFileHash().GetHash());
863 if(Kademlia::CSearchManager::PrepareLookup(Kademlia::CSearch::STORENOTES, true, kadFileID )==NULL)
864 pCurKnownFile->SetLastPublishTimeKadNotes(0);
867 m_currFileNotes++;
869 // even if we did not publish a source, reset the timer so that this list is processed
870 // only every KADEMLIAPUBLISHTIME seconds.
871 m_lastPublishKadNotes = KADEMLIAPUBLISHTIME+tNow;
878 void CSharedFileList::AddKeywords(CKnownFile* pFile)
880 m_keywords->AddKeywords(pFile);
884 void CSharedFileList::RemoveKeywords(CKnownFile* pFile)
886 m_keywords->RemoveKeywords(pFile);
890 bool CSharedFileList::RenameFile(CKnownFile* file, const CPath& newName)
892 if (file->IsPartFile()) {
893 CPartFile* pfile = dynamic_cast<CPartFile*>(file);
895 if (file->GetStatus() != PS_COMPLETING) {
896 pfile->SetFileName(newName);
897 pfile->SavePartFile();
899 Notify_SharedFilesUpdateItem(file);
900 Notify_DownloadCtrlUpdateItem(file);
902 return true;
904 } else {
905 CPath oldPath = file->GetFilePath().JoinPaths(file->GetFileName());
906 CPath newPath = file->GetFilePath().JoinPaths(newName);
908 if (CPath::RenameFile(oldPath, newPath)) {
909 // Must create a copy of the word list because:
910 // 1) it will be reset on SetFileName()
911 // 2) we will want to edit it
912 Kademlia::WordList oldwords = file->GetKadKeywords();
913 file->SetFileName(newName);
914 theApp->knownfiles->Save();
915 UpdateItem(file);
916 RepublishFile(file);
918 const Kademlia::WordList& newwords = file->GetKadKeywords();
919 Kademlia::WordList::iterator itold;
920 Kademlia::WordList::const_iterator itnew;
921 // compare keywords in old and new names
922 for (itnew = newwords.begin(); itnew != newwords.end(); ++itnew) {
923 for (itold = oldwords.begin(); itold != oldwords.end(); ++itold) {
924 if (*itold == *itnew) {
925 break;
928 if (itold != oldwords.end()) {
929 // Remove keyword from old name which also exist in new name
930 oldwords.erase(itold);
931 } else {
932 // This is a new keyword not present in the old name
933 m_keywords->AddKeyword(*itnew, file);
936 // Remove all remaining old keywords not present in the new name
937 for (itold = oldwords.begin(); itold != oldwords.end(); ++itold) {
938 m_keywords->RemoveKeyword(*itold, file);
941 Notify_DownloadCtrlUpdateItem(file);
942 Notify_SharedFilesUpdateItem(file);
944 return true;
948 return false;
952 const CPath* CSharedFileList::GetDirForPublicSharedDirName(const wxString& strSharedDir) const
954 StringPathMap::const_iterator it = m_PublicSharedDirNames.find(strSharedDir);
956 if (it != m_PublicSharedDirNames.end()) {
957 return &(it->second);
958 } else {
959 return NULL;
964 wxString CSharedFileList::GetPublicSharedDirName(const CPath& dir)
966 // safety check: is the directory supposed to be shared after all?
967 if (!IsShared(dir)) {
968 wxFAIL;
969 return wxT("");
971 // check if the public name for the directory is cached in our Map
972 StringPathMap::const_iterator it;
973 for (it = m_PublicSharedDirNames.begin(); it != m_PublicSharedDirNames.end(); ++it) {
974 if (it->second.IsSameDir(dir)) {
975 // public name for directory was determined earlier
976 return it->first;
980 // we store the path separator (forward or back slash) for quick access
981 wxChar cPathSepa = wxFileName::GetPathSeparator();
983 // determine and cache the public name for "dir" ...
984 // We need to use the 'raw' filename, so the receiving client can recognize it.
985 wxString strDirectoryTmp = dir.GetRaw();
986 if (strDirectoryTmp.EndsWith(&cPathSepa)) {
987 strDirectoryTmp.RemoveLast();
990 wxString strPublicName;
991 int iPos;
992 // check all the subdirectories in the path for being shared
993 // the public name will consist of these concatenated
994 while ((iPos = strDirectoryTmp.Find( cPathSepa, true )) != wxNOT_FOUND) {
995 strPublicName = strDirectoryTmp.Right(strDirectoryTmp.Length() - iPos) + strPublicName;
996 strDirectoryTmp.Truncate(iPos);
997 if (!IsShared(CPath(strDirectoryTmp)))
998 break;
1000 if (!strPublicName.IsEmpty()) {
1001 // remove first path separator ???
1002 wxASSERT( strPublicName.GetChar(0) == cPathSepa );
1003 strPublicName = strPublicName.Right(strPublicName.Length() - 1);
1004 } else {
1005 // must be a rootdirectory on Windos
1006 wxASSERT( strDirectoryTmp.Length() == 2 );
1007 strPublicName = strDirectoryTmp;
1009 // we have the name, make sure it is unique by appending an index if necessary
1010 if (m_PublicSharedDirNames.find(strPublicName) != m_PublicSharedDirNames.end()) {
1011 wxString strUniquePublicName;
1012 for (iPos = 2; ; ++iPos) {
1013 strUniquePublicName = CFormat(wxT("%s_%i")) % strPublicName % iPos;
1015 if (m_PublicSharedDirNames.find(strUniquePublicName) == m_PublicSharedDirNames.end()) {
1016 AddDebugLogLineN(logClient, CFormat(wxT("Using public name '%s' for directory '%s'"))
1017 % strUniquePublicName
1018 % dir.GetPrintable());
1019 m_PublicSharedDirNames.insert(std::pair<wxString, CPath> (strUniquePublicName, dir));
1020 return strUniquePublicName;
1022 // This is from eMule and it checks if there are more than 200 shared folders with the same public name.
1023 // The condition can be true if many shared subfolders with the same name exist in folders that are not
1024 // shared. So they get the names of each shared subfolders concatenated. But those might all be the same!
1025 // It's here for safety reasons so we should not run out of memory.
1026 else if (iPos > 200) // Only 200 identical names are indexed.
1028 wxASSERT( false );
1029 return wxT("");
1032 } else {
1033 AddDebugLogLineN(logClient, CFormat(wxT("Using public name '%s' for directory '%s'")) % strPublicName % dir.GetPrintable());
1034 m_PublicSharedDirNames.insert(std::pair<wxString, CPath> (strPublicName, dir));
1035 return strPublicName;
1040 bool CSharedFileList::IsShared(const CPath& path) const
1042 if( path.IsDir(CPath::exists) ) {
1043 // check if it's a shared folder
1044 const unsigned folderCount = theApp->glob_prefs->shareddir_list.size();
1045 for (unsigned i = 0; i < folderCount; ++i) {
1046 if (path.IsSameDir(theApp->glob_prefs->shareddir_list[i])) {
1047 return true;
1051 // check if it's one of the categories folders (category 0 = incoming)
1052 for (unsigned i = 0; i < theApp->glob_prefs->GetCatCount(); ++i) {
1053 if (path.IsSameDir(theApp->glob_prefs->GetCategory(i)->path)) {
1054 return true;
1059 return false;
1063 void CSharedFileList::CheckAICHHashes(const std::list<CAICHHash>& hashes)
1065 wxMutexLocker locker(list_mut);
1067 // Now we check that all files which are in the sharedfilelist have a
1068 // corresponding hash in our list. Those how don't are queued for hashing.
1069 CKnownFileMap::iterator it = m_Files_map.begin();
1070 for (; it != m_Files_map.end(); ++it) {
1071 const CKnownFile* file = it->second;
1073 if (file->IsPartFile() == false) {
1074 CAICHHashSet* hashset = file->GetAICHHashset();
1076 if (hashset->GetStatus() == AICH_HASHSETCOMPLETE) {
1077 if (std::find(hashes.begin(), hashes.end(), hashset->GetMasterHash()) != hashes.end()) {
1078 continue;
1082 hashset->SetStatus(AICH_ERROR);
1084 CThreadScheduler::AddTask(new CHashingTask(file));
1090 // File_checked_for_headers