interface.go 1.28 KB
Newer Older
1
package network
2 3 4

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

7
	bsmsg "github.com/jbenet/go-ipfs/exchange/bitswap/message"
8
	netmsg "github.com/jbenet/go-ipfs/net/message"
9 10 11
	peer "github.com/jbenet/go-ipfs/peer"
)

12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
// NetworkAdapter mediates the exchange's communication with the network.
type NetworkAdapter interface {

	// SendMessage sends a BitSwap message to a peer.
	SendMessage(
		context.Context,
		*peer.Peer,
		bsmsg.BitSwapMessage) error

	// SendRequest sends a BitSwap message to a peer and waits for a response.
	SendRequest(
		context.Context,
		*peer.Peer,
		bsmsg.BitSwapMessage) (incoming bsmsg.BitSwapMessage, err error)

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

type Receiver interface {
33 34
	ReceiveMessage(
		ctx context.Context, sender *peer.Peer, incoming bsmsg.BitSwapMessage) (
35
		destination *peer.Peer, outgoing bsmsg.BitSwapMessage, err error)
36
}
37 38 39 40 41 42 43

// TODO(brian): move this to go-ipfs/net package
type NetworkService interface {
	SendRequest(ctx context.Context, m netmsg.NetMessage) (netmsg.NetMessage, error)
	SendMessage(ctx context.Context, m netmsg.NetMessage) error
	SetHandler(netservice.Handler)
}