提交 94f04c7f 作者: Juan Batiz-Benet

net: add Connectedness var.

上级 26e76561
...@@ -64,7 +64,7 @@ func bootstrap(ctx context.Context, ...@@ -64,7 +64,7 @@ func bootstrap(ctx context.Context,
var notConnected []peer.Peer var notConnected []peer.Peer
for _, p := range bootstrapPeers { for _, p := range bootstrapPeers {
if !n.IsConnected(p) { if n.Connectedness(p) != inet.Connected {
notConnected = append(notConnected, p) notConnected = append(notConnected, p)
} }
} }
......
...@@ -26,8 +26,8 @@ type Network interface { ...@@ -26,8 +26,8 @@ type Network interface {
// ClosePeer connection to peer // ClosePeer connection to peer
ClosePeer(peer.Peer) error ClosePeer(peer.Peer) error
// IsConnected returns whether a connection to given peer exists. // Connectedness returns a state signaling connection capabilities
IsConnected(peer.Peer) bool Connectedness(peer.Peer) Connectedness
// GetProtocols returns the protocols registered in the network. // GetProtocols returns the protocols registered in the network.
GetProtocols() *mux.ProtocolMap GetProtocols() *mux.ProtocolMap
...@@ -74,4 +74,26 @@ type Dialer interface { ...@@ -74,4 +74,26 @@ type Dialer interface {
// DialPeer attempts to establish a connection to a given peer // DialPeer attempts to establish a connection to a given peer
DialPeer(context.Context, peer.Peer) error DialPeer(context.Context, peer.Peer) error
// Connectedness returns a state signaling connection capabilities
Connectedness(peer.Peer) Connectedness
} }
// Connectedness signals the capacity for a connection with a given node.
// It is used to signal to services and other peers whether a node is reachable.
type Connectedness int
const (
// NotConnected means no connection to peer, and no extra information (default)
NotConnected Connectedness = 0
// Connected means has an open, live connection to peer
Connected
// CanConnect means recently connected to peer, terminated gracefully
CanConnect
// CannotConnect means recently attempted connecting but failed to connect.
// (should signal "made effort, failed")
CannotConnect
)
// package net provides an interface for ipfs to interact with the network through // Package net provides an interface for ipfs to interact with the network through
package net package net
import ( import (
...@@ -126,3 +126,12 @@ func (n *IpfsNetwork) ListenAddresses() []ma.Multiaddr { ...@@ -126,3 +126,12 @@ func (n *IpfsNetwork) ListenAddresses() []ma.Multiaddr {
func (n *IpfsNetwork) InterfaceListenAddresses() ([]ma.Multiaddr, error) { func (n *IpfsNetwork) InterfaceListenAddresses() ([]ma.Multiaddr, error) {
return n.swarm.InterfaceListenAddresses() return n.swarm.InterfaceListenAddresses()
} }
// Connectedness returns a state signaling connection capabilities
// For now only returns Connecter || NotConnected. Expand into more later.
func (n *IpfsNetwork) Connectedness(p peer.Peer) Connectedness {
if n.swarm.GetConnection(p.ID()) != nil {
return Connected
}
return NotConnected
}
...@@ -8,6 +8,7 @@ import ( ...@@ -8,6 +8,7 @@ import (
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context" context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/goprotobuf/proto" "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/goprotobuf/proto"
ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore" ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
inet "github.com/jbenet/go-ipfs/net"
msg "github.com/jbenet/go-ipfs/net/message" msg "github.com/jbenet/go-ipfs/net/message"
mux "github.com/jbenet/go-ipfs/net/mux" mux "github.com/jbenet/go-ipfs/net/mux"
peer "github.com/jbenet/go-ipfs/peer" peer "github.com/jbenet/go-ipfs/peer"
...@@ -79,6 +80,7 @@ func (f *fauxSender) SendMessage(ctx context.Context, m msg.NetMessage) error { ...@@ -79,6 +80,7 @@ func (f *fauxSender) SendMessage(ctx context.Context, m msg.NetMessage) error {
// fauxNet is a standin for a swarm.Network in order to more easily recreate // fauxNet is a standin for a swarm.Network in order to more easily recreate
// different testing scenarios // different testing scenarios
type fauxNet struct { type fauxNet struct {
local peer.Peer
} }
// DialPeer attempts to establish a connection to a given peer // DialPeer attempts to establish a connection to a given peer
...@@ -86,6 +88,10 @@ func (f *fauxNet) DialPeer(context.Context, peer.Peer) error { ...@@ -86,6 +88,10 @@ func (f *fauxNet) DialPeer(context.Context, peer.Peer) error {
return nil return nil
} }
func (f *fauxNet) LocalPeer() peer.Peer {
return f.local
}
// ClosePeer connection to peer // ClosePeer connection to peer
func (f *fauxNet) ClosePeer(peer.Peer) error { func (f *fauxNet) ClosePeer(peer.Peer) error {
return nil return nil
...@@ -96,6 +102,11 @@ func (f *fauxNet) IsConnected(peer.Peer) (bool, error) { ...@@ -96,6 +102,11 @@ func (f *fauxNet) IsConnected(peer.Peer) (bool, error) {
return true, nil return true, nil
} }
// Connectedness returns whether a connection to given peer exists.
func (f *fauxNet) Connectedness(peer.Peer) inet.Connectedness {
return inet.Connected
}
// GetProtocols returns the protocols registered in the network. // GetProtocols returns the protocols registered in the network.
func (f *fauxNet) GetProtocols() *mux.ProtocolMap { return nil } func (f *fauxNet) GetProtocols() *mux.ProtocolMap { return nil }
...@@ -120,13 +131,13 @@ func TestGetFailures(t *testing.T) { ...@@ -120,13 +131,13 @@ func TestGetFailures(t *testing.T) {
t.SkipNow() t.SkipNow()
} }
ctx := context.Background()
fn := &fauxNet{}
fs := &fauxSender{}
peerstore := peer.NewPeerstore() peerstore := peer.NewPeerstore()
local := makePeerString(t, "") local := makePeerString(t, "")
ctx := context.Background()
fn := &fauxNet{local}
fs := &fauxSender{}
d := NewDHT(ctx, local, peerstore, fn, fs, ds.NewMapDatastore()) d := NewDHT(ctx, local, peerstore, fn, fs, ds.NewMapDatastore())
other := makePeerString(t, "") other := makePeerString(t, "")
d.Update(ctx, other) d.Update(ctx, other)
...@@ -219,14 +230,14 @@ func TestNotFound(t *testing.T) { ...@@ -219,14 +230,14 @@ func TestNotFound(t *testing.T) {
t.SkipNow() t.SkipNow()
} }
ctx := context.Background()
fn := &fauxNet{}
fs := &fauxSender{}
local := makePeerString(t, "") local := makePeerString(t, "")
peerstore := peer.NewPeerstore() peerstore := peer.NewPeerstore()
peerstore.Add(local) peerstore.Add(local)
ctx := context.Background()
fn := &fauxNet{local}
fs := &fauxSender{}
d := NewDHT(ctx, local, peerstore, fn, fs, ds.NewMapDatastore()) d := NewDHT(ctx, local, peerstore, fn, fs, ds.NewMapDatastore())
var ps []peer.Peer var ps []peer.Peer
...@@ -285,14 +296,15 @@ func TestNotFound(t *testing.T) { ...@@ -285,14 +296,15 @@ func TestNotFound(t *testing.T) {
func TestLessThanKResponses(t *testing.T) { func TestLessThanKResponses(t *testing.T) {
// t.Skip("skipping test because it makes a lot of output") // t.Skip("skipping test because it makes a lot of output")
ctx := context.Background()
u.Debug = false
fn := &fauxNet{}
fs := &fauxSender{}
local := makePeerString(t, "") local := makePeerString(t, "")
peerstore := peer.NewPeerstore() peerstore := peer.NewPeerstore()
peerstore.Add(local) peerstore.Add(local)
ctx := context.Background()
u.Debug = false
fn := &fauxNet{local}
fs := &fauxSender{}
d := NewDHT(ctx, local, peerstore, fn, fs, ds.NewMapDatastore()) d := NewDHT(ctx, local, peerstore, fn, fs, ds.NewMapDatastore())
var ps []peer.Peer var ps []peer.Peer
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论