Upstream tarball 20080603
[amule.git] / src / DownloadQueue.h
blob2e9c1f70239be0d5b3f8453a571cc53458305d85
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 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 * Returns the current number of downloading files.
251 uint16 GetDownloadingFileCount() const;
254 * Returns the current number of paused files.
256 uint16 GetPausedFileCount() const;
260 * This function is called when a DNS lookup is finished.
262 void OnHostnameResolved(uint32 ip);
266 * Adds an ed2k or magnet link to download queue.
268 bool AddLink( const wxString& link, int category = 0 );
270 bool AddED2KLink( const wxString& link, int category = 0 );
271 bool AddED2KLink( const CED2KLink* link, int category = 0 );
272 bool AddED2KLink( const CED2KFileLink* link, int category = 0 );
273 bool AddED2KLink( const CED2KServerLink* link );
274 bool AddED2KLink( const CED2KServerListLink* link );
278 * Returns the current server which is beening queried by UDP packets.
280 CServer* GetUDPServer() const;
283 * Set the server to query through UDP packest.
285 void SetUDPServer( CServer* server );
289 * Stop the source-requests from non-connected servers.
291 void StopUDPRequests();
293 /* Kad Stuff */
296 * Add a Kad source to a download
298 void KademliaSearchFile(uint32 searchID, const Kademlia::CUInt128* pcontactID, const Kademlia::CUInt128* pkadID, uint8 type, uint32 ip, uint16 tcp, uint16 udp, uint32 serverip, uint16 serverport, uint8 byCryptOptions);
300 CPartFile* GetFileByKadFileSearchID(uint32 id) const;
302 bool DoKademliaFileRequest();
304 void SetLastKademliaFileRequest() {lastkademliafilerequest = ::GetTickCount();}
306 private:
308 * This function initializes new observers with the current contents of the queue.
310 virtual void ObserverAdded( ObserverType* o );
314 * Helper-function, sorts the filelist so that high-priority files are first.
316 void DoSortByPriority();
318 /** Checks that there is enough free spaces for temp-files at that specified path. */
319 void CheckDiskspace(const CPath& path);
322 * Parses all links in the ED2KLink file and resets it.
324 void AddLinksFromFile();
327 * Stops performing UDP requests.
329 void DoStopUDPRequests();
332 void ProcessLocalRequests();
334 bool SendNextUDPPacket();
335 int GetMaxFilesPerUDPServerPacket() const;
336 bool SendGlobGetSourcesUDPPacket(CMemFile& data);
338 void AddToResolve(const CMD4Hash& fileid, const wxString& pszHostname, uint16 port, const wxString& hash, uint8 cryptoptions);
340 //! The mutex assosiated with this class, mutable to allow for const functions.
341 mutable wxMutex m_mutex;
344 uint32 m_datarate;
345 uint32 m_lastDiskCheck;
346 uint32 m_lastudpsearchtime;
347 uint32 m_lastsorttime;
348 uint32 m_lastudpstattime;
349 uint32 m_nLastED2KLinkCheck;
350 uint8 m_cRequestsSentToServer;
351 uint32 m_dwNextTCPSrcReq;
352 uint8 m_udcounter;
353 CServer* m_udpserver;
357 * Structure used to store sources with dynamic hostnames.
359 struct Hostname_Entry
361 //! The ID of the file the source provides.
362 CMD4Hash fileid;
363 //! The dynamic hostname.
364 wxString strHostname;
365 //! The user-port of the source.
366 uint16 port;
367 //! The hash of the source
368 wxString hash;
369 //! The cryptoptions for the source
370 uint8 cryptoptions;
373 std::deque<Hostname_Entry> m_toresolve;
375 typedef std::deque<CPartFile*> FileQueue;
376 FileQueue m_filelist;
378 std::list<CPartFile*> m_localServerReqQueue;
380 //! Observer used to keep track of which servers have yet to be asked for sources
381 CQueueObserver<CServer*> m_queueServers;
383 //! Observer used to keep track of which file to send UDP requests for
384 CQueueObserver<CPartFile*> m_queueFiles;
386 /* Kad Stuff */
387 uint32 lastkademliafilerequest;
391 #endif // DOWNLOADQUEUE_H
392 // File_checked_for_headers