Skip to content

Commit f075370

Browse files
authored
on new connection event subscription (#93)
* on new connection event subscription Signed-off-by: iceseer <[email protected]> * const callback Signed-off-by: iceseer <[email protected]>
1 parent 517ca6e commit f075370

File tree

4 files changed

+31
-0
lines changed

4 files changed

+31
-0
lines changed

include/libp2p/host/basic_host/basic_host.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ namespace libp2p::host {
7575

7676
event::Bus &getBus() override;
7777

78+
event::Handle setOnNewConnectionHandler(const NewConnectionHandler &h) const override;
79+
7880
private:
7981
std::shared_ptr<peer::IdentityManager> idmgr_;
8082
std::unique_ptr<network::Network> network_;

include/libp2p/host/host.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,19 @@ namespace libp2p {
3939
using StreamResultHandler = std::function<void(
4040
outcome::result<std::shared_ptr<connection::Stream>>)>;
4141

42+
using NewConnectionHandler = std::function<void(peer::PeerInfo &&)>;
43+
4244
/**
4345
* @brief Get a version of Libp2p, supported by this Host
4446
*/
4547
virtual std::string_view getLibp2pVersion() const = 0;
4648

49+
/**
50+
* @brief Stores OnNewConnectionHandler.
51+
* @param h handler function to store
52+
*/
53+
virtual event::Handle setOnNewConnectionHandler(const NewConnectionHandler &h) const = 0;
54+
4755
/**
4856
* @brief Get a version of this Libp2p client
4957
*/

src/host/basic_host/basic_host.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,26 @@ namespace libp2p::host {
109109
network_->getListener().start();
110110
}
111111

112+
event::Handle BasicHost::setOnNewConnectionHandler(const NewConnectionHandler &h) const {
113+
return bus_->getChannel<network::event::OnNewConnectionChannel>().subscribe(
114+
[h{std::move(h)}](auto &&conn) {
115+
if (auto connection = conn.lock()) {
116+
auto remote_peer_res = connection->remotePeer();
117+
if (!remote_peer_res)
118+
return;
119+
120+
auto remote_peer_addr_res = connection->remoteMultiaddr();
121+
if (!remote_peer_addr_res)
122+
return;
123+
124+
if (h != nullptr)
125+
h(peer::PeerInfo{std::move(remote_peer_res.value()),
126+
std::vector<multi::Multiaddress>{
127+
std::move(remote_peer_addr_res.value())}});
128+
}
129+
});
130+
}
131+
112132
void BasicHost::stop() {
113133
network_->getListener().stop();
114134
}

test/mock/libp2p/host/host_mock.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ namespace libp2p {
1616
public:
1717
~HostMock() override = default;
1818

19+
MOCK_CONST_METHOD1(setOnNewConnectionHandler, event::Handle(const NewConnectionHandler &));
1920
MOCK_CONST_METHOD0(getLibp2pVersion, std::string_view());
2021
MOCK_CONST_METHOD0(getLibp2pClientVersion, std::string_view());
2122
MOCK_CONST_METHOD0(getId, peer::PeerId());

0 commit comments

Comments
 (0)