interface.go 2.25 KB
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1 2 3 4
package conn

import (
	peer "github.com/jbenet/go-ipfs/peer"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
5
	u "github.com/jbenet/go-ipfs/util"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
6
	ctxc "github.com/jbenet/go-ipfs/util/ctxcloser"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
7 8 9 10

	ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
)

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
11 12 13
// Map maps Keys (Peer.IDs) to Connections.
type Map map[u.Key]Conn

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
14 15
// Conn is a generic message-based Peer-to-Peer connection.
type Conn interface {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
16
	// implement ContextCloser too!
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
17
	ctxc.ContextCloser
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
18

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
19 20 21 22 23 24
	// ID is an identifier unique to this connection.
	ID() string

	// LocalMultiaddr is the Multiaddr on this side
	LocalMultiaddr() ma.Multiaddr

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
25 26 27
	// LocalPeer is the Peer on this side
	LocalPeer() *peer.Peer

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
28 29 30
	// RemoteMultiaddr is the Multiaddr on the remote side
	RemoteMultiaddr() ma.Multiaddr

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
31 32 33
	// RemotePeer is the Peer on the remote side
	RemotePeer() *peer.Peer

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
34 35
	// In returns a readable message channel
	In() <-chan []byte
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
36

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
37 38
	// Out returns a writable message channel
	Out() chan<- []byte
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
39 40

	// Close ends the connection
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
41
	// Close() error  -- already in ContextCloser
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
42 43
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57
// Dialer is an object that can open connections. We could have a "convenience"
// Dial function as before, but it would have many arguments, as dialing is
// no longer simple (need a peerstore, a local peer, a context, a network, etc)
type Dialer struct {

	// LocalPeer is the identity of the local Peer.
	LocalPeer *peer.Peer

	// Peerstore is the set of peers we know about locally. The Dialer needs it
	// because when an incoming connection is identified, we should reuse the
	// same peer objects (otherwise things get inconsistent).
	Peerstore peer.Peerstore
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
// Listener is an object that can accept connections. It matches net.Listener
type Listener interface {

	// Accept waits for and returns the next connection to the listener.
	Accept() <-chan Conn

	// Multiaddr is the identity of the local Peer.
	Multiaddr() ma.Multiaddr

	// LocalPeer is the identity of the local Peer.
	LocalPeer() *peer.Peer

	// Peerstore is the set of peers we know about locally. The Listener needs it
	// because when an incoming connection is identified, we should reuse the
	// same peer objects (otherwise things get inconsistent).
	Peerstore() peer.Peerstore

	// Close closes the listener.
	// Any blocked Accept operations will be unblocked and return errors.
	Close() error
}