accept save to and open file (truncated)

This commit is contained in:
Green Sky 2023-08-17 22:43:16 +02:00
parent 8237f902c9
commit 100f4f68c0
No known key found for this signature in database
4 changed files with 74 additions and 2 deletions

@ -1 +1 @@
Subproject commit c73e429df138ce32f5cdb5588a3af8257a946680
Subproject commit 53d65a06855129135e5884f50d9983676290ac24

@ -1 +1 @@
Subproject commit de332421f783a94877b7b529a698e0752273e7a4
Subproject commit 70a234cdae751141f48b7a53a4e1cbedd84f31b2

View File

@ -8,6 +8,7 @@
#include <solanaceae/tox_messages/components.hpp>
#include <solanaceae/message3/file_r_file.hpp>
#include <solanaceae/message3/file_rw_file.hpp>
#include "./ft1_sha1_info.hpp"
#include "./hash_utils.hpp"
@ -18,6 +19,7 @@
#include <iostream>
#include <variant>
#include <filesystem>
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<size_t> 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<Message::Components::Transfer::ActionAccept, Message::Components::Content>()) {
return false;
}
//accept(e.e, e.e.get<Message::Components::Transfer::ActionAccept>().save_to_path);
auto ce = e.e.get<Message::Components::Content>();
//if (!ce.all_of<Components::FT1InfoSHA1, Components::FT1ChunkSHA1Cache>()) {
if (!ce.all_of<Components::FT1InfoSHA1>()) {
// not ready to load yet, skip
return false;
}
assert(!ce.all_of<Components::FT1ChunkSHA1Cache>());
assert(!ce.all_of<Message::Components::Transfer::File>());
// first, open file for write(+readback)
std::string full_file_path{e.e.get<Message::Components::Transfer::ActionAccept>().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<Components::FT1InfoSHA1>();
full_file_path += info.file_name;
ce.emplace<Message::Components::Transfer::FileInfoLocal>(std::vector{full_file_path});
// HACK truncate
auto file_impl = std::make_unique<FileRWFile>(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<Message::Components::Transfer::ActionAccept>(); // stop
return false;
}
ce.emplace<Message::Components::Transfer::File>(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<Message::Components::Transfer::ActionAccept>();
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<Message::Components::Transfer::File>(std::move(file_impl));
}
ce.get_or_emplace<Components::Messages>().messages.push_back({reg, new_msg_e});
reg_ptr->emplace<Message::Components::Content>(new_msg_e, ce);
ce.get_or_emplace<Components::SuspectedParticipants>().participants.emplace(c);

View File

@ -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;