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

import (
4
	key "github.com/ipfs/go-ipfs/blocks/key"
5
	bsmsg "github.com/ipfs/go-ipfs/exchange/bitswap/message"
Jeromy's avatar
Jeromy committed
6
	peer "gx/ipfs/QmWtbQU15LaB5B1JC2F7TV9P4K88vD3PpA4AJrwfCjhML8/go-libp2p-peer"
Jeromy's avatar
Jeromy committed
7
	context "gx/ipfs/QmZy2y8t9zQH2a1b8q2ZSLKp17ATuJoCNxxyMFG5qFExpt/go-net/context"
Jeromy's avatar
Jeromy committed
8
	protocol "gx/ipfs/Qmf4ETeAWXuThBfWwonVyFqGFSgTWepUDEr1txcctvpTXS/go-libp2p/p2p/protocol"
9 10
)

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

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

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

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

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

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

30
	Routing
31 32
}

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

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

	ReceiveError(error)
46 47 48 49

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

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

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