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 virtual bool LoadFromFile(const CFileDataIO
* WXUNUSED(file
)) { return false; }
107 bool WriteToFile(CFileDataIO
* WXUNUSED(file
)) { return false; }
108 bool IsPartFile() const { return !(status
== PS_COMPLETE
); }
109 bool IsCPartFile() const { return true; }
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
);
129 uint8
GetStatus() const { return status
; }
130 uint8
GetStatus(bool /*ignorepause = false*/) const { return status
; }
132 uint8
GetStatus(bool ignorepause
= false) const;
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
; }
143 uint16
GetSourceCount() const { return (uint16
)m_SrcList
.size(); }
144 uint16
GetSrcA4AFCount() const { return (uint16
)m_A4AFsrclist
.size(); }
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
; }
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
);
177 void StopFile(bool bCancel
= false);
178 void PauseFile(bool bInsufficient
= false);
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
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 const SourceSet
& GetSourceList() const { return m_SrcList
; }
252 const SourceSet
& GetA4AFList() const { return m_A4AFsrclist
; }
254 const CReqBlockPtrList
GetRequestedBlockList() const { return m_requestedblocks_list
; }
256 const CGapList
& GetGapList() const { return m_gaplist
; }
259 * Adds a source to the list of dead sources.
261 * @param client The source to be recorded as dead for this file.
263 void AddDeadSource(const CUpDownClient
* client
);
266 * Checks if a source is recorded as being dead for this file.
268 * @param client The client to evaluate.
269 * @return True if dead, false otherwise.
271 * Sources that are dead are not to be considered valid
272 * sources and should not be added to the partfile.
274 bool IsDeadSource(const CUpDownClient
* client
);
277 uint16
GetMaxSources() const;
278 uint16
GetMaxSourcePerFileSoft() const;
279 uint16
GetMaxSourcePerFileUDP() const;
281 void GetRatingAndComments(FileRatingList
& list
) const;
283 void AllocationFinished();
286 // partfile handle (opened on demand)
287 CFileAutoClose m_hpartfile
;
288 //! A local list of sources that are invalid for this file.
289 CDeadSourceList m_deadSources
;
291 class CCorruptionBlackBox
* m_CorruptionBlackBox
;
294 uint16 m_notCurrentSources
;
296 uint32 m_validSources
;
298 void AddGap(uint64 start
, uint64 end
);
299 void AddGap(uint16 part
);
300 void FillGap(uint64 start
, uint64 end
);
301 void FillGap(uint16 part
);
302 bool GetNextEmptyBlockInPart(uint16 partnumber
,Requested_Block_Struct
* result
);
303 bool IsAlreadyRequested(uint64 start
, uint64 end
);
304 void CompleteFile(bool hashingdone
);
305 void CreatePartFile();
308 bool CheckFreeDiskSpace( uint64 neededSpace
= 0 );
310 bool IsCorruptedPart(uint16 partnumber
);
312 uint32 m_iLastPausePurge
;
314 uint16 transferingsrc
;
315 uint64 completedsize
;
318 uint64 m_iLostDueToCorruption
;
319 uint64 m_iGainDueToCompression
;
320 uint32 m_iTotalPacketsSavedDueToICH
;
322 CPath m_fullname
; // path/name of the met file
323 CPath m_partmetfilename
; // name of the met file
324 CPath m_PartPath
; // path/name of the partfile
328 uint8 m_iDownPriority
;
329 bool m_bAutoDownPriority
;
331 uint32 lastpurgetime
;
332 uint32 m_LastNoNeededCheck
;
334 CReqBlockPtrList m_requestedblocks_list
;
335 double percentcompleted
;
336 std::list
<uint16
> m_corrupted_list
;
337 uint16 m_availablePartsCount
;
338 uint32 m_ClientSrcAnswered
;
339 bool m_bPercentUpdated
;
341 void PerformFileComplete();
343 uint32 m_lastRefreshedDLDisplay
;
345 // Buffered data to be written
346 std::list
<class PartFileBufferedData
*> m_BufferedData_list
;
348 uint32 m_nTotalBufferData
;
349 uint32 m_nLastBufferFlushTime
;
352 uint32 m_nDlActiveTime
;
357 SourceSet m_A4AFsrclist
;
358 bool m_hashsetneeded
;
359 uint32 m_lastsearchtime
;
360 bool m_localSrcReqQueued
;
363 FileRatingList m_FileRatingList
;
364 const FileRatingList
&GetFileRatingList() { return m_FileRatingList
; }
365 void ClearFileRatingList() { m_FileRatingList
.clear(); }
366 void AddFileRatingList(const wxString
& u
, const wxString
& f
, sint16 r
, const wxString
& c
) {
367 m_FileRatingList
.push_back(SFileRating(u
, f
, r
, c
)); }
370 uint8 m_iDownPriorityEC
;
372 SourcenameItemMap m_SourcenameItemMap
;
374 bool IsShared() const { return m_isShared
; }
375 SourcenameItemMap
&GetSourcenameItemMap() { return m_SourcenameItemMap
; }
376 PartFileEncoderData m_PartFileEncoderData
;
379 bool IsHashSetNeeded() const { return m_hashsetneeded
; }
380 void SetHashSetNeeded(bool value
) { m_hashsetneeded
= value
; }
382 uint64
GetCompletedSize() const { return completedsize
; }
383 void SetCompletedSize(uint64 size
) { completedsize
= size
; }
385 bool IsLocalSrcRequestQueued() const { return m_localSrcReqQueued
; }
386 void SetLocalSrcRequestQueued(bool value
) { m_localSrcReqQueued
= value
; }
388 void AddA4AFSource(CUpDownClient
* src
) { m_A4AFsrclist
.insert(src
); }
389 bool RemoveA4AFSource(CUpDownClient
* src
) { return (m_A4AFsrclist
.erase(src
) > 0); }
391 uint32
GetLastSearchTime() const { return m_lastsearchtime
; }
392 void SetLastSearchTime(uint32 time
) { m_lastsearchtime
= time
; }
394 // void CleanUpSources( bool noNeeded, bool fullQueue = false, bool highQueue = false );
396 void AddDownloadingSource(CUpDownClient
* client
);
398 void RemoveDownloadingSource(CUpDownClient
* client
);
399 void SetStatus(uint8 in
);
400 void StopPausedFile();
402 // [sivka / Tarod] Imported from eMule 0.30c (Creteil) ...
403 void SetA4AFAuto(bool in
) { m_is_A4AF_auto
= in
; }
404 bool IsA4AFAuto() const { return m_is_A4AF_auto
; }
406 // Kry -Sources seeds
407 void SaveSourceSeeds();
408 void LoadSourceSeeds();
410 // Dropping slow sources
411 CUpDownClient
* GetSlowerDownloadingClient(uint32 speed
, CUpDownClient
* caller
);
413 // Read data for sharing
414 bool ReadData(class CFileArea
& area
, uint64 offset
, uint32 toread
);
417 /* downloading sources list */
418 CClientPtrList m_downloadingSourcesList
;
421 uint32 m_LastSearchTimeKad
;
422 uint8 m_TotalSearchesKad
;
424 friend class CKnownFilesRem
;
425 friend class CPartFileConvert
;
429 // File_checked_for_headers