diff --git a/p2p/host/routed/routed.go b/p2p/host/routed/routed.go index 97493ad5e4590f959f5a32728c839fa3cb287da4..3dcf8247cf0d01b65c1d8809c919b409c88120b9 100644 --- a/p2p/host/routed/routed.go +++ b/p2p/host/routed/routed.go @@ -1,3 +1,6 @@ +// Package routedhost provides a wrapper for libp2p Host and a generic Routing +// mechanism, which allow them to discover hosts which are not in their +// peerstores. package routedhost import ( @@ -31,10 +34,13 @@ type RoutedHost struct { route Routing } +// The Routing interface allows to wrap any peer discovery implementations type Routing interface { FindPeer(context.Context, peer.ID) (pstore.PeerInfo, error) } +// Wrap creates a RoutedHost by wrapping a regular Host and a Routing +// implementation. func Wrap(h host.Host, r Routing) *RoutedHost { return &RoutedHost{h, r} } @@ -85,41 +91,59 @@ func logRoutingErrDifferentPeers(ctx context.Context, wanted, got peer.ID, err e log.Event(ctx, "routingError", lm) } +// ID returns the (local) peer.ID associated with this Host func (rh *RoutedHost) ID() peer.ID { return rh.host.ID() } +// Peerstore returns the Host's repository of Peer Addresses and Keys. func (rh *RoutedHost) Peerstore() pstore.Peerstore { return rh.host.Peerstore() } +// Addrs returns all the addresses of BasicHost at this moment in time. func (rh *RoutedHost) Addrs() []ma.Multiaddr { return rh.host.Addrs() } +// Network returns the Network interface of the Host func (rh *RoutedHost) Network() inet.Network { return rh.host.Network() } +// Mux returns the Mux multiplexing incoming streams to protocol handlers. func (rh *RoutedHost) Mux() *msmux.MultistreamMuxer { return rh.host.Mux() } +// SetStreamHandler sets the protocol handler on the Host's Mux. +// This is equivalent to: +// host.Mux().SetHandler(proto, handler) +// (Threadsafe) func (rh *RoutedHost) SetStreamHandler(pid protocol.ID, handler inet.StreamHandler) { rh.host.SetStreamHandler(pid, handler) } +// SetStreamHandlerMatch sets the protocol handler on the Host's Mux +// using a matching function to do protocol comparisons func (rh *RoutedHost) SetStreamHandlerMatch(pid protocol.ID, m func(string) bool, handler inet.StreamHandler) { rh.host.SetStreamHandlerMatch(pid, m, handler) } +// RemoveStreamHandler removes the handler matching the given protocol ID. func (rh *RoutedHost) RemoveStreamHandler(pid protocol.ID) { rh.host.RemoveStreamHandler(pid) } +// 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) func (rh *RoutedHost) NewStream(ctx context.Context, p peer.ID, pids ...protocol.ID) (inet.Stream, error) { return rh.host.NewStream(ctx, p, pids...) } + +// Close shuts down the Host's services (network, etc). func (rh *RoutedHost) Close() error { // no need to close IpfsRouting. we dont own it. return rh.host.Close()