2 // This file is part of the aMule Project.
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 Merkur ( devs@emule-project.net / http://www.emule-project.net )
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
30 #include "ThreadScheduler.h"
31 #include <common/Path.h>
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
47 * For existing shared files (using the second constructor),
48 * only an AICH hash is created.
53 class CHashingTask
: public CThreadTask
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
);
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
74 CHashingTask(const CKnownFile
* toAICHHash
);
77 //! Specifies which hashes should be calculated when the task is executed.
83 //! @see CThreadTask::OnLastTask
84 virtual void OnLastTask();
86 //! @see CThreadTask::Entry
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.
106 //! The filename of the file to be hashed (filename only).
108 //! Specifies which hash-types should be calculated
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
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
145 * Creates a thread which will complete the given download.
147 CCompletionTask(const CPartFile
* file
);
150 /** See CThreadTask::Entry */
151 virtual void Entry();
153 /** See CThreadTask::OnExit */
154 virtual void OnExit();
156 //! The target filename.
158 //! The full path to the .met-file
160 //! The category of the download.
162 //! Owner of the file, used when sending completion-event.
163 const CPartFile
* m_owner
;
164 //! Specifies if an error occured during completion.
166 //! The resulting full path. File may be be renamed.
173 * This event is used to signal the completion of a hashing event.
177 class CHashingEvent
: public wxEvent
181 * @param type MULE_EVT_HASHING or MULE_EVT_AICH_HASHING.
184 CHashingEvent(wxEventType type
, CKnownFile
* result
, const CKnownFile
* owner
= NULL
);
186 /** @see wxEvent::Clone */
187 virtual wxEvent
* Clone() const;
189 /** Returns the owner (may be NULL) of the hashing result. */
190 const CKnownFile
* GetOwner() const;
191 /** Returns a CKnownfile used to store the results of the hashing. */
192 CKnownFile
* GetResult() const;
196 const CKnownFile
* m_owner
;
197 //! The hashing results.
198 CKnownFile
* m_result
;
203 * This event is sent when a part-file has been completed.
205 class CCompletionEvent
: public wxEvent
208 /** Constructor, see getter funtion for description of parameters. */
209 CCompletionEvent(bool errorOccured
, const CPartFile
* owner
, const CPath
& fullPath
);
211 /** @see wxEvent::Clone */
212 virtual wxEvent
* Clone() const;
214 /** Returns true if completion failed. */
215 bool ErrorOccured() const;
217 /** Returns the owner of the file that was being completed. */
218 const CPartFile
* GetOwner() const;
220 /** Returns the full path to the completed file (empty on failure). */
221 const CPath
& GetFullPath() const;
223 //! The full path to the completed file.
226 //! The owner of the completed .part file.
227 const CPartFile
* m_owner
;
229 //! Specifies if completion failed.
234 DECLARE_LOCAL_EVENT_TYPE(MULE_EVT_HASHING
, -1)
235 DECLARE_LOCAL_EVENT_TYPE(MULE_EVT_AICH_HASHING
, -1)
236 DECLARE_LOCAL_EVENT_TYPE(MULE_EVT_FILE_COMPLETED
, -1)
239 typedef void (wxEvtHandler::*MuleHashingEventFunction
)(CHashingEvent
&);
240 typedef void (wxEvtHandler::*MuleCompletionEventFunction
)(CCompletionEvent
&);
242 //! Event-handler for completed hashings of new shared files and partfiles.
243 #define EVT_MULE_HASHING(func) \
244 DECLARE_EVENT_TABLE_ENTRY(MULE_EVT_HASHING, -1, -1, \
245 (wxObjectEventFunction) (wxEventFunction) \
246 wxStaticCastEvent(MuleHashingEventFunction, &func), (wxObject*) NULL),
248 //! Event-handler for completed hashings of files that were missing a AICH hash.
249 #define EVT_MULE_AICH_HASHING(func) \
250 DECLARE_EVENT_TABLE_ENTRY(MULE_EVT_AICH_HASHING, -1, -1, \
251 (wxObjectEventFunction) (wxEventFunction) \
252 wxStaticCastEvent(MuleHashingEventFunction, &func), (wxObject*) NULL),
254 //! Event-handler for completion of part-files.
255 #define EVT_MULE_FILE_COMPLETED(func) \
256 DECLARE_EVENT_TABLE_ENTRY(MULE_EVT_FILE_COMPLETED, -1, -1, \
257 (wxObjectEventFunction) (wxEventFunction) \
258 wxStaticCastEvent(MuleCompletionEventFunction, &func), (wxObject*) NULL),
262 // File_checked_for_headers