From 0f9f623b3112a22b32c8aa52f0d596ae1ffa2b48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20B=C3=A9raud?= Date: Fri, 17 Jan 2020 12:47:14 -0500 Subject: [PATCH] dht: use unique_ptr for SearchNode --- src/dht.cpp | 36 +++++++++++----------- src/search.h | 84 ++++++++++++++++++++++++++-------------------------- 2 files changed, 60 insertions(+), 60 deletions(-) diff --git a/src/dht.cpp b/src/dht.cpp index dc9ed1d4..aaf56634 100644 --- a/src/dht.cpp +++ b/src/dht.cpp @@ -346,8 +346,8 @@ Dht::searchSendGetValues(Sp sr, SearchNode* pn, bool update) n = pn; } else { for (auto& sn : sr->nodes) { - if (sn.canGet(now, up, query)) { - n = &sn; + if (sn->canGet(now, up, query)) { + n = sn.get(); break; } } @@ -488,7 +488,8 @@ void Dht::searchSendAnnounceValue(const Sp& sr) { Sp probe_query {}; const auto& now = scheduler.time(); - for (auto& n : sr->nodes) { + for (auto& np : sr->nodes) { + auto& n = *np; if (not n.isSynced(now)) continue; @@ -650,8 +651,8 @@ Dht::searchStep(Sp sr) // clear corresponding queries for (const auto& get : completed_gets) for (auto& sn : sr->nodes) { - sn.getStatus.erase(get.query); - sn.pagination_queries.erase(get.query); + sn->getStatus.erase(get.query); + sn->pagination_queries.erase(get.query); } /* clearing callbacks for announced values */ @@ -670,10 +671,10 @@ Dht::searchStep(Sp sr) if (not sr->listeners.empty()) { unsigned i = 0; for (auto& n : sr->nodes) { - if (not n.isSynced(now)) + if (not n->isSynced(now)) continue; - searchSynchedNodeListen(sr, n); - if (not n.candidate and ++i == LISTEN_NODES) + searchSynchedNodeListen(sr, *n); + if (not n->candidate and ++i == LISTEN_NODES) break; } } @@ -808,8 +809,8 @@ Dht::announce(const InfoHash& id, if (a_sr == sr->announce.end()) { sr->announce.emplace_back(Announce {permanent, value, created, callback}); for (auto& n : sr->nodes) { - n.probe_query.reset(); - n.acked[value->id].first.reset(); + n->probe_query.reset(); + n->acked[value->id].first.reset(); } } else { a_sr->permanent = permanent; @@ -817,8 +818,8 @@ Dht::announce(const InfoHash& id, if (a_sr->value != value) { a_sr->value = value; for (auto& n : sr->nodes) { - n.acked[value->id].first.reset(); - n.probe_query.reset(); + n->acked[value->id].first.reset(); + n->probe_query.reset(); } } if (sr->isAnnounced(value->id)) { @@ -1537,10 +1538,9 @@ Dht::dumpSearch(const Search& sr, std::ostream& out) const } out << " Common bits InfoHash Conn. Get Ops IP" << std::endl; - unsigned i = 0; auto last_get = sr.getLastGetTime(); - for (const auto& n : sr.nodes) { - i++; + for (const auto& np : sr.nodes) { + auto& n = *np; out << std::setfill (' ') << std::setw(3) << InfoHash::commonBits(sr.id, n.node->id) << ' ' << n.node->id; out << ' ' << (findNode(n.node->id, sr.af) ? '*' : ' '); out << " ["; @@ -2162,9 +2162,9 @@ Dht::onError(Sp req, net::DhtProtocolException e) { for (auto& srp : searches(node->getFamily())) { auto& sr = srp.second; for (auto& n : sr->nodes) { - if (n.node != node) continue; - n.token.clear(); - n.last_get_reply = time_point::min(); + if (n->node != node) continue; + n->token.clear(); + n->last_get_reply = time_point::min(); searchSendGetValues(sr); scheduler.edit(sr->nextSearchStep, scheduler.time()); break; diff --git a/src/search.h b/src/search.h index c1945a2f..88ce4ee1 100644 --- a/src/search.h +++ b/src/search.h @@ -86,15 +86,15 @@ struct Dht::SearchNode { Blob token {}; /* last token the node sent to us after a get request */ time_point last_get_reply {time_point::min()}; /* last time received valid token */ + Sp syncJob {}; bool candidate {false}; /* A search node is candidate if the search is/was synced and this node is a new candidate for inclusion. */ - Sp syncJob {}; SearchNode() : node() {} SearchNode(const SearchNode&) = delete; - SearchNode(SearchNode&&) = default; + SearchNode(SearchNode&&) = delete; SearchNode& operator=(const SearchNode&) = delete; - SearchNode& operator=(SearchNode&&) = default; + SearchNode& operator=(SearchNode&&) = delete; SearchNode(const Sp& node) : node(node) {} ~SearchNode() { @@ -376,7 +376,7 @@ struct Dht::Search { bool expired {false}; /* no node, or all nodes expired */ bool done {false}; /* search is over, cached for later */ - std::vector nodes {}; + std::vector> nodes {}; /* pending puts */ std::vector announce {}; @@ -416,17 +416,17 @@ struct Dht::Search { bool insertNode(const Sp& n, time_point now, const Blob& token={}); SearchNode* getNode(const Sp& n) { - auto srn = std::find_if(nodes.begin(), nodes.end(), [&](SearchNode& sn) { - return n == sn.node; + auto srn = std::find_if(nodes.begin(), nodes.end(), [&](std::unique_ptr& sn) { + return n == sn->node; }); - return (srn == nodes.end()) ? nullptr : &(*srn); + return (srn == nodes.end()) ? nullptr : (*srn).get(); } /* number of concurrent sync requests */ unsigned currentlySolicitedNodeCount() const { unsigned count = 0; for (const auto& n : nodes) - if (not n.isBad() and n.pendingGet()) + if (not n->isBad() and n->pendingGet()) count++; return count; } @@ -460,12 +460,12 @@ struct Dht::Search { */ void setDone(const Get& get) { for (auto& n : nodes) { - auto pqs = n.pagination_queries.find(get.query); - if (pqs != n.pagination_queries.cend()) { + auto pqs = n->pagination_queries.find(get.query); + if (pqs != n->pagination_queries.cend()) { for (auto& pq : pqs->second) - n.getStatus.erase(pq); + n->getStatus.erase(pq); } - n.getStatus.erase(get.query); + n->getStatus.erase(get.query); } if (get.done_cb) get.done_cb(true, getNodes()); @@ -478,9 +478,9 @@ struct Dht::Search { */ void setDone() { for (auto& n : nodes) { - n.getStatus.clear(); - n.listenStatus.clear(); - n.acked.clear(); + n->getStatus.clear(); + n->listenStatus.clear(); + n->acked.clear(); } done = true; } @@ -522,9 +522,9 @@ struct Dht::Search { } for (auto& sn : nodes) { if (listeners.empty()) - sn.cancelListen(); + sn->cancelListen(); else if (query) - sn.cancelListen(query); + sn->cancelListen(query); } }); scheduler.edit(opExpirationJob, nextExpire); @@ -565,14 +565,14 @@ struct Dht::Search { * @return The number of non-good search nodes. */ unsigned getNumberOfBadNodes() const { - return std::count_if(nodes.begin(), nodes.end(), [](const SearchNode& sn) { - return sn.isBad(); + return std::count_if(nodes.begin(), nodes.end(), [](const std::unique_ptr& sn) { + return sn->isBad(); }); } unsigned getNumberOfConsecutiveBadNodes() const { unsigned count = 0; - std::find_if(nodes.begin(), nodes.end(), [&count](const SearchNode& sn) { - if (not sn.isBad()) + std::find_if(nodes.begin(), nodes.end(), [&count](const std::unique_ptr& sn) { + if (not sn->isBad()) return true; ++count; return false; @@ -591,7 +591,7 @@ struct Dht::Search { */ bool removeExpiredNode(const time_point& now) { for (auto e = nodes.cend(); e != nodes.cbegin();) { - const Node& n = *(--e)->node; + const Node& n = *(*(--e))->node; if (n.isRemovable(now)) { //std::cout << "Removing expired node " << n.id << " from IPv" << (af==AF_INET?'4':'6') << " search " << id << std::endl; nodes.erase(e); @@ -664,7 +664,7 @@ struct Dht::Search { // remove acked for cleared annouces for (auto it = announced; it != announce.end(); ++it) { for (auto& n : nodes) - n.acked.erase(it->value->id); + n->acked.erase(it->value->id); } announce.erase(announced, announce.end()); } @@ -697,13 +697,13 @@ Dht::Search::insertNode(const Sp& snode, time_point now, const Blob& token auto n = nodes.end(); while (n != nodes.begin()) { --n; - if (n->node == snode) { + if ((*n)->node == snode) { found = true; break; } /* Node not found. We could insert it after this one. */ - if (id.xorCmp(nid, n->node->id) > 0) { + if (id.xorCmp(nid, (*n)->node->id) > 0) { ++n; break; } @@ -727,7 +727,7 @@ Dht::Search::insertNode(const Sp& snode, time_point now, const Blob& token full = nodes.size() - bad >= SEARCH_NODES; while (std::distance(nodes.cbegin(), t) - bad > SEARCH_NODES) { --t; - if (t->isBad()) + if ((*t)->isBad()) bad--; } } @@ -743,7 +743,7 @@ Dht::Search::insertNode(const Sp& snode, time_point now, const Blob& token if (nodes.empty()) { step_time = time_point::min(); } - n = nodes.insert(n, SearchNode(snode)); + n = nodes.emplace(n, std::make_unique(snode)); node.setTime(now); new_search_node = true; if (node.isExpired()) { @@ -755,16 +755,16 @@ Dht::Search::insertNode(const Sp& snode, time_point now, const Blob& token } while (nodes.size() - bad > SEARCH_NODES) { - if (not expired and nodes.back().isBad()) + if (not expired and nodes.back()->isBad()) bad--; nodes.pop_back(); } } if (not token.empty()) { - n->candidate = false; - n->last_get_reply = now; + (*n)->candidate = false; + (*n)->last_get_reply = now; if (token.size() <= 64) - n->token = token; + (*n)->token = token; expired = false; } if (new_search_node) @@ -778,7 +778,7 @@ Dht::Search::getNodes() const std::vector> ret {}; ret.reserve(nodes.size()); for (const auto& sn : nodes) - ret.emplace_back(sn.node); + ret.emplace_back(sn->node); return ret; } @@ -787,9 +787,9 @@ Dht::Search::isSynced(time_point now) const { unsigned i = 0; for (const auto& n : nodes) { - if (n.isBad()) + if (n->isBad()) continue; - if (not n.isSynced(now)) + if (not n->isSynced(now)) return false; if (++i == TARGET_NODES) break; @@ -820,9 +820,9 @@ Dht::Search::isDone(const Get& get) const { unsigned i = 0; for (const auto& sn : nodes) { - if (sn.isBad()) + if (sn->isBad()) continue; - if (not sn.isDone(get)) + if (not sn->isDone(get)) return false; if (++i == TARGET_NODES) break; @@ -837,9 +837,9 @@ Dht::Search::isAnnounced(Value::Id id) const return false; unsigned i = 0; for (const auto& n : nodes) { - if (n.isBad()) + if (n->isBad()) continue; - if (not n.isAnnounced(id)) + if (not n->isAnnounced(id)) return false; if (++i == TARGET_NODES) return true; @@ -854,14 +854,14 @@ Dht::Search::isListening(time_point now) const return false; unsigned i = 0; for (const auto& n : nodes) { - if (n.isBad()) + if (n->isBad()) continue; SearchNode::NodeListenerStatus::const_iterator ls {}; - for (ls = n.listenStatus.begin(); ls != n.listenStatus.end() ; ++ls) { - if (n.isListening(now, ls)) + for (ls = n->listenStatus.begin(); ls != n->listenStatus.end() ; ++ls) { + if (n->isListening(now, ls)) break; } - if (ls == n.listenStatus.end()) + if (ls == n->listenStatus.end()) return false; if (++i == LISTEN_NODES) break;