2024-11-03 18:21:02 +01:00
|
|
|
#include "./ngc_hs2_send.hpp"
|
2024-11-06 10:50:42 +01:00
|
|
|
|
|
|
|
#include <solanaceae/util/span.hpp>
|
2024-11-03 18:21:02 +01:00
|
|
|
|
|
|
|
#include <solanaceae/tox_contacts/tox_contact_model2.hpp>
|
|
|
|
|
|
|
|
#include <solanaceae/contact/components.hpp>
|
|
|
|
|
2024-11-22 13:51:25 +01:00
|
|
|
#include <iostream>
|
|
|
|
|
2024-11-03 18:21:02 +01:00
|
|
|
// https://www.youtube.com/watch?v=AdAqsgga3qo
|
|
|
|
|
2024-11-22 13:51:25 +01:00
|
|
|
namespace Components {
|
|
|
|
|
2024-11-28 12:39:57 +01:00
|
|
|
void IncommingTimeRangeRequestQueue::queueRequest(const TimeRangeRequest& new_request) {
|
2024-11-22 13:51:25 +01:00
|
|
|
// TODO: do more than exact dedupe
|
|
|
|
for (const auto& [ts_start, ts_end] : _queue) {
|
|
|
|
if (ts_start == new_request.ts_start && ts_end == new_request.ts_end) {
|
|
|
|
return; // already enqueued
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_queue.push_back(new_request);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // Components
|
|
|
|
|
|
|
|
|
2024-11-03 18:21:02 +01:00
|
|
|
NGCHS2Send::NGCHS2Send(
|
|
|
|
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_request)
|
|
|
|
//.subscribe(NGCFT1_Event::recv_init) // we only send init
|
|
|
|
//.subscribe(NGCFT1_Event::recv_data) // we only send data
|
|
|
|
.subscribe(NGCFT1_Event::send_data)
|
|
|
|
//.subscribe(NGCFT1_Event::recv_done)
|
|
|
|
.subscribe(NGCFT1_Event::send_done)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
NGCHS2Send::~NGCHS2Send(void) {
|
|
|
|
}
|
|
|
|
|
|
|
|
float NGCHS2Send::iterate(float delta) {
|
2024-11-22 13:51:25 +01:00
|
|
|
// limit how often we update here (new fts usually)
|
|
|
|
if (_iterate_heat > 0.f) {
|
|
|
|
_iterate_heat -= delta;
|
|
|
|
return 1000.f;
|
|
|
|
} else {
|
|
|
|
_iterate_heat = _iterate_cooldown;
|
|
|
|
}
|
2024-11-03 18:21:02 +01:00
|
|
|
|
|
|
|
// work request queue
|
|
|
|
// check if already running, discard
|
|
|
|
|
2024-11-22 13:51:25 +01:00
|
|
|
auto fn_iirq = [this](auto&& view) {
|
|
|
|
for (auto&& [cv, iirq] : view.each()) {
|
|
|
|
Contact3Handle c{_cr, cv};
|
2024-11-28 12:39:57 +01:00
|
|
|
auto& iirr = c.get_or_emplace<Components::IncommingTimeRangeRequestRunning>();
|
2024-11-22 13:51:25 +01:00
|
|
|
|
|
|
|
// dedup queued from running
|
|
|
|
|
|
|
|
if (iirr._list.size() >= _max_parallel_per_peer) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// new ft here?
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// first handle range requests on weak self
|
2024-11-28 12:39:57 +01:00
|
|
|
//for (auto&& [cv, iirq] : _cr.view<Contact::Components::TagSelfWeak, Components::IncommingTimeRangeRequestQueue>().each()) {
|
|
|
|
fn_iirq(_cr.view<Contact::Components::TagSelfWeak, Components::IncommingTimeRangeRequestQueue>());
|
2024-11-22 13:51:25 +01:00
|
|
|
|
|
|
|
// we could stop here, if too much is already running
|
|
|
|
|
|
|
|
// then range on others
|
2024-11-28 12:39:57 +01:00
|
|
|
//for (auto&& [cv, iirq] : _cr.view<Components::IncommingTimeRangeRequestQueue>(entt::exclude_t<Contact::Components::TagSelfWeak>{}).each()) {
|
|
|
|
fn_iirq(_cr.view<Components::IncommingTimeRangeRequestQueue>(entt::exclude_t<Contact::Components::TagSelfWeak>{}));
|
2024-11-22 13:51:25 +01:00
|
|
|
|
2024-11-03 18:21:02 +01:00
|
|
|
return 1000.f;
|
|
|
|
}
|
|
|
|
|
2024-11-22 13:51:25 +01:00
|
|
|
template<typename Type>
|
|
|
|
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 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);
|
|
|
|
}
|
|
|
|
|
2024-11-28 12:39:57 +01:00
|
|
|
void NGCHS2Send::handleTimeRange(Contact3Handle c, const Events::NGCFT1_recv_request& e) {
|
2024-11-03 18:21:02 +01:00
|
|
|
ByteSpan fid{e.file_id, e.file_id_size};
|
2024-11-22 13:51:25 +01:00
|
|
|
// TODO: better size check
|
|
|
|
if (fid.size != sizeof(uint64_t)+sizeof(uint64_t)) {
|
|
|
|
std::cerr << "NGCHS2S error: range not lange enough\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// seconds
|
|
|
|
uint64_t ts_start{0};
|
|
|
|
uint64_t ts_end{0};
|
|
|
|
|
2024-11-03 18:21:02 +01:00
|
|
|
// parse
|
2024-11-22 13:51:25 +01:00
|
|
|
try {
|
|
|
|
ByteSpan ts_start_bytes{fid.ptr, sizeof(uint64_t)};
|
|
|
|
ts_start = deserlTS(ts_start_bytes);
|
|
|
|
|
|
|
|
ByteSpan ts_end_bytes{ts_start_bytes.ptr+ts_start_bytes.size, sizeof(uint64_t)};
|
|
|
|
ts_end = deserlTS(ts_end_bytes);
|
|
|
|
} catch (...) {
|
|
|
|
std::cerr << "NGCHS2S error: failed to parse range\n";
|
|
|
|
return;
|
|
|
|
}
|
2024-11-03 18:21:02 +01:00
|
|
|
|
|
|
|
// dedupe insert into queue
|
2024-11-22 13:51:25 +01:00
|
|
|
// how much overlap do we allow?
|
2024-11-28 12:39:57 +01:00
|
|
|
c.get_or_emplace<Components::IncommingTimeRangeRequestQueue>().queueRequest({
|
2024-11-22 13:51:25 +01:00
|
|
|
ts_start,
|
|
|
|
ts_end,
|
|
|
|
});
|
2024-11-03 18:21:02 +01:00
|
|
|
}
|
|
|
|
|
2024-11-28 12:39:57 +01:00
|
|
|
#if 0
|
2024-11-03 18:21:02 +01:00
|
|
|
void NGCHS2Send::handleSingleMessage(Contact3Handle c, const Events::NGCFT1_recv_request& e) {
|
|
|
|
ByteSpan fid{e.file_id, e.file_id_size};
|
2024-11-22 13:51:25 +01:00
|
|
|
// TODO: better size check
|
2024-11-06 10:50:42 +01:00
|
|
|
if (fid.size != 32+sizeof(uint32_t)+sizeof(uint64_t)) {
|
2024-11-22 13:51:25 +01:00
|
|
|
std::cerr << "NGCHS2S error: singlemessage not lange enough\n";
|
2024-11-06 10:50:42 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-11-22 13:51:25 +01:00
|
|
|
ByteSpan ppk;
|
|
|
|
uint32_t mid {0};
|
|
|
|
uint64_t ts {0}; // deciseconds
|
2024-11-06 10:50:42 +01:00
|
|
|
|
2024-11-22 13:51:25 +01:00
|
|
|
// 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;
|
|
|
|
}
|
2024-11-03 18:21:02 +01:00
|
|
|
|
|
|
|
|
2024-11-06 10:50:42 +01:00
|
|
|
// for queue, we need group, peer, msg_ppk, msg_mid, msg_ts
|
|
|
|
|
2024-11-03 18:21:02 +01:00
|
|
|
// dedupe insert into queue
|
2024-11-22 13:51:25 +01:00
|
|
|
c.get_or_emplace<Components::IncommingMsgRequestQueue>().queueRequest({
|
|
|
|
ppk,
|
|
|
|
mid,
|
|
|
|
ts,
|
|
|
|
});
|
2024-11-03 18:21:02 +01:00
|
|
|
}
|
2024-11-28 12:39:57 +01:00
|
|
|
#endif
|
2024-11-03 18:21:02 +01:00
|
|
|
|
|
|
|
bool NGCHS2Send::onEvent(const Message::Events::MessageConstruct&) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NGCHS2Send::onEvent(const Message::Events::MessageUpdated&) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NGCHS2Send::onEvent(const Message::Events::MessageDestory&) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NGCHS2Send::onEvent(const Events::NGCFT1_recv_request& e) {
|
|
|
|
if (
|
2024-11-28 12:39:57 +01:00
|
|
|
e.file_kind != NGCFT1_file_kind::HS2_RANGE_TIME_MSGPACK
|
2024-11-03 18:21:02 +01:00
|
|
|
) {
|
|
|
|
return false; // not for us
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: when is it done from queue?
|
|
|
|
|
|
|
|
auto c = _tcm.getContactGroupPeer(e.group_number, e.peer_number);
|
|
|
|
if (!c) {
|
|
|
|
return false; // how
|
|
|
|
}
|
|
|
|
|
|
|
|
// is other peer allowed to make requests
|
|
|
|
//bool quick_allow {false};
|
|
|
|
bool quick_allow {true}; // HACK: disable all restrictions for this early test
|
|
|
|
// TODO: quick deny?
|
|
|
|
{
|
|
|
|
// - tagged as weakself
|
|
|
|
if (!quick_allow && c.all_of<Contact::Components::TagSelfWeak>()) {
|
|
|
|
quick_allow = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// - sub perm level??
|
|
|
|
// - out of max time range (ft specific, not a quick_allow)
|
|
|
|
}
|
|
|
|
|
2024-11-28 12:39:57 +01:00
|
|
|
if (e.file_kind == NGCFT1_file_kind::HS2_RANGE_TIME_MSGPACK) {
|
|
|
|
handleTimeRange(c, e);
|
2024-11-03 18:21:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NGCHS2Send::onEvent(const Events::NGCFT1_send_data&) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NGCHS2Send::onEvent(const Events::NGCFT1_send_done&) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|