handle pc1 announce and reduce chance to sample random peer

(will remove random sample sometime in the future)
This commit is contained in:
Green Sky 2024-06-22 17:01:52 +02:00
parent e9f22bc9ae
commit bcde244a3c
No known key found for this signature in database
3 changed files with 52 additions and 7 deletions

View File

@ -18,7 +18,7 @@ struct SHA1Digest {
bool operator==(const SHA1Digest& other) const { return data == other.data; } bool operator==(const SHA1Digest& other) const { return data == other.data; }
bool operator!=(const SHA1Digest& other) const { return data != other.data; } bool operator!=(const SHA1Digest& other) const { return data != other.data; }
size_t size(void) const { return data.size(); } constexpr size_t size(void) const { return data.size(); }
}; };
std::ostream& operator<<(std::ostream& out, const SHA1Digest& v); std::ostream& operator<<(std::ostream& out, const SHA1Digest& v);

View File

@ -60,6 +60,7 @@ namespace Components {
entt::dense_map<size_t, float> chunks; entt::dense_map<size_t, float> chunks;
}; };
// TODO: once announce is shipped, remove the "Suspected"
struct SuspectedParticipants { struct SuspectedParticipants {
entt::dense_set<Contact3> participants; entt::dense_set<Contact3> participants;
}; };
@ -199,11 +200,11 @@ std::optional<std::pair<uint32_t, uint32_t>> SHA1_NGCFT1::selectPeerForRequest(O
} }
} }
// 1 in 20 chance to ask random peer instead // 1 in 40 chance to ask random peer instead
// TODO: config + tweak // TODO: config + tweak
// TODO: save group in content to avoid the tox_peers list build // TODO: save group in content to avoid the tox_peers list build
// TODO: remove once pc1_announce is shipped // TODO: remove once pc1_announce is shipped
if (tox_peers.empty() || (_rng()%20) == 0) { if (tox_peers.empty() || (_rng()%40) == 0) {
// meh // meh
// HACK: determain group based on last tox_peers // HACK: determain group based on last tox_peers
if (!tox_peers.empty()) { if (!tox_peers.empty()) {
@ -251,14 +252,16 @@ SHA1_NGCFT1::SHA1_NGCFT1(
RegistryMessageModel& rmm, RegistryMessageModel& rmm,
NGCFT1& nft, NGCFT1& nft,
ToxContactModel2& tcm, ToxContactModel2& tcm,
ToxEventProviderI& tep ToxEventProviderI& tep,
NGCEXTEventProviderI& neep
) : ) :
_os(os), _os(os),
_cr(cr), _cr(cr),
_rmm(rmm), _rmm(rmm),
_nft(nft), _nft(nft),
_tcm(tcm), _tcm(tcm),
_tep(tep) _tep(tep),
_neep(neep)
{ {
// TODO: also create and destroy // TODO: also create and destroy
_rmm.subscribe(this, RegistryMessageModel_Event::message_updated); _rmm.subscribe(this, RegistryMessageModel_Event::message_updated);
@ -278,6 +281,8 @@ SHA1_NGCFT1::SHA1_NGCFT1(
_rmm.subscribe(this, RegistryMessageModel_Event::send_file_path); _rmm.subscribe(this, RegistryMessageModel_Event::send_file_path);
_tep.subscribe(this, Tox_Event_Type::TOX_EVENT_GROUP_PEER_EXIT); _tep.subscribe(this, Tox_Event_Type::TOX_EVENT_GROUP_PEER_EXIT);
_neep.subscribe(this, NGCEXT_Event::PC1_ANNOUNCE);
} }
void SHA1_NGCFT1::iterate(float delta) { void SHA1_NGCFT1::iterate(float delta) {
@ -1463,3 +1468,39 @@ bool SHA1_NGCFT1::onToxEvent(const Tox_Event_Group_Peer_Exit* e) {
return false; return false;
} }
bool SHA1_NGCFT1::onEvent(const Events::NGCEXT_pc1_announce& e) {
// id is file_kind + id
uint32_t file_kind = 0u;
static_assert(SHA1Digest{}.size() == 20);
if (e.id.size() != sizeof(file_kind) + 20) {
// not for us
return false;
}
for (size_t i = 0; i < sizeof(file_kind); i++) {
file_kind |= uint32_t(e.id[i]) << (i*8);
}
SHA1Digest hash{e.id.data()+sizeof(file_kind), 20};
// if have use hash(-info) for file, add to participants
std::cout << "SHA1_NGCFT1: got ParticipationChatter1 announce from " << e.group_number << ":" << e.peer_number << " for " << hash << "\n";
auto itc_it = _info_to_content.find(hash);
if (itc_it == _info_to_content.end()) {
// we are not interested and dont track this
return false;
}
// add them to participants
auto ce = itc_it->second;
const auto c = _tcm.getContactGroupPeer(e.group_number, e.peer_number);
const auto [_, was_new] = ce.get_or_emplace<Components::SuspectedParticipants>().participants.emplace(c);
if (was_new) {
std::cout << "SHA1_NGCFT1: and we where interested!\n";
}
return false;
}

View File

@ -21,7 +21,7 @@
#include <mutex> #include <mutex>
#include <list> #include <list>
class SHA1_NGCFT1 : public ToxEventI, public RegistryMessageModelEventI, public NGCFT1EventI { class SHA1_NGCFT1 : public ToxEventI, public RegistryMessageModelEventI, public NGCFT1EventI, public NGCEXTEventI {
ObjectStore2& _os; ObjectStore2& _os;
// TODO: backend abstraction // TODO: backend abstraction
Contact3Registry& _cr; Contact3Registry& _cr;
@ -29,6 +29,7 @@ class SHA1_NGCFT1 : public ToxEventI, public RegistryMessageModelEventI, public
NGCFT1& _nft; NGCFT1& _nft;
ToxContactModel2& _tcm; ToxContactModel2& _tcm;
ToxEventProviderI& _tep; ToxEventProviderI& _tep;
NGCEXTEventProviderI& _neep;
std::minstd_rand _rng {1337*11}; std::minstd_rand _rng {1337*11};
@ -124,7 +125,8 @@ class SHA1_NGCFT1 : public ToxEventI, public RegistryMessageModelEventI, public
RegistryMessageModel& rmm, RegistryMessageModel& rmm,
NGCFT1& nft, NGCFT1& nft,
ToxContactModel2& tcm, ToxContactModel2& tcm,
ToxEventProviderI& tep ToxEventProviderI& tep,
NGCEXTEventProviderI& neep
); );
void iterate(float delta); void iterate(float delta);
@ -144,5 +146,7 @@ class SHA1_NGCFT1 : public ToxEventI, public RegistryMessageModelEventI, public
bool sendFilePath(const Contact3 c, std::string_view file_name, std::string_view file_path) override; bool sendFilePath(const Contact3 c, std::string_view file_name, std::string_view file_path) override;
bool onToxEvent(const Tox_Event_Group_Peer_Exit* e) override; bool onToxEvent(const Tox_Event_Group_Peer_Exit* e) override;
bool onEvent(const Events::NGCEXT_pc1_announce&) override;
}; };