Upstream tarball 9931
[amule.git] / src / PartFile.h
blob1ce629c4ceab35a90227c2de8c00795c9d387223
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 Gap_Struct
34 #include "DeadSourceList.h" // Needed for CDeadSourceList
35 #include "GapList.h"
38 class CSearchFile;
39 class CUpDownClient;
40 class CMemFile;
41 class CFileDataIO;
42 class CED2KFileLink;
44 //#define BUFFER_SIZE_LIMIT 500000 // Max bytes before forcing a flush
45 #define BUFFER_TIME_LIMIT 60000 // Max milliseconds before forcing a flush
47 // Ok, eMule and aMule are building incompatible backup files because
48 // of the different name. aMule was using ".BAK" and eMule ".bak".
49 // This should fix it.
50 #define PARTMET_BAK_EXT wxT(".bak")
52 enum EPartFileFormat {
53 PMT_UNKNOWN = 0,
54 PMT_DEFAULTOLD,
55 PMT_SPLITTED,
56 PMT_NEWOLD,
57 PMT_SHAREAZA,
58 PMT_BADFORMAT
62 class SFileRating
64 public:
65 wxString UserName;
66 wxString FileName;
67 sint16 Rating;
68 wxString Comment;
69 public:
70 SFileRating(const wxString &u, const wxString &f, sint16 r, const wxString &c);
71 SFileRating(const SFileRating &fr);
72 SFileRating(const CUpDownClient &client);
73 ~SFileRating();
76 typedef std::list<SFileRating> FileRatingList;
78 class SourcenameItem
80 public:
81 wxString name;
82 long count;
83 public:
84 SourcenameItem(const wxString &n, long c)
86 name(n), count(c) {}
89 typedef std::list<SourcenameItem> SourcenameItemList;
91 class CPartFile : public CKnownFile {
92 public:
93 typedef std::list<Requested_Block_Struct*> CReqBlockPtrList;
95 CPartFile();
96 #ifdef CLIENT_GUI
97 CPartFile(CEC_PartFile_Tag *tag);
98 #else
99 virtual void SetFileName(const CPath& filename);
100 #endif
101 CPartFile(CSearchFile* searchresult); //used when downloading a new file
102 CPartFile(const CED2KFileLink* fileLink);
103 virtual ~CPartFile();
105 bool CreateFromFile(wxString WXUNUSED(directory), wxString WXUNUSED(filename), void* WXUNUSED(pvProgressParam)) {return false;}// not supported in this class
106 void SetPartFileStatus(uint8 newstatus);
107 virtual bool LoadFromFile(const CFileDataIO* WXUNUSED(file)) { return false; }
108 bool WriteToFile(CFileDataIO* WXUNUSED(file)) { return false; }
109 bool IsPartFile() const { return !(status == PS_COMPLETE); }
110 uint32 Process(uint32 reducedownload, uint8 m_icounter);
111 uint8 LoadPartFile(const CPath& in_directory, const CPath& filename, bool from_backup = false, bool getsizeonly = false);
112 bool SavePartFile(bool Initial = false);
113 void PartFileHashFinished(CKnownFile* result);
114 bool HashSinglePart(uint16 partnumber); // true = ok , false = corrupted
116 bool CheckShowItemInGivenCat(int inCategory);
118 bool IsComplete(uint64 start, uint64 end) { return m_gaplist.IsComplete(start, end); }
119 bool IsComplete(uint16 part) { return m_gaplist.IsComplete(part); }
121 void UpdateCompletedInfos();
123 bool GetNextRequestedBlock(CUpDownClient* sender, std::vector<Requested_Block_Struct*>& toadd, uint16& count);
124 void WritePartStatus(CMemFile* file);
125 void WriteCompleteSourcesCount(CMemFile* file);
126 static bool CanAddSource(uint32 userid, uint16 port, uint32 serverip, uint16 serverport, uint8* pdebug_lowiddropped = NULL, bool ed2kID = true);
127 void AddSources(CMemFile& sources, uint32 serverip, uint16 serverport, unsigned origin, bool bWithObfuscationAndHash);
128 #ifdef CLIENT_GUI
129 uint8 GetStatus() const { return status; }
130 uint8 GetStatus(bool /*ignorepause = false*/) const { return status; }
131 #else
132 uint8 GetStatus(bool ignorepause = false) const;
133 #endif
134 virtual void UpdatePartsInfo();
135 const CPath& GetPartMetFileName() const { return m_partmetfilename; }
136 uint64 GetTransferred() const { return transferred; }
137 const CPath& GetFullName() const { return m_fullname; }
138 float GetKBpsDown() const { return kBpsDown; }
139 double GetPercentCompleted() const { return percentcompleted; }
141 #ifndef CLIENT_GUI
142 uint16 GetSourceCount() const { return (uint16)m_SrcList.size(); }
143 uint16 GetSrcA4AFCount() const { return (uint16)m_A4AFsrclist.size(); }
144 #else
145 uint16 m_source_count, m_a4af_source_count;
146 uint16 GetSourceCount() const { return m_source_count; }
147 uint16 GetSrcA4AFCount() const { return m_a4af_source_count; }
148 #endif
149 uint16 GetTransferingSrcCount() const { return transferingsrc; }
150 uint16 GetNotCurrentSourcesCount() const { return m_notCurrentSources; };
151 void SetNotCurrentSourcesCount(uint16 new_count) { m_notCurrentSources = new_count; };
152 uint16 GetValidSourcesCount() const { return m_validSources; };
154 uint64 GetNeededSpace();
156 virtual wxString GetFeedback() const;
158 wxString getPartfileStatus() const; //<<--9/21/02
159 sint32 getTimeRemaining() const; //<<--9/21/02
160 time_t lastseencomplete;
161 int getPartfileStatusRang() const;
163 // Barry - Added as replacement for BlockReceived to buffer data before writing to disk
164 uint32 WriteToBuffer(uint32 transize, byte *data, uint64 start, uint64 end, Requested_Block_Struct *block, const CUpDownClient* client);
165 void FlushBuffer(bool fromAICHRecoveryDataAvailable = false);
167 // Barry - Is archive recovery in progress
168 volatile bool m_bRecoveringArchive;
170 // Barry - Added to prevent list containing deleted blocks on shutdown
171 void RemoveAllRequestedBlocks(void);
173 void RemoveBlockFromList(uint64 start,uint64 end);
174 void RemoveAllSources(bool bTryToSwap);
175 void Delete();
176 void StopFile(bool bCancel = false);
177 void PauseFile(bool bInsufficient = false);
178 void ResumeFile();
180 virtual CPacket* CreateSrcInfoPacket(const CUpDownClient* forClient, uint8 byRequestedVersion, uint16 nRequestedOptions);
181 void AddClientSources(CMemFile* sources, unsigned nSourceFrom, uint8 uClientSXVersion, bool bSourceExchange2, const CUpDownClient* pClient = NULL);
183 bool PreviewAvailable();
184 uint16 GetAvailablePartCount() const { return m_availablePartsCount; }
185 uint32 GetLastAnsweredTime() const { return m_ClientSrcAnswered; }
186 void SetLastAnsweredTime();
187 void SetLastAnsweredTimeTimeout();
188 uint64 GetLostDueToCorruption() const { return m_iLostDueToCorruption; }
189 uint64 GetGainDueToCompression() const { return m_iGainDueToCompression; }
190 uint32 TotalPacketsSavedDueToICH()const{ return m_iTotalPacketsSavedDueToICH; }
191 bool IsStopped() const { return this ? m_stopped : true; }
192 bool IsPaused() const { return m_paused; }
193 void UpdateFileRatingCommentAvail();
195 int GetCommonFilePenalty();
196 void UpdateDisplayedInfo(bool force = false);
198 uint8 GetCategory() const { return m_category; }
199 void SetCategory(uint8 cat);
201 volatile bool m_bPreviewing;
202 void SetDownPriority(uint8 newDownPriority, bool bSave = true, bool bRefresh = true);
203 bool IsAutoDownPriority() const { return m_bAutoDownPriority; }
204 void SetAutoDownPriority(bool flag) { m_bAutoDownPriority = flag; }
205 void UpdateAutoDownPriority();
206 uint8 GetDownPriority() const { return m_iDownPriority; }
207 void SetActive(bool bActive);
208 uint32 GetDlActiveTime() const;
209 bool GetInsufficient() const { return m_insufficient; }
211 void CompleteFileEnded(bool errorOccured, const CPath& newname);
213 bool RemoveSource(CUpDownClient* toremove, bool updatewindow = true, bool bDoStatsUpdate = true);
215 void RequestAICHRecovery(uint16 nPart);
216 void AICHRecoveryDataAvailable(uint16 nPart);
219 * This function is used to update source-counts.
221 * @param oldState The old state of the client, or -1 to ignore.
222 * @param newState The new state of the client, or -1 to ignore.
224 * Call this function for a client belonging to this file, which has changed
225 * its state. The value -1 can be used to make the function ignore one of
226 * the two states.
228 * AddSource and DelSource takes care of calling this function when a source is
229 * removed, so there's no need to call this function when calling either of those.
231 void ClientStateChanged( int oldState, int newState );
233 bool AddSource( CUpDownClient* client );
234 bool DelSource( CUpDownClient* client );
237 * Updates the requency of avilable parts from with the data the client provides.
239 * @param client The clients whoose available parts should be considered.
240 * @param increment If true, the counts are incremented, otherwise they are decremented.
242 * This functions updates the frequency list of file-parts, using the clients
243 * parts-status. This function should be called by clients every time they update their
244 * parts-status, or when they are added or removed from the file.
246 void UpdatePartsFrequency( CUpDownClient* client, bool increment );
248 ArrayOfUInts16 m_SrcpartFrequency;
250 void SetShowSources( bool val ) { m_showSources = val; }
251 bool ShowSources() const { return m_showSources; }
253 typedef std::set<CUpDownClient*> SourceSet;
255 const SourceSet& GetSourceList() const { return m_SrcList; }
256 const SourceSet& GetA4AFList() const { return m_A4AFsrclist; }
258 const CReqBlockPtrList GetRequestedBlockList() const { return m_requestedblocks_list; }
260 // LEGACY - to be removed when possible
261 class CGapPtrList {
262 public:
263 void Init(const CGapList * gaplist) { m_gaplist = gaplist; }
264 class const_iterator {
265 CGapList::const_iterator m_it;
266 Gap_Struct m_gap;
267 public:
268 const_iterator() {};
269 const_iterator(const CGapList::const_iterator& it) { m_it = it; };
270 bool operator != (const const_iterator& it) { return m_it != it.m_it; }
271 const_iterator& operator ++ () { ++ m_it; return *this; }
272 Gap_Struct * operator * () {
273 m_gap.start = m_it.start();
274 m_gap.end = m_it.end();
275 return & m_gap;
278 const_iterator begin() const { return const_iterator(m_gaplist->begin()); }
279 const_iterator end() const { return const_iterator(m_gaplist->end()); }
280 bool empty() const { return m_gaplist->IsComplete(); }
281 uint32 size() const { return m_gaplist->size(); }
283 private:
284 const CGapList * m_gaplist;
286 // this function must stay, but return the m_gaplist instead
287 const CGapPtrList& GetGapList() const { return m_gapptrlist; }
288 // meanwhile use
289 const CGapList& GetNewGapList() const { return m_gaplist; }
290 private:
291 CGapPtrList m_gapptrlist;
292 public:
293 // END LEGACY
296 * Adds a source to the list of dead sources.
298 * @param client The source to be recorded as dead for this file.
300 void AddDeadSource(const CUpDownClient* client);
303 * Checks if a source is recorded as being dead for this file.
305 * @param client The client to evaluate.
306 * @return True if dead, false otherwise.
308 * Sources that are dead are not to be considered valid
309 * sources and should not be added to the partfile.
311 bool IsDeadSource(const CUpDownClient* client);
313 /* Kad Stuff */
314 uint16 GetMaxSources() const;
315 uint16 GetMaxSourcePerFileSoft() const;
316 uint16 GetMaxSourcePerFileUDP() const;
318 const FileRatingList &GetRatingAndComments();
320 void AllocationFinished();
321 private:
322 #ifndef CLIENT_GUI
323 // partfile handle (opened on demand)
324 CFileAutoClose m_hpartfile;
325 //! A local list of sources that are invalid for this file.
326 CDeadSourceList m_deadSources;
328 class CCorruptionBlackBox* m_CorruptionBlackBox;
329 #endif
331 uint16 m_notCurrentSources;
333 bool m_showSources;
335 uint32 m_validSources;
337 void AddGap(uint64 start, uint64 end);
338 void AddGap(uint16 part);
339 void FillGap(uint64 start, uint64 end);
340 void FillGap(uint16 part);
341 bool GetNextEmptyBlockInPart(uint16 partnumber,Requested_Block_Struct* result);
342 bool IsAlreadyRequested(uint64 start, uint64 end);
343 void CompleteFile(bool hashingdone);
344 void CreatePartFile();
345 void Init();
347 bool CheckFreeDiskSpace( uint64 neededSpace = 0 );
349 bool IsCorruptedPart(uint16 partnumber);
351 uint32 m_iLastPausePurge;
352 uint16 m_count;
353 uint16 transferingsrc;
354 uint64 completedsize;
355 uint64 transferred;
357 uint64 m_iLostDueToCorruption;
358 uint64 m_iGainDueToCompression;
359 uint32 m_iTotalPacketsSavedDueToICH;
360 float kBpsDown;
361 CPath m_fullname; // path/name of the met file
362 CPath m_partmetfilename; // name of the met file
363 CPath m_PartPath; // path/name of the partfile
364 bool m_paused;
365 bool m_stopped;
366 bool m_insufficient;
367 uint8 m_iDownPriority;
368 bool m_bAutoDownPriority;
369 uint8 status;
370 uint32 lastpurgetime;
371 uint32 m_LastNoNeededCheck;
372 CGapList m_gaplist;
373 CReqBlockPtrList m_requestedblocks_list;
374 double percentcompleted;
375 std::list<uint16> m_corrupted_list;
376 uint16 m_availablePartsCount;
377 uint32 m_ClientSrcAnswered;
378 bool m_bPercentUpdated;
380 void PerformFileComplete();
382 uint32 m_lastRefreshedDLDisplay;
384 // Buffered data to be written
385 std::list<class PartFileBufferedData*> m_BufferedData_list;
387 uint32 m_nTotalBufferData;
388 uint32 m_nLastBufferFlushTime;
390 uint8 m_category;
391 uint32 m_nDlActiveTime;
392 time_t m_tActivated;
393 bool m_is_A4AF_auto;
395 SourceSet m_SrcList;
396 SourceSet m_A4AFsrclist;
397 bool m_hashsetneeded;
398 uint32 m_lastsearchtime;
399 bool m_localSrcReqQueued;
401 FileRatingList m_FileRatingList;
402 #ifdef CLIENT_GUI
403 uint32 m_kbpsDown;
404 uint8 m_iDownPriorityEC;
405 SourcenameItemList m_SourcenameItem_list;
406 public:
407 const SourcenameItemList &GetSourcenameItemList() { return m_SourcenameItem_list; }
408 void ClearSourcenameItemList() { m_SourcenameItem_list.clear(); }
409 void AddSourcenameItemList(const wxString &name, long count) { m_SourcenameItem_list.push_back(SourcenameItem(name, count)); }
410 #endif
411 public:
412 const FileRatingList &GetFileRatingList() { return m_FileRatingList; }
413 void ClearFileRatingList() { m_FileRatingList.clear(); }
414 void AddFileRatingList(const wxString & u, const wxString & f, sint16 r, const wxString & c) {
415 m_FileRatingList.push_back(SFileRating(u, f, r, c)); }
417 bool IsHashSetNeeded() const { return m_hashsetneeded; }
418 void SetHashSetNeeded(bool value) { m_hashsetneeded = value; }
420 uint64 GetCompletedSize() const { return completedsize; }
421 void SetCompletedSize(uint64 size) { completedsize = size; }
423 bool IsLocalSrcRequestQueued() const { return m_localSrcReqQueued; }
424 void SetLocalSrcRequestQueued(bool value) { m_localSrcReqQueued = value; }
426 void AddA4AFSource(CUpDownClient* src) { m_A4AFsrclist.insert(src); }
427 bool RemoveA4AFSource(CUpDownClient* src) { return (m_A4AFsrclist.erase(src) > 0); }
429 uint32 GetLastSearchTime() const { return m_lastsearchtime; }
430 void SetLastSearchTime(uint32 time) { m_lastsearchtime = time; }
432 // void CleanUpSources( bool noNeeded, bool fullQueue = false, bool highQueue = false );
434 void AddDownloadingSource(CUpDownClient* client);
436 void RemoveDownloadingSource(CUpDownClient* client);
437 void SetStatus(uint8 in);
438 void StopPausedFile();
440 // [sivka / Tarod] Imported from eMule 0.30c (Creteil) ...
441 void SetA4AFAuto(bool in) { m_is_A4AF_auto = in; }
442 bool IsA4AFAuto() const { return m_is_A4AF_auto; }
444 // Kry -Sources seeds
445 void SaveSourceSeeds();
446 void LoadSourceSeeds();
448 // Dropping slow sources
449 CUpDownClient* GetSlowerDownloadingClient(uint32 speed, CUpDownClient* caller);
451 // Read data for sharing
452 bool ReadData(class CFileArea & area, uint64 offset, uint32 toread);
454 private:
455 /* downloading sources list */
456 CClientPtrList m_downloadingSourcesList;
458 /* Kad Stuff */
459 uint32 m_LastSearchTimeKad;
460 uint8 m_TotalSearchesKad;
462 friend class CDownQueueRem;
463 friend class CPartFileConvert;
466 #endif // PARTFILE_H
467 // File_checked_for_headers