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

import (
4 5
	"context"

6
	bsmsg "github.com/ipfs/go-ipfs/exchange/bitswap/message"
7

8
	cid "gx/ipfs/QmNp85zy9RLrQ5oQD4hPyS39ezrrXpcaa7R4Y9kxdWQLLQ/go-cid"
Steven Allen's avatar
Steven Allen committed
9
	ifconnmgr "gx/ipfs/QmWfkNorhirGE1Qp3VwBWcnGaj4adv4hNqCYwabMrEYc21/go-libp2p-interface-connmgr"
10
	peer "gx/ipfs/QmXYjuNuxVzXKJCfWasQk1RqkhVLDM9jtUKhqc2WPQmFSB/go-libp2p-peer"
11
	protocol "gx/ipfs/QmZNkThpqfVXs9GNbexPrfBbXSLNYeKrE7jwFM2oqHbyqN/go-libp2p-protocol"
12 13
)

14 15 16 17 18 19 20
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
21

22 23
// BitSwapNetwork provides network connectivity for BitSwap sessions
type BitSwapNetwork interface {
24 25 26 27

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

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

35 36
	ConnectTo(context.Context, peer.ID) error

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

39 40
	ConnectionManager() ifconnmgr.ConnManager

41
	Routing
42 43
}

Jeromy's avatar
Jeromy committed
44
type MessageSender interface {
45
	SendMsg(context.Context, bsmsg.BitSwapMessage) error
Jeromy's avatar
Jeromy committed
46
	Close() error
47
	Reset() error
Jeromy's avatar
Jeromy committed
48 49
}

50
// Implement Receiver to receive messages from the BitSwapNetwork
51 52
type Receiver interface {
	ReceiveMessage(
Jeromy Johnson's avatar
Jeromy Johnson committed
53 54
		ctx context.Context,
		sender peer.ID,
55
		incoming bsmsg.BitSwapMessage)
56 57

	ReceiveError(error)
58 59 60 61

	// Connected/Disconnected warns bitswap about peer connections
	PeerConnected(peer.ID)
	PeerDisconnected(peer.ID)
62 63
}

64 65
type Routing interface {
	// FindProvidersAsync returns a channel of providers for the given key
66
	FindProvidersAsync(context.Context, *cid.Cid, int) <-chan peer.ID
67 68

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