interface.go 1.4 KB
Newer Older
1 2 3 4 5 6 7
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"
	peer "github.com/jbenet/go-ipfs/peer"
8
	u "github.com/jbenet/go-ipfs/util"
9 10
)

11 12
// BitSwapNetwork provides network connectivity for BitSwap sessions
type BitSwapNetwork interface {
13

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
14
	// DialPeer ensures there is a connection to peer.
15
	DialPeer(context.Context, peer.ID) error
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
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

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

29 30
	Peerstore() peer.Peerstore

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

	Routing
36 37
}

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

	ReceiveError(error)
45 46
}

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

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