diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 9f16e29..397bb00 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -30,6 +30,9 @@ add_executable(tox_ngc_ft1_tool ./states/send_start_sha1.hpp ./states/send_start_sha1.cpp + ./states/receive_start_sha1.hpp + ./states/receive_start_sha1.cpp + ./states/sha1.hpp ./states/sha1.cpp ) diff --git a/src/ft_sha1_info.hpp b/src/ft_sha1_info.hpp index 0015c0d..a2672eb 100644 --- a/src/ft_sha1_info.hpp +++ b/src/ft_sha1_info.hpp @@ -10,8 +10,8 @@ struct SHA1Digest { std::array data; + SHA1Digest(void) = default; SHA1Digest(const std::vector& v); - SHA1Digest(const uint8_t* d, size_t s); bool operator==(const SHA1Digest& other) const { return data == other.data; } diff --git a/src/states/receive_start_sha1.cpp b/src/states/receive_start_sha1.cpp new file mode 100644 index 0000000..ee60d15 --- /dev/null +++ b/src/states/receive_start_sha1.cpp @@ -0,0 +1,83 @@ +#include "./receive_start_sha1.hpp" + +#include "./sha1.hpp" + +#include "../tox_utils.hpp" +#include "../ft_sha1_info.hpp" + +#include + +#include +#include +#include + +namespace States { + +ReceiveStartSHA1::ReceiveStartSHA1(ToxClient& tcl, const CommandLine& cl) : StateI(tcl) { + if (cl.receive_id.empty()) { + throw std::runtime_error("receiver missing id"); + } + + _sha1_info_hash = hex2bin(cl.receive_id); + assert(_sha1_info_hash.size() == 20); + + std::cout << "ReceiveStartSHA1 downloading info for " << cl.receive_id << "\n"; +} + +bool ReceiveStartSHA1::iterate(float delta) { + // TODO: return true if done + return false; +} + +std::unique_ptr ReceiveStartSHA1::nextState(void) { + std::cout << "ReceiveStartSHA1 switching state to SHA1\n"; + + FTInfoSHA1 sha1_info; + // from buffer + + // open file for writing (pre allocate?) + mio::mmap_sink file_map; + + std::vector have_chunk(sha1_info.chunks.size(), false); + + return std::make_unique( + _tcl, + std::move(file_map), + std::move(sha1_info), + std::move(_sha1_info_data), + std::move(_sha1_info_hash), + std::move(have_chunk) + ); +} + +// sha1_info +void ReceiveStartSHA1::onFT1ReceiveRequestSHA1Info(uint32_t group_number, uint32_t peer_number, const uint8_t* file_id, size_t file_id_size) { +} + +bool ReceiveStartSHA1::onFT1ReceiveInitSHA1Info(uint32_t group_number, uint32_t peer_number, const uint8_t* file_id, size_t file_id_size, const uint8_t transfer_id, const size_t file_size) { + // accept + return true; +} + +void ReceiveStartSHA1::onFT1ReceiveDataSHA1Info(uint32_t group_number, uint32_t peer_number, uint8_t transfer_id, size_t data_offset, const uint8_t* data, size_t data_size) { +} + +void ReceiveStartSHA1::onFT1SendDataSHA1Info(uint32_t group_number, uint32_t peer_number, uint8_t transfer_id, size_t data_offset, uint8_t* data, size_t data_size) { +} + +// sha1_chunk +void ReceiveStartSHA1::onFT1ReceiveRequestSHA1Chunk(uint32_t, uint32_t, const uint8_t*, size_t) { +} + +bool ReceiveStartSHA1::onFT1ReceiveInitSHA1Chunk(uint32_t, uint32_t, const uint8_t*, size_t, const uint8_t, const size_t) { + return false; // nope, not interested in chunks yet +} + +void ReceiveStartSHA1::onFT1ReceiveDataSHA1Chunk(uint32_t, uint32_t, uint8_t, size_t, const uint8_t*, size_t) { +} + +void ReceiveStartSHA1::onFT1SendDataSHA1Chunk(uint32_t, uint32_t, uint8_t, size_t, uint8_t*, size_t) { +} + +} // States + diff --git a/src/states/receive_start_sha1.hpp b/src/states/receive_start_sha1.hpp new file mode 100644 index 0000000..3b2fa8b --- /dev/null +++ b/src/states/receive_start_sha1.hpp @@ -0,0 +1,41 @@ +#pragma once + +#include "../state.hpp" + +#include "../command_line.hpp" +#include "../ft_sha1_info.hpp" + +#include + +namespace States { + +// we have the info hash and request the info until we have it +struct ReceiveStartSHA1 final : public StateI { + public: // general interface + ReceiveStartSHA1(ToxClient& tcl, const CommandLine& cl); + ~ReceiveStartSHA1(void) override = default; + + bool iterate(float delta) override; + std::unique_ptr nextState(void) override; + + public: // callbacks + // sha1_info + void onFT1ReceiveRequestSHA1Info(uint32_t group_number, uint32_t peer_number, const uint8_t* file_id, size_t file_id_size) override; + bool onFT1ReceiveInitSHA1Info(uint32_t group_number, uint32_t peer_number, const uint8_t* file_id, size_t file_id_size, const uint8_t transfer_id, const size_t file_size) override; + void onFT1ReceiveDataSHA1Info(uint32_t group_number, uint32_t peer_number, uint8_t transfer_id, size_t data_offset, const uint8_t* data, size_t data_size) override; + void onFT1SendDataSHA1Info(uint32_t group_number, uint32_t peer_number, uint8_t transfer_id, size_t data_offset, uint8_t* data, size_t data_size) override; + + // sha1_chunk + void onFT1ReceiveRequestSHA1Chunk(uint32_t group_number, uint32_t peer_number, const uint8_t* file_id, size_t file_id_size) override; + bool onFT1ReceiveInitSHA1Chunk(uint32_t group_number, uint32_t peer_number, const uint8_t* file_id, size_t file_id_size, const uint8_t transfer_id, const size_t file_size) override; + void onFT1ReceiveDataSHA1Chunk(uint32_t group_number, uint32_t peer_number, uint8_t transfer_id, size_t data_offset, const uint8_t* data, size_t data_size) override; + void onFT1SendDataSHA1Chunk(uint32_t group_number, uint32_t peer_number, uint8_t transfer_id, size_t data_offset, uint8_t* data, size_t data_size) override; + + private: + //FTInfoSHA1 _sha1_info; + std::vector _sha1_info_data; + SHA1Digest _sha1_info_hash; // treat as const +}; + +} // States + diff --git a/src/states/sha1.cpp b/src/states/sha1.cpp index b9fa5ff..0871329 100644 --- a/src/states/sha1.cpp +++ b/src/states/sha1.cpp @@ -11,7 +11,8 @@ SHA1::SHA1( mio::mmap_source&& file_map, const FTInfoSHA1&& sha1_info, const std::vector&& sha1_info_data, - const std::vector&& sha1_info_hash, + //const std::vector&& sha1_info_hash, + const SHA1Digest&& sha1_info_hash, std::vector&& have_chunk ) : StateI(tcl), diff --git a/src/states/sha1.hpp b/src/states/sha1.hpp index 9d79224..0ff4fb6 100644 --- a/src/states/sha1.hpp +++ b/src/states/sha1.hpp @@ -20,7 +20,8 @@ struct SHA1 final : public StateI { mio::mmap_source&& file_map, const FTInfoSHA1&& sha1_info, const std::vector&& sha1_info_data, - const std::vector&& sha1_info_hash, + //const std::vector&& sha1_info_hash, + const SHA1Digest&& sha1_info_hash, std::vector&& have_chunk ); ~SHA1(void) override = default; diff --git a/src/tox_client.cpp b/src/tox_client.cpp index a5c3e93..a82f190 100644 --- a/src/tox_client.cpp +++ b/src/tox_client.cpp @@ -4,6 +4,7 @@ #include "./tox_callbacks.hpp" #include "./states/send_start_sha1.hpp" +#include "./states/receive_start_sha1.hpp" #include #include @@ -127,6 +128,7 @@ ToxClient::ToxClient(const CommandLine& cl) : if (!cl.send_path.empty()) { _state = std::make_unique(*this, cl); } else { // receiver + _state = std::make_unique(*this, cl); } }