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

import (
4 5
	"context"

6
	bsmsg "github.com/ipfs/go-ipfs/exchange/bitswap/message"
7
	protocol "gx/ipfs/QmZNkThpqfVXs9GNbexPrfBbXSLNYeKrE7jwFM2oqHbyqN/go-libp2p-protocol"
8
	cid "gx/ipfs/QmakyCk6Vnn16WEKjbkxieZmM2YLTzkFWizbmGowoYPjro/go-cid"
9
	peer "gx/ipfs/QmfMmLGoKzCHDN7cGgk64PJr4iipzidDRME8HABSJqvmhC/go-libp2p-peer"
10 11
)

12 13
var ProtocolBitswap protocol.ID = "/ipfs/bitswap/1.0.0"
var ProtocolBitswapOld protocol.ID = "/ipfs/bitswap"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
14

15 16
// BitSwapNetwork provides network connectivity for BitSwap sessions
type BitSwapNetwork interface {
17 18 19 20

	// SendMessage sends a BitSwap message to a peer.
	SendMessage(
		context.Context,
21
		peer.ID,
22 23 24 25 26
		bsmsg.BitSwapMessage) error

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

28 29
	ConnectTo(context.Context, peer.ID) error

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

32
	Routing
33 34
}

Jeromy's avatar
Jeromy committed
35 36 37 38 39
type MessageSender interface {
	SendMsg(bsmsg.BitSwapMessage) error
	Close() error
}

40
// Implement Receiver to receive messages from the BitSwapNetwork
41 42
type Receiver interface {
	ReceiveMessage(
Jeromy Johnson's avatar
Jeromy Johnson committed
43 44
		ctx context.Context,
		sender peer.ID,
45
		incoming bsmsg.BitSwapMessage)
46 47

	ReceiveError(error)
48 49 50 51

	// Connected/Disconnected warns bitswap about peer connections
	PeerConnected(peer.ID)
	PeerDisconnected(peer.ID)
52 53
}

54 55
type Routing interface {
	// FindProvidersAsync returns a channel of providers for the given key
56
	FindProvidersAsync(context.Context, *cid.Cid, int) <-chan peer.ID
57 58

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