interface.go 1.33 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 7 8 9

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

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

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
13 14 15 16 17 18 19 20 21
// Conn is a generic message-based Peer-to-Peer connection.
type Conn interface {

	// LocalPeer is the Peer on this side
	LocalPeer() *peer.Peer

	// RemotePeer is the Peer on the remote side
	RemotePeer() *peer.Peer

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
22 23
	// In returns a readable message channel
	In() <-chan []byte
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
24

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
25 26
	// Out returns a writable message channel
	Out() chan<- []byte
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

	// Close ends the connection
	Close() error
}

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