swarm.go 1.52 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 9 10
	"gitlab.dms3.io/p2p/go-p2p-core/network"
	"gitlab.dms3.io/p2p/go-p2p-core/peer"
	"gitlab.dms3.io/p2p/go-p2p-core/protocol"
Raúl Kripalani's avatar
Raúl Kripalani committed
11

Łukasz Magiera's avatar
Łukasz Magiera committed
12
	ma "github.com/multiformats/go-multiaddr"
Ł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
	// Direction returns which way the connection was established
Raúl Kripalani's avatar
Raúl Kripalani committed
29
	Direction() network.Direction
30

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

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

38
// SwarmAPI specifies the interface to p2p swarm
Łukasz Magiera's avatar
Łukasz Magiera committed
39
type SwarmAPI interface {
40
	// Connect to a given peer
Raúl Kripalani's avatar
Raúl Kripalani committed
41
	Connect(context.Context, peer.AddrInfo) 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
	// KnownAddrs returns the list of all addresses this node is aware of
50
	KnownAddrs(context.Context) (map[peer.ID][]ma.Multiaddr, error)
51 52

	// LocalAddrs returns the list of announced listening addresses
53
	LocalAddrs(context.Context) ([]ma.Multiaddr, error)
54 55

	// ListenAddrs returns the list of all listening addresses
56
	ListenAddrs(context.Context) ([]ma.Multiaddr, error)
Łukasz Magiera's avatar
Łukasz Magiera committed
57
}