@cryptotaxi247 / kubo / commits / 94f04c7fc

net: add Connectedness var.

Juan Batiz-Benet committed Nov 20, 2014 at 18:45 UTC 94f04c7fcc6c2d69c8e25d5cfb1f25b2679c693b
4 files changed +59 -16
core/bootstrap.go
+1 -1
@@ -64,7 +64,7 @@ func bootstrap(ctx context.Context,
64
65 var notConnected []peer.Peer
66 for _, p := range bootstrapPeers {
67 - if !n.IsConnected(p) {
67 + if n.Connectedness(p) != inet.Connected {
68 notConnected = append(notConnected, p)
69 }
70 }
net/interface.go
+24 -2
@@ -26,8 +26,8 @@ type Network interface {
26 // ClosePeer connection to peer
27 ClosePeer(peer.Peer) error
28
29 - // IsConnected returns whether a connection to given peer exists.
30 - IsConnected(peer.Peer) bool
29 + // Connectedness returns a state signaling connection capabilities
30 + Connectedness(peer.Peer) Connectedness
31
32 // GetProtocols returns the protocols registered in the network.
33 GetProtocols() *mux.ProtocolMap
@@ -74,4 +74,26 @@ type Dialer interface {
74
75 // DialPeer attempts to establish a connection to a given peer
76 DialPeer(context.Context, peer.Peer) error
77 +
78 + // Connectedness returns a state signaling connection capabilities
79 + Connectedness(peer.Peer) Connectedness
80 }
81 +
82 +// Connectedness signals the capacity for a connection with a given node.
83 +// It is used to signal to services and other peers whether a node is reachable.
84 +type Connectedness int
85 +
86 +const (
87 + // NotConnected means no connection to peer, and no extra information (default)
88 + NotConnected Connectedness = 0
89 +
90 + // Connected means has an open, live connection to peer
91 + Connected
92 +
93 + // CanConnect means recently connected to peer, terminated gracefully
94 + CanConnect
95 +
96 + // CannotConnect means recently attempted connecting but failed to connect.
97 + // (should signal "made effort, failed")
98 + CannotConnect
99 +)
net/net.go
+10 -1
@@ -1,4 +1,4 @@
1 -// package net provides an interface for ipfs to interact with the network through
1 +// Package net provides an interface for ipfs to interact with the network through
2 package net
3
4 import (
@@ -126,3 +126,12 @@ func (n *IpfsNetwork) ListenAddresses() []ma.Multiaddr {
126 func (n *IpfsNetwork) InterfaceListenAddresses() ([]ma.Multiaddr, error) {
127 return n.swarm.InterfaceListenAddresses()
128 }
129 +
130 +// Connectedness returns a state signaling connection capabilities
131 +// For now only returns Connecter || NotConnected. Expand into more later.
132 +func (n *IpfsNetwork) Connectedness(p peer.Peer) Connectedness {
133 + if n.swarm.GetConnection(p.ID()) != nil {
134 + return Connected
135 + }
136 + return NotConnected
137 +}
routing/dht/ext_test.go
+24 -12
@@ -8,6 +8,7 @@ import (
8 context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
9 "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/goprotobuf/proto"
10 ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
11 + inet "github.com/jbenet/go-ipfs/net"
12 msg "github.com/jbenet/go-ipfs/net/message"
13 mux "github.com/jbenet/go-ipfs/net/mux"
14 peer "github.com/jbenet/go-ipfs/peer"
@@ -79,6 +80,7 @@ func (f *fauxSender) SendMessage(ctx context.Context, m msg.NetMessage) error {
80 // fauxNet is a standin for a swarm.Network in order to more easily recreate
81 // different testing scenarios
82 type fauxNet struct {
83 + local peer.Peer
84 }
85
86 // DialPeer attempts to establish a connection to a given peer
@@ -86,6 +88,10 @@ func (f *fauxNet) DialPeer(context.Context, peer.Peer) error {
88 return nil
89 }
90
91 +func (f *fauxNet) LocalPeer() peer.Peer {
92 + return f.local
93 +}
94 +
95 // ClosePeer connection to peer
96 func (f *fauxNet) ClosePeer(peer.Peer) error {
97 return nil
@@ -96,6 +102,11 @@ func (f *fauxNet) IsConnected(peer.Peer) (bool, error) {
102 return true, nil
103 }
104
105 +// Connectedness returns whether a connection to given peer exists.
106 +func (f *fauxNet) Connectedness(peer.Peer) inet.Connectedness {
107 + return inet.Connected
108 +}
109 +
110 // GetProtocols returns the protocols registered in the network.
111 func (f *fauxNet) GetProtocols() *mux.ProtocolMap { return nil }
112
@@ -120,13 +131,13 @@ func TestGetFailures(t *testing.T) {
131 t.SkipNow()
132 }
133
123 - ctx := context.Background()
124 - fn := &fauxNet{}
125 - fs := &fauxSender{}
126 -
134 peerstore := peer.NewPeerstore()
135 local := makePeerString(t, "")
136
137 + ctx := context.Background()
138 + fn := &fauxNet{local}
139 + fs := &fauxSender{}
140 +
141 d := NewDHT(ctx, local, peerstore, fn, fs, ds.NewMapDatastore())
142 other := makePeerString(t, "")
143 d.Update(ctx, other)
@@ -219,14 +230,14 @@ func TestNotFound(t *testing.T) {
230 t.SkipNow()
231 }
232
222 - ctx := context.Background()
223 - fn := &fauxNet{}
224 - fs := &fauxSender{}
225 -
233 local := makePeerString(t, "")
234 peerstore := peer.NewPeerstore()
235 peerstore.Add(local)
236
237 + ctx := context.Background()
238 + fn := &fauxNet{local}
239 + fs := &fauxSender{}
240 +
241 d := NewDHT(ctx, local, peerstore, fn, fs, ds.NewMapDatastore())
242
243 var ps []peer.Peer
@@ -285,14 +296,15 @@ func TestNotFound(t *testing.T) {
296 func TestLessThanKResponses(t *testing.T) {
297 // t.Skip("skipping test because it makes a lot of output")
298
288 - ctx := context.Background()
289 - u.Debug = false
290 - fn := &fauxNet{}
291 - fs := &fauxSender{}
299 local := makePeerString(t, "")
300 peerstore := peer.NewPeerstore()
301 peerstore.Add(local)
302
303 + ctx := context.Background()
304 + u.Debug = false
305 + fn := &fauxNet{local}
306 + fs := &fauxSender{}
307 +
308 d := NewDHT(ctx, local, peerstore, fn, fs, ds.NewMapDatastore())
309
310 var ps []peer.Peer