github.com/keltia/go-ipfs@v0.3.8-0.20150909044612-210793031c63/p2p/host/host.go (about) 1 package host 2 3 import ( 4 ma "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr" 5 context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context" 6 metrics "github.com/ipfs/go-ipfs/metrics" 7 inet "github.com/ipfs/go-ipfs/p2p/net" 8 peer "github.com/ipfs/go-ipfs/p2p/peer" 9 protocol "github.com/ipfs/go-ipfs/p2p/protocol" 10 eventlog "github.com/ipfs/go-ipfs/thirdparty/eventlog" 11 ) 12 13 var log = eventlog.Logger("p2p/host") 14 15 // Host is an object participating in a p2p network, which 16 // implements protocols or provides services. It handles 17 // requests like a Server, and issues requests like a Client. 18 // It is called Host because it is both Server and Client (and Peer 19 // may be confusing). 20 type Host interface { 21 // ID returns the (local) peer.ID associated with this Host 22 ID() peer.ID 23 24 // Peerstore returns the Host's repository of Peer Addresses and Keys. 25 Peerstore() peer.Peerstore 26 27 // Returns the listen addresses of the Host 28 Addrs() []ma.Multiaddr 29 30 // Networks returns the Network interface of the Host 31 Network() inet.Network 32 33 // Mux returns the Mux multiplexing incoming streams to protocol handlers 34 Mux() *protocol.Mux 35 36 // Connect ensures there is a connection between this host and the peer with 37 // given peer.ID. Connect will absorb the addresses in pi into its internal 38 // peerstore. If there is not an active connection, Connect will issue a 39 // h.Network.Dial, and block until a connection is open, or an error is 40 // returned. // TODO: Relay + NAT. 41 Connect(ctx context.Context, pi peer.PeerInfo) error 42 43 // SetStreamHandler sets the protocol handler on the Host's Mux. 44 // This is equivalent to: 45 // host.Mux().SetHandler(proto, handler) 46 // (Threadsafe) 47 SetStreamHandler(pid protocol.ID, handler inet.StreamHandler) 48 49 // RemoveStreamHandler removes a handler on the mux that was set by 50 // SetStreamHandler 51 RemoveStreamHandler(pid protocol.ID) 52 53 // NewStream opens a new stream to given peer p, and writes a p2p/protocol 54 // header with given protocol.ID. If there is no connection to p, attempts 55 // to create one. If ProtocolID is "", writes no header. 56 // (Threadsafe) 57 NewStream(pid protocol.ID, p peer.ID) (inet.Stream, error) 58 59 // Close shuts down the host, its Network, and services. 60 Close() error 61 62 GetBandwidthReporter() metrics.Reporter 63 }