Skip to content

Commit

Permalink
peer discovery: re-publish and re-discover immediately when a node di…
Browse files Browse the repository at this point in the history
…sconnects, instead of on the connectivityChanged signal.
  • Loading branch information
AmnaSnene committed Jun 25, 2024
1 parent 313b69d commit 6409285
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
5 changes: 3 additions & 2 deletions include/opendht/dhtrunner.h
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ class OPENDHT_PUBLIC DhtRunner {
void run(const Config& config, Context&& context);

void setOnStatusChanged(StatusCallback&& cb) {
statusCb = std::move(cb);
statusCbs.emplace_back(std::move(cb));
}

/**
Expand Down Expand Up @@ -519,7 +519,8 @@ class OPENDHT_PUBLIC DhtRunner {

NodeStatus status4 {NodeStatus::Disconnected},
status6 {NodeStatus::Disconnected};
StatusCallback statusCb {nullptr};

std::list<StatusCallback> statusCbs {};

/** PeerDiscovery Parameters */
std::shared_ptr<PeerDiscovery> peerDiscovery_;
Expand Down
25 changes: 16 additions & 9 deletions src/dhtrunner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,8 @@ DhtRunner::run(const Config& config, Context&& context)
throw;
}

if (context.statusChangedCallback) {
statusCb = std::move(context.statusChangedCallback);
}
statusCbs.clear();
statusCbs.emplace_back(std::move(context.statusChangedCallback));
if (context.certificateStore) {
dht_->setLocalCertificateStore(std::move(context.certificateStore));
}
Expand Down Expand Up @@ -264,6 +263,13 @@ DhtRunner::run(const Config& config, Context&& context)
}
}
}
if (config.peer_discovery && config.peer_publish) {
statusCbs.emplace_back([this](NodeStatus status4, NodeStatus status6) {
if (status4 == NodeStatus::Disconnected && status6 == NodeStatus::Disconnected) {
peerDiscovery_->connectivityChanged();
}
});
}
#endif
}
}
Expand Down Expand Up @@ -690,8 +696,13 @@ DhtRunner::loop_()
if (nstatus4 != status4 || nstatus6 != status6) {
status4 = nstatus4;
status6 = nstatus6;
if (statusCb)
statusCb(status4, status6);
if (!statusCbs.empty())
{
for (auto& cb : statusCbs){
if (cb)
cb(status4, status6);
}
}
}

return wakeup;
Expand Down Expand Up @@ -1039,10 +1050,6 @@ DhtRunner::connectivityChanged()
std::lock_guard<std::mutex> lck(storage_mtx);
pending_ops_prio.emplace([=](SecureDht& dht) {
dht.connectivityChanged();
#ifdef OPENDHT_PEER_DISCOVERY
if (peerDiscovery_)
peerDiscovery_->connectivityChanged();
#endif
});
cv.notify_all();
}
Expand Down
2 changes: 2 additions & 0 deletions src/peer_discovery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,8 @@ PeerDiscovery::DomainPeerDiscovery::connectivityChanged()
reDiscover();
publish(sockAddrSend_);
});
if (logger_)
logger_->d("PeerDiscovery: connectivity changed");
}

PeerDiscovery::PeerDiscovery(in_port_t port, Sp<asio::io_context> ioContext, Sp<Logger> logger)
Expand Down

0 comments on commit 6409285

Please sign in to comment.