Commit 4557f31f authored by Hector Sanjuan's avatar Hector Sanjuan
Browse files

Make golint happy on routed host



License: MIT
Signed-off-by: default avatarHector Sanjuan <hector@protocol.ai>
parent b90873d8
// 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 package routedhost
import ( import (
...@@ -31,10 +34,13 @@ type RoutedHost struct { ...@@ -31,10 +34,13 @@ type RoutedHost struct {
route Routing route Routing
} }
// The Routing interface allows to wrap any peer discovery implementations
type Routing interface { type Routing interface {
FindPeer(context.Context, peer.ID) (pstore.PeerInfo, error) 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 { func Wrap(h host.Host, r Routing) *RoutedHost {
return &RoutedHost{h, r} return &RoutedHost{h, r}
} }
...@@ -85,41 +91,59 @@ func logRoutingErrDifferentPeers(ctx context.Context, wanted, got peer.ID, err e ...@@ -85,41 +91,59 @@ func logRoutingErrDifferentPeers(ctx context.Context, wanted, got peer.ID, err e
log.Event(ctx, "routingError", lm) log.Event(ctx, "routingError", lm)
} }
// ID returns the (local) peer.ID associated with this Host
func (rh *RoutedHost) ID() peer.ID { func (rh *RoutedHost) ID() peer.ID {
return rh.host.ID() return rh.host.ID()
} }
// Peerstore returns the Host's repository of Peer Addresses and Keys.
func (rh *RoutedHost) Peerstore() pstore.Peerstore { func (rh *RoutedHost) Peerstore() pstore.Peerstore {
return rh.host.Peerstore() return rh.host.Peerstore()
} }
// Addrs returns all the addresses of BasicHost at this moment in time.
func (rh *RoutedHost) Addrs() []ma.Multiaddr { func (rh *RoutedHost) Addrs() []ma.Multiaddr {
return rh.host.Addrs() return rh.host.Addrs()
} }
// Network returns the Network interface of the Host
func (rh *RoutedHost) Network() inet.Network { func (rh *RoutedHost) Network() inet.Network {
return rh.host.Network() return rh.host.Network()
} }
// Mux returns the Mux multiplexing incoming streams to protocol handlers.
func (rh *RoutedHost) Mux() *msmux.MultistreamMuxer { func (rh *RoutedHost) Mux() *msmux.MultistreamMuxer {
return rh.host.Mux() 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) { func (rh *RoutedHost) SetStreamHandler(pid protocol.ID, handler inet.StreamHandler) {
rh.host.SetStreamHandler(pid, handler) 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) { func (rh *RoutedHost) SetStreamHandlerMatch(pid protocol.ID, m func(string) bool, handler inet.StreamHandler) {
rh.host.SetStreamHandlerMatch(pid, m, handler) rh.host.SetStreamHandlerMatch(pid, m, handler)
} }
// RemoveStreamHandler removes the handler matching the given protocol ID.
func (rh *RoutedHost) RemoveStreamHandler(pid protocol.ID) { func (rh *RoutedHost) RemoveStreamHandler(pid protocol.ID) {
rh.host.RemoveStreamHandler(pid) 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) { func (rh *RoutedHost) NewStream(ctx context.Context, p peer.ID, pids ...protocol.ID) (inet.Stream, error) {
return rh.host.NewStream(ctx, p, pids...) return rh.host.NewStream(ctx, p, pids...)
} }
// Close shuts down the Host's services (network, etc).
func (rh *RoutedHost) Close() error { func (rh *RoutedHost) Close() error {
// no need to close IpfsRouting. we dont own it. // no need to close IpfsRouting. we dont own it.
return rh.host.Close() return rh.host.Close()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment