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. 11// 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. 16// 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 25// 26 27#ifndef TASKS_H 28#define TASKS_H 29 30#include "ThreadScheduler.h" 31#include <common/Path.h> 32 33class CKnownFile; 34class CPartFile; 35class CFileAutoClose; 36 37 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. 46 * 47 * For existing shared files (using the second constructor), 48 * only an AICH hash is created. 49 * 50 * @see CHashingEvent 51 * @see CAICHSyncTask 52 */ 53class CHashingTask : public CThreadTask 54{ 55public: 56 /** 57 * Schedules a partfile or new shared file for hashing. 58 * 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). 62 * 63 * CHashingEvents sent by this type of tasks have the id MULE_EVT_HASHING. 64 * @see EVT_MULE_HASHING 65 */ 66 CHashingTask(const CPath& path, const CPath& filename, const CPartFile* part = NULL); 67 68 /** 69 * Schedules a KnownFile to have a AICH hashset created, used by CAICHSyncTask. 70 * 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); 75 76protected: 77 //! Specifies which hashes should be calculated when the task is executed. 78 enum EHashes { 79 EH_AICH = 1, 80 EH_MD4 = 2 81 }; 82 83 //! @see CThreadTask::OnLastTask 84 virtual void OnLastTask(); 85 86 //! @see CThreadTask::Entry 87 virtual void Entry(); 88 89 /** 90 * Helper function for hashing a PARTSIZE chunk of a file. 91 * 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. 97 * 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. 101 */ 102 bool CreateNextPartHash(CFileAutoClose& file, uint16 part, CKnownFile* owner, EHashes toHash); 103 104 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; 113}; 114 115 116/** 117 * This task synchronizes the AICH hashlist. 118 * 119 * Shared files that are lacking a AICH-hash are scheduled for hashing. 120 */ 121class CAICHSyncTask : public CThreadTask 122{ 123public: 124 CAICHSyncTask(); 125 126protected: 127 /** See CThreadTask::Entry */ 128 virtual void Entry(); 129 130 /** Converts old known2.met files to known2_64.met files. */ 131 bool ConvertToKnown2ToKnown264(); 132}; 133 134 135/** 136 * This task performs the final tasks on a complete download. 137 * 138 * This includes finding a usable destination filename, removing 139 * old data files and moving the part-file (potentially to a 140 * different partition). 141 **/ 142class CCompletionTask : public CThreadTask 143{ 144public: 145 /** 146 * Creates a thread which will complete the given download. 147 */ 148 CCompletionTask(const CPartFile* file); 149 150protected: 151 /** See CThreadTask::Entry */ 152 virtual void Entry(); 153 154 /** See CThreadTask::OnExit */ 155 virtual void OnExit(); 156 157 //! The target filename. 158 CPath m_filename; 159 //! The full path to the .met-file 160 CPath m_metPath; 161 //! The category of the download. 162 uint8 m_category; 163 //! Owner of the file, used when sending completion-event. 164 const CPartFile* m_owner; 165 //! Specifies if an error occured during completion. 166 bool m_error; 167 //! The resulting full path. File may be be renamed. 168 CPath m_newName; 169}; 170 171 172/** 173 * This task preallocates space for a newly created partfile. 174 */ 175class CAllocateFileTask : public CThreadTask 176{ 177 public: 178 /** Creates a thread that will allocate disk space for the full file. */ 179 CAllocateFileTask(CPartFile *file, bool pause); 180 181 protected: 182 /** See CThreadTask::Entry */ 183 virtual void Entry(); 184 185 /** See CThreadTask::OnExit */ 186 virtual void OnExit(); 187 188 private: 189 //! The partfile for which this task allocates space. 190 CPartFile * m_file; 191 192 //! Should this download start paused? 193 bool m_pause; 194 195 //! Result of the preallocation. 196 long m_result; 197}; 198 199 200/** 201 * This event is used to signal the completion of a hashing event. 202 * 203 * @see CHashingTask 204 */ 205class CHashingEvent : public wxEvent 206{ 207public: 208 /** 209 * @param type MULE_EVT_HASHING or MULE_EVT_AICH_HASHING. 210 * @param result 211 */ 212 CHashingEvent(wxEventType type, CKnownFile* result, const CKnownFile* owner = NULL); 213 214 /** @see wxEvent::Clone */ 215 virtual wxEvent* Clone() const; 216 217 /** Returns the owner (may be NULL) of the hashing result. */ 218 const CKnownFile* GetOwner() const; 219 /** Returns a CKnownfile used to store the results of the hashing. */ 220 CKnownFile* GetResult() const; 221 222private: 223 //! The file owner. 224 const CKnownFile* m_owner; 225 //! The hashing results. 226 CKnownFile* m_result; 227}; 228 229 230/** 231 * This event is sent when a part-file has been completed. 232 */ 233class CCompletionEvent : public wxEvent 234{ 235public: 236 /** Constructor, see getter funtion for description of parameters. */ 237 CCompletionEvent(bool errorOccured, const CPartFile* owner, const CPath& fullPath); 238 239 /** @see wxEvent::Clone */ 240 virtual wxEvent* Clone() const; 241 242 /** Returns true if completion failed. */ 243 bool ErrorOccured() const; 244 245 /** Returns the owner of the file that was being completed. */ 246 const CPartFile* GetOwner() const; 247 248 /** Returns the full path to the completed file (empty on failure). */ 249 const CPath& GetFullPath() const; 250private: 251 //! The full path to the completed file. 252 CPath m_fullPath; 253 254 //! The owner of the completed .part file. 255 const CPartFile* m_owner; 256 257 //! Specifies if completion failed. 258 bool m_error; 259}; 260 261 262/** 263 * This event is sent when preallocation of a new partfile is finished. 264 */ 265DECLARE_LOCAL_EVENT_TYPE(MULE_EVT_ALLOC_FINISHED, -1); 266class CAllocFinishedEvent : public wxEvent 267{ 268 public: 269 /** Constructor, see getter function for description of parameters. */ 270 CAllocFinishedEvent(CPartFile *file, bool pause, long result) 271 : wxEvent(-1, MULE_EVT_ALLOC_FINISHED), 272 m_file(file), m_pause(pause), m_result(result) 273 {} 274 275 /** @see wxEvent::Clone */ 276 virtual wxEvent *Clone() const; 277 278 /** Returns the partfile for which preallocation was requested. */ 279 CPartFile *GetFile() const throw() { return m_file; } 280 281 /** Returns whether the partfile should start paused. */ 282 bool IsPaused() const throw() { return m_pause; } 283 284 /** Returns the result of preallocation: true on success, false otherwise. */ 285 bool Succeeded() const throw() { return m_result == 0; } 286 287 /** Returns the result of the preallocation. */ 288 long GetResult() const throw() { return m_result; } 289 290 private: 291 //! The partfile for which preallocation was requested. 292 CPartFile * m_file; 293 294 //! Should the download start paused? 295 bool m_pause; 296 297 //! Result of preallocation 298 long m_result; 299}; 300 301DECLARE_LOCAL_EVENT_TYPE(MULE_EVT_HASHING, -1) 302DECLARE_LOCAL_EVENT_TYPE(MULE_EVT_AICH_HASHING, -1) 303DECLARE_LOCAL_EVENT_TYPE(MULE_EVT_FILE_COMPLETED, -1) 304 305 306typedef void (wxEvtHandler::*MuleHashingEventFunction)(CHashingEvent&); 307typedef void (wxEvtHandler::*MuleCompletionEventFunction)(CCompletionEvent&); 308typedef void (wxEvtHandler::*MuleAllocFinishedEventFunction)(CAllocFinishedEvent&); 309 310//! Event-handler for completed hashings of new shared files and partfiles. 311#define EVT_MULE_HASHING(func) \ 312 DECLARE_EVENT_TABLE_ENTRY(MULE_EVT_HASHING, -1, -1, \ 313 (wxObjectEventFunction) (wxEventFunction) \ 314 wxStaticCastEvent(MuleHashingEventFunction, &func), (wxObject*) NULL), 315 316//! Event-handler for completed hashings of files that were missing a AICH hash. 317#define EVT_MULE_AICH_HASHING(func) \ 318 DECLARE_EVENT_TABLE_ENTRY(MULE_EVT_AICH_HASHING, -1, -1, \ 319 (wxObjectEventFunction) (wxEventFunction) \ 320 wxStaticCastEvent(MuleHashingEventFunction, &func), (wxObject*) NULL), 321 322//! Event-handler for completion of part-files. 323#define EVT_MULE_FILE_COMPLETED(func) \ 324 DECLARE_EVENT_TABLE_ENTRY(MULE_EVT_FILE_COMPLETED, -1, -1, \ 325 (wxObjectEventFunction) (wxEventFunction) \ 326 wxStaticCastEvent(MuleCompletionEventFunction, &func), (wxObject*) NULL), 327 328//! Event-handler for partfile preallocation finished events. 329#define EVT_MULE_ALLOC_FINISHED(func) \ 330 DECLARE_EVENT_TABLE_ENTRY(MULE_EVT_ALLOC_FINISHED, -1, -1, \ 331 (wxObjectEventFunction) (wxEventFunction) \ 332 wxStaticCastEvent(MuleAllocFinishedEventFunction, &func), (wxObject*) NULL), 333 334 335#endif // TASKS_H 336// File_checked_for_headers 337