Compare commits

...

2 Commits

Author SHA1 Message Date
Green Sky
70620a901b
starting the work on hs2 rizzler 2024-12-07 23:34:19 +01:00
Green Sky
231928339e
sigma 2024-12-07 11:38:31 +01:00
5 changed files with 208 additions and 68 deletions

View File

@ -124,11 +124,11 @@ endif()
add_library(solanaceae_ngchs2
./solanaceae/ngc_hs2/ts_find_start.hpp
./solanaceae/ngc_hs2/ngc_hs2_send.hpp
./solanaceae/ngc_hs2/ngc_hs2_send.cpp
./solanaceae/ngc_hs2/ngc_hs2_sigma.hpp
./solanaceae/ngc_hs2/ngc_hs2_sigma.cpp
#./solanaceae/ngc_hs2/ngc_hs2_recv.hpp
#./solanaceae/ngc_hs2/ngc_hs2_recv.cpp
./solanaceae/ngc_hs2/ngc_hs2_rizzler.hpp
./solanaceae/ngc_hs2/ngc_hs2_rizzler.cpp
)
target_include_directories(solanaceae_ngchs2 PUBLIC .)
target_compile_features(solanaceae_ngchs2 PUBLIC cxx_std_17)

View File

@ -0,0 +1,123 @@
#include "./ngc_hs2_rizzler.hpp"
#include <solanaceae/tox_contacts/tox_contact_model2.hpp>
#include <solanaceae/tox_contacts/components.hpp>
#include <solanaceae/message3/registry_message_model.hpp>
#include <iostream>
NGCHS2Rizzler::NGCHS2Rizzler(
Contact3Registry& cr,
RegistryMessageModelI& rmm,
ToxContactModel2& tcm,
NGCFT1& nft
) :
_cr(cr),
_rmm(rmm),
_tcm(tcm),
_nft(nft),
_nftep_sr(_nft.newSubRef(this))
{
_nftep_sr
.subscribe(NGCFT1_Event::recv_init)
.subscribe(NGCFT1_Event::recv_data)
.subscribe(NGCFT1_Event::recv_done)
;
}
NGCHS2Rizzler::~NGCHS2Rizzler(void) {
}
float NGCHS2Rizzler::iterate(float delta) {
for (auto it = _request_queue.begin(); it != _request_queue.end();) {
it->second.timer += delta;
if (it->second.timer < it->second.delay) {
it++;
continue;
}
if (!_cr.all_of<Contact::Components::ToxGroupPeerEphemeral>(it->first)) {
// peer nolonger online
it = _request_queue.erase(it);
continue;
}
const auto [group_number, peer_number] = _cr.get<Contact::Components::ToxGroupPeerEphemeral>(it->first);
// now in sec
const uint64_t ts_now = Message::getTimeMS()/1000;
if (sendRequest(group_number, peer_number, ts_now, ts_now-(60*48))) {
// TODO: requeue
// TODO: segment
// TODO: dont request already received ranges
//// on success, requeue with longer delay (minutes)
//it->second.timer = 0.f;
//it->second.delay = _delay_next_request_min + _rng_dist(_rng)*_delay_next_request_add;
//// double the delay for overlap (9m-15m)
//// TODO: finetune
//it->second.sync_delta = uint8_t((it->second.delay/60.f)*2.f) + 1;
//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);
}
return 1000.f;
}
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;
}
bool NGCHS2Rizzler::onEvent(const Events::NGCFT1_recv_init&) {
return false;
}
bool NGCHS2Rizzler::onEvent(const Events::NGCFT1_recv_data&) {
return false;
}
bool NGCHS2Rizzler::onEvent(const Events::NGCFT1_recv_done&) {
return false;
}
bool NGCHS2Rizzler::onToxEvent(const Tox_Event_Group_Peer_Join* e) {
const auto group_number = tox_event_group_peer_join_get_group_number(e);
const auto peer_number = tox_event_group_peer_join_get_peer_id(e);
const auto c = _tcm.getContactGroupPeer(group_number, peer_number);
if (!c) {
return false;
}
if (!_request_queue.count(c)) {
_request_queue[c] = {
_delay_before_first_request_min + _rng_dist(_rng)*_delay_before_first_request_add,
0.f,
0,
};
}
return false;
}

View File

@ -0,0 +1,60 @@
#pragma once
#include <solanaceae/contact/contact_model3.hpp>
#include <solanaceae/ngc_ft1/ngcft1.hpp>
// fwd
class ToxContactModel2;
class RegistryMessageModelI;
class NGCHS2Rizzler : public ToxEventI, public NGCFT1EventI {
Contact3Registry& _cr;
RegistryMessageModelI& _rmm;
ToxContactModel2& _tcm;
NGCFT1& _nft;
NGCFT1EventProviderI::SubscriptionReference _nftep_sr;
// 5s-6s
const float _delay_before_first_request_min {5.f};
const float _delay_before_first_request_add {1.f};
std::uniform_real_distribution<float> _rng_dist {0.0f, 1.0f};
std::minstd_rand _rng;
struct RequestQueueInfo {
float delay; // const
float timer;
uint64_t sync_delta; //?
};
// request queue
// c -> delay, timer
std::map<Contact3, RequestQueueInfo> _request_queue;
public:
NGCHS2Rizzler(
Contact3Registry& cr,
RegistryMessageModelI& rmm,
ToxContactModel2& tcm,
NGCFT1& nf
);
~NGCHS2Rizzler(void);
float iterate(float delta);
protected:
bool sendRequest(
uint32_t group_number, uint32_t peer_number,
uint64_t ts_start, uint64_t ts_end
);
protected:
bool onEvent(const Events::NGCFT1_recv_init&) override;
bool onEvent(const Events::NGCFT1_recv_data&) override;
bool onEvent(const Events::NGCFT1_recv_done&) override;
protected:
bool onToxEvent(const Tox_Event_Group_Peer_Join* e) override;
};

View File

@ -1,4 +1,4 @@
#include "./ngc_hs2_send.hpp"
#include "./ngc_hs2_sigma.hpp"
#include <solanaceae/util/span.hpp>
@ -39,7 +39,7 @@ void IncommingTimeRangeRequestQueue::queueRequest(const TimeRangeRequest& new_re
} // Components
NGCHS2Send::NGCHS2Send(
NGCHS2Sigma::NGCHS2Sigma(
Contact3Registry& cr,
RegistryMessageModelI& rmm,
ToxContactModel2& tcm,
@ -58,10 +58,10 @@ NGCHS2Send::NGCHS2Send(
;
}
NGCHS2Send::~NGCHS2Send(void) {
NGCHS2Sigma::~NGCHS2Sigma(void) {
}
float NGCHS2Send::iterate(float delta) {
float NGCHS2Sigma::iterate(float delta) {
// limit how often we update here (new fts usually)
if (_iterate_heat > 0.f) {
_iterate_heat -= delta;
@ -147,7 +147,7 @@ float NGCHS2Send::iterate(float delta) {
}
}
for (const auto it : to_remove) {
std::cout << "NGCHS2Send warning: timed out ." << (int)it << "\n";
std::cout << "NGCHS2Sigma warning: timed out ." << (int)it << "\n";
// TODO: need a way to tell ft?
irr._list.erase(it);
// technically we are not supposed to timeout and instead rely on the done event
@ -173,15 +173,15 @@ static uint64_t deserlSimpleType(ByteSpan bytes) {
return value;
}
static uint32_t deserlMID(ByteSpan mid_bytes) {
return deserlSimpleType<uint32_t>(mid_bytes);
}
//static uint32_t deserlMID(ByteSpan mid_bytes) {
// return deserlSimpleType<uint32_t>(mid_bytes);
//}
static uint64_t deserlTS(ByteSpan ts_bytes) {
return deserlSimpleType<uint64_t>(ts_bytes);
}
void NGCHS2Send::handleTimeRange(Contact3Handle c, const Events::NGCFT1_recv_request& e) {
void NGCHS2Sigma::handleTimeRange(Contact3Handle c, const Events::NGCFT1_recv_request& e) {
ByteSpan fid{e.file_id, e.file_id_size};
// TODO: better size check
if (fid.size != sizeof(uint64_t)+sizeof(uint64_t)) {
@ -213,50 +213,7 @@ void NGCHS2Send::handleTimeRange(Contact3Handle c, const Events::NGCFT1_recv_req
);
}
#if 0
void NGCHS2Send::handleSingleMessage(Contact3Handle c, const Events::NGCFT1_recv_request& e) {
ByteSpan fid{e.file_id, e.file_id_size};
// TODO: better size check
if (fid.size != 32+sizeof(uint32_t)+sizeof(uint64_t)) {
std::cerr << "NGCHS2S error: singlemessage not lange enough\n";
return;
}
ByteSpan ppk;
uint32_t mid {0};
uint64_t ts {0}; // deciseconds
// parse
try {
// - ppk
// TOX_GROUP_PEER_PUBLIC_KEY_SIZE (32)
ppk = {fid.ptr, 32};
// - mid
ByteSpan mid_bytes{fid.ptr+ppk.size, sizeof(uint32_t)};
mid = deserlMID(mid_bytes);
// - ts
ByteSpan ts_bytes{mid_bytes.ptr+mid_bytes.size, sizeof(uint64_t)};
ts = deserlTS(ts_bytes);
} catch (...) {
std::cerr << "NGCHS2S error: failed to parse singlemessage\n";
return;
}
// for queue, we need group, peer, msg_ppk, msg_mid, msg_ts
// dedupe insert into queue
c.get_or_emplace<Components::IncommingMsgRequestQueue>().queueRequest({
ppk,
mid,
ts,
});
}
#endif
std::vector<uint8_t> NGCHS2Send::buildChatLogFileRange(Contact3Handle c, uint64_t ts_start, uint64_t ts_end) {
std::vector<uint8_t> NGCHS2Sigma::buildChatLogFileRange(Contact3Handle c, uint64_t ts_start, uint64_t ts_end) {
const Message3Registry* reg_ptr = static_cast<const RegistryMessageModelI&>(_rmm).get(c);
if (reg_ptr == nullptr) {
return {};
@ -368,19 +325,19 @@ std::vector<uint8_t> NGCHS2Send::buildChatLogFileRange(Contact3Handle c, uint64_
return nlohmann::json::to_msgpack(j_array);
}
bool NGCHS2Send::onEvent(const Message::Events::MessageConstruct&) {
bool NGCHS2Sigma::onEvent(const Message::Events::MessageConstruct&) {
return false;
}
bool NGCHS2Send::onEvent(const Message::Events::MessageUpdated&) {
bool NGCHS2Sigma::onEvent(const Message::Events::MessageUpdated&) {
return false;
}
bool NGCHS2Send::onEvent(const Message::Events::MessageDestory&) {
bool NGCHS2Sigma::onEvent(const Message::Events::MessageDestory&) {
return false;
}
bool NGCHS2Send::onEvent(const Events::NGCFT1_recv_request& e) {
bool NGCHS2Sigma::onEvent(const Events::NGCFT1_recv_request& e) {
if (
e.file_kind != NGCFT1_file_kind::HS2_RANGE_TIME_MSGPACK
) {
@ -415,7 +372,7 @@ bool NGCHS2Send::onEvent(const Events::NGCFT1_recv_request& e) {
return true;
}
bool NGCHS2Send::onEvent(const Events::NGCFT1_send_data& e) {
bool NGCHS2Sigma::onEvent(const Events::NGCFT1_send_data& e) {
auto c = _tcm.getContactGroupPeer(e.group_number, e.peer_number);
if (!c) {
return false;
@ -432,7 +389,7 @@ bool NGCHS2Send::onEvent(const Events::NGCFT1_send_data& e) {
auto& transfer = irr._list.at(e.transfer_id);
if (transfer.data.size() < e.data_offset+e.data_size) {
std::cerr << "NGCHS2Send error: ft send data larger then file???\n";
std::cerr << "NGCHS2Sigma error: ft send data larger then file???\n";
assert(false && "how");
}
std::memcpy(e.data, transfer.data.data()+e.data_offset, e.data_size);
@ -441,7 +398,7 @@ bool NGCHS2Send::onEvent(const Events::NGCFT1_send_data& e) {
return true;
}
bool NGCHS2Send::onEvent(const Events::NGCFT1_send_done& e) {
bool NGCHS2Sigma::onEvent(const Events::NGCFT1_send_done& e) {
auto c = _tcm.getContactGroupPeer(e.group_number, e.peer_number);
if (!c) {
return false;
@ -459,7 +416,7 @@ bool NGCHS2Send::onEvent(const Events::NGCFT1_send_done& e) {
irr._list.erase(e.transfer_id);
// TODO: check if we completed it
std::cout << "NGCHS2Send: sent chatlog to " << e.group_number << ":" << e.peer_number << "." << (int)e.transfer_id << "\n";
std::cout << "NGCHS2Sigma: sent chatlog to " << e.group_number << ":" << e.peer_number << "." << (int)e.transfer_id << "\n";
return true;
}

View File

@ -47,7 +47,7 @@ namespace Components {
};
} // Components
class NGCHS2Send : public RegistryMessageModelEventI, public NGCFT1EventI {
class NGCHS2Sigma : public RegistryMessageModelEventI, public NGCFT1EventI {
Contact3Registry& _cr;
RegistryMessageModelI& _rmm;
ToxContactModel2& _tcm;
@ -72,14 +72,14 @@ class NGCHS2Send : public RegistryMessageModelEventI, public NGCFT1EventI {
constexpr static int64_t _max_time_into_past_default {60*15}; // s
public:
NGCHS2Send(
NGCHS2Sigma(
Contact3Registry& cr,
RegistryMessageModelI& rmm,
ToxContactModel2& tcm,
NGCFT1& nf
);
~NGCHS2Send(void);
~NGCHS2Sigma(void);
float iterate(float delta);