Dynamic adjustment of threshold for rarity of download files
[amule.git] / src / DownloadQueue.h
blob9cef8116fbf774d203d77c8073e20db255632406
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 #ifndef DOWNLOADQUEUE_H
27 #define DOWNLOADQUEUE_H
29 #include "MD4Hash.h" // Needed for CMD4Hash
30 #include "ObservableQueue.h" // Needed for CObservableQueue
31 #include "GetTickCount.h" // Needed fot GetTickCount
34 #include <deque>
37 class CSharedFileList;
38 class CSearchFile;
39 class CPartFile;
40 class CUpDownClient;
41 class CServer;
42 class CMemFile;
43 class CKnownFile;
44 class CED2KLink;
45 class CED2KFileLink;
46 class CED2KServerLink;
47 class CED2KServerListLink;
48 class CPath;
50 namespace Kademlia {
51 class CUInt128;
54 /**
55 * The download queue houses all active downloads.
58 * This class should be thread-safe.
60 class CDownloadQueue : public CObservableQueue<CPartFile*>
62 public:
63 /**
64 * Constructor.
66 CDownloadQueue();
68 /**
69 * Destructor.
71 ~CDownloadQueue();
73 /** Loads met-files from the specified directory. */
74 void LoadMetFiles(const CPath& path);
76 /**
77 * Main worker function.
79 void Process();
82 /**
83 * Returns a pointer to the file with the specified hash, or NULL.
85 * @param filehash The hash to search for.
86 * @return The corresponding file or NULL.
88 CPartFile* GetFileByID(const CMD4Hash& filehash) const;
90 /**
91 * Returns the file at the specified position in the file-list, or NULL if invalid.
93 * @param A valid position in the file-list.
94 * @return A valid pointer or NULL if the index was invalid.
96 CPartFile* GetFileByIndex(unsigned int idx) const;
99 /**
100 * Returns true if the file is currently being shared or downloaded
102 bool IsFileExisting(const CMD4Hash& fileid) const;
105 * Returns true if the specified file is on the download-queue.
107 bool IsPartFile(const CKnownFile* file) const;
110 * Updates the file's download active time
112 void OnConnectionState(bool bConnected);
115 * Starts a new download based on the specified search-result.
117 * @param toadd The search-result to add.
118 * @param category The category to assign to the new download.
120 * The download will only be started if no identical files are either
121 * being downloaded or shared currently.
123 void AddSearchToDownload(CSearchFile* toadd, uint8 category);
127 * Adds an existing partfile to the queue.
129 * @param newfile The file to add.
130 * @param paused If the file should be stopped when added.
131 * @param category The category to assign to the file.
133 void AddDownload(CPartFile* newfile, bool paused, uint8 category);
137 * Removes the specified file from the queue.
139 * @param toremove A pointer to the file object to be removed.
141 void RemoveFile(CPartFile* toremove);
145 * Saves the source-seeds of every file on the queue.
147 void SaveSourceSeeds();
150 * Loads the source-seeds of every file on the queue.
152 void LoadSourceSeeds();
156 * Adds a potiential new client to the specified file.
158 * @param sender The owner of the new source.
159 * @param source The client in question, might be deleted!
161 * This function will check the new client against the already existing
162 * clients. The source will then be queued as is appropriate, or deleted
163 * if it is duplicate of an existing client.
165 void CheckAndAddSource(CPartFile* sender, CUpDownClient* source);
168 * This function adds already known source to the specified file.
170 * @param sender The owner fo the new source.
171 * @param source The client in question.
173 * This function acts like CheckAndAddSource, with the exception that no
174 * checks are made to see if the client is a duplicate. It is assumed that
175 * it is in fact a valid client.
177 void CheckAndAddKnownSource(CPartFile* sender, CUpDownClient* source);
181 * Removes the specified client completly.
183 * @param toremove The client to be removed.
184 * @param updatewindow NOT USED!
185 * @param bDoStatsUdpate Specifies if the affected files should update their statistics.
186 * @return True if the sources was found and removed.
188 * This function will remove the specified source from both normal source
189 * lists, A4AF lists and the downloadqueue-widget. The requestfile of the
190 * source is also reset.
192 bool RemoveSource(CUpDownClient* toremove, bool updatewindow = true, bool bDoStatsUpdate = true);
196 * Finds the queued client by IP and UDP-port, by looking at file-sources.
198 * @param dwIP The IP-address of the client.
199 * @param nUDPPort The UDP-port of the client.
200 * @return The matching client or NULL if none was found.
202 CUpDownClient* GetDownloadClientByIP_UDP(uint32 dwIP, uint16 nUDPPort) const;
206 * Queues the specified file for source-requestion from the connected server.
208 void SendLocalSrcRequest(CPartFile* sender);
211 * Removes the specified server from the request-queue.
213 void RemoveLocalServerRequest(CPartFile* pFile);
216 * Resets all queued server-requests.
218 void ResetLocalServerRequests();
222 * Starts the next paused file on the queue, going after priority.
223 * Also checks for categories if enabled on preferences.
225 void StartNextFile(CPartFile* oldfile);
229 * Resets the category of all files with the specified category.
231 void ResetCatParts(uint8 cat);
234 * Sets the priority of all files with the specified category.
236 void SetCatPrio(uint8 cat, uint8 newprio);
239 * Sets the status of all files with the specified category.
241 void SetCatStatus(uint8 cat, int newstatus);
244 * Returns the current number of queued files.
246 uint16 GetFileCount() const;
249 * Makes a copy of the file list.
251 void CopyFileList(std::vector<CPartFile*>& out_list) const;
254 * Returns the current number of downloading files.
256 uint16 GetDownloadingFileCount() const;
259 * Returns the current number of paused files.
261 uint16 GetPausedFileCount() const;
265 * This function is called when a DNS lookup is finished.
267 void OnHostnameResolved(uint32 ip);
271 * Adds an ed2k or magnet link to download queue.
273 bool AddLink( const wxString& link, uint8 category = 0 );
275 bool AddED2KLink( const wxString& link, uint8 category = 0 );
276 bool AddED2KLink( const CED2KLink* link, uint8 category = 0 );
277 bool AddED2KLink( const CED2KFileLink* link, uint8 category = 0 );
278 bool AddED2KLink( const CED2KServerLink* link );
279 bool AddED2KLink( const CED2KServerListLink* link );
283 * Returns the current server which is beening queried by UDP packets.
285 CServer* GetUDPServer() const;
288 * Set the server to query through UDP packest.
290 void SetUDPServer( CServer* server );
294 * Stop the source-requests from non-connected servers.
296 void StopUDPRequests();
298 /* Kad Stuff */
301 * Add a Kad source to a download
303 void KademliaSearchFile(uint32_t searchID, const Kademlia::CUInt128* pcontactID, const Kademlia::CUInt128* pkadID, uint8_t type, uint32_t ip, uint16_t tcp, uint16_t udp, uint32_t buddyip, uint16_t buddyport, uint8_t byCryptOptions);
305 CPartFile* GetFileByKadFileSearchID(uint32 id) const;
307 bool DoKademliaFileRequest();
309 void SetLastKademliaFileRequest() {lastkademliafilerequest = ::GetTickCount();}
311 uint32 GetRareFileThreshold() const { return m_rareFileThreshold; }
312 uint32 GetCommonFileThreshold() const { return m_commonFileThreshold; }
314 private:
316 * This function initializes new observers with the current contents of the queue.
318 virtual void ObserverAdded( ObserverType* o );
322 * Helper-function, sorts the filelist so that high-priority files are first.
324 void DoSortByPriority();
326 /** Checks that there is enough free spaces for temp-files at that specified path. */
327 void CheckDiskspace(const CPath& path);
330 * Stops performing UDP requests.
332 void DoStopUDPRequests();
335 void ProcessLocalRequests();
337 bool SendNextUDPPacket();
338 int GetMaxFilesPerUDPServerPacket() const;
339 bool SendGlobGetSourcesUDPPacket(CMemFile& data);
341 void AddToResolve(const CMD4Hash& fileid, const wxString& pszHostname, uint16 port, const wxString& hash, uint8 cryptoptions);
343 //! The mutex assosiated with this class, mutable to allow for const functions.
344 mutable wxMutex m_mutex;
347 uint32 m_datarate;
348 uint32 m_lastDiskCheck;
349 uint32 m_lastudpsearchtime;
350 uint32 m_lastsorttime;
351 uint32 m_lastudpstattime;
352 uint32 m_nLastED2KLinkCheck;
353 uint8 m_cRequestsSentToServer;
354 uint32 m_dwNextTCPSrcReq;
355 uint8 m_udcounter;
356 CServer* m_udpserver;
360 * Structure used to store sources with dynamic hostnames.
362 struct Hostname_Entry
364 //! The ID of the file the source provides.
365 CMD4Hash fileid;
366 //! The dynamic hostname.
367 wxString strHostname;
368 //! The user-port of the source.
369 uint16 port;
370 //! The hash of the source
371 wxString hash;
372 //! The cryptoptions for the source
373 uint8 cryptoptions;
376 std::deque<Hostname_Entry> m_toresolve;
378 typedef std::deque<CPartFile*> FileQueue;
379 FileQueue m_filelist;
381 std::list<CPartFile*> m_localServerReqQueue;
383 //! Observer used to keep track of which servers have yet to be asked for sources
384 CQueueObserver<CServer*> m_queueServers;
386 //! Observer used to keep track of which file to send UDP requests for
387 CQueueObserver<CPartFile*> m_queueFiles;
389 /* Kad Stuff */
390 uint32 lastkademliafilerequest;
392 //! Threshold for rare files, dynamically based on the sources for each.
393 uint32 m_rareFileThreshold;
395 //! Threshold for common files, dynamically based on the sources for each.
396 uint32 m_commonFileThreshold;
399 #endif // DOWNLOADQUEUE_H
400 // File_checked_for_headers