diff --git a/plugins/plugin_zox_ngc_hs.cpp b/plugins/plugin_zox_ngc_hs.cpp index 3e38c37..13c3c8a 100644 --- a/plugins/plugin_zox_ngc_hs.cpp +++ b/plugins/plugin_zox_ngc_hs.cpp @@ -36,13 +36,13 @@ SOLANA_PLUGIN_EXPORT uint32_t solana_plugin_start(struct SolanaAPI* solana_api) auto* tox_i = PLUG_RESOLVE_INSTANCE(ToxI); auto* tox_event_provider_i = PLUG_RESOLVE_INSTANCE(ToxEventProviderI); auto* zox_ngc_event_provider_i = PLUG_RESOLVE_INSTANCE(ZoxNGCEventProviderI); - auto* cr = PLUG_RESOLVE_INSTANCE_VERSIONED(Contact3Registry, "1"); + auto* cs = PLUG_RESOLVE_INSTANCE(ContactStore4I); auto* tcm = PLUG_RESOLVE_INSTANCE(ToxContactModel2); auto* rmm = PLUG_RESOLVE_INSTANCE(RegistryMessageModelI); // static store, could be anywhere tho // construct with fetched dependencies - g_zngchs = std::make_unique(*tox_event_provider_i, *zox_ngc_event_provider_i, *tox_i, *cr, *tcm, *rmm); + g_zngchs = std::make_unique(*tox_event_provider_i, *zox_ngc_event_provider_i, *tox_i, *cs, *tcm, *rmm); // register types PLUG_PROVIDE_INSTANCE(ZoxNGCHistorySync, plugin_name, g_zngchs.get()); diff --git a/src/solanaceae/zox/ngc_hs.cpp b/src/solanaceae/zox/ngc_hs.cpp index 623db44..75c110e 100644 --- a/src/solanaceae/zox/ngc_hs.cpp +++ b/src/solanaceae/zox/ngc_hs.cpp @@ -3,6 +3,7 @@ #include #include +#include #include #include #include @@ -16,8 +17,8 @@ #include #include -ZoxNGCHistorySync::ZoxNGCHistorySync(ToxEventProviderI& tep, ZoxNGCEventProviderI& zngcepi, ToxI& t, Contact3Registry& cr, ToxContactModel2& tcm, RegistryMessageModelI& rmm) - : _tep_sr(tep.newSubRef(this)), _zngcepi_sr(zngcepi.newSubRef(this)), _t(t), _cr(cr), _tcm(tcm), _rmm(rmm), _rng(std::random_device{}()) +ZoxNGCHistorySync::ZoxNGCHistorySync(ToxEventProviderI& tep, ZoxNGCEventProviderI& zngcepi, ToxI& t, ContactStore4I& cs, ToxContactModel2& tcm, RegistryMessageModelI& rmm) + : _tep_sr(tep.newSubRef(this)), _zngcepi_sr(zngcepi.newSubRef(this)), _t(t), _cs(cs), _tcm(tcm), _rmm(rmm), _rng(std::random_device{}()) { _tep_sr.subscribe(Tox_Event_Type::TOX_EVENT_GROUP_PEER_JOIN); @@ -35,12 +36,14 @@ float ZoxNGCHistorySync::tick(float delta) { it->second.timer += delta; if (it->second.timer >= it->second.delay) { - if (!_cr.all_of(it->first)) { + const auto& cr = _cs.registry(); + + if (!cr.all_of(it->first)) { // peer nolonger online it = _request_queue.erase(it); continue; } - const auto [group_number, peer_number] = _cr.get(it->first); + const auto [group_number, peer_number] = cr.get(it->first); if (sendRequest(group_number, peer_number, it->second.sync_delta)) { // on success, requeue with longer delay (minutes) @@ -77,12 +80,14 @@ float ZoxNGCHistorySync::tick(float delta) { Message3 msg_e = it->second.ents.front(); it->second.ents.pop(); - if (!_cr.all_of(it->first)) { + const auto& cr = _cs.registry(); + + if (!cr.all_of(it->first)) { // peer nolonger online it = _sync_queue.erase(it); continue; } - const auto [group_number, peer_number] = _cr.get(it->first); + const auto [group_number, peer_number] = cr.get(it->first); auto* reg_ptr = _rmm.get(it->first); if (reg_ptr == nullptr) { @@ -105,7 +110,7 @@ float ZoxNGCHistorySync::tick(float delta) { } const auto& msg_sender = reg.get(msg_e).c; - if (!_cr.all_of(msg_sender)) { + if (!cr.all_of(msg_sender)) { std::cerr << "ZOX NGCHS error: msg sender without persistant\n"; continue; } @@ -114,8 +119,8 @@ float ZoxNGCHistorySync::tick(float delta) { // TODO: make sure there is no alias leaked //const auto msg_sender_name = _cm.getContactName(msg_sender); std::string_view msg_sender_name; - if (_cr.all_of(msg_sender)) { - msg_sender_name = _cr.get(msg_sender).name; + if (cr.all_of(msg_sender)) { + msg_sender_name = cr.get(msg_sender).name; } @@ -123,7 +128,7 @@ float ZoxNGCHistorySync::tick(float delta) { group_number, peer_number, reg.get(msg_e).id, - _cr.get(msg_sender).peer_key.data, + cr.get(msg_sender).peer_key.data, std::chrono::duration_cast(std::chrono::milliseconds{reg.get(msg_e).ts}).count(), msg_sender_name, reg.get(msg_e).text @@ -289,8 +294,10 @@ bool ZoxNGCHistorySync::onEvent(const Events::ZoxNGC_ngch_request& e) { const auto& c_t = reg.get(e); const auto& ts = view.get(e); + const auto& cr = _cs.registry(); + // private - if (!_cr.all_of(c_t.c)) { + if (!cr.all_of(c_t.c)) { continue; } @@ -305,9 +312,9 @@ bool ZoxNGCHistorySync::onEvent(const Events::ZoxNGC_ngch_request& e) { if ( std::find_if( list.cbegin(), list.cend(), - [this](const auto&& it) { + [this, &cr](const auto&& it) { // TODO: add weak self - return _cr.all_of(it.first); + return cr.all_of(it.first); } ) == list.cend() ) { diff --git a/src/solanaceae/zox/ngc_hs.hpp b/src/solanaceae/zox/ngc_hs.hpp index dcdd637..281fa4b 100644 --- a/src/solanaceae/zox/ngc_hs.hpp +++ b/src/solanaceae/zox/ngc_hs.hpp @@ -2,7 +2,7 @@ #include "./ngc.hpp" -#include +#include #include #include @@ -23,7 +23,7 @@ class ZoxNGCHistorySync : public ToxEventI, public ZoxNGCEventI { ToxEventProviderI::SubscriptionReference _tep_sr; ZoxNGCEventProviderI::SubscriptionReference _zngcepi_sr; ToxI& _t; - Contact3Registry& _cr; + ContactStore4I& _cs; ToxContactModel2& _tcm; RegistryMessageModelI& _rmm; @@ -52,7 +52,7 @@ class ZoxNGCHistorySync : public ToxEventI, public ZoxNGCEventI { }; // request queue // c -> delay, timer - std::map _request_queue; + std::map _request_queue; struct SyncQueueInfo { float delay; // const @@ -60,10 +60,10 @@ class ZoxNGCHistorySync : public ToxEventI, public ZoxNGCEventI { std::queue ents; //std::reference_wrapper reg; }; - std::map _sync_queue; + std::map _sync_queue; public: - ZoxNGCHistorySync(ToxEventProviderI& tep, ZoxNGCEventProviderI& zngcepi, ToxI& t, Contact3Registry& cr, ToxContactModel2& tcm, RegistryMessageModelI& rmm); + ZoxNGCHistorySync(ToxEventProviderI& tep, ZoxNGCEventProviderI& zngcepi, ToxI& t, ContactStore4I& cs, ToxContactModel2& tcm, RegistryMessageModelI& rmm); float tick(float delta);