Upstream tarball 9324
[amule.git] / src / PartFile.h
blobee46b1fdd28a3ba31bae08dd22353b31ec207be6
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 "CFile.h" // Needed for CFile
33 #include "OtherStructs.h" // Needed for Gap_Struct
34 #include "DeadSourceList.h" // Needed for CDeadSourceList
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 #define STATES_COUNT 13
49 // Ok, eMule and aMule are building incompatible backup files because
50 // of the different name. aMule was using ".BAK" and eMule ".bak".
51 // This should fix it.
52 #define PARTMET_BAK_EXT wxT(".bak")
54 enum EPartFileFormat {
55 PMT_UNKNOWN = 0,
56 PMT_DEFAULTOLD,
57 PMT_SPLITTED,
58 PMT_NEWOLD,
59 PMT_SHAREAZA,
60 PMT_BADFORMAT
63 struct PartFileBufferedData
65 byte *data; // Barry - This is the data to be written
66 uint64 start; // Barry - This is the start offset of the data
67 uint64 end; // Barry - This is the end offset of the data
68 Requested_Block_Struct *block; // Barry - This is the requested block that this data relates to
72 class SFileRating
74 public:
75 wxString UserName;
76 wxString FileName;
77 sint16 Rating;
78 wxString Comment;
79 public:
80 SFileRating(const wxString &u, const wxString &f, sint16 r, const wxString &c);
81 SFileRating(const SFileRating &fr);
82 SFileRating(const CUpDownClient &client);
83 ~SFileRating();
86 typedef std::list<SFileRating> FileRatingList;
88 class SourcenameItem
90 public:
91 wxString name;
92 long count;
93 public:
94 SourcenameItem(const wxString &n, long c)
96 name(n), count(c) {}
99 typedef std::list<SourcenameItem> SourcenameItemList;
101 class CPartFile : public CKnownFile {
102 public:
103 typedef std::list<Gap_Struct*> CGapPtrList;
104 typedef std::list<Requested_Block_Struct*> CReqBlockPtrList;
107 CPartFile();
108 #ifdef CLIENT_GUI
109 CPartFile(CEC_PartFile_Tag *tag);
110 #else
111 virtual void SetFileName(const CPath& filename);
112 #endif
113 CPartFile(CSearchFile* searchresult); //used when downloading a new file
114 CPartFile(const CED2KFileLink* fileLink);
115 virtual ~CPartFile();
117 bool CreateFromFile(wxString WXUNUSED(directory), wxString WXUNUSED(filename), void* WXUNUSED(pvProgressParam)) {return false;}// not supported in this class
118 void SetPartFileStatus(uint8 newstatus);
119 virtual bool LoadFromFile(const CFileDataIO* WXUNUSED(file)) { return false; }
120 bool WriteToFile(CFileDataIO* WXUNUSED(file)) { return false; }
121 bool IsPartFile() const { return !(status == PS_COMPLETE); }
122 uint32 Process(uint32 reducedownload, uint8 m_icounter);
123 uint8 LoadPartFile(const CPath& in_directory, const CPath& filename, bool from_backup = false, bool getsizeonly = false);
124 bool SavePartFile(bool Initial = false);
125 void PartFileHashFinished(CKnownFile* result);
126 bool HashSinglePart(uint16 partnumber); // true = ok , false = corrupted
128 bool CheckShowItemInGivenCat(int inCategory);
130 bool IsComplete(uint64 start, uint64 end);
132 void UpdateCompletedInfos();
134 bool GetNextRequestedBlock(CUpDownClient* sender, std::vector<Requested_Block_Struct*>& toadd, uint16& count);
135 void WritePartStatus(CMemFile* file);
136 void WriteCompleteSourcesCount(CMemFile* file);
137 static bool CanAddSource(uint32 userid, uint16 port, uint32 serverip, uint16 serverport, uint8* pdebug_lowiddropped = NULL, bool ed2kID = true);
138 void AddSources(CMemFile& sources, uint32 serverip, uint16 serverport, unsigned origin, bool bWithObfuscationAndHash);
139 #ifdef CLIENT_GUI
140 uint8 GetStatus() const { return status; }
141 uint8 GetStatus(bool /*ignorepause = false*/) const { return status; }
142 #else
143 uint8 GetStatus(bool ignorepause = false) const;
144 #endif
145 virtual void UpdatePartsInfo();
146 const CPath& GetPartMetFileName() const { return m_partmetfilename; }
147 uint64 GetTransferred() const { return transferred; }
148 const CPath& GetFullName() const { return m_fullname; }
149 float GetKBpsDown() const { return kBpsDown; }
150 double GetPercentCompleted() const { return percentcompleted; }
152 #ifndef CLIENT_GUI
153 uint16 GetSourceCount() const { return (uint16)m_SrcList.size(); }
154 uint16 GetSrcA4AFCount() const { return (uint16)m_A4AFsrclist.size(); }
155 #else
156 uint16 m_source_count, m_a4af_source_count;
157 uint16 GetSourceCount() const { return m_source_count; }
158 uint16 GetSrcA4AFCount() const { return m_a4af_source_count; }
159 #endif
160 uint16 GetTransferingSrcCount() const { return transferingsrc; }
161 uint16 GetNotCurrentSourcesCount() const { return m_notCurrentSources; };
162 void SetNotCurrentSourcesCount(uint16 new_count) { m_notCurrentSources = new_count; };
163 uint16 GetValidSourcesCount() const { return m_validSources; };
165 uint64 GetNeededSpace();
167 virtual wxString GetFeedback() const;
169 wxString getPartfileStatus() const; //<<--9/21/02
170 sint32 getTimeRemaining() const; //<<--9/21/02
171 time_t lastseencomplete;
172 int getPartfileStatusRang() const;
174 // Barry - Added as replacement for BlockReceived to buffer data before writing to disk
175 uint32 WriteToBuffer(uint32 transize, byte *data, uint64 start, uint64 end, Requested_Block_Struct *block);
176 void FlushBuffer(bool forcewait=false, bool bForceICH = false, bool bNoAICH = false);
178 // Barry - Is archive recovery in progress
179 volatile bool m_bRecoveringArchive;
181 // Barry - Added to prevent list containing deleted blocks on shutdown
182 void RemoveAllRequestedBlocks(void);
184 void RemoveBlockFromList(uint64 start,uint64 end);
185 void RemoveAllSources(bool bTryToSwap);
186 void Delete();
187 void StopFile(bool bCancel = false);
188 void PauseFile(bool bInsufficient = false);
189 void ResumeFile();
191 virtual CPacket* CreateSrcInfoPacket(const CUpDownClient* forClient, uint8 byRequestedVersion, uint16 nRequestedOptions);
192 void AddClientSources(CMemFile* sources, unsigned nSourceFrom, uint8 uClientSXVersion, bool bSourceExchange2, const CUpDownClient* pClient = NULL);
194 bool PreviewAvailable();
195 uint16 GetAvailablePartCount() const { return m_availablePartsCount; }
196 uint32 GetLastAnsweredTime() const { return m_ClientSrcAnswered; }
197 void SetLastAnsweredTime();
198 void SetLastAnsweredTimeTimeout();
199 uint64 GetLostDueToCorruption() const { return m_iLostDueToCorruption; }
200 uint64 GetGainDueToCompression() const { return m_iGainDueToCompression; }
201 uint32 TotalPacketsSavedDueToICH()const{ return m_iTotalPacketsSavedDueToICH; }
202 bool IsStopped() const { return this ? m_stopped : true; }
203 bool IsPaused() const { return m_paused; }
204 void UpdateFileRatingCommentAvail();
205 bool m_CommentUpdated;
207 int GetCommonFilePenalty();
208 void UpdateDisplayedInfo(bool force = false);
210 uint8 GetCategory() const { return m_category; }
211 void SetCategory(uint8 cat);
213 volatile bool m_bPreviewing;
214 void SetDownPriority(uint8 newDownPriority, bool bSave = true, bool bRefresh = true);
215 bool IsAutoDownPriority() const { return m_bAutoDownPriority; }
216 void SetAutoDownPriority(bool flag) { m_bAutoDownPriority = flag; }
217 void UpdateAutoDownPriority();
218 uint8 GetDownPriority() const { return m_iDownPriority; }
219 void SetActive(bool bActive);
220 uint32 GetDlActiveTime() const;
221 bool GetInsufficient() const { return m_insufficient; }
223 void CompleteFileEnded(bool errorOccured, const CPath& newname);
225 bool RemoveSource(CUpDownClient* toremove, bool updatewindow = true, bool bDoStatsUpdate = true);
227 void RequestAICHRecovery(uint16 nPart);
228 void AICHRecoveryDataAvailable(uint16 nPart);
231 * This function is used to update source-counts.
233 * @param oldState The old state of the client, or -1 to ignore.
234 * @param newState The new state of the client, or -1 to ignore.
236 * Call this function for a client belonging to this file, which has changed
237 * its state. The value -1 can be used to make the function ignore one of
238 * the two states.
240 * AddSource and DelSource takes care of calling this function when a source is
241 * removed, so there's no need to call this function when calling either of those.
243 void ClientStateChanged( int oldState, int newState );
245 bool AddSource( CUpDownClient* client );
246 bool DelSource( CUpDownClient* client );
249 * Updates the requency of avilable parts from with the data the client provides.
251 * @param client The clients whoose available parts should be considered.
252 * @param increment If true, the counts are incremented, otherwise they are decremented.
254 * This functions updates the frequency list of file-parts, using the clients
255 * parts-status. This function should be called by clients every time they update their
256 * parts-status, or when they are added or removed from the file.
258 void UpdatePartsFrequency( CUpDownClient* client, bool increment );
260 ArrayOfUInts16 m_SrcpartFrequency;
262 void SetShowSources( bool val ) { m_showSources = val; }
263 bool ShowSources() const { return m_showSources; }
265 typedef std::set<CUpDownClient*> SourceSet;
267 const SourceSet& GetSourceList() const { return m_SrcList; }
268 const SourceSet& GetA4AFList() const { return m_A4AFsrclist; }
270 const CGapPtrList GetGapList() const { return m_gaplist; }
271 const CReqBlockPtrList GetRequestedBlockList() const { return m_requestedblocks_list; }
275 * Adds a source to the list of dead sources.
277 * @param client The source to be recorded as dead for this file.
279 void AddDeadSource(const CUpDownClient* client);
282 * Checks if a source is recorded as being dead for this file.
284 * @param client The client to evaluate.
285 * @return True if dead, false otherwise.
287 * Sources that are dead are not to be considered valid
288 * sources and should not be added to the partfile.
290 bool IsDeadSource(const CUpDownClient* client);
292 /* Kad Stuff */
293 uint16 GetMaxSources() const;
294 uint16 GetMaxSourcePerFileSoft() const;
295 uint16 GetMaxSourcePerFileUDP() const;
297 const FileRatingList &GetRatingAndComments();
299 void AllocationFinished();
300 private:
301 CFile m_hpartfile; //permanent opened handle to avoid write conflicts
302 //! A local list of sources that are invalid for this file.
303 #ifndef CLIENT_GUI
304 CDeadSourceList m_deadSources;
305 #endif
307 uint16 m_notCurrentSources;
309 bool m_showSources;
311 uint32 m_validSources;
313 void AddGap(uint64 start, uint64 end);
314 void FillGap(uint64 start, uint64 end);
315 bool GetNextEmptyBlockInPart(uint16 partnumber,Requested_Block_Struct* result);
316 bool IsAlreadyRequested(uint64 start, uint64 end);
317 void CompleteFile(bool hashingdone);
318 void CreatePartFile();
319 void Init();
321 bool CheckFreeDiskSpace( uint64 neededSpace = 0 );
323 bool IsCorruptedPart(uint16 partnumber);
325 uint64 GetTotalGapSizeInPart(uint32 uPart) const;
327 uint64 GetTotalGapSizeInRange(uint64 uRangeStart, uint64 uRangeEnd) const;
329 uint32 m_iLastPausePurge;
330 uint16 m_count;
331 uint16 m_anStates[STATES_COUNT];
332 uint16 transferingsrc;
333 uint64 completedsize;
334 uint64 transferred;
336 uint64 m_iLostDueToCorruption;
337 uint64 m_iGainDueToCompression;
338 uint32 m_iTotalPacketsSavedDueToICH;
339 float kBpsDown;
340 CPath m_fullname; // path/name of the met file
341 CPath m_partmetfilename; // name of the met file
342 CPath m_PartPath; // path/name of the partfile
343 bool m_paused;
344 bool m_stopped;
345 bool m_insufficient;
346 uint8 m_iDownPriority;
347 bool m_bAutoDownPriority;
348 uint8 status;
349 uint32 lastpurgetime;
350 uint32 m_LastNoNeededCheck;
351 CGapPtrList m_gaplist;
352 CReqBlockPtrList m_requestedblocks_list;
353 double percentcompleted;
354 std::list<uint16> m_corrupted_list;
355 uint16 m_availablePartsCount;
356 uint32 m_ClientSrcAnswered;
357 bool m_bPercentUpdated;
359 void PerformFileComplete();
361 uint32 m_lastRefreshedDLDisplay;
363 // Barry - Buffered data to be written
364 std::list<PartFileBufferedData*> m_BufferedData_list;
366 uint32 m_nTotalBufferData;
367 uint32 m_nLastBufferFlushTime;
369 uint8 m_category;
370 uint32 m_nDlActiveTime;
371 time_t m_tActivated;
372 bool m_is_A4AF_auto;
374 SourceSet m_SrcList;
375 SourceSet m_A4AFsrclist;
376 bool m_hashsetneeded;
377 uint32 m_lastsearchtime;
378 bool m_localSrcReqQueued;
380 FileRatingList m_FileRatingList;
381 #ifdef CLIENT_GUI
382 SourcenameItemList m_SourcenameItem_list;
383 public:
384 const SourcenameItemList &GetSourcenameItemList() { return m_SourcenameItem_list; }
385 void ClearSourcenameItemList() { m_SourcenameItem_list.clear(); }
386 void AddSourcenameItemList(const wxString &name, long count) { m_SourcenameItem_list.push_back(SourcenameItem(name, count)); }
387 #endif
388 public:
389 const FileRatingList &GetFileRatingList() { return m_FileRatingList; }
390 void ClearFileRatingList() { m_FileRatingList.clear(); }
391 void AddFileRatingList(const wxString & u, const wxString & f, sint16 r, const wxString & c) {
392 m_FileRatingList.push_back(SFileRating(u, f, r, c)); }
394 bool IsHashSetNeeded() const { return m_hashsetneeded; }
395 void SetHashSetNeeded(bool value) { m_hashsetneeded = value; }
397 uint64 GetCompletedSize() const { return completedsize; }
398 void SetCompletedSize(uint64 size) { completedsize = size; }
400 bool IsLocalSrcRequestQueued() const { return m_localSrcReqQueued; }
401 void SetLocalSrcRequestQueued(bool value) { m_localSrcReqQueued = value; }
403 void AddA4AFSource(CUpDownClient* src) { m_A4AFsrclist.insert(src); }
404 bool RemoveA4AFSource(CUpDownClient* src) { return (m_A4AFsrclist.erase(src) > 0); }
406 uint32 GetLastSearchTime() const { return m_lastsearchtime; }
407 void SetLastSearchTime(uint32 time) { m_lastsearchtime = time; }
409 // size of a certain part, last is different, all others are PARTSIZE
410 uint32 GetPartSize(uint16 part) const;
412 // void CleanUpSources( bool noNeeded, bool fullQueue = false, bool highQueue = false );
414 void AddDownloadingSource(CUpDownClient* client);
416 void RemoveDownloadingSource(CUpDownClient* client);
417 void SetStatus(uint8 in);
418 void StopPausedFile();
420 // [sivka / Tarod] Imported from eMule 0.30c (Creteil) ...
421 void SetA4AFAuto(bool in) { m_is_A4AF_auto = in; }
422 bool IsA4AFAuto() const { return m_is_A4AF_auto; }
424 // Kry -Sources seeds
425 void SaveSourceSeeds();
426 void LoadSourceSeeds();
428 // Dropping slow sources
429 CUpDownClient* GetSlowerDownloadingClient(uint32 speed, CUpDownClient* caller);
431 // Read data for sharing
432 bool ReadData(uint64 offset, byte * adr, uint32 toread);
434 private:
435 /* downloading sources list */
436 CClientPtrList m_downloadingSourcesList;
438 /* Kad Stuff */
439 uint32 m_LastSearchTimeKad;
440 uint8 m_TotalSearchesKad;
442 friend class CDownQueueRem;
443 friend class CPartFileConvert;
446 #endif // PARTFILE_H
447 // File_checked_for_headers