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

import (
4 5
	"context"

Jeromy's avatar
Jeromy committed
6
	bsmsg "github.com/ipfs/go-bitswap/message"
7

Jeromy's avatar
Jeromy committed
8 9 10 11
	cid "github.com/ipfs/go-cid"
	ifconnmgr "github.com/libp2p/go-libp2p-interface-connmgr"
	peer "github.com/libp2p/go-libp2p-peer"
	protocol "github.com/libp2p/go-libp2p-protocol"
12 13
)

14 15 16 17 18 19 20
var (
	// These two are equivalent, legacy
	ProtocolBitswapOne    protocol.ID = "/ipfs/bitswap/1.0.0"
	ProtocolBitswapNoVers protocol.ID = "/ipfs/bitswap"

	ProtocolBitswap protocol.ID = "/ipfs/bitswap/1.1.0"
)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
21

22 23
// BitSwapNetwork provides network connectivity for BitSwap sessions
type BitSwapNetwork interface {
24 25 26 27

	// SendMessage sends a BitSwap message to a peer.
	SendMessage(
		context.Context,
28
		peer.ID,
29 30 31 32 33
		bsmsg.BitSwapMessage) error

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

35 36
	ConnectTo(context.Context, peer.ID) error

Jeromy's avatar
Jeromy committed
37 38
	NewMessageSender(context.Context, peer.ID) (MessageSender, error)

39 40
	ConnectionManager() ifconnmgr.ConnManager

41 42
	Stats() NetworkStats

43
	Routing
44 45
}

Jeromy's avatar
Jeromy committed
46
type MessageSender interface {
47
	SendMsg(context.Context, bsmsg.BitSwapMessage) error
Jeromy's avatar
Jeromy committed
48
	Close() error
49
	Reset() error
Jeromy's avatar
Jeromy committed
50 51
}

52
// Implement Receiver to receive messages from the BitSwapNetwork
53 54
type Receiver interface {
	ReceiveMessage(
Jeromy Johnson's avatar
Jeromy Johnson committed
55 56
		ctx context.Context,
		sender peer.ID,
57
		incoming bsmsg.BitSwapMessage)
58 59

	ReceiveError(error)
60 61 62 63

	// Connected/Disconnected warns bitswap about peer connections
	PeerConnected(peer.ID)
	PeerDisconnected(peer.ID)
64 65
}

66 67
type Routing interface {
	// FindProvidersAsync returns a channel of providers for the given key
68
	FindProvidersAsync(context.Context, cid.Cid, int) <-chan peer.ID
69 70

	// Provide provides the key to the network
71
	Provide(context.Context, cid.Cid) error
72
}
73 74 75 76 77 78 79 80

// NetworkStats is a container for statistics about the bitswap network
// the numbers inside are specific to bitswap, and not any other protocols
// using the same underlying network.
type NetworkStats struct {
	MessagesSent  uint64
	MessagesRecvd uint64
}