Upstream tarball 10019
[amule.git] / src / PartFile.h
blob6b83de05d3309e8fd26b4ad568084a7f1f02cf64
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 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 uint16 GetPartMetNumber() const;
137 uint64 GetTransferred() const { return transferred; }
138 const CPath& GetFullName() const { return m_fullname; }
139 float GetKBpsDown() const { return kBpsDown; }
140 double GetPercentCompleted() const { return percentcompleted; }
142 #ifndef CLIENT_GUI
143 uint16 GetSourceCount() const { return (uint16)m_SrcList.size(); }
144 uint16 GetSrcA4AFCount() const { return (uint16)m_A4AFsrclist.size(); }
145 #else
146 uint16 m_source_count, m_a4af_source_count;
147 uint16 GetSourceCount() const { return m_source_count; }
148 uint16 GetSrcA4AFCount() const { return m_a4af_source_count; }
149 #endif
150 uint16 GetTransferingSrcCount() const { return transferingsrc; }
151 uint16 GetNotCurrentSourcesCount() const { return m_notCurrentSources; };
152 void SetNotCurrentSourcesCount(uint16 new_count) { m_notCurrentSources = new_count; };
153 uint16 GetValidSourcesCount() const { return m_validSources; };
155 uint64 GetNeededSpace();
157 virtual wxString GetFeedback() const;
159 wxString getPartfileStatus() const; //<<--9/21/02
160 sint32 getTimeRemaining() const; //<<--9/21/02
161 time_t lastseencomplete;
162 int getPartfileStatusRang() const;
164 // Barry - Added as replacement for BlockReceived to buffer data before writing to disk
165 uint32 WriteToBuffer(uint32 transize, byte *data, uint64 start, uint64 end, Requested_Block_Struct *block, const CUpDownClient* client);
166 void FlushBuffer(bool fromAICHRecoveryDataAvailable = false);
168 // Barry - Is archive recovery in progress
169 volatile bool m_bRecoveringArchive;
171 // Barry - Added to prevent list containing deleted blocks on shutdown
172 void RemoveAllRequestedBlocks(void);
174 void RemoveBlockFromList(uint64 start,uint64 end);
175 void RemoveAllSources(bool bTryToSwap);
176 void Delete();
177 void StopFile(bool bCancel = false);
178 void PauseFile(bool bInsufficient = false);
179 void ResumeFile();
181 virtual CPacket* CreateSrcInfoPacket(const CUpDownClient* forClient, uint8 byRequestedVersion, uint16 nRequestedOptions);
182 void AddClientSources(CMemFile* sources, unsigned nSourceFrom, uint8 uClientSXVersion, bool bSourceExchange2, const CUpDownClient* pClient = NULL);
184 bool PreviewAvailable();
185 uint16 GetAvailablePartCount() const { return m_availablePartsCount; }
186 uint32 GetLastAnsweredTime() const { return m_ClientSrcAnswered; }
187 void SetLastAnsweredTime();
188 void SetLastAnsweredTimeTimeout();
189 uint64 GetLostDueToCorruption() const { return m_iLostDueToCorruption; }
190 uint64 GetGainDueToCompression() const { return m_iGainDueToCompression; }
191 uint32 TotalPacketsSavedDueToICH()const{ return m_iTotalPacketsSavedDueToICH; }
192 bool IsStopped() const { return this ? m_stopped : true; }
193 bool IsPaused() const { return m_paused; }
194 void UpdateFileRatingCommentAvail();
196 int GetCommonFilePenalty();
197 void UpdateDisplayedInfo(bool force = false);
199 uint8 GetCategory() const { return m_category; }
200 void SetCategory(uint8 cat);
202 volatile bool m_bPreviewing;
203 void SetDownPriority(uint8 newDownPriority, bool bSave = true, bool bRefresh = true);
204 bool IsAutoDownPriority() const { return m_bAutoDownPriority; }
205 void SetAutoDownPriority(bool flag) { m_bAutoDownPriority = flag; }
206 void UpdateAutoDownPriority();
207 uint8 GetDownPriority() const { return m_iDownPriority; }
208 void SetActive(bool bActive);
209 uint32 GetDlActiveTime() const;
210 bool GetInsufficient() const { return m_insufficient; }
212 void CompleteFileEnded(bool errorOccured, const CPath& newname);
214 bool RemoveSource(CUpDownClient* toremove, bool updatewindow = true, bool bDoStatsUpdate = true);
216 void RequestAICHRecovery(uint16 nPart);
217 void AICHRecoveryDataAvailable(uint16 nPart);
220 * This function is used to update source-counts.
222 * @param oldState The old state of the client, or -1 to ignore.
223 * @param newState The new state of the client, or -1 to ignore.
225 * Call this function for a client belonging to this file, which has changed
226 * its state. The value -1 can be used to make the function ignore one of
227 * the two states.
229 * AddSource and DelSource takes care of calling this function when a source is
230 * removed, so there's no need to call this function when calling either of those.
232 void ClientStateChanged( int oldState, int newState );
234 bool AddSource( CUpDownClient* client );
235 bool DelSource( CUpDownClient* client );
238 * Updates the requency of avilable parts from with the data the client provides.
240 * @param client The clients whoose available parts should be considered.
241 * @param increment If true, the counts are incremented, otherwise they are decremented.
243 * This functions updates the frequency list of file-parts, using the clients
244 * parts-status. This function should be called by clients every time they update their
245 * parts-status, or when they are added or removed from the file.
247 void UpdatePartsFrequency( CUpDownClient* client, bool increment );
249 ArrayOfUInts16 m_SrcpartFrequency;
251 void SetShowSources( bool val ) { m_showSources = val; }
252 bool ShowSources() const { return m_showSources; }
254 typedef std::set<CUpDownClient*> SourceSet;
256 const SourceSet& GetSourceList() const { return m_SrcList; }
257 const SourceSet& GetA4AFList() const { return m_A4AFsrclist; }
259 const CReqBlockPtrList GetRequestedBlockList() const { return m_requestedblocks_list; }
261 // LEGACY - to be removed when possible
262 class CGapPtrList {
263 public:
264 void Init(const CGapList * gaplist) { m_gaplist = gaplist; }
265 class const_iterator {
266 CGapList::const_iterator m_it;
267 Gap_Struct m_gap;
268 public:
269 const_iterator() {};
270 const_iterator(const CGapList::const_iterator& it) { m_it = it; };
271 bool operator != (const const_iterator& it) { return m_it != it.m_it; }
272 const_iterator& operator ++ () { ++ m_it; return *this; }
273 Gap_Struct * operator * () {
274 m_gap.start = m_it.start();
275 m_gap.end = m_it.end();
276 return & m_gap;
279 const_iterator begin() const { return const_iterator(m_gaplist->begin()); }
280 const_iterator end() const { return const_iterator(m_gaplist->end()); }
281 bool empty() const { return m_gaplist->IsComplete(); }
282 uint32 size() const { return m_gaplist->size(); }
284 private:
285 const CGapList * m_gaplist;
287 // this function must stay, but return the m_gaplist instead
288 const CGapPtrList& GetGapList() const { return m_gapptrlist; }
289 // meanwhile use
290 const CGapList& GetNewGapList() const { return m_gaplist; }
291 private:
292 CGapPtrList m_gapptrlist;
293 public:
294 // END LEGACY
297 * Adds a source to the list of dead sources.
299 * @param client The source to be recorded as dead for this file.
301 void AddDeadSource(const CUpDownClient* client);
304 * Checks if a source is recorded as being dead for this file.
306 * @param client The client to evaluate.
307 * @return True if dead, false otherwise.
309 * Sources that are dead are not to be considered valid
310 * sources and should not be added to the partfile.
312 bool IsDeadSource(const CUpDownClient* client);
314 /* Kad Stuff */
315 uint16 GetMaxSources() const;
316 uint16 GetMaxSourcePerFileSoft() const;
317 uint16 GetMaxSourcePerFileUDP() const;
319 const FileRatingList &GetRatingAndComments();
321 void AllocationFinished();
322 private:
323 #ifndef CLIENT_GUI
324 // partfile handle (opened on demand)
325 CFileAutoClose m_hpartfile;
326 //! A local list of sources that are invalid for this file.
327 CDeadSourceList m_deadSources;
329 class CCorruptionBlackBox* m_CorruptionBlackBox;
330 #endif
332 uint16 m_notCurrentSources;
334 bool m_showSources;
336 uint32 m_validSources;
338 void AddGap(uint64 start, uint64 end);
339 void AddGap(uint16 part);
340 void FillGap(uint64 start, uint64 end);
341 void FillGap(uint16 part);
342 bool GetNextEmptyBlockInPart(uint16 partnumber,Requested_Block_Struct* result);
343 bool IsAlreadyRequested(uint64 start, uint64 end);
344 void CompleteFile(bool hashingdone);
345 void CreatePartFile();
346 void Init();
348 bool CheckFreeDiskSpace( uint64 neededSpace = 0 );
350 bool IsCorruptedPart(uint16 partnumber);
352 uint32 m_iLastPausePurge;
353 uint16 m_count;
354 uint16 transferingsrc;
355 uint64 completedsize;
356 uint64 transferred;
358 uint64 m_iLostDueToCorruption;
359 uint64 m_iGainDueToCompression;
360 uint32 m_iTotalPacketsSavedDueToICH;
361 float kBpsDown;
362 CPath m_fullname; // path/name of the met file
363 CPath m_partmetfilename; // name of the met file
364 CPath m_PartPath; // path/name of the partfile
365 bool m_paused;
366 bool m_stopped;
367 bool m_insufficient;
368 uint8 m_iDownPriority;
369 bool m_bAutoDownPriority;
370 uint8 status;
371 uint32 lastpurgetime;
372 uint32 m_LastNoNeededCheck;
373 CGapList m_gaplist;
374 CReqBlockPtrList m_requestedblocks_list;
375 double percentcompleted;
376 std::list<uint16> m_corrupted_list;
377 uint16 m_availablePartsCount;
378 uint32 m_ClientSrcAnswered;
379 bool m_bPercentUpdated;
381 void PerformFileComplete();
383 uint32 m_lastRefreshedDLDisplay;
385 // Buffered data to be written
386 std::list<class PartFileBufferedData*> m_BufferedData_list;
388 uint32 m_nTotalBufferData;
389 uint32 m_nLastBufferFlushTime;
391 uint8 m_category;
392 uint32 m_nDlActiveTime;
393 time_t m_tActivated;
394 bool m_is_A4AF_auto;
396 SourceSet m_SrcList;
397 SourceSet m_A4AFsrclist;
398 bool m_hashsetneeded;
399 uint32 m_lastsearchtime;
400 bool m_localSrcReqQueued;
402 FileRatingList m_FileRatingList;
403 #ifdef CLIENT_GUI
404 uint32 m_kbpsDown;
405 uint8 m_iDownPriorityEC;
406 SourcenameItemMap m_SourcenameItemMap;
407 public:
408 SourcenameItemMap &GetSourcenameItemMap() { return m_SourcenameItemMap; }
409 #endif
410 public:
411 const FileRatingList &GetFileRatingList() { return m_FileRatingList; }
412 void ClearFileRatingList() { m_FileRatingList.clear(); }
413 void AddFileRatingList(const wxString & u, const wxString & f, sint16 r, const wxString & c) {
414 m_FileRatingList.push_back(SFileRating(u, f, r, c)); }
416 bool IsHashSetNeeded() const { return m_hashsetneeded; }
417 void SetHashSetNeeded(bool value) { m_hashsetneeded = value; }
419 uint64 GetCompletedSize() const { return completedsize; }
420 void SetCompletedSize(uint64 size) { completedsize = size; }
422 bool IsLocalSrcRequestQueued() const { return m_localSrcReqQueued; }
423 void SetLocalSrcRequestQueued(bool value) { m_localSrcReqQueued = value; }
425 void AddA4AFSource(CUpDownClient* src) { m_A4AFsrclist.insert(src); }
426 bool RemoveA4AFSource(CUpDownClient* src) { return (m_A4AFsrclist.erase(src) > 0); }
428 uint32 GetLastSearchTime() const { return m_lastsearchtime; }
429 void SetLastSearchTime(uint32 time) { m_lastsearchtime = time; }
431 // void CleanUpSources( bool noNeeded, bool fullQueue = false, bool highQueue = false );
433 void AddDownloadingSource(CUpDownClient* client);
435 void RemoveDownloadingSource(CUpDownClient* client);
436 void SetStatus(uint8 in);
437 void StopPausedFile();
439 // [sivka / Tarod] Imported from eMule 0.30c (Creteil) ...
440 void SetA4AFAuto(bool in) { m_is_A4AF_auto = in; }
441 bool IsA4AFAuto() const { return m_is_A4AF_auto; }
443 // Kry -Sources seeds
444 void SaveSourceSeeds();
445 void LoadSourceSeeds();
447 // Dropping slow sources
448 CUpDownClient* GetSlowerDownloadingClient(uint32 speed, CUpDownClient* caller);
450 // Read data for sharing
451 bool ReadData(class CFileArea & area, uint64 offset, uint32 toread);
453 private:
454 /* downloading sources list */
455 CClientPtrList m_downloadingSourcesList;
457 /* Kad Stuff */
458 uint32 m_LastSearchTimeKad;
459 uint8 m_TotalSearchesKad;
461 friend class CDownQueueRem;
462 friend class CPartFileConvert;
465 #endif // PARTFILE_H
466 // File_checked_for_headers