Improve comment
[amule.git] / src / PartFile.h
blob6f59bcfbc0be70a1164fe0d220fee4e493517a73
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-2008 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 PARTFILE_H
27 #define PARTFILE_H
30 #include "KnownFile.h" // Needed for CKnownFile
31 #include "FileAutoClose.h" // Needed for CFileAutoClose
33 #include "OtherStructs.h" // Needed for Requested_Block_Struct
34 #include "DeadSourceList.h" // Needed for CDeadSourceList
35 #include "GapList.h"
37 class CSearchFile;
38 class CMemFile;
39 class CFileDataIO;
40 class CED2KFileLink;
42 //#define BUFFER_SIZE_LIMIT 500000 // Max bytes before forcing a flush
43 #define BUFFER_TIME_LIMIT 60000 // Max milliseconds before forcing a flush
45 // Ok, eMule and aMule are building incompatible backup files because
46 // of the different name. aMule was using ".BAK" and eMule ".bak".
47 // This should fix it.
48 #define PARTMET_BAK_EXT wxT(".bak")
50 enum EPartFileFormat {
51 PMT_UNKNOWN = 0,
52 PMT_DEFAULTOLD,
53 PMT_SPLITTED,
54 PMT_NEWOLD,
55 PMT_SHAREAZA,
56 PMT_BADFORMAT
60 class SFileRating
62 public:
63 wxString UserName;
64 wxString FileName;
65 sint16 Rating;
66 wxString Comment;
67 public:
68 SFileRating(const wxString &u, const wxString &f, sint16 r, const wxString &c);
69 SFileRating(const SFileRating &fr);
70 SFileRating(const CUpDownClient &client);
71 ~SFileRating();
74 typedef std::list<SFileRating> FileRatingList;
76 class SourcenameItem
78 public:
79 wxString name;
80 int count;
81 public:
82 SourcenameItem(const wxString &n = EmptyString, int c = 0)
84 name(n), count(c) {}
87 typedef std::map<uint32,SourcenameItem> SourcenameItemMap;
89 class CPartFile : public CKnownFile {
90 public:
91 typedef std::list<Requested_Block_Struct*> CReqBlockPtrList;
93 CPartFile();
94 #ifdef CLIENT_GUI
95 CPartFile(CEC_PartFile_Tag *tag);
96 #else
97 virtual void SetFileName(const CPath& filename);
98 #endif
99 CPartFile(CSearchFile* searchresult); //used when downloading a new file
100 CPartFile(const CED2KFileLink* fileLink);
101 virtual ~CPartFile();
103 bool CreateFromFile(wxString WXUNUSED(directory), wxString WXUNUSED(filename), void* WXUNUSED(pvProgressParam)) {return false;}// not supported in this class
104 virtual bool LoadFromFile(const CFileDataIO* WXUNUSED(file)) { return false; }
105 bool WriteToFile(CFileDataIO* WXUNUSED(file)) { return false; }
107 // virtual functions for CKnownFile and CPartFile:
108 bool IsPartFile() const { return status != PS_COMPLETE; } // true if not completed
109 bool IsCompleted() const { return status == PS_COMPLETE; } // true if completed
110 bool IsCPartFile() const { return true; } // true if it's a CPartFile
112 uint32 Process(uint32 reducedownload, uint8 m_icounter);
113 uint8 LoadPartFile(const CPath& in_directory, const CPath& filename, bool from_backup = false, bool getsizeonly = false);
114 bool SavePartFile(bool Initial = false);
115 void PartFileHashFinished(CKnownFile* result);
116 bool HashSinglePart(uint16 partnumber); // true = ok , false = corrupted
118 bool CheckShowItemInGivenCat(int inCategory);
120 bool IsComplete(uint64 start, uint64 end) { return m_gaplist.IsComplete(start, end); }
121 bool IsComplete(uint16 part) { return m_gaplist.IsComplete(part); }
123 void UpdateCompletedInfos();
125 bool GetNextRequestedBlock(CUpDownClient* sender, std::vector<Requested_Block_Struct*>& toadd, uint16& count);
126 void WritePartStatus(CMemFile* file);
127 void WriteCompleteSourcesCount(CMemFile* file);
128 static bool CanAddSource(uint32 userid, uint16 port, uint32 serverip, uint16 serverport, uint8* pdebug_lowiddropped = NULL, bool ed2kID = true);
129 void AddSources(CMemFile& sources, uint32 serverip, uint16 serverport, unsigned origin, bool bWithObfuscationAndHash);
130 #ifdef CLIENT_GUI
131 uint8 GetStatus() const { return status; }
132 uint8 GetStatus(bool /*ignorepause = false*/) const { return status; }
133 #else
134 uint8 GetStatus(bool ignorepause = false) const;
135 #endif
136 virtual void UpdatePartsInfo();
137 const CPath& GetPartMetFileName() const { return m_partmetfilename; }
138 uint16 GetPartMetNumber() const;
139 uint64 GetTransferred() const { return transferred; }
140 const CPath& GetFullName() const { return m_fullname; }
141 float GetKBpsDown() const { return kBpsDown; }
142 double GetPercentCompleted() const { return percentcompleted; }
144 #ifndef CLIENT_GUI
145 uint16 GetSourceCount() const { return (uint16)m_SrcList.size(); }
146 uint16 GetSrcA4AFCount() const { return (uint16)m_A4AFsrclist.size(); }
147 #else
148 uint16 m_source_count, m_a4af_source_count;
149 uint16 GetSourceCount() const { return m_source_count; }
150 uint16 GetSrcA4AFCount() const { return m_a4af_source_count; }
151 #endif
152 uint16 GetTransferingSrcCount() const { return transferingsrc; }
153 uint16 GetNotCurrentSourcesCount() const { return m_notCurrentSources; };
154 //void SetNotCurrentSourcesCount(uint16 new_count) { m_notCurrentSources = new_count; };
155 uint16 GetValidSourcesCount() const { return m_validSources; };
157 uint64 GetNeededSpace();
159 virtual wxString GetFeedback() const;
161 wxString getPartfileStatus() const; //<<--9/21/02
162 sint32 getTimeRemaining() const; //<<--9/21/02
163 time_t lastseencomplete;
164 int getPartfileStatusRang() const;
166 // Barry - Added as replacement for BlockReceived to buffer data before writing to disk
167 uint32 WriteToBuffer(uint32 transize, byte *data, uint64 start, uint64 end, Requested_Block_Struct *block, const CUpDownClient* client);
168 void FlushBuffer(bool fromAICHRecoveryDataAvailable = false);
170 // Barry - Is archive recovery in progress
171 volatile bool m_bRecoveringArchive;
173 // Barry - Added to prevent list containing deleted blocks on shutdown
174 void RemoveAllRequestedBlocks(void);
176 void RemoveBlockFromList(uint64 start,uint64 end);
177 void RemoveAllSources(bool bTryToSwap);
178 void Delete();
179 void StopFile(bool bCancel = false);
180 void PauseFile(bool bInsufficient = false);
181 void ResumeFile();
183 virtual CPacket* CreateSrcInfoPacket(const CUpDownClient* forClient, uint8 byRequestedVersion, uint16 nRequestedOptions);
184 void AddClientSources(CMemFile* sources, unsigned nSourceFrom, uint8 uClientSXVersion, bool bSourceExchange2, const CUpDownClient* pClient = NULL);
186 bool PreviewAvailable();
187 uint16 GetAvailablePartCount() const { return m_availablePartsCount; }
188 uint32 GetLastAnsweredTime() const { return m_ClientSrcAnswered; }
189 void SetLastAnsweredTime();
190 void SetLastAnsweredTimeTimeout();
191 uint64 GetLostDueToCorruption() const { return m_iLostDueToCorruption; }
192 uint64 GetGainDueToCompression() const { return m_iGainDueToCompression; }
193 uint32 TotalPacketsSavedDueToICH()const{ return m_iTotalPacketsSavedDueToICH; }
194 bool IsStopped() const { return this ? m_stopped : true; }
195 bool IsPaused() const { return m_paused; }
196 void UpdateFileRatingCommentAvail();
198 int GetCommonFilePenalty();
199 void UpdateDisplayedInfo(bool force = false);
201 uint8 GetCategory() const { return m_category; }
202 void SetCategory(uint8 cat);
203 void RemoveCategory(uint8 cat);
205 volatile bool m_bPreviewing;
206 void SetDownPriority(uint8 newDownPriority, bool bSave = true, bool bRefresh = true);
207 bool IsAutoDownPriority() const { return m_bAutoDownPriority; }
208 void SetAutoDownPriority(bool flag) { m_bAutoDownPriority = flag; }
209 void UpdateAutoDownPriority();
210 uint8 GetDownPriority() const { return m_iDownPriority; }
211 void SetActive(bool bActive);
212 uint32 GetDlActiveTime() const;
213 bool GetInsufficient() const { return m_insufficient; }
215 void CompleteFileEnded(bool errorOccured, const CPath& newname);
217 bool RemoveSource(CUpDownClient* toremove, bool updatewindow = true, bool bDoStatsUpdate = true);
219 void RequestAICHRecovery(uint16 nPart);
220 void AICHRecoveryDataAvailable(uint16 nPart);
223 * This function is used to update source-counts.
225 * @param oldState The old state of the client, or -1 to ignore.
226 * @param newState The new state of the client, or -1 to ignore.
228 * Call this function for a client belonging to this file, which has changed
229 * its state. The value -1 can be used to make the function ignore one of
230 * the two states.
232 * AddSource and DelSource takes care of calling this function when a source is
233 * removed, so there's no need to call this function when calling either of those.
235 void ClientStateChanged( int oldState, int newState );
237 bool AddSource( CUpDownClient* client );
238 bool DelSource( CUpDownClient* client );
241 * Updates the requency of avilable parts from with the data the client provides.
243 * @param client The clients whoose available parts should be considered.
244 * @param increment If true, the counts are incremented, otherwise they are decremented.
246 * This functions updates the frequency list of file-parts, using the clients
247 * parts-status. This function should be called by clients every time they update their
248 * parts-status, or when they are added or removed from the file.
250 void UpdatePartsFrequency( CUpDownClient* client, bool increment );
252 ArrayOfUInts16 m_SrcpartFrequency;
254 const SourceSet& GetSourceList() const { return m_SrcList; }
255 const SourceSet& GetA4AFList() const { return m_A4AFsrclist; }
256 void ClearA4AFList() { m_A4AFsrclist.clear(); }
258 const CReqBlockPtrList GetRequestedBlockList() const { return m_requestedblocks_list; }
260 const CGapList& GetGapList() const { return m_gaplist; }
263 * Adds a source to the list of dead sources.
265 * @param client The source to be recorded as dead for this file.
267 void AddDeadSource(const CUpDownClient* client);
270 * Checks if a source is recorded as being dead for this file.
272 * @param client The client to evaluate.
273 * @return True if dead, false otherwise.
275 * Sources that are dead are not to be considered valid
276 * sources and should not be added to the partfile.
278 bool IsDeadSource(const CUpDownClient* client);
280 /* Kad Stuff */
281 uint16 GetMaxSources() const;
282 uint16 GetMaxSourcePerFileSoft() const;
283 uint16 GetMaxSourcePerFileUDP() const;
285 void GetRatingAndComments(FileRatingList & list) const;
287 void AllocationFinished();
288 private:
289 #ifndef CLIENT_GUI
290 // partfile handle (opened on demand)
291 CFileAutoClose m_hpartfile;
292 //! A local list of sources that are invalid for this file.
293 CDeadSourceList m_deadSources;
295 class CCorruptionBlackBox* m_CorruptionBlackBox;
296 #endif
298 uint16 m_notCurrentSources;
300 uint32 m_validSources;
302 void AddGap(uint64 start, uint64 end);
303 void AddGap(uint16 part);
304 void FillGap(uint64 start, uint64 end);
305 void FillGap(uint16 part);
306 bool GetNextEmptyBlockInPart(uint16 partnumber,Requested_Block_Struct* result);
307 bool IsAlreadyRequested(uint64 start, uint64 end);
308 void CompleteFile(bool hashingdone);
309 void CreatePartFile();
310 void Init();
312 bool CheckFreeDiskSpace( uint64 neededSpace = 0 );
314 bool IsCorruptedPart(uint16 partnumber);
316 uint32 m_iLastPausePurge;
317 uint16 m_count;
318 uint16 transferingsrc;
319 uint64 completedsize;
320 uint64 transferred;
322 uint64 m_iLostDueToCorruption;
323 uint64 m_iGainDueToCompression;
324 uint32 m_iTotalPacketsSavedDueToICH;
325 float kBpsDown;
326 CPath m_fullname; // path/name of the met file
327 CPath m_partmetfilename; // name of the met file
328 CPath m_PartPath; // path/name of the partfile
329 bool m_paused;
330 bool m_stopped;
331 bool m_insufficient;
332 uint8 m_iDownPriority;
333 bool m_bAutoDownPriority;
334 uint8 status;
335 uint32 lastpurgetime;
336 uint32 m_LastNoNeededCheck;
337 CGapList m_gaplist;
338 CReqBlockPtrList m_requestedblocks_list;
339 double percentcompleted;
340 std::list<uint16> m_corrupted_list;
341 uint16 m_availablePartsCount;
342 uint32 m_ClientSrcAnswered;
343 bool m_bPercentUpdated;
345 void PerformFileComplete();
347 uint32 m_lastRefreshedDLDisplay;
349 // Buffered data to be written
350 std::list<class PartFileBufferedData*> m_BufferedData_list;
352 uint32 m_nTotalBufferData;
353 uint32 m_nLastBufferFlushTime;
355 uint8 m_category;
356 uint32 m_nDlActiveTime;
357 time_t m_tActivated;
358 bool m_is_A4AF_auto;
360 SourceSet m_SrcList;
361 SourceSet m_A4AFsrclist;
362 bool m_hashsetneeded;
363 uint32 m_lastsearchtime;
364 bool m_localSrcReqQueued;
366 #ifdef CLIENT_GUI
367 FileRatingList m_FileRatingList;
368 const FileRatingList &GetFileRatingList() { return m_FileRatingList; }
369 void ClearFileRatingList() { m_FileRatingList.clear(); }
370 void AddFileRatingList(const wxString & u, const wxString & f, sint16 r, const wxString & c) {
371 m_FileRatingList.push_back(SFileRating(u, f, r, c)); }
373 uint32 m_kbpsDown;
374 uint8 m_iDownPriorityEC;
375 bool m_isShared;
376 SourcenameItemMap m_SourcenameItemMap;
378 ListOfUInts32 m_A4AFClientIDs;
379 ListOfUInts32 & GetA4AFClientIDs() { return m_A4AFClientIDs; }
380 public:
381 bool IsShared() const { return m_isShared; }
382 SourcenameItemMap &GetSourcenameItemMap() { return m_SourcenameItemMap; }
383 PartFileEncoderData m_PartFileEncoderData;
384 #endif
385 public:
386 bool IsHashSetNeeded() const { return m_hashsetneeded; }
387 void SetHashSetNeeded(bool value) { m_hashsetneeded = value; }
389 uint64 GetCompletedSize() const { return completedsize; }
390 void SetCompletedSize(uint64 size) { completedsize = size; }
392 bool IsLocalSrcRequestQueued() const { return m_localSrcReqQueued; }
393 void SetLocalSrcRequestQueued(bool value) { m_localSrcReqQueued = value; }
395 void AddA4AFSource(CUpDownClient* src) { m_A4AFsrclist.insert(CCLIENTREF(src, wxT("A4AFSource"))); }
396 bool RemoveA4AFSource(CUpDownClient* src) { return (m_A4AFsrclist.erase(CCLIENTREF(src, wxEmptyString)) > 0); }
398 uint32 GetLastSearchTime() const { return m_lastsearchtime; }
399 void SetLastSearchTime(uint32 time) { m_lastsearchtime = time; }
401 // void CleanUpSources( bool noNeeded, bool fullQueue = false, bool highQueue = false );
403 void AddDownloadingSource(CUpDownClient* client);
405 void RemoveDownloadingSource(CUpDownClient* client);
406 void SetStatus(uint8 in);
407 void StopPausedFile();
409 // [sivka / Tarod] Imported from eMule 0.30c (Creteil) ...
410 void SetA4AFAuto(bool in) { m_is_A4AF_auto = in; }
411 bool IsA4AFAuto() const { return m_is_A4AF_auto; }
413 // Kry -Sources seeds
414 void SaveSourceSeeds();
415 void LoadSourceSeeds();
417 // Dropping slow sources
418 CUpDownClient* GetSlowerDownloadingClient(uint32 speed, CUpDownClient* caller);
420 // Read data for sharing
421 bool ReadData(class CFileArea & area, uint64 offset, uint32 toread);
423 private:
424 /* downloading sources list */
425 CClientRefList m_downloadingSourcesList;
427 /* Kad Stuff */
428 uint32 m_LastSearchTimeKad;
429 uint8 m_TotalSearchesKad;
431 friend class CKnownFilesRem;
432 friend class CPartFileConvert;
435 #endif // PARTFILE_H
436 // File_checked_for_headers