From a139f412b1f2fb3adf138d38fc26877beea01dcf Mon Sep 17 00:00:00 2001 From: Green Sky Date: Sun, 8 Dec 2024 16:08:30 +0100 Subject: [PATCH] various fixes resulting in the first time running the code --- solanaceae/ngc_ft1/ngcft1.cpp | 5 +-- solanaceae/ngc_ft1/ngcft1.hpp | 2 +- solanaceae/ngc_hs2/ngc_hs2_rizzler.cpp | 62 +++++++++++++++++++++----- solanaceae/ngc_hs2/ngc_hs2_sigma.cpp | 28 +++++++++++- 4 files changed, 79 insertions(+), 18 deletions(-) diff --git a/solanaceae/ngc_ft1/ngcft1.cpp b/solanaceae/ngc_ft1/ngcft1.cpp index 331cf2e..c27e1db 100644 --- a/solanaceae/ngc_ft1/ngcft1.cpp +++ b/solanaceae/ngc_ft1/ngcft1.cpp @@ -308,13 +308,12 @@ float NGCFT1::iterate(float time_delta) { } } -void NGCFT1::NGC_FT1_send_request_private( +bool NGCFT1::NGC_FT1_send_request_private( uint32_t group_number, uint32_t peer_number, uint32_t file_kind, const uint8_t* file_id, uint32_t file_id_size ) { - // TODO: error check - _neep.send_ft1_request(group_number, peer_number, file_kind, file_id, file_id_size); + return _neep.send_ft1_request(group_number, peer_number, file_kind, file_id, file_id_size); } bool NGCFT1::NGC_FT1_send_init_private( diff --git a/solanaceae/ngc_ft1/ngcft1.hpp b/solanaceae/ngc_ft1/ngcft1.hpp index 44d9dda..f52a4d5 100644 --- a/solanaceae/ngc_ft1/ngcft1.hpp +++ b/solanaceae/ngc_ft1/ngcft1.hpp @@ -223,7 +223,7 @@ class NGCFT1 : public ToxEventI, public NGCEXTEventI, public NGCFT1EventProvider float iterate(float delta); public: // ft1 api - void NGC_FT1_send_request_private( + bool NGC_FT1_send_request_private( uint32_t group_number, uint32_t peer_number, uint32_t file_kind, const uint8_t* file_id, uint32_t file_id_size diff --git a/solanaceae/ngc_hs2/ngc_hs2_rizzler.cpp b/solanaceae/ngc_hs2/ngc_hs2_rizzler.cpp index 5424a66..ce9d3cf 100644 --- a/solanaceae/ngc_hs2/ngc_hs2_rizzler.cpp +++ b/solanaceae/ngc_hs2/ngc_hs2_rizzler.cpp @@ -1,11 +1,11 @@ #include "./ngc_hs2_rizzler.hpp" -#include "tox/tox_events.h" #include - -#include - #include +#include +#include + +#include #include @@ -57,7 +57,7 @@ float NGCHS2Rizzler::iterate(float delta) { // now in sec const uint64_t ts_now = Message::getTimeMS()/1000; - if (sendRequest(group_number, peer_number, ts_now, ts_now-(60*48))) { + if (sendRequest(group_number, peer_number, ts_now, ts_now-(60*60*48))) { // TODO: requeue // TODO: segment // TODO: dont request already received ranges @@ -73,29 +73,67 @@ float NGCHS2Rizzler::iterate(float delta) { //std::cout << "ZOX #### requeued request in " << it->second.delay << "s\n"; - it++; } else { // on failure, assume disconnected - it = _request_queue.erase(it); } - // just choose something small, since we expect a response might arrive soon - //min_interval = std::min(min_interval, _delay_between_syncs_min); + // remove from request queue + it = _request_queue.erase(it); } - return 1000.f; } +template +static uint64_t deserlSimpleType(ByteSpan bytes) { + if (bytes.size < sizeof(Type)) { + throw int(1); + } + + Type value{}; + + for (size_t i = 0; i < sizeof(Type); i++) { + value |= Type(bytes[i]) << (i*8); + } + + return value; +} + +static uint64_t deserlTS(ByteSpan ts_bytes) { + return deserlSimpleType(ts_bytes); +} + +template +static void serlSimpleType(std::vector& bytes, const Type& value) { + for (size_t i = 0; i < sizeof(Type); i++) { + bytes.push_back(uint8_t(value >> (i*8) & 0xff)); + } +} + bool NGCHS2Rizzler::sendRequest( uint32_t group_number, uint32_t peer_number, uint64_t ts_start, uint64_t ts_end ) { std::cout << "NGCHS2Rizzler: sending request to " << group_number << ":" << peer_number << " (" << ts_start << "," << ts_end << ")\n"; - return false; + + // build fid + std::vector fid; + fid.reserve(sizeof(uint64_t)+sizeof(uint64_t)); + + serlSimpleType(fid, ts_start); + serlSimpleType(fid, ts_end); + + assert(fid.size() == sizeof(uint64_t)+sizeof(uint64_t)); + + return _nft.NGC_FT1_send_request_private( + group_number, peer_number, + (uint32_t)NGCFT1_file_kind::HS2_RANGE_TIME_MSGPACK, + fid.data(), fid.size() // fid + ); } -bool NGCHS2Rizzler::onEvent(const Events::NGCFT1_recv_init&) { +bool NGCHS2Rizzler::onEvent(const Events::NGCFT1_recv_init& e) { + std::cout << "NGCHS2Rizzler: recv_init " << e.group_number << ":" << e.peer_number << "." << (int)e.transfer_id << "\n"; return false; } diff --git a/solanaceae/ngc_hs2/ngc_hs2_sigma.cpp b/solanaceae/ngc_hs2/ngc_hs2_sigma.cpp index 758fbe9..2b161ec 100644 --- a/solanaceae/ngc_hs2/ngc_hs2_sigma.cpp +++ b/solanaceae/ngc_hs2/ngc_hs2_sigma.cpp @@ -30,10 +30,14 @@ void IncommingTimeRangeRequestQueue::queueRequest(const TimeRangeRequest& new_re for (const auto& [time_range, _] : _queue) { if (time_range.ts_start == new_request.ts_start && time_range.ts_end == new_request.ts_end) { return; // already enqueued + // TODO: what about fid? } } - _queue.emplace_back(Entry{new_request, std::vector{fid.cbegin(), fid.cend()}}); + _queue.emplace_back(Entry{ + new_request, + std::vector{fid.cbegin(), fid.cend()} + }); } } // Components @@ -75,6 +79,11 @@ float NGCHS2Sigma::iterate(float delta) { auto fn_iirq = [this](auto&& view) { for (auto&& [cv, iirq] : view.each()) { + if (iirq._queue.empty()) { + // TODO: remove comp? + continue; + } + Contact3Handle c{_cr, cv}; auto& iirr = c.get_or_emplace(); @@ -164,7 +173,7 @@ static uint64_t deserlSimpleType(ByteSpan bytes) { throw int(1); } - Type value; + Type value{}; for (size_t i = 0; i < sizeof(Type); i++) { value |= Type(bytes[i]) << (i*8); @@ -205,6 +214,11 @@ void NGCHS2Sigma::handleTimeRange(Contact3Handle c, const Events::NGCFT1_recv_re return; } + if (ts_end >= ts_start) { + std::cerr << "NGCHS2S error: end not < start\n"; + return; + } + // dedupe insert into queue // how much overlap do we allow? c.get_or_emplace().queueRequest( @@ -226,6 +240,13 @@ std::vector NGCHS2Sigma::buildChatLogFileRange(Contact3Handle c, uint64 return {}; } + std::cout << "NGCHS2Sigma: building chatlog for time range " << ts_start-ts_end << "s\n"; + + // convert seconds to milliseconds + // TODO: lift out? + ts_start *= 1000; + ts_end *= 1000; + //std::cout << "!!!! starting msg ts search, ts_start:" << ts_start << " ts_end:" << ts_end << "\n"; auto ts_view = msg_reg.view(); @@ -322,6 +343,8 @@ std::vector NGCHS2Sigma::buildChatLogFileRange(Contact3Handle c, uint64 j_array.push_back(j_entry); } + std::cout << "NGCHS2Sigma: built chat log with " << j_array.size() << " entries\n"; + return nlohmann::json::to_msgpack(j_array); } @@ -399,6 +422,7 @@ bool NGCHS2Sigma::onEvent(const Events::NGCFT1_send_data& e) { } bool NGCHS2Sigma::onEvent(const Events::NGCFT1_send_done& e) { + // TODO: this will return null if the peer just disconnected auto c = _tcm.getContactGroupPeer(e.group_number, e.peer_number); if (!c) { return false;