host.go 2.49 KB
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1
2
3
package host

import (
4
5
	"context"

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
	logging "github.com/ipfs/go-log"
	ma "github.com/jbenet/go-multiaddr"
Jeromy's avatar
Jeromy committed
10
	protocol "github.com/libp2p/go-libp2p-protocol"
11
12
	metrics "github.com/libp2p/go-libp2p/p2p/metrics"
	inet "github.com/libp2p/go-libp2p/p2p/net"
Jeromy's avatar
Jeromy committed
13
	msmux "github.com/whyrusleeping/go-multistream"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
14
15
)

16
var log = logging.Logger("github.com/libp2p/go-libp2p/p2p/host")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
17
18
19
20
21
22
23
24
25
26
27

// Host is an object participating in a p2p network, which
// implements protocols or provides services. It handles
// requests like a Server, and issues requests like a Client.
// It is called Host because it is both Server and Client (and Peer
// may be confusing).
type Host interface {
	// ID returns the (local) peer.ID associated with this Host
	ID() peer.ID

	// Peerstore returns the Host's repository of Peer Addresses and Keys.
Jeromy's avatar
Jeromy committed
28
	Peerstore() pstore.Peerstore
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
29

30
31
32
	// Returns the listen addresses of the Host
	Addrs() []ma.Multiaddr

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
33
34
35
36
	// Networks returns the Network interface of the Host
	Network() inet.Network

	// Mux returns the Mux multiplexing incoming streams to protocol handlers
Jeromy's avatar
Jeromy committed
37
	Mux() *msmux.MultistreamMuxer
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
38
39
40
41
42
43

	// Connect ensures there is a connection between this host and the peer with
	// given peer.ID. Connect will absorb the addresses in pi into its internal
	// peerstore. If there is not an active connection, Connect will issue a
	// h.Network.Dial, and block until a connection is open, or an error is
	// returned. // TODO: Relay + NAT.
Jeromy's avatar
Jeromy committed
44
	Connect(ctx context.Context, pi pstore.PeerInfo) error
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
45
46
47
48
49
50
51

	// SetStreamHandler sets the protocol handler on the Host's Mux.
	// This is equivalent to:
	//   host.Mux().SetHandler(proto, handler)
	// (Threadsafe)
	SetStreamHandler(pid protocol.ID, handler inet.StreamHandler)

52
53
54
55
	// SetStreamHandlerMatch sets the protocol handler on the Host's Mux
	// using a matching function for protocol selection.
	SetStreamHandlerMatch(protocol.ID, func(string) bool, inet.StreamHandler)

Jeromy's avatar
Jeromy committed
56
57
58
59
	// RemoveStreamHandler removes a handler on the mux that was set by
	// SetStreamHandler
	RemoveStreamHandler(pid protocol.ID)

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
60
61
62
63
	// NewStream opens a new stream to given peer p, and writes a p2p/protocol
	// header with given protocol.ID. If there is no connection to p, attempts
	// to create one. If ProtocolID is "", writes no header.
	// (Threadsafe)
64
	NewStream(ctx context.Context, p peer.ID, pids ...protocol.ID) (inet.Stream, error)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
65
66
67

	// Close shuts down the host, its Network, and services.
	Close() error
Jeromy's avatar
Jeromy committed
68
69

	GetBandwidthReporter() metrics.Reporter
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
70
}