swarm.go 1.73 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
	pstore "gx/ipfs/QmQAGG1zxfePqj2t7bLxyN8AFccZ889DDR9Gn8kVLDrGZo/go-libp2p-peerstore"
Steven Allen's avatar
Steven Allen committed
9
	ma "gx/ipfs/QmRKLtwMw131aK7ugC3G7ybpumMz78YrJe5dzneyindvG1/go-multiaddr"
10
	"gx/ipfs/QmZNkThpqfVXs9GNbexPrfBbXSLNYeKrE7jwFM2oqHbyqN/go-libp2p-protocol"
Steven Allen's avatar
Steven Allen committed
11
	"gx/ipfs/QmcqU6QUDSXprb1518vYDGczrTJTyGwLG9eUa5iNX4xUtS/go-libp2p-peer"
12
	net "gx/ipfs/QmenvQQy4bFGSiHJUGupVmCRHfetg5rH3vTp9Z2f6v2KXR/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
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 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
	// 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
}