Emit an event when a shared file hashing complete

Properly notify when a shared file has been hashed
Deprecate event with arbitrary data packed in std::string
This commit is contained in:
Gioacchino Mazzurco 2021-07-14 20:36:34 +02:00
parent 58016fff65
commit 34593d1b6f
No known key found for this signature in database
GPG Key ID: A1FBCA3872E87051
3 changed files with 50 additions and 14 deletions

View File

@ -187,9 +187,10 @@ void HashStorage::threadTick()
else else
rs_sprintf(tmpout, "%lu/%lu (%s - %d%%) : %s", (unsigned long int)mHashCounter+1, (unsigned long int)mTotalFilesToHash, friendlyUnit(mTotalHashedSize).c_str(), int(mTotalHashedSize/double(mTotalSizeToHash)*100.0), job.full_path.c_str()) ; rs_sprintf(tmpout, "%lu/%lu (%s - %d%%) : %s", (unsigned long int)mHashCounter+1, (unsigned long int)mTotalFilesToHash, friendlyUnit(mTotalHashedSize).c_str(), int(mTotalHashedSize/double(mTotalSizeToHash)*100.0), job.full_path.c_str()) ;
//RsServer::notify()->notifyHashingInfo(NOTIFY_HASHTYPE_HASH_FILE, tmpout) ;
if(rsEvents)
{ {
/* Emit deprecated event only for retrocompatibility
* TODO: create a proper event with structured data instead of a
* formatted string */
auto ev = std::make_shared<RsSharedDirectoriesEvent>(); auto ev = std::make_shared<RsSharedDirectoriesEvent>();
ev->mEventCode = RsSharedDirectoriesEventCode::HASHING_FILE; ev->mEventCode = RsSharedDirectoriesEventCode::HASHING_FILE;
ev->mMessage = tmpout; ev->mMessage = tmpout;
@ -198,7 +199,7 @@ void HashStorage::threadTick()
double seconds_origin = rstime::RsScopeTimer::currentTime() ; double seconds_origin = rstime::RsScopeTimer::currentTime() ;
if(RsDirUtil::getFileHash(job.full_path, hash,size, this)) if(RsDirUtil::getFileHash(job.full_path, hash, size, this))
{ {
// store the result // store the result
@ -218,8 +219,7 @@ void HashStorage::threadTick()
mChanged = true ; mChanged = true ;
mTotalHashedSize += size ; mTotalHashedSize += size ;
} }
else else RS_ERR("Failure hashing file: ", job.full_path);
std::cerr << "ERROR: cannot hash file " << job.full_path << std::endl;
mHashingTime += rstime::RsScopeTimer::currentTime() - seconds_origin ; mHashingTime += rstime::RsScopeTimer::currentTime() - seconds_origin ;
mHashedBytes += size ; mHashedBytes += size ;
@ -233,11 +233,18 @@ void HashStorage::threadTick()
++mHashCounter ; ++mHashCounter ;
} }
} }
// call the client
if(!hash.isNull()) // call the client
job.client->hash_callback(job.client_param, job.full_path, hash, size); if(!hash.isNull())
job.client->hash_callback(job.client_param, job.full_path, hash, size);
/* Notify we completed hashing a file */
auto ev = std::make_shared<RsFileHashingCompletedEvent>();
ev->mFilePath = job.full_path;
ev->mHashingSpeed = mCurrentHashingSpeed;
ev->mFileHash = hash;
rsEvents->postEvent(ev);
} }
bool HashStorage::requestHash(const std::string& full_path,uint64_t size,rstime_t mod_time,RsFileHash& known_hash,HashStorageClient *c,uint32_t client_param) bool HashStorage::requestHash(const std::string& full_path,uint64_t size,rstime_t mod_time,RsFileHash& known_hash,HashStorageClient *c,uint32_t client_param)

View File

@ -91,8 +91,8 @@ enum class RsEventType : uint32_t
/// @see RsGxsPostedEvent /// @see RsGxsPostedEvent
GXS_IDENTITY = 12, GXS_IDENTITY = 12,
/// @see RsFiles /// @see RsFiles @deprecated
SHARED_DIRECTORIES = 13, SHARED_DIRECTORIES = 13,
/// @see RsFiles /// @see RsFiles
FILE_TRANSFER = 14, FILE_TRANSFER = 14,
@ -100,8 +100,11 @@ enum class RsEventType : uint32_t
/// @see RsMsgs /// @see RsMsgs
CHAT_MESSAGE = 15, CHAT_MESSAGE = 15,
/// @see rspeers.h /// @see rspeers.h
NETWORK = 16, NETWORK = 16,
/** Emitted to update library clients about file hashing being completed */
FILE_HASHING_COMPLETED = 20,
__MAX /// Used internally, keep last __MAX /// Used internally, keep last
}; };

View File

@ -194,7 +194,8 @@ enum class RsFileTransferEventCode: uint8_t {
COMPLETED_FILES_REMOVED = 0x02, // COMPLETED_FILES_REMOVED = 0x02, //
}; };
struct RsSharedDirectoriesEvent: RsEvent struct RS_DEPRECATED_FOR("Packing arbitrary data into an std::string is bad idea")
RsSharedDirectoriesEvent: RsEvent
{ {
RsSharedDirectoriesEvent() : RsEvent(RsEventType::SHARED_DIRECTORIES), mEventCode(RsSharedDirectoriesEventCode::UNKNOWN) {} RsSharedDirectoriesEvent() : RsEvent(RsEventType::SHARED_DIRECTORIES), mEventCode(RsSharedDirectoriesEventCode::UNKNOWN) {}
~RsSharedDirectoriesEvent() override = default; ~RsSharedDirectoriesEvent() override = default;
@ -212,6 +213,31 @@ struct RsSharedDirectoriesEvent: RsEvent
std::string mMessage; std::string mMessage;
}; };
struct RsFileHashingCompletedEvent: RsEvent
{
RsFileHashingCompletedEvent():
RsEvent(RsEventType::FILE_HASHING_COMPLETED), mHashingSpeed(0) {}
///* @see RsEvent @see RsSerializable
void serial_process( RsGenericSerializer::SerializeJob j,
RsGenericSerializer::SerializeContext& ctx ) override
{
RsEvent::serial_process(j, ctx);
RS_SERIAL_PROCESS(mFilePath);
RS_SERIAL_PROCESS(mFileHash);
RS_SERIAL_PROCESS(mHashingSpeed);
}
/// Complete path of the file being hashed
std::string mFilePath;
/// File hash, null if error occurred
RsFileHash mFileHash;
/// Hashing speed in MB/s
double mHashingSpeed;
};
struct RsFileTransferEvent: RsEvent struct RsFileTransferEvent: RsEvent
{ {
RsFileTransferEvent() : RsEvent(RsEventType::FILE_TRANSFER), mFileTransferEventCode(RsFileTransferEventCode::UNKNOWN) {} RsFileTransferEvent() : RsEvent(RsEventType::FILE_TRANSFER), mFileTransferEventCode(RsFileTransferEventCode::UNKNOWN) {}