network_adapter.go 2.14 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
package network

import (
	"errors"

	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"
	netmsg "github.com/jbenet/go-ipfs/net/message"
	peer "github.com/jbenet/go-ipfs/peer"
)

// NewSender wraps a network Service to perform translation between
// BitSwapMessage and NetMessage formats. This allows the BitSwap session to
// ignore these details.
func NewNetworkAdapter(s NetworkService, r Receiver) NetworkAdapter {
	adapter := networkAdapter{
		networkService: s,
		receiver:       r,
	}
	s.SetHandler(&adapter)
	return &adapter
}

// networkAdapter implements NetworkAdapter
type networkAdapter struct {
	networkService NetworkService
	receiver       Receiver
}

// HandleMessage marshals and unmarshals net messages, forwarding them to the
// BitSwapMessage receiver
func (adapter *networkAdapter) HandleMessage(
	ctx context.Context, incoming netmsg.NetMessage) (netmsg.NetMessage, error) {

	if adapter.receiver == nil {
		return nil, errors.New("No receiver. NetMessage dropped")
	}

	received, err := bsmsg.FromNet(incoming)
	if err != nil {
		return nil, err
	}

	p, bsmsg, err := adapter.receiver.ReceiveMessage(ctx, incoming.Peer(), received)
	if err != nil {
		return nil, err
	}

	// TODO(brian): put this in a helper function
	if bsmsg == nil || p == nil {
		return nil, nil
	}

	outgoing, err := bsmsg.ToNet(p)
	if err != nil {
		return nil, err
	}

	return outgoing, nil
}

func (adapter *networkAdapter) SendMessage(
	ctx context.Context,
	p *peer.Peer,
	outgoing bsmsg.BitSwapMessage) error {

	nmsg, err := outgoing.ToNet(p)
	if err != nil {
		return err
	}
	return adapter.networkService.SendMessage(ctx, nmsg)
}

func (adapter *networkAdapter) SendRequest(
	ctx context.Context,
	p *peer.Peer,
	outgoing bsmsg.BitSwapMessage) (bsmsg.BitSwapMessage, error) {

	outgoingMsg, err := outgoing.ToNet(p)
	if err != nil {
		return nil, err
	}
	incomingMsg, err := adapter.networkService.SendRequest(ctx, outgoingMsg)
	if err != nil {
		return nil, err
	}
	return bsmsg.FromNet(incomingMsg)
}

func (adapter *networkAdapter) SetDelegate(r Receiver) {
	adapter.receiver = r
}