Skip to content

Commit b648b76

Browse files
committed
feat(p2p): add wait() to Node and runtime
1 parent fc3ef1d commit b648b76

3 files changed

Lines changed: 22 additions & 2 deletions

File tree

include/vix/p2p/Node.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ namespace vix::p2p
5858
virtual void start() = 0;
5959
virtual void stop() = 0;
6060
virtual bool running() const = 0;
61+
virtual void wait() = 0;
6162

6263
virtual bool connect(const PeerEndpoint &ep) = 0;
6364
virtual void disconnect(const PeerId &peer_id) = 0;

include/vix/p2p/P2P.hpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,8 @@ namespace vix::p2p
3333

3434
virtual void start() = 0;
3535
virtual void stop() = 0;
36-
36+
virtual void wait() = 0;
3737
virtual bool connect(const PeerEndpoint &ep) = 0;
38-
3938
virtual NodeStats stats() const = 0;
4039
};
4140

@@ -74,6 +73,12 @@ namespace vix::p2p
7473
node_->stop();
7574
}
7675

76+
void wait() override
77+
{
78+
if (node_)
79+
node_->wait();
80+
}
81+
7782
// Default: manual connect (same spirit as CLI --connect)
7883
bool connect(const PeerEndpoint &ep) override
7984
{

src/Node.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#include <random>
2323
#include <deque>
2424
#include <span>
25+
#include <atomic>
26+
#include <future>
2527

2628
#include <vix/p2p/Node.hpp>
2729
#include <vix/p2p/Peer.hpp>
@@ -176,6 +178,18 @@ namespace vix::p2p
176178

177179
bool running() const override { return running_; }
178180

181+
void wait() override
182+
{
183+
if (!running_)
184+
start();
185+
186+
if (io_thread_.joinable() && std::this_thread::get_id() == io_thread_.get_id())
187+
return;
188+
189+
if (io_thread_.joinable())
190+
io_thread_.join();
191+
}
192+
179193
bool connect(const PeerEndpoint &ep) override
180194
{
181195
if (!running_)

0 commit comments

Comments
 (0)