interface.go 2.96 KB
Newer Older
1 2 3
package network

import (
4
	"context"
5
	"time"
6

7
	bsmsg "gitlab.dms3.io/dms3/go-bitswap/message"
8

9
	cid "gitlab.dms3.io/dms3/go-cid"
Raúl Kripalani's avatar
Raúl Kripalani committed
10

11 12 13 14
	"gitlab.dms3.io/p2p/go-p2p-core/connmgr"
	"gitlab.dms3.io/p2p/go-p2p-core/peer"
	"gitlab.dms3.io/p2p/go-p2p-core/protocol"
	"gitlab.dms3.io/p2p/go-p2p/p2p/protocol/ping"
15 16
)

17
var (
18
	// ProtocolBitswapNoVers is equivalent to the legacy bitswap protocol
19
	ProtocolBitswapNoVers protocol.ID = "/dms3/bitswap"
dirkmc's avatar
dirkmc committed
20
	// ProtocolBitswapOneZero is the prefix for the legacy bitswap protocol
21
	ProtocolBitswapOneZero protocol.ID = "/dms3/bitswap/1.0.0"
dirkmc's avatar
dirkmc committed
22
	// ProtocolBitswapOneOne is the the prefix for version 1.1.0
23
	ProtocolBitswapOneOne protocol.ID = "/dms3/bitswap/1.1.0"
dirkmc's avatar
dirkmc committed
24
	// ProtocolBitswap is the current version of the bitswap protocol: 1.2.0
25
	ProtocolBitswap protocol.ID = "/dms3/bitswap/1.2.0"
26
)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
27

28
// BitSwapNetwork provides network connectivity for BitSwap sessions.
29
type BitSwapNetwork interface {
dirkmc's avatar
dirkmc committed
30
	Self() peer.ID
31

32 33 34
	// SendMessage sends a BitSwap message to a peer.
	SendMessage(
		context.Context,
35
		peer.ID,
36 37 38 39 40
		bsmsg.BitSwapMessage) error

	// SetDelegate registers the Reciver to handle messages received from the
	// network.
	SetDelegate(Receiver)
41

42
	ConnectTo(context.Context, peer.ID) error
dirkmc's avatar
dirkmc committed
43
	DisconnectFrom(context.Context, peer.ID) error
44

45
	NewMessageSender(context.Context, peer.ID, *MessageSenderOpts) (MessageSender, error)
Jeromy's avatar
Jeromy committed
46

Raúl Kripalani's avatar
Raúl Kripalani committed
47
	ConnectionManager() connmgr.ConnManager
48

49
	Stats() Stats
50

51
	Routing
52 53

	Pinger
54 55
}

56 57
// MessageSender is an interface for sending a series of messages over the bitswap
// network
Jeromy's avatar
Jeromy committed
58
type MessageSender interface {
59
	SendMsg(context.Context, bsmsg.BitSwapMessage) error
Jeromy's avatar
Jeromy committed
60
	Close() error
61
	Reset() error
dirkmc's avatar
dirkmc committed
62 63
	// Indicates whether the remote peer supports HAVE / DONT_HAVE messages
	SupportsHave() bool
Jeromy's avatar
Jeromy committed
64 65
}

66 67 68 69 70 71
type MessageSenderOpts struct {
	MaxRetries       int
	SendTimeout      time.Duration
	SendErrorBackoff time.Duration
}

72
// Receiver is an interface that can receive messages from the BitSwapNetwork.
73 74
type Receiver interface {
	ReceiveMessage(
Jeromy Johnson's avatar
Jeromy Johnson committed
75 76
		ctx context.Context,
		sender peer.ID,
77
		incoming bsmsg.BitSwapMessage)
78 79

	ReceiveError(error)
80

81
	// Connected/Disconnected warns bitswap about peer connections.
82 83
	PeerConnected(peer.ID)
	PeerDisconnected(peer.ID)
84 85
}

86 87
// Routing is an interface to providing and finding providers on a bitswap
// network.
88
type Routing interface {
89
	// FindProvidersAsync returns a channel of providers for the given key.
90
	FindProvidersAsync(context.Context, cid.Cid, int) <-chan peer.ID
91

92
	// Provide provides the key to the network.
93
	Provide(context.Context, cid.Cid) error
94
}
95

96 97 98 99 100 101 102 103
// Pinger is an interface to ping a peer and get the average latency of all pings
type Pinger interface {
	// Ping a peer
	Ping(context.Context, peer.ID) ping.Result
	// Get the average latency of all pings
	Latency(peer.ID) time.Duration
}

104
// Stats is a container for statistics about the bitswap network
105 106
// the numbers inside are specific to bitswap, and not any other protocols
// using the same underlying network.
107
type Stats struct {
108 109 110
	MessagesSent  uint64
	MessagesRecvd uint64
}