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

import (
4 5
	"context"

6
	bsmsg "github.com/ipfs/go-ipfs/exchange/bitswap/message"
7

8
	protocol "gx/ipfs/QmZNkThpqfVXs9GNbexPrfBbXSLNYeKrE7jwFM2oqHbyqN/go-libp2p-protocol"
Steven Allen's avatar
Steven Allen committed
9 10
	peer "gx/ipfs/QmZoWKhxUmZ2seW4BzX6fJkNR8hh9PsGModr7q171yq2SS/go-libp2p-peer"
	ifconnmgr "gx/ipfs/Qmax8X1Kfahf5WfSB68EWDG3d3qyS3Sqs1v412fjPTfRwx/go-libp2p-interface-connmgr"
Steven Allen's avatar
Steven Allen committed
11
	cid "gx/ipfs/QmcZfnkapfECQGcLZaf9B79NRg7cRa9EnZh4LSbkCzwNvY/go-cid"
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
	Routing
42 43
}

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

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

	ReceiveError(error)
58 59 60 61

	// Connected/Disconnected warns bitswap about peer connections
	PeerConnected(peer.ID)
	PeerDisconnected(peer.ID)
62 63
}

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

	// Provide provides the key to the network
69
	Provide(context.Context, *cid.Cid) error
70
}