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

import (
4
	context "context"
5
	bsmsg "github.com/ipfs/go-ipfs/exchange/bitswap/message"
6 7 8
	key "gx/ipfs/QmYEoKZXHoAToWfhGF3vryhMn3WWhE1o2MasQ8uzY5iDi9/go-key"
	protocol "gx/ipfs/QmZNkThpqfVXs9GNbexPrfBbXSLNYeKrE7jwFM2oqHbyqN/go-libp2p-protocol"
	peer "gx/ipfs/QmfMmLGoKzCHDN7cGgk64PJr4iipzidDRME8HABSJqvmhC/go-libp2p-peer"
9 10
)

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

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

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

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

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

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

31
	Routing
32 33
}

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

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

	ReceiveError(error)
47 48 49 50

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

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

	// Provide provides the key to the network
58
	Provide(context.Context, key.Key) error
59
}