interface.go 3.22 KB
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1 2 3
package net

import (
4
	"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
5
	conn "github.com/jbenet/go-ipfs/net/conn"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
6 7
	msg "github.com/jbenet/go-ipfs/net/message"
	mux "github.com/jbenet/go-ipfs/net/mux"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
8
	srv "github.com/jbenet/go-ipfs/net/service"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
9
	peer "github.com/jbenet/go-ipfs/peer"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
10
	ctxc "github.com/jbenet/go-ipfs/util/ctxcloser"
11 12

	ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
13 14 15 16
)

// Network is the interface IPFS uses for connecting to the world.
type Network interface {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
17
	ctxc.ContextCloser
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
18 19 20 21 22 23

	// Listen handles incoming connections on given Multiaddr.
	// Listen(*ma.Muliaddr) error
	// TODO: for now, only listen on addrs in local peer when initializing.

	// DialPeer attempts to establish a connection to a given peer
24
	DialPeer(context.Context, peer.Peer) error
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
25 26

	// ClosePeer connection to peer
27
	ClosePeer(peer.Peer) error
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
28

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
29 30
	// Connectedness returns a state signaling connection capabilities
	Connectedness(peer.Peer) Connectedness
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
31 32 33 34

	// GetProtocols returns the protocols registered in the network.
	GetProtocols() *mux.ProtocolMap

35
	// GetPeerList returns the list of peers currently connected in this network.
36
	GetPeerList() []peer.Peer
37

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
38 39 40
	// GetConnections returns the list of connections currently open in this network.
	GetConnections() []conn.Conn

41 42 43 44
	// GetBandwidthTotals returns the total number of bytes passed through
	// the network since it was instantiated
	GetBandwidthTotals() (uint64, uint64)

Jeromy's avatar
Jeromy committed
45 46 47 48
	// GetMessageCounts returns the total number of messages passed through
	// the network since it was instantiated
	GetMessageCounts() (uint64, uint64)

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
49
	// SendMessage sends given Message out
50
	SendMessage(msg.NetMessage) error
51 52 53 54 55 56 57 58

	// ListenAddresses returns a list of addresses at which this network listens.
	ListenAddresses() []ma.Multiaddr

	// InterfaceListenAddresses returns a list of addresses at which this network
	// listens. It expands "any interface" addresses (/ip4/0.0.0.0, /ip6/::) to
	// use the known local interfaces.
	InterfaceListenAddresses() ([]ma.Multiaddr, error)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
59
}
60 61

// Sender interface for network services.
62
type Sender srv.Sender
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
63 64 65

// Handler interface for network services.
type Handler srv.Handler
66 67 68

// Service interface for network resources.
type Service srv.Service
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
69

Jeromy's avatar
Jeromy committed
70
// Dialer represents a service that can dial out to peers
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
71
// (this is usually just a Network, but other services may not need the whole
Jeromy's avatar
Jeromy committed
72
// stack, and thus it becomes easier to mock)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
73 74 75
type Dialer interface {

	// DialPeer attempts to establish a connection to a given peer
76
	DialPeer(context.Context, peer.Peer) error
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
77 78 79

	// Connectedness returns a state signaling connection capabilities
	Connectedness(peer.Peer) Connectedness
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
80
}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99

// 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
)