Upstream tarball 10138
[amule.git] / src / PartFile.h
blobc641361a696278bbcef6a3c9032730be42952474
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 int count;
83 public:
84 SourcenameItem(const wxString &n = EmptyString, int c = 0)
86 name(n), count(c) {}
89 typedef std::map<uint32,SourcenameItem> SourcenameItemMap;
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 bool IsCPartFile() const { return true; }
111 uint32 Process(uint32 reducedownload, uint8 m_icounter);
112 uint8 LoadPartFile(const CPath& in_directory, const CPath& filename, bool from_backup = false, bool getsizeonly = false);
113 bool SavePartFile(bool Initial = false);
114 void PartFileHashFinished(CKnownFile* result);
115 bool HashSinglePart(uint16 partnumber); // true = ok , false = corrupted
117 bool CheckShowItemInGivenCat(int inCategory);
119 bool IsComplete(uint64 start, uint64 end) { return m_gaplist.IsComplete(start, end); }
120 bool IsComplete(uint16 part) { return m_gaplist.IsComplete(part); }
122 void UpdateCompletedInfos();
124 bool GetNextRequestedBlock(CUpDownClient* sender, std::vector<Requested_Block_Struct*>& toadd, uint16& count);
125 void WritePartStatus(CMemFile* file);
126 void WriteCompleteSourcesCount(CMemFile* file);
127 static bool CanAddSource(uint32 userid, uint16 port, uint32 serverip, uint16 serverport, uint8* pdebug_lowiddropped = NULL, bool ed2kID = true);
128 void AddSources(CMemFile& sources, uint32 serverip, uint16 serverport, unsigned origin, bool bWithObfuscationAndHash);
129 #ifdef CLIENT_GUI
130 uint8 GetStatus() const { return status; }
131 uint8 GetStatus(bool /*ignorepause = false*/) const { return status; }
132 #else
133 uint8 GetStatus(bool ignorepause = false) const;
134 #endif
135 virtual void UpdatePartsInfo();
136 const CPath& GetPartMetFileName() const { return m_partmetfilename; }
137 uint16 GetPartMetNumber() const;
138 uint64 GetTransferred() const { return transferred; }
139 const CPath& GetFullName() const { return m_fullname; }
140 float GetKBpsDown() const { return kBpsDown; }
141 double GetPercentCompleted() const { return percentcompleted; }
143 #ifndef CLIENT_GUI
144 uint16 GetSourceCount() const { return (uint16)m_SrcList.size(); }
145 uint16 GetSrcA4AFCount() const { return (uint16)m_A4AFsrclist.size(); }
146 #else
147 uint16 m_source_count, m_a4af_source_count;
148 uint16 GetSourceCount() const { return m_source_count; }
149 uint16 GetSrcA4AFCount() const { return m_a4af_source_count; }
150 #endif
151 uint16 GetTransferingSrcCount() const { return transferingsrc; }
152 uint16 GetNotCurrentSourcesCount() const { return m_notCurrentSources; };
153 void SetNotCurrentSourcesCount(uint16 new_count) { m_notCurrentSources = new_count; };
154 uint16 GetValidSourcesCount() const { return m_validSources; };
156 uint64 GetNeededSpace();
158 virtual wxString GetFeedback() const;
160 wxString getPartfileStatus() const; //<<--9/21/02
161 sint32 getTimeRemaining() const; //<<--9/21/02
162 time_t lastseencomplete;
163 int getPartfileStatusRang() const;
165 // Barry - Added as replacement for BlockReceived to buffer data before writing to disk
166 uint32 WriteToBuffer(uint32 transize, byte *data, uint64 start, uint64 end, Requested_Block_Struct *block, const CUpDownClient* client);
167 void FlushBuffer(bool fromAICHRecoveryDataAvailable = false);
169 // Barry - Is archive recovery in progress
170 volatile bool m_bRecoveringArchive;
172 // Barry - Added to prevent list containing deleted blocks on shutdown
173 void RemoveAllRequestedBlocks(void);
175 void RemoveBlockFromList(uint64 start,uint64 end);
176 void RemoveAllSources(bool bTryToSwap);
177 void Delete();
178 void StopFile(bool bCancel = false);
179 void PauseFile(bool bInsufficient = false);
180 void ResumeFile();
182 virtual CPacket* CreateSrcInfoPacket(const CUpDownClient* forClient, uint8 byRequestedVersion, uint16 nRequestedOptions);
183 void AddClientSources(CMemFile* sources, unsigned nSourceFrom, uint8 uClientSXVersion, bool bSourceExchange2, const CUpDownClient* pClient = NULL);
185 bool PreviewAvailable();
186 uint16 GetAvailablePartCount() const { return m_availablePartsCount; }
187 uint32 GetLastAnsweredTime() const { return m_ClientSrcAnswered; }
188 void SetLastAnsweredTime();
189 void SetLastAnsweredTimeTimeout();
190 uint64 GetLostDueToCorruption() const { return m_iLostDueToCorruption; }
191 uint64 GetGainDueToCompression() const { return m_iGainDueToCompression; }
192 uint32 TotalPacketsSavedDueToICH()const{ return m_iTotalPacketsSavedDueToICH; }
193 bool IsStopped() const { return this ? m_stopped : true; }
194 bool IsPaused() const { return m_paused; }
195 void UpdateFileRatingCommentAvail();
197 int GetCommonFilePenalty();
198 void UpdateDisplayedInfo(bool force = false);
200 uint8 GetCategory() const { return m_category; }
201 void SetCategory(uint8 cat);
203 volatile bool m_bPreviewing;
204 void SetDownPriority(uint8 newDownPriority, bool bSave = true, bool bRefresh = true);
205 bool IsAutoDownPriority() const { return m_bAutoDownPriority; }
206 void SetAutoDownPriority(bool flag) { m_bAutoDownPriority = flag; }
207 void UpdateAutoDownPriority();
208 uint8 GetDownPriority() const { return m_iDownPriority; }
209 void SetActive(bool bActive);
210 uint32 GetDlActiveTime() const;
211 bool GetInsufficient() const { return m_insufficient; }
213 void CompleteFileEnded(bool errorOccured, const CPath& newname);
215 bool RemoveSource(CUpDownClient* toremove, bool updatewindow = true, bool bDoStatsUpdate = true);
217 void RequestAICHRecovery(uint16 nPart);
218 void AICHRecoveryDataAvailable(uint16 nPart);
221 * This function is used to update source-counts.
223 * @param oldState The old state of the client, or -1 to ignore.
224 * @param newState The new state of the client, or -1 to ignore.
226 * Call this function for a client belonging to this file, which has changed
227 * its state. The value -1 can be used to make the function ignore one of
228 * the two states.
230 * AddSource and DelSource takes care of calling this function when a source is
231 * removed, so there's no need to call this function when calling either of those.
233 void ClientStateChanged( int oldState, int newState );
235 bool AddSource( CUpDownClient* client );
236 bool DelSource( CUpDownClient* client );
239 * Updates the requency of avilable parts from with the data the client provides.
241 * @param client The clients whoose available parts should be considered.
242 * @param increment If true, the counts are incremented, otherwise they are decremented.
244 * This functions updates the frequency list of file-parts, using the clients
245 * parts-status. This function should be called by clients every time they update their
246 * parts-status, or when they are added or removed from the file.
248 void UpdatePartsFrequency( CUpDownClient* client, bool increment );
250 ArrayOfUInts16 m_SrcpartFrequency;
252 void SetShowSources( bool val ) { m_showSources = val; }
253 bool ShowSources() const { return m_showSources; }
255 typedef std::set<CUpDownClient*> SourceSet;
257 const SourceSet& GetSourceList() const { return m_SrcList; }
258 const SourceSet& GetA4AFList() const { return m_A4AFsrclist; }
260 const CReqBlockPtrList GetRequestedBlockList() const { return m_requestedblocks_list; }
262 // LEGACY - to be removed when possible
263 class CGapPtrList {
264 public:
265 void Init(const CGapList * gaplist) { m_gaplist = gaplist; }
266 class const_iterator {
267 CGapList::const_iterator m_it;
268 Gap_Struct m_gap;
269 public:
270 const_iterator() {};
271 const_iterator(const CGapList::const_iterator& it) { m_it = it; };
272 bool operator != (const const_iterator& it) { return m_it != it.m_it; }
273 const_iterator& operator ++ () { ++ m_it; return *this; }
274 Gap_Struct * operator * () {
275 m_gap.start = m_it.start();
276 m_gap.end = m_it.end();
277 return & m_gap;
280 const_iterator begin() const { return const_iterator(m_gaplist->begin()); }
281 const_iterator end() const { return const_iterator(m_gaplist->end()); }
282 bool empty() const { return m_gaplist->IsComplete(); }
283 uint32 size() const { return m_gaplist->size(); }
285 private:
286 const CGapList * m_gaplist;
288 // this function must stay, but return the m_gaplist instead
289 const CGapPtrList& GetGapList() const { return m_gapptrlist; }
290 // meanwhile use
291 const CGapList& GetNewGapList() const { return m_gaplist; }
292 private:
293 CGapPtrList m_gapptrlist;
294 public:
295 // END LEGACY
298 * Adds a source to the list of dead sources.
300 * @param client The source to be recorded as dead for this file.
302 void AddDeadSource(const CUpDownClient* client);
305 * Checks if a source is recorded as being dead for this file.
307 * @param client The client to evaluate.
308 * @return True if dead, false otherwise.
310 * Sources that are dead are not to be considered valid
311 * sources and should not be added to the partfile.
313 bool IsDeadSource(const CUpDownClient* client);
315 /* Kad Stuff */
316 uint16 GetMaxSources() const;
317 uint16 GetMaxSourcePerFileSoft() const;
318 uint16 GetMaxSourcePerFileUDP() const;
320 void GetRatingAndComments(FileRatingList & list) const;
322 void AllocationFinished();
323 private:
324 #ifndef CLIENT_GUI
325 // partfile handle (opened on demand)
326 CFileAutoClose m_hpartfile;
327 //! A local list of sources that are invalid for this file.
328 CDeadSourceList m_deadSources;
330 class CCorruptionBlackBox* m_CorruptionBlackBox;
331 #endif
333 uint16 m_notCurrentSources;
335 bool m_showSources;
337 uint32 m_validSources;
339 void AddGap(uint64 start, uint64 end);
340 void AddGap(uint16 part);
341 void FillGap(uint64 start, uint64 end);
342 void FillGap(uint16 part);
343 bool GetNextEmptyBlockInPart(uint16 partnumber,Requested_Block_Struct* result);
344 bool IsAlreadyRequested(uint64 start, uint64 end);
345 void CompleteFile(bool hashingdone);
346 void CreatePartFile();
347 void Init();
349 bool CheckFreeDiskSpace( uint64 neededSpace = 0 );
351 bool IsCorruptedPart(uint16 partnumber);
353 uint32 m_iLastPausePurge;
354 uint16 m_count;
355 uint16 transferingsrc;
356 uint64 completedsize;
357 uint64 transferred;
359 uint64 m_iLostDueToCorruption;
360 uint64 m_iGainDueToCompression;
361 uint32 m_iTotalPacketsSavedDueToICH;
362 float kBpsDown;
363 CPath m_fullname; // path/name of the met file
364 CPath m_partmetfilename; // name of the met file
365 CPath m_PartPath; // path/name of the partfile
366 bool m_paused;
367 bool m_stopped;
368 bool m_insufficient;
369 uint8 m_iDownPriority;
370 bool m_bAutoDownPriority;
371 uint8 status;
372 uint32 lastpurgetime;
373 uint32 m_LastNoNeededCheck;
374 CGapList m_gaplist;
375 CReqBlockPtrList m_requestedblocks_list;
376 double percentcompleted;
377 std::list<uint16> m_corrupted_list;
378 uint16 m_availablePartsCount;
379 uint32 m_ClientSrcAnswered;
380 bool m_bPercentUpdated;
382 void PerformFileComplete();
384 uint32 m_lastRefreshedDLDisplay;
386 // Buffered data to be written
387 std::list<class PartFileBufferedData*> m_BufferedData_list;
389 uint32 m_nTotalBufferData;
390 uint32 m_nLastBufferFlushTime;
392 uint8 m_category;
393 uint32 m_nDlActiveTime;
394 time_t m_tActivated;
395 bool m_is_A4AF_auto;
397 SourceSet m_SrcList;
398 SourceSet m_A4AFsrclist;
399 bool m_hashsetneeded;
400 uint32 m_lastsearchtime;
401 bool m_localSrcReqQueued;
403 #ifdef CLIENT_GUI
404 FileRatingList m_FileRatingList;
405 const FileRatingList &GetFileRatingList() { return m_FileRatingList; }
406 void ClearFileRatingList() { m_FileRatingList.clear(); }
407 void AddFileRatingList(const wxString & u, const wxString & f, sint16 r, const wxString & c) {
408 m_FileRatingList.push_back(SFileRating(u, f, r, c)); }
410 uint32 m_kbpsDown;
411 uint8 m_iDownPriorityEC;
412 bool m_isShared;
413 SourcenameItemMap m_SourcenameItemMap;
414 public:
415 bool IsShared() const { return m_isShared; }
416 SourcenameItemMap &GetSourcenameItemMap() { return m_SourcenameItemMap; }
417 PartFileEncoderData m_PartFileEncoderData;
418 #endif
419 public:
420 bool IsHashSetNeeded() const { return m_hashsetneeded; }
421 void SetHashSetNeeded(bool value) { m_hashsetneeded = value; }
423 uint64 GetCompletedSize() const { return completedsize; }
424 void SetCompletedSize(uint64 size) { completedsize = size; }
426 bool IsLocalSrcRequestQueued() const { return m_localSrcReqQueued; }
427 void SetLocalSrcRequestQueued(bool value) { m_localSrcReqQueued = value; }
429 void AddA4AFSource(CUpDownClient* src) { m_A4AFsrclist.insert(src); }
430 bool RemoveA4AFSource(CUpDownClient* src) { return (m_A4AFsrclist.erase(src) > 0); }
432 uint32 GetLastSearchTime() const { return m_lastsearchtime; }
433 void SetLastSearchTime(uint32 time) { m_lastsearchtime = time; }
435 // void CleanUpSources( bool noNeeded, bool fullQueue = false, bool highQueue = false );
437 void AddDownloadingSource(CUpDownClient* client);
439 void RemoveDownloadingSource(CUpDownClient* client);
440 void SetStatus(uint8 in);
441 void StopPausedFile();
443 // [sivka / Tarod] Imported from eMule 0.30c (Creteil) ...
444 void SetA4AFAuto(bool in) { m_is_A4AF_auto = in; }
445 bool IsA4AFAuto() const { return m_is_A4AF_auto; }
447 // Kry -Sources seeds
448 void SaveSourceSeeds();
449 void LoadSourceSeeds();
451 // Dropping slow sources
452 CUpDownClient* GetSlowerDownloadingClient(uint32 speed, CUpDownClient* caller);
454 // Read data for sharing
455 bool ReadData(class CFileArea & area, uint64 offset, uint32 toread);
457 private:
458 /* downloading sources list */
459 CClientPtrList m_downloadingSourcesList;
461 /* Kad Stuff */
462 uint32 m_LastSearchTimeKad;
463 uint8 m_TotalSearchesKad;
465 friend class CKnownFilesRem;
466 friend class CPartFileConvert;
469 #endif // PARTFILE_H
470 // File_checked_for_headers