From 100f4f68c03da04567d179117a68de5a167841fb Mon Sep 17 00:00:00 2001 From: Green Sky Date: Thu, 17 Aug 2023 22:43:16 +0200 Subject: [PATCH] accept save to and open file (truncated) --- external/solanaceae_message3 | 2 +- external/solanaceae_tox | 2 +- src/sha1_ngcft1.cpp | 69 ++++++++++++++++++++++++++++++++++++ src/sha1_ngcft1.hpp | 3 ++ 4 files changed, 74 insertions(+), 2 deletions(-) diff --git a/external/solanaceae_message3 b/external/solanaceae_message3 index c73e429..53d65a0 160000 --- a/external/solanaceae_message3 +++ b/external/solanaceae_message3 @@ -1 +1 @@ -Subproject commit c73e429df138ce32f5cdb5588a3af8257a946680 +Subproject commit 53d65a06855129135e5884f50d9983676290ac24 diff --git a/external/solanaceae_tox b/external/solanaceae_tox index de33242..70a234c 160000 --- a/external/solanaceae_tox +++ b/external/solanaceae_tox @@ -1 +1 @@ -Subproject commit de332421f783a94877b7b529a698e0752273e7a4 +Subproject commit 70a234cdae751141f48b7a53a4e1cbedd84f31b2 diff --git a/src/sha1_ngcft1.cpp b/src/sha1_ngcft1.cpp index 5ab317c..c500036 100644 --- a/src/sha1_ngcft1.cpp +++ b/src/sha1_ngcft1.cpp @@ -8,6 +8,7 @@ #include #include +#include #include "./ft1_sha1_info.hpp" #include "./hash_utils.hpp" @@ -18,6 +19,7 @@ #include #include +#include namespace Message::Components { @@ -60,6 +62,14 @@ namespace Components { float timer {0.f}; }; + struct ReadHeadHint { + // points to the first byte we want + // this is just a hint, that can be set from outside + // to guide the sequential "piece picker" strategy + // the strategy *should* set this to the first byte we dont yet have + uint64_t offset_into_file {0u}; + }; + } // Components std::optional Components::FT1ChunkSHA1Cache::chunkIndex(const SHA1Digest& hash) const { @@ -207,6 +217,9 @@ SHA1_NGCFT1::SHA1_NGCFT1( _nft(nft), _tcm(tcm) { + // TODO: also create and destroy + _rmm.subscribe(this, RegistryMessageModel_Event::message_updated); + _nft.subscribe(this, NGCFT1_Event::recv_request); _nft.subscribe(this, NGCFT1_Event::recv_init); _nft.subscribe(this, NGCFT1_Event::recv_data); @@ -378,6 +391,7 @@ void SHA1_NGCFT1::iterate(float delta) { auto selected_peer_opt = selectPeerForRequest(ce); if (selected_peer_opt.has_value()) { const auto [group_number, peer_number] = selected_peer_opt.value(); + std::cout << "SHA1_NGCFT1: should ask " << group_number << ":" << peer_number << " for content here\n"; // ... @@ -387,6 +401,60 @@ void SHA1_NGCFT1::iterate(float delta) { } } +bool SHA1_NGCFT1::onEvent(const Message::Events::MessageUpdated& e) { + // see tox_transfer_manager.cpp for reference + if (!e.e.all_of()) { + return false; + } + + //accept(e.e, e.e.get().save_to_path); + auto ce = e.e.get(); + + //if (!ce.all_of()) { + if (!ce.all_of()) { + // not ready to load yet, skip + return false; + } + assert(!ce.all_of()); + assert(!ce.all_of()); + + // first, open file for write(+readback) + std::string full_file_path{e.e.get().save_to_path}; + // TODO: replace with filesystem or something + // TODO: ensure dir exists + if (full_file_path.back() != '/') { + full_file_path += "/"; + } + + std::filesystem::create_directories(full_file_path); + + const auto& info = ce.get(); + full_file_path += info.file_name; + + ce.emplace(std::vector{full_file_path}); + + // HACK truncate + auto file_impl = std::make_unique(full_file_path, info.file_size, true); + if (!file_impl->isGood()) { + std::cerr << "SHA1_NGCFT1 error: failed opening file '" << full_file_path << "'!\n"; + //e.e.remove(); // stop + return false; + } + ce.emplace(std::move(file_impl)); + + // next, create chuck cache and check for existing data + + // now, enque + _queue_content_want_chunk.push_back(ce); + + // should? + e.e.remove(); + + updateMessages(ce); + + return false; +} + bool SHA1_NGCFT1::onEvent(const Events::NGCFT1_recv_request& e) { // only interested in sha1 if (e.file_kind != NGCFT1_file_kind::HASH_SHA1_INFO && e.file_kind != NGCFT1_file_kind::HASH_SHA1_CHUNK) { @@ -769,6 +837,7 @@ bool SHA1_NGCFT1::onEvent(const Events::NGCFT1_recv_message& e) { //ce.emplace(std::move(file_impl)); } ce.get_or_emplace().messages.push_back({reg, new_msg_e}); + reg_ptr->emplace(new_msg_e, ce); ce.get_or_emplace().participants.emplace(c); diff --git a/src/sha1_ngcft1.hpp b/src/sha1_ngcft1.hpp index 14f0634..bfd74c9 100644 --- a/src/sha1_ngcft1.hpp +++ b/src/sha1_ngcft1.hpp @@ -113,6 +113,9 @@ class SHA1_NGCFT1 : public RegistryMessageModelEventI, public NGCFT1EventI { void iterate(float delta); + protected: // rmm events (actions) + bool onEvent(const Message::Events::MessageUpdated&) override; + protected: // events bool onEvent(const Events::NGCFT1_recv_request&) override; bool onEvent(const Events::NGCFT1_recv_init&) override;