Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

re-publish and re-discover immediately when the node disconnects #713

Merged
merged 3 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

emplace if not empty

}

/**
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 {};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could be a vector


/** 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));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

only emplace if statusChangedCallback is not {}.

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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if only peer_discovery or peer_publish is true ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be config.peer_discovery or config.peer_publish so i changed it to if (peerDiscovery_).

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())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if not needed

{
for (auto& cb : statusCbs){
if (cb)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check not needed

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
8 changes: 6 additions & 2 deletions src/peer_discovery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,8 +317,12 @@ PeerDiscovery::DomainPeerDiscovery::reDiscover()
void
PeerDiscovery::DomainPeerDiscovery::connectivityChanged()
{
reDiscover();
publish(sockAddrSend_);
ioContext_->post([this] () {
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
Loading