interface.go 1.47 KB
Newer Older
1 2 3 4 5 6
package network

import (
	context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"

	bsmsg "github.com/jbenet/go-ipfs/exchange/bitswap/message"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
7
	peer "github.com/jbenet/go-ipfs/p2p/peer"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
8
	protocol "github.com/jbenet/go-ipfs/p2p/protocol"
9
	u "github.com/jbenet/go-ipfs/util"
10 11
)

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

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

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
17
	// DialPeer ensures there is a connection to peer.
18
	DialPeer(context.Context, peer.ID) error
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
19

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

	// SendRequest sends a BitSwap message to a peer and waits for a response.
	SendRequest(
		context.Context,
29
		peer.ID,
30 31 32 33 34
		bsmsg.BitSwapMessage) (incoming bsmsg.BitSwapMessage, err error)

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

	Routing
37 38
}

39
// Implement Receiver to receive messages from the BitSwapNetwork
40 41
type Receiver interface {
	ReceiveMessage(
42 43
		ctx context.Context, sender peer.ID, incoming bsmsg.BitSwapMessage) (
		destination peer.ID, outgoing bsmsg.BitSwapMessage)
44 45

	ReceiveError(error)
46 47
}

48 49
type Routing interface {
	// FindProvidersAsync returns a channel of providers for the given key
50
	FindProvidersAsync(context.Context, u.Key, int) <-chan peer.ID
51 52

	// Provide provides the key to the network
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
53
	Provide(context.Context, u.Key) error
54
}