swarm.go 1.57 KB
Newer Older
Łukasz Magiera's avatar
Łukasz Magiera committed
1 2 3
package iface

import (
Łukasz Magiera's avatar
Łukasz Magiera committed
4
	"context"
5
	"errors"
Łukasz Magiera's avatar
Łukasz Magiera committed
6 7
	"time"

8
	ma "gx/ipfs/QmYmsdtJ3HsodkePE3eU3TsCaP2YvPZJ4LoXnNkDE5Tpt7/go-multiaddr"
9 10 11
	"gx/ipfs/QmZNkThpqfVXs9GNbexPrfBbXSLNYeKrE7jwFM2oqHbyqN/go-libp2p-protocol"
	"gx/ipfs/QmbNepETomvmXfz1X5pHNFD2QuPqnqi47dTd94QJWSorQ3/go-libp2p-peer"
	pstore "gx/ipfs/QmfAQMFpgDU2U4BXG64qVr8HSiictfWvkSBz7Y2oDj65st/go-libp2p-peerstore"
12
	net "gx/ipfs/QmfDPh144WGBqRxZb1TGDHerbMnZATrHZggAPw7putNnBq/go-libp2p-net"
Łukasz Magiera's avatar
Łukasz Magiera committed
13 14
)

15 16 17
var (
	ErrNotConnected = errors.New("not connected")
	ErrConnNotFound = errors.New("conn not found")
18
)
19 20 21

// ConnectionInfo contains information about a peer
type ConnectionInfo interface {
Łukasz Magiera's avatar
Łukasz Magiera committed
22 23 24 25 26 27
	// ID returns PeerID
	ID() peer.ID

	// Address returns the multiaddress via which we are connected with the peer
	Address() ma.Multiaddr

28 29 30
	// Direction returns which way the connection was established
	Direction() net.Direction

Łukasz Magiera's avatar
Łukasz Magiera committed
31
	// Latency returns last known round trip time to the peer
Łukasz Magiera's avatar
Łukasz Magiera committed
32
	Latency(context.Context) (time.Duration, error)
Łukasz Magiera's avatar
Łukasz Magiera committed
33 34

	// Streams returns list of streams established with the peer
35
	Streams(context.Context) ([]protocol.ID, error)
Łukasz Magiera's avatar
Łukasz Magiera committed
36 37 38 39
}

// SwarmAPI specifies the interface to libp2p swarm
type SwarmAPI interface {
40 41
	// Connect to a given peer
	Connect(context.Context, pstore.PeerInfo) error
Łukasz Magiera's avatar
Łukasz Magiera committed
42 43 44 45 46

	// Disconnect from a given address
	Disconnect(context.Context, ma.Multiaddr) error

	// Peers returns the list of peers we are connected to
47
	Peers(context.Context) ([]ConnectionInfo, error)
48 49 50 51

	KnownAddrs(context.Context) (map[peer.ID][]ma.Multiaddr, error)
	LocalAddrs(context.Context) ([]ma.Multiaddr, error)
	ListenAddrs(context.Context) ([]ma.Multiaddr, error)
Łukasz Magiera's avatar
Łukasz Magiera committed
52
}