diff --git a/libretroshare/src/file_sharing/hash_cache.cc b/libretroshare/src/file_sharing/hash_cache.cc index 331f1a481..62924d70a 100644 --- a/libretroshare/src/file_sharing/hash_cache.cc +++ b/libretroshare/src/file_sharing/hash_cache.cc @@ -187,9 +187,10 @@ void HashStorage::threadTick() 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()) ; - //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(); ev->mEventCode = RsSharedDirectoriesEventCode::HASHING_FILE; ev->mMessage = tmpout; @@ -198,7 +199,7 @@ void HashStorage::threadTick() 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 @@ -218,8 +219,7 @@ void HashStorage::threadTick() mChanged = true ; mTotalHashedSize += size ; } - else - std::cerr << "ERROR: cannot hash file " << job.full_path << std::endl; + else RS_ERR("Failure hashing file: ", job.full_path); mHashingTime += rstime::RsScopeTimer::currentTime() - seconds_origin ; mHashedBytes += size ; @@ -233,11 +233,18 @@ void HashStorage::threadTick() ++mHashCounter ; } - } - // call the client + } - if(!hash.isNull()) - job.client->hash_callback(job.client_param, job.full_path, hash, size); + // call the client + 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(); + 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) diff --git a/libretroshare/src/retroshare/rsevents.h b/libretroshare/src/retroshare/rsevents.h index ec2e7b97f..9b9fbf106 100644 --- a/libretroshare/src/retroshare/rsevents.h +++ b/libretroshare/src/retroshare/rsevents.h @@ -91,8 +91,8 @@ enum class RsEventType : uint32_t /// @see RsGxsPostedEvent GXS_IDENTITY = 12, - /// @see RsFiles - SHARED_DIRECTORIES = 13, + /// @see RsFiles @deprecated + SHARED_DIRECTORIES = 13, /// @see RsFiles FILE_TRANSFER = 14, @@ -100,8 +100,11 @@ enum class RsEventType : uint32_t /// @see RsMsgs CHAT_MESSAGE = 15, - /// @see rspeers.h - NETWORK = 16, + /// @see rspeers.h + NETWORK = 16, + + /** Emitted to update library clients about file hashing being completed */ + FILE_HASHING_COMPLETED = 20, __MAX /// Used internally, keep last }; diff --git a/libretroshare/src/retroshare/rsfiles.h b/libretroshare/src/retroshare/rsfiles.h index f25d90957..17b68aede 100644 --- a/libretroshare/src/retroshare/rsfiles.h +++ b/libretroshare/src/retroshare/rsfiles.h @@ -194,7 +194,8 @@ enum class RsFileTransferEventCode: uint8_t { 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() override = default; @@ -212,6 +213,31 @@ struct RsSharedDirectoriesEvent: RsEvent 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 { RsFileTransferEvent() : RsEvent(RsEventType::FILE_TRANSFER), mFileTransferEventCode(RsFileTransferEventCode::UNKNOWN) {}