Commit 324aae38 authored by Jeromy's avatar Jeromy
Browse files

swarm: add optimized method for checking connectedness to peer

parent 9a9a8de2
...@@ -286,6 +286,15 @@ func (s *Swarm) ConnectionsToPeer(p peer.ID) []*Conn { ...@@ -286,6 +286,15 @@ func (s *Swarm) ConnectionsToPeer(p peer.ID) []*Conn {
return wrapConns(ps.ConnsWithGroup(p, s.swarm.Conns())) return wrapConns(ps.ConnsWithGroup(p, s.swarm.Conns()))
} }
func (s *Swarm) HaveConnsToPeer(p peer.ID) bool {
for _, c := range s.swarm.Conns() {
if c.InGroup(p) {
return true
}
}
return false
}
// Connections returns a slice of all connections. // Connections returns a slice of all connections.
func (s *Swarm) Connections() []*Conn { func (s *Swarm) Connections() []*Conn {
return wrapConns(s.swarm.Conns()) return wrapConns(s.swarm.Conns())
......
...@@ -123,8 +123,7 @@ func (n *Network) InterfaceListenAddresses() ([]ma.Multiaddr, error) { ...@@ -123,8 +123,7 @@ func (n *Network) InterfaceListenAddresses() ([]ma.Multiaddr, error) {
// Connectedness returns a state signaling connection capabilities // Connectedness returns a state signaling connection capabilities
// For now only returns Connected || NotConnected. Expand into more later. // For now only returns Connected || NotConnected. Expand into more later.
func (n *Network) Connectedness(p peer.ID) inet.Connectedness { func (n *Network) Connectedness(p peer.ID) inet.Connectedness {
c := n.Swarm().ConnectionsToPeer(p) if n.Swarm().HaveConnsToPeer(p) {
if len(c) > 0 {
return inet.Connected return inet.Connected
} }
return inet.NotConnected return inet.NotConnected
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment