mirror of https://github.com/libp2p/go-libp2p.git
Jeromy
8 years ago
17 changed files with 20 additions and 153 deletions
@ -1,70 +0,0 @@ |
|||
package host |
|||
|
|||
import ( |
|||
"context" |
|||
|
|||
peer "github.com/ipfs/go-libp2p-peer" |
|||
pstore "github.com/ipfs/go-libp2p-peerstore" |
|||
logging "github.com/ipfs/go-log" |
|||
ma "github.com/jbenet/go-multiaddr" |
|||
metrics "github.com/libp2p/go-libp2p-metrics" |
|||
inet "github.com/libp2p/go-libp2p-net" |
|||
protocol "github.com/libp2p/go-libp2p-protocol" |
|||
msmux "github.com/whyrusleeping/go-multistream" |
|||
) |
|||
|
|||
var log = logging.Logger("github.com/libp2p/go-libp2p/p2p/host") |
|||
|
|||
// 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.
|
|||
Peerstore() pstore.Peerstore |
|||
|
|||
// Returns the listen addresses of the Host
|
|||
Addrs() []ma.Multiaddr |
|||
|
|||
// Networks returns the Network interface of the Host
|
|||
Network() inet.Network |
|||
|
|||
// Mux returns the Mux multiplexing incoming streams to protocol handlers
|
|||
Mux() *msmux.MultistreamMuxer |
|||
|
|||
// 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.
|
|||
Connect(ctx context.Context, pi pstore.PeerInfo) error |
|||
|
|||
// 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) |
|||
|
|||
// 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) |
|||
|
|||
// RemoveStreamHandler removes a handler on the mux that was set by
|
|||
// SetStreamHandler
|
|||
RemoveStreamHandler(pid protocol.ID) |
|||
|
|||
// 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)
|
|||
NewStream(ctx context.Context, p peer.ID, pids ...protocol.ID) (inet.Stream, error) |
|||
|
|||
// Close shuts down the host, its Network, and services.
|
|||
Close() error |
|||
|
|||
GetBandwidthReporter() metrics.Reporter |
|||
} |
@ -1,36 +0,0 @@ |
|||
package host |
|||
|
|||
import ( |
|||
"github.com/libp2p/go-libp2p-protocol" |
|||
"strings" |
|||
|
|||
semver "github.com/coreos/go-semver/semver" |
|||
) |
|||
|
|||
func MultistreamSemverMatcher(base protocol.ID) (func(string) bool, error) { |
|||
parts := strings.Split(string(base), "/") |
|||
vers, err := semver.NewVersion(parts[len(parts)-1]) |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
|
|||
return func(check string) bool { |
|||
chparts := strings.Split(check, "/") |
|||
if len(chparts) != len(parts) { |
|||
return false |
|||
} |
|||
|
|||
for i, v := range chparts[:len(chparts)-1] { |
|||
if parts[i] != v { |
|||
return false |
|||
} |
|||
} |
|||
|
|||
chvers, err := semver.NewVersion(chparts[len(chparts)-1]) |
|||
if err != nil { |
|||
return false |
|||
} |
|||
|
|||
return vers.Major == chvers.Major && vers.Minor >= chvers.Minor |
|||
}, nil |
|||
} |
@ -1,33 +0,0 @@ |
|||
package host |
|||
|
|||
import ( |
|||
"testing" |
|||
) |
|||
|
|||
func TestSemverMatching(t *testing.T) { |
|||
m, err := MultistreamSemverMatcher("/testing/4.3.5") |
|||
if err != nil { |
|||
t.Fatal(err) |
|||
} |
|||
|
|||
cases := map[string]bool{ |
|||
"/testing/4.3.0": true, |
|||
"/testing/4.3.7": true, |
|||
"/testing/4.3.5": true, |
|||
"/testing/4.2.7": true, |
|||
"/testing/4.0.0": true, |
|||
"/testing/5.0.0": false, |
|||
"/cars/dogs/4.3.5": false, |
|||
"/foo/1.0.0": false, |
|||
"": false, |
|||
"dogs": false, |
|||
"/foo": false, |
|||
"/foo/1.1.1.1": false, |
|||
} |
|||
|
|||
for p, ok := range cases { |
|||
if m(p) != ok { |
|||
t.Fatalf("expected %s to be %t", p, ok) |
|||
} |
|||
} |
|||
} |
Loading…
Reference in new issue