From bc32532401f268d92fc64a01cf3e03a3b1640320 Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Wed, 24 Mar 2021 16:39:12 -0400 Subject: [PATCH] close peer connection and get num of connected peers --- waku/v2/node/wakunode2.go | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/waku/v2/node/wakunode2.go b/waku/v2/node/wakunode2.go index 5a6abb83..6a6ba903 100644 --- a/waku/v2/node/wakunode2.go +++ b/waku/v2/node/wakunode2.go @@ -272,7 +272,7 @@ func (node *WakuNode) Subscribe(topic *Topic) (*Subscription, error) { wakuMessage := &protocol.WakuMessage{} if err := proto.Unmarshal(msg.Data, wakuMessage); err != nil { - log.Error("could not decode message", err) // TODO: use log lib + log.Error("could not decode message", err) return } @@ -373,3 +373,26 @@ func (w *WakuNode) DialPeer(address string) error { w.host.Connect(w.ctx, *info) return nil } + +func (w *WakuNode) ClosePeerByAddress(address string) error { + p, err := ma.NewMultiaddr(address) + if err != nil { + return err + } + + // Extract the peer ID from the multiaddr. + info, err := peer.AddrInfoFromP2pAddr(p) + if err != nil { + return err + } + + return w.ClosePeerById(info.ID) +} + +func (w *WakuNode) ClosePeerById(id peer.ID) error { + return w.host.Network().ClosePeer(id) +} + +func (w *WakuNode) PeerCount() int { + return len(w.host.Network().Peers()) +}