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

import (
4
	context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
5
	bsmsg "github.com/jbenet/go-ipfs/exchange/bitswap/message"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
6
	peer "github.com/jbenet/go-ipfs/p2p/peer"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
7
	protocol "github.com/jbenet/go-ipfs/p2p/protocol"
8
	u "github.com/jbenet/go-ipfs/util"
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

	// SendRequest sends a BitSwap message to a peer and waits for a response.
	SendRequest(
		context.Context,
25
		peer.ID,
26 27 28 29 30
		bsmsg.BitSwapMessage) (incoming bsmsg.BitSwapMessage, err error)

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

	Routing
33 34
}

35
// Implement Receiver to receive messages from the BitSwapNetwork
36 37
type Receiver interface {
	ReceiveMessage(
38 39
		ctx context.Context, sender peer.ID, incoming bsmsg.BitSwapMessage) (
		destination peer.ID, outgoing bsmsg.BitSwapMessage)
40 41

	ReceiveError(error)
42 43 44 45

	// Connected/Disconnected warns bitswap about peer connections
	PeerConnected(peer.ID)
	PeerDisconnected(peer.ID)
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
}