2 // This file is part of the aMule Project.
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 )
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
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.
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
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
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
{
70 SFileRating(const wxString
&u
, const wxString
&f
, sint16 r
, const wxString
&c
);
71 SFileRating(const SFileRating
&fr
);
72 SFileRating(const CUpDownClient
&client
);
76 typedef std::list
<SFileRating
> FileRatingList
;
84 SourcenameItem(const wxString
&n
= EmptyString
, int c
= 0)
89 typedef std::map
<uint32
,SourcenameItem
> SourcenameItemMap
;
91 class CPartFile
: public CKnownFile
{
93 typedef std::list
<Requested_Block_Struct
*> CReqBlockPtrList
;
97 CPartFile(CEC_PartFile_Tag
*tag
);
99 virtual void SetFileName(const CPath
& filename
);
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
);
130 uint8
GetStatus() const { return status
; }
131 uint8
GetStatus(bool /*ignorepause = false*/) const { return status
; }
133 uint8
GetStatus(bool ignorepause
= false) const;
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
; }
144 uint16
GetSourceCount() const { return (uint16
)m_SrcList
.size(); }
145 uint16
GetSrcA4AFCount() const { return (uint16
)m_A4AFsrclist
.size(); }
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
; }
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
);
178 void StopFile(bool bCancel
= false);
179 void PauseFile(bool bInsufficient
= false);
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
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 const SourceSet
& GetSourceList() const { return m_SrcList
; }
253 const SourceSet
& GetA4AFList() const { return m_A4AFsrclist
; }
255 const CReqBlockPtrList
GetRequestedBlockList() const { return m_requestedblocks_list
; }
257 const CGapList
& GetGapList() const { return m_gaplist
; }
260 * Adds a source to the list of dead sources.
262 * @param client The source to be recorded as dead for this file.
264 void AddDeadSource(const CUpDownClient
* client
);
267 * Checks if a source is recorded as being dead for this file.
269 * @param client The client to evaluate.
270 * @return True if dead, false otherwise.
272 * Sources that are dead are not to be considered valid
273 * sources and should not be added to the partfile.
275 bool IsDeadSource(const CUpDownClient
* client
);
278 uint16
GetMaxSources() const;
279 uint16
GetMaxSourcePerFileSoft() const;
280 uint16
GetMaxSourcePerFileUDP() const;
282 void GetRatingAndComments(FileRatingList
& list
) const;
284 void AllocationFinished();
287 // partfile handle (opened on demand)
288 CFileAutoClose m_hpartfile
;
289 //! A local list of sources that are invalid for this file.
290 CDeadSourceList m_deadSources
;
292 class CCorruptionBlackBox
* m_CorruptionBlackBox
;
295 uint16 m_notCurrentSources
;
297 uint32 m_validSources
;
299 void AddGap(uint64 start
, uint64 end
);
300 void AddGap(uint16 part
);
301 void FillGap(uint64 start
, uint64 end
);
302 void FillGap(uint16 part
);
303 bool GetNextEmptyBlockInPart(uint16 partnumber
,Requested_Block_Struct
* result
);
304 bool IsAlreadyRequested(uint64 start
, uint64 end
);
305 void CompleteFile(bool hashingdone
);
306 void CreatePartFile();
309 bool CheckFreeDiskSpace( uint64 neededSpace
= 0 );
311 bool IsCorruptedPart(uint16 partnumber
);
313 uint32 m_iLastPausePurge
;
315 uint16 transferingsrc
;
316 uint64 completedsize
;
319 uint64 m_iLostDueToCorruption
;
320 uint64 m_iGainDueToCompression
;
321 uint32 m_iTotalPacketsSavedDueToICH
;
323 CPath m_fullname
; // path/name of the met file
324 CPath m_partmetfilename
; // name of the met file
325 CPath m_PartPath
; // path/name of the partfile
329 uint8 m_iDownPriority
;
330 bool m_bAutoDownPriority
;
332 uint32 lastpurgetime
;
333 uint32 m_LastNoNeededCheck
;
335 CReqBlockPtrList m_requestedblocks_list
;
336 double percentcompleted
;
337 std::list
<uint16
> m_corrupted_list
;
338 uint16 m_availablePartsCount
;
339 uint32 m_ClientSrcAnswered
;
340 bool m_bPercentUpdated
;
342 void PerformFileComplete();
344 uint32 m_lastRefreshedDLDisplay
;
346 // Buffered data to be written
347 std::list
<class PartFileBufferedData
*> m_BufferedData_list
;
349 uint32 m_nTotalBufferData
;
350 uint32 m_nLastBufferFlushTime
;
353 uint32 m_nDlActiveTime
;
358 SourceSet m_A4AFsrclist
;
359 bool m_hashsetneeded
;
360 uint32 m_lastsearchtime
;
361 bool m_localSrcReqQueued
;
364 FileRatingList m_FileRatingList
;
365 const FileRatingList
&GetFileRatingList() { return m_FileRatingList
; }
366 void ClearFileRatingList() { m_FileRatingList
.clear(); }
367 void AddFileRatingList(const wxString
& u
, const wxString
& f
, sint16 r
, const wxString
& c
) {
368 m_FileRatingList
.push_back(SFileRating(u
, f
, r
, c
)); }
371 uint8 m_iDownPriorityEC
;
373 SourcenameItemMap m_SourcenameItemMap
;
375 bool IsShared() const { return m_isShared
; }
376 SourcenameItemMap
&GetSourcenameItemMap() { return m_SourcenameItemMap
; }
377 PartFileEncoderData m_PartFileEncoderData
;
380 bool IsHashSetNeeded() const { return m_hashsetneeded
; }
381 void SetHashSetNeeded(bool value
) { m_hashsetneeded
= value
; }
383 uint64
GetCompletedSize() const { return completedsize
; }
384 void SetCompletedSize(uint64 size
) { completedsize
= size
; }
386 bool IsLocalSrcRequestQueued() const { return m_localSrcReqQueued
; }
387 void SetLocalSrcRequestQueued(bool value
) { m_localSrcReqQueued
= value
; }
389 void AddA4AFSource(CUpDownClient
* src
) { m_A4AFsrclist
.insert(src
); }
390 bool RemoveA4AFSource(CUpDownClient
* src
) { return (m_A4AFsrclist
.erase(src
) > 0); }
392 uint32
GetLastSearchTime() const { return m_lastsearchtime
; }
393 void SetLastSearchTime(uint32 time
) { m_lastsearchtime
= time
; }
395 // void CleanUpSources( bool noNeeded, bool fullQueue = false, bool highQueue = false );
397 void AddDownloadingSource(CUpDownClient
* client
);
399 void RemoveDownloadingSource(CUpDownClient
* client
);
400 void SetStatus(uint8 in
);
401 void StopPausedFile();
403 // [sivka / Tarod] Imported from eMule 0.30c (Creteil) ...
404 void SetA4AFAuto(bool in
) { m_is_A4AF_auto
= in
; }
405 bool IsA4AFAuto() const { return m_is_A4AF_auto
; }
407 // Kry -Sources seeds
408 void SaveSourceSeeds();
409 void LoadSourceSeeds();
411 // Dropping slow sources
412 CUpDownClient
* GetSlowerDownloadingClient(uint32 speed
, CUpDownClient
* caller
);
414 // Read data for sharing
415 bool ReadData(class CFileArea
& area
, uint64 offset
, uint32 toread
);
418 /* downloading sources list */
419 CClientPtrList m_downloadingSourcesList
;
422 uint32 m_LastSearchTimeKad
;
423 uint8 m_TotalSearchesKad
;
425 friend class CKnownFilesRem
;
426 friend class CPartFileConvert
;
430 // File_checked_for_headers