Improve the code by static code analysis [2/3]: Performance
[amule.git] / src / ThreadTasks.h
blob732f4206f0f0fc079e43ebc62ff610566a1ad03c
1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2006-2011 Mikkel Schubert ( xaignar@amule.org / http:://www.amule.org )
5 // Copyright (c) 2003-2011 aMule Team ( admin@amule.org / http://www.amule.org )
6 // Copyright (c) 2002-2011 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.
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 CFileAutoClose;
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 a PARTSIZE chunk of a file.
92 * @param file The file to read from.
93 * @param part The number of the part to hash.
94 * @param owner The known- (or part) file representing that file.
95 * @bool createAICH Specifies if AICH hash-sets should be created as well.
96 * @return Returns false on read-errors, true otherwise.
98 * This function will create a MD4 hash and, if specified, a AICH hashset for
99 * the next part of the file. This function makes the assumption that it wont
100 * be called for closed or EOF files.
102 bool CreateNextPartHash(CFileAutoClose& file, uint16 part, CKnownFile* owner, EHashes toHash);
105 //! The path to the file to be hashed (shared or part), without filename.
106 CPath m_path;
107 //! The filename of the file to be hashed (filename only).
108 CPath m_filename;
109 //! Specifies which hash-types should be calculated
110 EHashes m_toHash;
111 //! If a partfile or an AICH hashing, this pointer stores it for callbacks.
112 const CKnownFile* m_owner;
114 private:
115 void SetHashingProgress(uint16 part);
120 * This task synchronizes the AICH hashlist.
122 * Shared files that are lacking a AICH-hash are scheduled for hashing.
124 class CAICHSyncTask : public CThreadTask
126 public:
127 CAICHSyncTask();
129 protected:
130 /** See CThreadTask::Entry */
131 virtual void Entry();
133 /** Converts old known2.met files to known2_64.met files. */
134 bool ConvertToKnown2ToKnown264();
139 * This task performs the final tasks on a complete download.
141 * This includes finding a usable destination filename, removing
142 * old data files and moving the part-file (potentially to a
143 * different partition).
145 class CCompletionTask : public CThreadTask
147 public:
149 * Creates a thread which will complete the given download.
151 CCompletionTask(const CPartFile* file);
153 protected:
154 /** See CThreadTask::Entry */
155 virtual void Entry();
157 /** See CThreadTask::OnExit */
158 virtual void OnExit();
160 //! The target filename.
161 CPath m_filename;
162 //! The full path to the .met-file
163 CPath m_metPath;
164 //! The category of the download.
165 uint8 m_category;
166 //! Owner of the file, used when sending completion-event.
167 const CPartFile* m_owner;
168 //! Specifies if an error occured during completion.
169 bool m_error;
170 //! The resulting full path. File may be be renamed.
171 CPath m_newName;
176 * This task preallocates space for a newly created partfile.
178 class CAllocateFileTask : public CThreadTask
180 public:
181 /** Creates a thread that will allocate disk space for the full file. */
182 CAllocateFileTask(CPartFile *file, bool pause);
184 protected:
185 /** See CThreadTask::Entry */
186 virtual void Entry();
188 /** See CThreadTask::OnExit */
189 virtual void OnExit();
191 private:
192 //! The partfile for which this task allocates space.
193 CPartFile * m_file;
195 //! Should this download start paused?
196 bool m_pause;
198 //! Result of the preallocation.
199 long m_result;
204 * This event is used to signal the completion of a hashing event.
206 * @see CHashingTask
208 class CHashingEvent : public wxEvent
210 public:
212 * @param type MULE_EVT_HASHING or MULE_EVT_AICH_HASHING.
213 * @param result
215 CHashingEvent(wxEventType type, CKnownFile* result, const CKnownFile* owner = NULL);
217 /** @see wxEvent::Clone */
218 virtual wxEvent* Clone() const;
220 /** Returns the owner (may be NULL) of the hashing result. */
221 const CKnownFile* GetOwner() const;
222 /** Returns a CKnownfile used to store the results of the hashing. */
223 CKnownFile* GetResult() const;
225 private:
226 //! The file owner.
227 const CKnownFile* m_owner;
228 //! The hashing results.
229 CKnownFile* m_result;
234 * This event is sent when a part-file has been completed.
236 class CCompletionEvent : public wxEvent
238 public:
239 /** Constructor, see getter funtion for description of parameters. */
240 CCompletionEvent(bool errorOccured, const CPartFile* owner, const CPath& fullPath);
242 /** @see wxEvent::Clone */
243 virtual wxEvent* Clone() const;
245 /** Returns true if completion failed. */
246 bool ErrorOccured() const;
248 /** Returns the owner of the file that was being completed. */
249 const CPartFile* GetOwner() const;
251 /** Returns the full path to the completed file (empty on failure). */
252 const CPath& GetFullPath() const;
253 private:
254 //! The full path to the completed file.
255 CPath m_fullPath;
257 //! The owner of the completed .part file.
258 const CPartFile* m_owner;
260 //! Specifies if completion failed.
261 bool m_error;
266 * This event is sent when preallocation of a new partfile is finished.
268 DECLARE_LOCAL_EVENT_TYPE(MULE_EVT_ALLOC_FINISHED, -1);
269 class CAllocFinishedEvent : public wxEvent
271 public:
272 /** Constructor, see getter function for description of parameters. */
273 CAllocFinishedEvent(CPartFile *file, bool pause, long result)
274 : wxEvent(-1, MULE_EVT_ALLOC_FINISHED),
275 m_file(file), m_pause(pause), m_result(result)
278 /** @see wxEvent::Clone */
279 virtual wxEvent *Clone() const;
281 /** Returns the partfile for which preallocation was requested. */
282 CPartFile *GetFile() const throw() { return m_file; }
284 /** Returns whether the partfile should start paused. */
285 bool IsPaused() const throw() { return m_pause; }
287 /** Returns the result of preallocation: true on success, false otherwise. */
288 bool Succeeded() const throw() { return m_result == 0; }
290 /** Returns the result of the preallocation. */
291 long GetResult() const throw() { return m_result; }
293 private:
294 //! The partfile for which preallocation was requested.
295 CPartFile * m_file;
297 //! Should the download start paused?
298 bool m_pause;
300 //! Result of preallocation
301 long m_result;
304 DECLARE_LOCAL_EVENT_TYPE(MULE_EVT_HASHING, -1)
305 DECLARE_LOCAL_EVENT_TYPE(MULE_EVT_AICH_HASHING, -1)
306 DECLARE_LOCAL_EVENT_TYPE(MULE_EVT_FILE_COMPLETED, -1)
309 typedef void (wxEvtHandler::*MuleHashingEventFunction)(CHashingEvent&);
310 typedef void (wxEvtHandler::*MuleCompletionEventFunction)(CCompletionEvent&);
311 typedef void (wxEvtHandler::*MuleAllocFinishedEventFunction)(CAllocFinishedEvent&);
313 //! Event-handler for completed hashings of new shared files and partfiles.
314 #define EVT_MULE_HASHING(func) \
315 DECLARE_EVENT_TABLE_ENTRY(MULE_EVT_HASHING, -1, -1, \
316 (wxObjectEventFunction) (wxEventFunction) \
317 wxStaticCastEvent(MuleHashingEventFunction, &func), (wxObject*) NULL),
319 //! Event-handler for completed hashings of files that were missing a AICH hash.
320 #define EVT_MULE_AICH_HASHING(func) \
321 DECLARE_EVENT_TABLE_ENTRY(MULE_EVT_AICH_HASHING, -1, -1, \
322 (wxObjectEventFunction) (wxEventFunction) \
323 wxStaticCastEvent(MuleHashingEventFunction, &func), (wxObject*) NULL),
325 //! Event-handler for completion of part-files.
326 #define EVT_MULE_FILE_COMPLETED(func) \
327 DECLARE_EVENT_TABLE_ENTRY(MULE_EVT_FILE_COMPLETED, -1, -1, \
328 (wxObjectEventFunction) (wxEventFunction) \
329 wxStaticCastEvent(MuleCompletionEventFunction, &func), (wxObject*) NULL),
331 //! Event-handler for partfile preallocation finished events.
332 #define EVT_MULE_ALLOC_FINISHED(func) \
333 DECLARE_EVENT_TABLE_ENTRY(MULE_EVT_ALLOC_FINISHED, -1, -1, \
334 (wxObjectEventFunction) (wxEventFunction) \
335 wxStaticCastEvent(MuleAllocFinishedEventFunction, &func), (wxObject*) NULL),
338 #endif // TASKS_H
339 // File_checked_for_headers