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

import (
4
	bsmsg "github.com/ipfs/go-ipfs/exchange/bitswap/message"
5
	peer "gx/ipfs/QmWXjJo15p4pzT7cayEwZi2sWgJqLnGDof6ZGMh9xBgU1p/go-libp2p-peer"
Jeromy's avatar
Jeromy committed
6
	context "gx/ipfs/QmZy2y8t9zQH2a1b8q2ZSLKp17ATuJoCNxxyMFG5qFExpt/go-net/context"
Jeromy's avatar
Jeromy committed
7
	protocol "gx/ipfs/QmbiRCGZqhfcSjnm9icGz3oNQQdPLAnLWnKHXixaEWXVCN/go-libp2p/p2p/protocol"
George Antoniadis's avatar
George Antoniadis committed
8
	key "gx/ipfs/Qmce4Y4zg3sYr7xKM5UueS67vhNni6EeWgCRnb7MbLJMew/go-key"
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
}