Upstream tarball 20080929
[amule.git] / src / ThreadTasks.h
blob11018c1bba7ffb03d6ba1ad63cf8704a4a414682
1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2006-2008 Mikkel Schubert ( xaignar@amule.org / http:://www.amule.org )
5 // Copyright (c) 2003-2008 aMule Team ( admin@amule.org / http://www.amule.org )
6 // Copyright (c) 2002-2008 Merkur ( devs@emule-project.net / http://www.emule-project.net )
7 //
8 // Any parts of this program derived from the xMule, lMule or eMule project,
9 // or contributed by third-party developers are copyrighted by their
10 // respective authors.
12 // This program is free software; you can redistribute it and/or modify
13 // it under the terms of the GNU General Public License as published by
14 // the Free Software Foundation; either version 2 of the License, or
15 // (at your option) any later version.
17 // This program is distributed in the hope that it will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 // GNU General Public License for more details.
21 //
22 // You should have received a copy of the GNU General Public License
23 // along with this program; if not, write to the Free Software
24 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #ifndef TASKS_H
28 #define TASKS_H
30 #include "ThreadScheduler.h"
31 #include <common/Path.h>
33 class CKnownFile;
34 class CPartFile;
35 class CFile;
38 /**
39 * This task performs MD4 and/or AICH hashings of a file,
40 * depending on the type. For new shared files (using the
41 * first constructor, with part == NULL), both MD4 and
42 * AICH hashes are created. For incomplete partfiles
43 * (rehashed due to changed timestamps), only MD4 hashing
44 * is done. For complete partfiles, both MD4 and AICH
45 * hashing is done.
47 * For existing shared files (using the second constructor),
48 * only an AICH hash is created.
50 * @see CHashingEvent
51 * @see CAICHSyncTask
53 class CHashingTask : public CThreadTask
55 public:
56 /**
57 * Schedules a partfile or new shared file for hashing.
59 * @param path The full path, without filename.
60 * @param filename The actual filename.
61 * @param part Used to identify the owner in the event-handler (PartFiles only).
63 * CHashingEvents sent by this type of tasks have the id MULE_EVT_HASHING.
64 * @see EVT_MULE_HASHING
66 CHashingTask(const CPath& path, const CPath& filename, const CPartFile* part = NULL);
68 /**
69 * Schedules a KnownFile to have a AICH hashset created, used by CAICHSyncTask.
71 * CHashingEvents sent by this type of tasks have the id MULE_EVT_AICH_HASHING.
72 * @see EVT_MULE_AICH_HASHING
73 **/
74 CHashingTask(const CKnownFile* toAICHHash);
76 protected:
77 //! Specifies which hashes should be calculated when the task is executed.
78 enum EHashes {
79 EH_AICH = 1,
80 EH_MD4 = 2
83 //! @see CThreadTask::OnLastTask
84 virtual void OnLastTask();
86 //! @see CThreadTask::Entry
87 virtual void Entry();
89 /**
90 * Helper function for hashing the next PARTSIZE chunk of a file.
92 * @param file The file to read from.
93 * @param owner The known- (or part) file representing that file.
94 * @bool createAICH Specifies if AICH hash-sets should be created as well.
95 * @return Returns false on read-errors, true otherwise.
97 * This function will create a MD4 hash and, if specified, a AICH hashset for
98 * the next part of the file. This function makes the assumption that it wont
99 * be called for closed or EOF files.
101 bool CreateNextPartHash(CFile* file, CKnownFile* owner, EHashes toHash);
104 //! The path to the file to be hashed (shared or part), without filename.
105 CPath m_path;
106 //! The filename of the file to be hashed (filename only).
107 CPath m_filename;
108 //! Specifies which hash-types should be calculated
109 EHashes m_toHash;
110 //! If a partfile or an AICH hashing, this pointer stores it for callbacks.
111 const CKnownFile* m_owner;
116 * This task synchronizes the AICH hashlist.
118 * Shared files that are lacking a AICH-hash are scheduled for hashing.
120 class CAICHSyncTask : public CThreadTask
122 public:
123 CAICHSyncTask();
125 protected:
126 /** See CThreadTask::Entry */
127 virtual void Entry();
129 /** Converts old known2.met files to known2_64.met files. */
130 bool ConvertToKnown2ToKnown264();
135 * This task performs the final tasks on a complete download.
137 * This includes finding a usable destination filename, removing
138 * old data files and moving the part-file (potentially to a
139 * different partition).
141 class CCompletionTask : public CThreadTask
143 public:
145 * Creates a thread which will complete the given download.
147 CCompletionTask(const CPartFile* file);
149 protected:
150 /** See CThreadTask::Entry */
151 virtual void Entry();
153 /** See CThreadTask::OnExit */
154 virtual void OnExit();
156 //! The target filename.
157 CPath m_filename;
158 //! The full path to the .met-file
159 CPath m_metPath;
160 //! The category of the download.
161 uint8 m_category;
162 //! Owner of the file, used when sending completion-event.
163 const CPartFile* m_owner;
164 //! Specifies if an error occured during completion.
165 bool m_error;
166 //! The resulting full path. File may be be renamed.
167 CPath m_newName;
172 * This task preallocates space for a newly created partfile.
174 class CAllocateFileTask : public CThreadTask
176 public:
177 /** Creates a thread that will allocate disk space for the full file. */
178 CAllocateFileTask(CPartFile *file, bool pause);
180 protected:
181 /** See CThreadTask::Entry */
182 virtual void Entry();
184 /** See CThreadTask::OnExit */
185 virtual void OnExit();
187 private:
188 //! The partfile for which this task allocates space.
189 CPartFile * m_file;
191 //! Should this download start paused?
192 bool m_pause;
194 //! Result of the preallocation.
195 long m_result;
200 * This event is used to signal the completion of a hashing event.
202 * @see CHashingTask
204 class CHashingEvent : public wxEvent
206 public:
208 * @param type MULE_EVT_HASHING or MULE_EVT_AICH_HASHING.
209 * @param result
211 CHashingEvent(wxEventType type, CKnownFile* result, const CKnownFile* owner = NULL);
213 /** @see wxEvent::Clone */
214 virtual wxEvent* Clone() const;
216 /** Returns the owner (may be NULL) of the hashing result. */
217 const CKnownFile* GetOwner() const;
218 /** Returns a CKnownfile used to store the results of the hashing. */
219 CKnownFile* GetResult() const;
221 private:
222 //! The file owner.
223 const CKnownFile* m_owner;
224 //! The hashing results.
225 CKnownFile* m_result;
230 * This event is sent when a part-file has been completed.
232 class CCompletionEvent : public wxEvent
234 public:
235 /** Constructor, see getter funtion for description of parameters. */
236 CCompletionEvent(bool errorOccured, const CPartFile* owner, const CPath& fullPath);
238 /** @see wxEvent::Clone */
239 virtual wxEvent* Clone() const;
241 /** Returns true if completion failed. */
242 bool ErrorOccured() const;
244 /** Returns the owner of the file that was being completed. */
245 const CPartFile* GetOwner() const;
247 /** Returns the full path to the completed file (empty on failure). */
248 const CPath& GetFullPath() const;
249 private:
250 //! The full path to the completed file.
251 CPath m_fullPath;
253 //! The owner of the completed .part file.
254 const CPartFile* m_owner;
256 //! Specifies if completion failed.
257 bool m_error;
262 * This event is sent when preallocation of a new partfile is finished.
264 DECLARE_LOCAL_EVENT_TYPE(MULE_EVT_ALLOC_FINISHED, -1);
265 class CAllocFinishedEvent : public wxEvent
267 public:
268 /** Constructor, see getter function for description of parameters. */
269 CAllocFinishedEvent(CPartFile *file, bool pause, long result)
270 : wxEvent(-1, MULE_EVT_ALLOC_FINISHED),
271 m_file(file), m_pause(pause), m_result(result)
274 /** @see wxEvent::Clone */
275 virtual wxEvent *Clone() const;
277 /** Returns the partfile for which preallocation was requested. */
278 CPartFile *GetFile() const throw() { return m_file; }
280 /** Returns whether the partfile should start paused. */
281 bool IsPaused() const throw() { return m_pause; }
283 /** Returns the result of preallocation: true on success, false otherwise. */
284 bool Succeeded() const throw() { return m_result == 0; }
286 /** Returns the result of the preallocation. */
287 long GetResult() const throw() { return m_result; }
289 private:
290 //! The partfile for which preallocation was requested.
291 CPartFile * m_file;
293 //! Should the download start paused?
294 bool m_pause;
296 //! Result of preallocation
297 long m_result;
300 DECLARE_LOCAL_EVENT_TYPE(MULE_EVT_HASHING, -1)
301 DECLARE_LOCAL_EVENT_TYPE(MULE_EVT_AICH_HASHING, -1)
302 DECLARE_LOCAL_EVENT_TYPE(MULE_EVT_FILE_COMPLETED, -1)
305 typedef void (wxEvtHandler::*MuleHashingEventFunction)(CHashingEvent&);
306 typedef void (wxEvtHandler::*MuleCompletionEventFunction)(CCompletionEvent&);
307 typedef void (wxEvtHandler::*MuleAllocFinishedEventFunction)(CAllocFinishedEvent&);
309 //! Event-handler for completed hashings of new shared files and partfiles.
310 #define EVT_MULE_HASHING(func) \
311 DECLARE_EVENT_TABLE_ENTRY(MULE_EVT_HASHING, -1, -1, \
312 (wxObjectEventFunction) (wxEventFunction) \
313 wxStaticCastEvent(MuleHashingEventFunction, &func), (wxObject*) NULL),
315 //! Event-handler for completed hashings of files that were missing a AICH hash.
316 #define EVT_MULE_AICH_HASHING(func) \
317 DECLARE_EVENT_TABLE_ENTRY(MULE_EVT_AICH_HASHING, -1, -1, \
318 (wxObjectEventFunction) (wxEventFunction) \
319 wxStaticCastEvent(MuleHashingEventFunction, &func), (wxObject*) NULL),
321 //! Event-handler for completion of part-files.
322 #define EVT_MULE_FILE_COMPLETED(func) \
323 DECLARE_EVENT_TABLE_ENTRY(MULE_EVT_FILE_COMPLETED, -1, -1, \
324 (wxObjectEventFunction) (wxEventFunction) \
325 wxStaticCastEvent(MuleCompletionEventFunction, &func), (wxObject*) NULL),
327 //! Event-handler for partfile preallocation finished events.
328 #define EVT_MULE_ALLOC_FINISHED(func) \
329 DECLARE_EVENT_TABLE_ENTRY(MULE_EVT_ALLOC_FINISHED, -1, -1, \
330 (wxObjectEventFunction) (wxEventFunction) \
331 wxStaticCastEvent(MuleAllocFinishedEventFunction, &func), (wxObject*) NULL),
334 #endif // TASKS_H
335 // File_checked_for_headers