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

import (
4 5
	"context"

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

12 13 14 15 16 17 18
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
19

20 21
// BitSwapNetwork provides network connectivity for BitSwap sessions
type BitSwapNetwork interface {
22 23 24 25

	// SendMessage sends a BitSwap message to a peer.
	SendMessage(
		context.Context,
26
		peer.ID,
27 28 29 30 31
		bsmsg.BitSwapMessage) error

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

33 34
	ConnectTo(context.Context, peer.ID) error

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

37
	Routing
38 39
}

Jeromy's avatar
Jeromy committed
40
type MessageSender interface {
41
	SendMsg(context.Context, bsmsg.BitSwapMessage) error
Jeromy's avatar
Jeromy committed
42 43 44
	Close() error
}

45
// Implement Receiver to receive messages from the BitSwapNetwork
46 47
type Receiver interface {
	ReceiveMessage(
Jeromy Johnson's avatar
Jeromy Johnson committed
48 49
		ctx context.Context,
		sender peer.ID,
50
		incoming bsmsg.BitSwapMessage)
51 52

	ReceiveError(error)
53 54 55 56

	// Connected/Disconnected warns bitswap about peer connections
	PeerConnected(peer.ID)
	PeerDisconnected(peer.ID)
57 58
}

59 60
type Routing interface {
	// FindProvidersAsync returns a channel of providers for the given key
61
	FindProvidersAsync(context.Context, *cid.Cid, int) <-chan peer.ID
62 63

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