Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions include/libp2p/host/basic_host.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ namespace libp2p::host {
*/
StreamProtocols getSupportedProtocols() const;

size_t getConnectedPeerCount() const;

private:
std::shared_ptr<peer::IdentityManager> idmgr_;
std::shared_ptr<network::ListenerManager> listener_;
Expand Down
6 changes: 6 additions & 0 deletions include/libp2p/network/connection_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ namespace libp2p::event::network {
channel_decl<struct OnNewConnection,
std::weak_ptr<connection::CapableConnection>>;

using OnConnectionClosedChannel =
channel_decl<struct OnConnectionClosed,
std::shared_ptr<connection::CapableConnection>>;

/// fired when all connections to peer closed
using OnPeerDisconnectedChannel =
channel_decl<struct PeerDisconnected, const libp2p::peer::PeerId &>;
Expand Down Expand Up @@ -54,6 +58,8 @@ namespace libp2p::network {
const peer::PeerId &peer_id,
const std::shared_ptr<connection::CapableConnection> &conn);

size_t getConnectedPeerCount() const;

private:
std::unordered_map<peer::PeerId, std::unordered_set<ConnectionSPtr>>
connections_;
Expand Down
4 changes: 4 additions & 0 deletions src/host/basic_host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,4 +238,8 @@ namespace libp2p::host {
StreamProtocols BasicHost::getSupportedProtocols() const {
return listener_->getSupportedProtocols();
}

size_t BasicHost::getConnectedPeerCount() const {
return connection_manager_->getConnectedPeerCount();
}
} // namespace libp2p::host
15 changes: 11 additions & 4 deletions src/network/connection_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ namespace libp2p::network {
return out;
}

ConnectionManager::ConnectionSPtr
ConnectionManager::getBestConnectionForPeer(const peer::PeerId &p) const {
ConnectionManager::ConnectionSPtr ConnectionManager::getBestConnectionForPeer(
const peer::PeerId &p) const {
auto it = connections_.find(p);
if (it != connections_.end()) {
for (const auto &conn : it->second) {
Expand Down Expand Up @@ -79,8 +79,7 @@ namespace libp2p::network {
return out;
}

ConnectionManager::ConnectionManager(
std::shared_ptr<libp2p::event::Bus> bus)
ConnectionManager::ConnectionManager(std::shared_ptr<libp2p::event::Bus> bus)
: bus_(std::move(bus)) {}

void ConnectionManager::collectGarbage() {
Expand Down Expand Up @@ -125,6 +124,8 @@ namespace libp2p::network {
// ignore errors
(void)conn->close();
}
bus_->getChannel<event::network::OnConnectionClosedChannel>().publish(
conn);
}

closing_connections_to_peer_.reset();
Expand Down Expand Up @@ -153,6 +154,9 @@ namespace libp2p::network {
[[maybe_unused]] auto erased = it->second.erase(conn);
if (erased == 0) {
log()->error("inconsistency in onConnectionClosed, connection not found");
} else {
bus_->getChannel<event::network::OnConnectionClosedChannel>().publish(
conn);
}

if (it->second.empty()) {
Expand All @@ -162,4 +166,7 @@ namespace libp2p::network {
}
}

size_t ConnectionManager::getConnectedPeerCount() const {
return connections_.size();
}
} // namespace libp2p::network
Loading