more minor refactoring

This commit is contained in:
Green Sky 2024-06-28 21:10:01 +02:00
parent b53e291c68
commit f53767c390
No known key found for this signature in database
2 changed files with 28 additions and 28 deletions

View File

@ -114,7 +114,7 @@ void SHA1_NGCFT1::updateMessages(ObjectHandle ce) {
}
}
bool SHA1_NGCFT1::addParticipation(Contact3 c, ObjectHandle o) {
bool SHA1_NGCFT1::addParticipation(Contact3Handle c, ObjectHandle o) {
bool was_new {false};
if (static_cast<bool>(o)) {
@ -122,25 +122,25 @@ bool SHA1_NGCFT1::addParticipation(Contact3 c, ObjectHandle o) {
was_new = inserted;
}
if (_cr.valid(c)) {
const auto [_, inserted] = _cr.get_or_emplace<ChunkPicker>(c).participating.emplace(o);
if (static_cast<bool>(c)) {
const auto [_, inserted] = c.get_or_emplace<ChunkPicker>().participating.emplace(o);
was_new = was_new || inserted;
// TODO: if not have_all
_cr.get_or_emplace<ChunkPicker>(c).participating_unfinished.emplace(o, ChunkPicker::ParticipationEntry{});
c.get_or_emplace<ChunkPicker>().participating_unfinished.emplace(o, ChunkPicker::ParticipationEntry{});
}
return was_new;
}
void SHA1_NGCFT1::removeParticipation(Contact3 c, ObjectHandle o) {
void SHA1_NGCFT1::removeParticipation(Contact3Handle c, ObjectHandle o) {
if (static_cast<bool>(o) && o.all_of<Components::SuspectedParticipants>()) {
o.get<Components::SuspectedParticipants>().participants.erase(c);
}
if (_cr.valid(c) && _cr.all_of<ChunkPicker>(c)) {
_cr.get<ChunkPicker>(c).participating.erase(o);
_cr.get<ChunkPicker>(c).participating_unfinished.erase(o);
if (static_cast<bool>(c) && c.all_of<ChunkPicker>()) {
c.get<ChunkPicker>().participating.erase(o);
c.get<ChunkPicker>().participating_unfinished.erase(o);
}
}
@ -1474,16 +1474,16 @@ bool SHA1_NGCFT1::onToxEvent(const Tox_Event_Group_Peer_Exit* e) {
// peer disconnected
// - remove from all participantions
auto ch = _tcm.getContactGroupPeer(group_number, peer_number);
if (!static_cast<bool>(ch)) {
auto c = _tcm.getContactGroupPeer(group_number, peer_number);
if (!static_cast<bool>(c)) {
return false;
}
for (const auto& [_, h] : _info_to_content) {
removeParticipation(ch, h);
for (const auto& [_, o] : _info_to_content) {
removeParticipation(c, o);
if (h.all_of<Components::RemoteHave>()) {
h.get<Components::RemoteHave>().others.erase(ch);
if (o.all_of<Components::RemoteHave>()) {
o.get<Components::RemoteHave>().others.erase(c);
}
}
@ -1515,21 +1515,21 @@ bool SHA1_NGCFT1::onEvent(const Events::NGCEXT_ft1_have& e) {
return false;
}
auto ce = itc_it->second;
auto o = itc_it->second;
if (!static_cast<bool>(ce)) {
if (!static_cast<bool>(o)) {
std::cerr << "SHA1_NGCFT1 error: tracking info has null object\n";
return false;
}
const size_t num_total_chunks = ce.get<Components::FT1InfoSHA1>().chunks.size();
const size_t num_total_chunks = o.get<Components::FT1InfoSHA1>().chunks.size();
const auto c = _tcm.getContactGroupPeer(e.group_number, e.peer_number);
// we might not know yet
addParticipation(c, ce);
addParticipation(c, o);
auto& remote_have = ce.get_or_emplace<Components::RemoteHave>().others;
auto& remote_have = o.get_or_emplace<Components::RemoteHave>().others;
if (!remote_have.contains(c)) {
// init
remote_have.emplace(c, Components::RemoteHave::Entry{false, num_total_chunks});
@ -1589,14 +1589,14 @@ bool SHA1_NGCFT1::onEvent(const Events::NGCEXT_ft1_bitset& e) {
return false;
}
auto ce = itc_it->second;
auto o = itc_it->second;
if (!static_cast<bool>(ce)) {
if (!static_cast<bool>(o)) {
std::cerr << "SHA1_NGCFT1 error: tracking info has null object\n";
return false;
}
const size_t num_total_chunks = ce.get<Components::FT1InfoSHA1>().chunks.size();
const size_t num_total_chunks = o.get<Components::FT1InfoSHA1>().chunks.size();
// +1 for byte rounding
if (num_total_chunks+1 < e.start_chunk + (e.chunk_bitset.size()*8)) {
std::cerr << "SHA1_NGCFT1 error: got bitset.size+start that is larger then number of chunks!!\n";
@ -1606,9 +1606,9 @@ bool SHA1_NGCFT1::onEvent(const Events::NGCEXT_ft1_bitset& e) {
const auto c = _tcm.getContactGroupPeer(e.group_number, e.peer_number);
// we might not know yet
addParticipation(c, ce);
addParticipation(c, o);
auto& remote_have = ce.get_or_emplace<Components::RemoteHave>().others;
auto& remote_have = o.get_or_emplace<Components::RemoteHave>().others;
if (!remote_have.contains(c)) {
// init
remote_have.emplace(c, Components::RemoteHave::Entry{false, num_total_chunks});
@ -1670,9 +1670,9 @@ bool SHA1_NGCFT1::onEvent(const Events::NGCEXT_pc1_announce& e) {
}
// add them to participants
auto ce = itc_it->second;
const auto c = _tcm.getContactGroupPeer(e.group_number, e.peer_number);
const bool was_new = addParticipation(c, ce);
auto o = itc_it->second;
const bool was_new = addParticipation(c, o);
if (was_new) {
std::cout << "SHA1_NGCFT1: and we where interested!\n";
// we should probably send the bitset back here / add to queue (can be multiple packets)

View File

@ -108,8 +108,8 @@ class SHA1_NGCFT1 : public ToxEventI, public RegistryMessageModelEventI, public
void updateMessages(ObjectHandle ce);
bool addParticipation(Contact3 c, ObjectHandle o);
void removeParticipation(Contact3 c, ObjectHandle o);
static bool addParticipation(Contact3Handle c, ObjectHandle o);
static void removeParticipation(Contact3Handle c, ObjectHandle o);
std::optional<std::pair<uint32_t, uint32_t>> selectPeerForRequest(ObjectHandle ce);