2008-06-19 18:49:23 -04:00
|
|
|
/*
|
|
|
|
* libretroshare/src/ft/ ftfilecreator.h
|
|
|
|
*
|
|
|
|
* File Transfer for RetroShare.
|
|
|
|
*
|
|
|
|
* Copyright 2008 by Robert Fernie.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License Version 2 as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
* USA.
|
|
|
|
*
|
|
|
|
* Please report all bugs and problems to "retroshare@lunamutt.com".
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef FT_FILE_CREATOR_HEADER
|
|
|
|
#define FT_FILE_CREATOR_HEADER
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ftFileCreator
|
|
|
|
*
|
|
|
|
* TODO: Serialiser Load / Save.
|
|
|
|
*
|
|
|
|
*/
|
2008-07-01 22:36:39 -04:00
|
|
|
#include "ftfileprovider.h"
|
2009-11-18 18:02:37 -05:00
|
|
|
#include "ftchunkmap.h"
|
2008-10-22 14:12:58 -04:00
|
|
|
#include <map>
|
2008-06-19 18:49:23 -04:00
|
|
|
|
|
|
|
class ftFileCreator: public ftFileProvider
|
|
|
|
{
|
2009-12-28 16:11:00 -05:00
|
|
|
public:
|
2008-06-19 18:49:23 -04:00
|
|
|
|
2010-03-06 18:29:47 -05:00
|
|
|
ftFileCreator(std::string savepath, uint64_t size, std::string hash);
|
2008-06-19 18:49:23 -04:00
|
|
|
|
2009-12-28 16:11:00 -05:00
|
|
|
~ftFileCreator();
|
2008-06-19 18:49:23 -04:00
|
|
|
|
2009-12-28 16:11:00 -05:00
|
|
|
/* overloaded from FileProvider */
|
|
|
|
virtual bool getFileData(uint64_t offset, uint32_t &chunk_size, void *data);
|
2010-02-17 17:10:12 -05:00
|
|
|
bool finished() ;
|
2009-12-28 16:11:00 -05:00
|
|
|
uint64_t getRecvd();
|
2008-10-22 14:12:58 -04:00
|
|
|
|
2010-03-06 18:29:47 -05:00
|
|
|
/// (temporarily) close the file, to save file descriptors.
|
|
|
|
void closeFile() ;
|
|
|
|
|
2009-12-28 16:11:00 -05:00
|
|
|
void getChunkMap(FileChunksInfo& info) ;
|
2010-01-11 11:00:42 -05:00
|
|
|
|
2009-12-28 16:11:00 -05:00
|
|
|
void setChunkStrategy(FileChunksInfo::ChunkStrategy s) ;
|
2010-01-11 11:00:42 -05:00
|
|
|
FileChunksInfo::ChunkStrategy getChunkStrategy() ;
|
2008-06-19 18:49:23 -04:00
|
|
|
|
2010-07-06 01:04:11 -04:00
|
|
|
// Computes a sha1sum of the partial file, to check that the data is overall consistent.
|
2010-07-09 17:04:29 -04:00
|
|
|
// This function is not mutexed. This is a bit dangerous, but otherwise we might stuck the GUI for a
|
|
|
|
// long time. Therefore, we must pay attention not to call this function
|
|
|
|
// at a time file_name nor hash can be modified, which is quite easy.
|
|
|
|
|
2010-07-06 01:04:11 -04:00
|
|
|
bool hashReceivedData(std::string& hash) ;
|
|
|
|
|
|
|
|
// Checks the CRC32 of all chunks against the given CRC32 map. Re-flag the bad chunks as being void.
|
2010-07-21 19:14:10 -04:00
|
|
|
// bad_chunks: count of achieved chunks that don't match the CRC
|
|
|
|
// incomplete_chunks: count of any bad or not yet downloaded chunk
|
2010-07-06 01:04:11 -04:00
|
|
|
//
|
2010-07-21 19:14:10 -04:00
|
|
|
bool crossCheckChunkMap(const CRC32Map& ref,uint32_t& bad_chunks,uint32_t& incomplete_chunks) ;
|
2009-12-28 16:11:00 -05:00
|
|
|
/*
|
|
|
|
* creation functions for FileCreator
|
|
|
|
*/
|
2009-12-10 17:55:27 -05:00
|
|
|
|
2009-12-28 16:11:00 -05:00
|
|
|
// Gets a new variable-sized chunk of size "size_hint" from the given peer id. The returned size, "size" is
|
2010-01-11 11:00:42 -05:00
|
|
|
// at most equal to size_hint. chunk_map_needed is set if
|
|
|
|
// - no chunkmap info is available. In such a case, the chunk info is irrelevant and false is returned.
|
|
|
|
// - the chunk info is too old. In tis case, true is returned, and the chunks info can be used.
|
2009-12-28 16:11:00 -05:00
|
|
|
//
|
2010-02-17 17:10:12 -05:00
|
|
|
bool getMissingChunk(const std::string& peer_id,uint32_t size_hint,uint64_t& offset, uint32_t& size,bool& is_chunk_map_too_old);
|
2008-10-22 14:12:58 -04:00
|
|
|
|
2010-01-21 07:31:00 -05:00
|
|
|
// Takes care of purging any inactive chunks. This should be called regularly, because some peers may disconnect
|
|
|
|
// and let inactive chunks not finished.
|
|
|
|
//
|
|
|
|
void removeInactiveChunks() ;
|
|
|
|
|
2010-03-07 09:42:13 -05:00
|
|
|
// removes the designated file source from the chunkmap.
|
|
|
|
void removeFileSource(const std::string& peer_id) ;
|
|
|
|
|
2010-03-21 17:07:12 -04:00
|
|
|
// Returns resets the time stamp of the last data receive.
|
2010-03-06 18:29:47 -05:00
|
|
|
time_t lastRecvTimeStamp() ;
|
2010-03-21 17:07:12 -04:00
|
|
|
void resetRecvTimeStamp() ;
|
2010-03-06 18:29:47 -05:00
|
|
|
|
2009-12-28 16:11:00 -05:00
|
|
|
// actually store data in the file, and update chunks info
|
|
|
|
//
|
|
|
|
bool addFileData(uint64_t offset, uint32_t chunk_size, void *data);
|
2008-10-22 14:12:58 -04:00
|
|
|
|
2009-12-28 16:11:00 -05:00
|
|
|
// Load/save the availability map for the file being downloaded, in a compact/compressed form.
|
|
|
|
// This is used for
|
|
|
|
// - loading and saving info about the current transfers
|
|
|
|
// - getting info about current chunks for the GUI
|
|
|
|
// - sending availability info to the peers for which we also are a source
|
|
|
|
//
|
2010-01-11 11:00:42 -05:00
|
|
|
virtual void getAvailabilityMap(CompressedChunkMap& cmap) ;
|
|
|
|
void setAvailabilityMap(const CompressedChunkMap& cmap) ;
|
2008-10-22 14:12:58 -04:00
|
|
|
|
2010-07-21 19:14:10 -04:00
|
|
|
// Provides a complete per-chunk CRC32 map to client who want to check their data.
|
|
|
|
// This is overloads ftFileProvider, but returns false, because we can't ensure that unchecked chunks
|
|
|
|
// will provide a CRC32 that is faithful to the original hash.
|
|
|
|
//
|
|
|
|
virtual bool getCRC32Map(CRC32Map& crc_map) { return false ; }
|
|
|
|
|
2009-12-28 16:11:00 -05:00
|
|
|
// This is called when receiving the availability map from a source peer, for the file being handled.
|
|
|
|
//
|
2010-01-11 11:00:42 -05:00
|
|
|
void setSourceMap(const std::string& peer_id,const CompressedChunkMap& map) ;
|
2008-06-19 18:49:23 -04:00
|
|
|
|
2009-12-28 16:11:00 -05:00
|
|
|
protected:
|
2008-07-01 22:36:39 -04:00
|
|
|
|
2010-03-17 11:34:36 -04:00
|
|
|
virtual int locked_initializeFileAttrs();
|
2009-12-28 16:11:00 -05:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
bool locked_printChunkMap();
|
|
|
|
int locked_notifyReceived(uint64_t offset, uint32_t chunk_size);
|
|
|
|
/*
|
|
|
|
* structure to track missing chunks
|
|
|
|
*/
|
|
|
|
|
|
|
|
uint64_t mStart;
|
|
|
|
uint64_t mEnd;
|
|
|
|
|
|
|
|
std::map<uint64_t, ftChunk> mChunks;
|
|
|
|
|
|
|
|
ChunkMap chunkMap ;
|
2010-03-06 18:29:47 -05:00
|
|
|
|
|
|
|
time_t _last_recv_time_t ; /// last time stamp when data was received.
|
2008-07-01 22:36:39 -04:00
|
|
|
};
|
|
|
|
|
2008-10-22 14:12:58 -04:00
|
|
|
#endif // FT_FILE_CREATOR_HEADER
|