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

import (
	"io"

Jeromy's avatar
Jeromy committed
6
	peer "github.com/ipfs/go-libp2p-peer"
Jeromy's avatar
Jeromy committed
7
	pstore "github.com/ipfs/go-libp2p-peerstore"
Jeromy's avatar
Jeromy committed
8
9
	ma "github.com/jbenet/go-multiaddr"
	"github.com/jbenet/goprocess"
10
	conn "github.com/libp2p/go-libp2p/p2p/net/conn"
11
	protocol "github.com/libp2p/go-libp2p/p2p/protocol"
Jeromy's avatar
Jeromy committed
12
	context "golang.org/x/net/context"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
)

// MessageSizeMax is a soft (recommended) maximum for network messages.
// One can write more, as the interface is a stream. But it is useful
// to bunch it up into multiple read/writes when the whole message is
// a single, large serialized object.
const MessageSizeMax = 2 << 22 // 4MB

// Stream represents a bidirectional channel between two agents in
// the IPFS network. "agent" is as granular as desired, potentially
// being a "request -> reply" pair, or whole protocols.
// Streams are backed by SPDY streams underneath the hood.
type Stream interface {
	io.Reader
	io.Writer
	io.Closer

30
31
	Protocol() protocol.ID
	SetProtocol(protocol.ID)
32

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
33
34
35
36
	// Conn returns the connection this stream is part of.
	Conn() Conn
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
37
38
// StreamHandler is the type of function used to listen for
// streams opened by the remote side.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
39
40
41
42
43
44
45
46
47
type StreamHandler func(Stream)

// Conn is a connection to a remote peer. It multiplexes streams.
// Usually there is no need to use a Conn directly, but it may
// be useful to get information about the peer on the other side:
//  stream.Conn().RemotePeer()
type Conn interface {
	conn.PeerConn

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
48
49
	// NewStream constructs a new Stream over this conn.
	NewStream() (Stream, error)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
50
51
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
52
53
54
55
56
// ConnHandler is the type of function used to listen for
// connections opened by the remote side.
type ConnHandler func(Conn)

// Network is the interface used to connect to the outside world.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
57
58
59
60
61
62
63
// It dials and listens for connections. it uses a Swarm to pool
// connnections (see swarm pkg, and peerstream.Swarm). Connections
// are encrypted with a TLS-like protocol.
type Network interface {
	Dialer
	io.Closer

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
64
65
66
	// SetStreamHandler sets the handler for new streams opened by the
	// remote side. This operation is threadsafe.
	SetStreamHandler(StreamHandler)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
67

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
68
69
70
	// SetConnHandler sets the handler for new connections opened by the
	// remote side. This operation is threadsafe.
	SetConnHandler(ConnHandler)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
71
72
73

	// NewStream returns a new stream to given peer p.
	// If there is no connection to p, attempts to create one.
74
	NewStream(context.Context, peer.ID) (Stream, error)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
75

76
77
78
	// Listen tells the network to start listening on given multiaddrs.
	Listen(...ma.Multiaddr) error

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
79
80
81
82
83
84
85
86
	// 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)

87
88
	// Process returns the network's Process
	Process() goprocess.Process
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
89
90
91
92
93
94
95
96
97
98
}

// Dialer represents a service that can dial out to peers
// (this is usually just a Network, but other services may not need the whole
// stack, and thus it becomes easier to mock)
type Dialer interface {

	// Peerstore returns the internal peerstore
	// This is useful to tell the dialer about a new address for a peer.
	// Or use one of the public keys found out over the network.
Jeromy's avatar
Jeromy committed
99
	Peerstore() pstore.Peerstore
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
100
101
102
103

	// LocalPeer returns the local peer associated with this network
	LocalPeer() peer.ID

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
104
105
	// DialPeer establishes a connection to a given peer
	DialPeer(context.Context, peer.ID) (Conn, error)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120

	// ClosePeer closes the connection to a given peer
	ClosePeer(peer.ID) error

	// Connectedness returns a state signaling connection capabilities
	Connectedness(peer.ID) Connectedness

	// Peers returns the peers connected
	Peers() []peer.ID

	// Conns returns the connections in this Netowrk
	Conns() []Conn

	// ConnsToPeer returns the connections in this Netowrk for given peer.
	ConnsToPeer(p peer.ID) []Conn
121
122
123
124

	// Notify/StopNotify register and unregister a notifiee for signals
	Notify(Notifiee)
	StopNotify(Notifiee)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
}

// 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 = iota

	// 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
)
145
146
147
148

// Notifiee is an interface for an object wishing to receive
// notifications from a Network.
type Notifiee interface {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
149
150
151
152
153
154
	Listen(Network, ma.Multiaddr)      // called when network starts listening on an addr
	ListenClose(Network, ma.Multiaddr) // called when network starts listening on an addr
	Connected(Network, Conn)           // called when a connection opened
	Disconnected(Network, Conn)        // called when a connection closed
	OpenedStream(Network, Stream)      // called when a stream opened
	ClosedStream(Network, Stream)      // called when a stream closed
155
156
157
158
159

	// TODO
	// PeerConnected(Network, peer.ID)    // called when a peer connected
	// PeerDisconnected(Network, peer.ID) // called when a peer disconnected
}