virtual.go 3.87 KB
Newer Older
Brian Tiger Chow's avatar
Brian Tiger Chow committed
1 2 3
package bitswap

import (
4
	"context"
Brian Tiger Chow's avatar
Brian Tiger Chow committed
5 6
	"errors"

7 8 9 10
	bsmsg "github.com/ipfs/go-ipfs/exchange/bitswap/message"
	bsnet "github.com/ipfs/go-ipfs/exchange/bitswap/network"
	mockrouting "github.com/ipfs/go-ipfs/routing/mock"
	delay "github.com/ipfs/go-ipfs/thirdparty/delay"
11
	testutil "github.com/ipfs/go-ipfs/thirdparty/testutil"
12 13 14 15
	routing "gx/ipfs/QmXKuGUzLcgoQvp8M6ZEJzupWUNmx8NoqXEbYLMDjL4rjj/go-libp2p-routing"
	key "gx/ipfs/QmYEoKZXHoAToWfhGF3vryhMn3WWhE1o2MasQ8uzY5iDi9/go-key"
	cid "gx/ipfs/QmakyCk6Vnn16WEKjbkxieZmM2YLTzkFWizbmGowoYPjro/go-cid"
	peer "gx/ipfs/QmfMmLGoKzCHDN7cGgk64PJr4iipzidDRME8HABSJqvmhC/go-libp2p-peer"
Brian Tiger Chow's avatar
Brian Tiger Chow committed
16 17
)

18
func VirtualNetwork(rs mockrouting.Server, d delay.D) Network {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
19
	return &network{
20 21
		clients:       make(map[peer.ID]bsnet.Receiver),
		delay:         d,
22
		routingserver: rs,
Brian Tiger Chow's avatar
Brian Tiger Chow committed
23 24 25 26
	}
}

type network struct {
27 28 29
	clients       map[peer.ID]bsnet.Receiver
	routingserver mockrouting.Server
	delay         delay.D
Brian Tiger Chow's avatar
Brian Tiger Chow committed
30 31
}

32
func (n *network) Adapter(p testutil.Identity) bsnet.BitSwapNetwork {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
33
	client := &networkClient{
34
		local:   p.ID(),
35
		network: n,
Brian Tiger Chow's avatar
Brian Tiger Chow committed
36
		routing: n.routingserver.Client(p),
Brian Tiger Chow's avatar
Brian Tiger Chow committed
37
	}
38
	n.clients[p.ID()] = client
Brian Tiger Chow's avatar
Brian Tiger Chow committed
39 40 41
	return client
}

42 43
func (n *network) HasPeer(p peer.ID) bool {
	_, found := n.clients[p]
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
44 45 46
	return found
}

Brian Tiger Chow's avatar
Brian Tiger Chow committed
47 48 49 50
// TODO should this be completely asynchronous?
// TODO what does the network layer do with errors received from services?
func (n *network) SendMessage(
	ctx context.Context,
51 52
	from peer.ID,
	to peer.ID,
Brian Tiger Chow's avatar
Brian Tiger Chow committed
53 54
	message bsmsg.BitSwapMessage) error {

55
	receiver, ok := n.clients[to]
Brian Tiger Chow's avatar
Brian Tiger Chow committed
56 57 58 59 60 61 62 63 64 65 66 67 68
	if !ok {
		return errors.New("Cannot locate peer on network")
	}

	// nb: terminate the context since the context wouldn't actually be passed
	// over the network in a real scenario

	go n.deliver(receiver, from, message)

	return nil
}

func (n *network) deliver(
69 70
	r bsnet.Receiver, from peer.ID, message bsmsg.BitSwapMessage) error {
	if message == nil || from == "" {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
71 72 73
		return errors.New("Invalid input")
	}

74 75
	n.delay.Wait()

76 77
	r.ReceiveMessage(context.TODO(), from, message)
	return nil
Brian Tiger Chow's avatar
Brian Tiger Chow committed
78 79 80
}

type networkClient struct {
81
	local peer.ID
Brian Tiger Chow's avatar
Brian Tiger Chow committed
82
	bsnet.Receiver
83
	network *network
84
	routing routing.IpfsRouting
Brian Tiger Chow's avatar
Brian Tiger Chow committed
85 86 87 88
}

func (nc *networkClient) SendMessage(
	ctx context.Context,
89
	to peer.ID,
Brian Tiger Chow's avatar
Brian Tiger Chow committed
90 91 92 93
	message bsmsg.BitSwapMessage) error {
	return nc.network.SendMessage(ctx, nc.local, to, message)
}

94
// FindProvidersAsync returns a channel of providers for the given key
95
func (nc *networkClient) FindProvidersAsync(ctx context.Context, k key.Key, max int) <-chan peer.ID {
96 97 98 99 100 101

	// NB: this function duplicates the PeerInfo -> ID transformation in the
	// bitswap network adapter. Not to worry. This network client will be
	// deprecated once the ipfsnet.Mock is added. The code below is only
	// temporary.

102
	c := cid.NewCidV0(k.ToMultihash())
103 104 105
	out := make(chan peer.ID)
	go func() {
		defer close(out)
106
		providers := nc.routing.FindProvidersAsync(ctx, c, max)
107 108 109 110 111 112 113 114
		for info := range providers {
			select {
			case <-ctx.Done():
			case out <- info.ID:
			}
		}
	}()
	return out
115 116
}

Jeromy's avatar
Jeromy committed
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
type messagePasser struct {
	net    *network
	target peer.ID
	local  peer.ID
	ctx    context.Context
}

func (mp *messagePasser) SendMsg(m bsmsg.BitSwapMessage) error {
	return mp.net.SendMessage(mp.ctx, mp.local, mp.target, m)
}

func (mp *messagePasser) Close() error {
	return nil
}

func (n *networkClient) NewMessageSender(ctx context.Context, p peer.ID) (bsnet.MessageSender, error) {
	return &messagePasser{
		net:    n.network,
		target: p,
		local:  n.local,
		ctx:    ctx,
	}, nil
}

141
// Provide provides the key to the network
142
func (nc *networkClient) Provide(ctx context.Context, k key.Key) error {
143 144
	c := cid.NewCidV0(k.ToMultihash())
	return nc.routing.Provide(ctx, c)
145 146
}

Brian Tiger Chow's avatar
Brian Tiger Chow committed
147 148 149
func (nc *networkClient) SetDelegate(r bsnet.Receiver) {
	nc.Receiver = r
}
150 151 152 153 154 155 156 157 158

func (nc *networkClient) ConnectTo(_ context.Context, p peer.ID) error {
	if !nc.network.HasPeer(p) {
		return errors.New("no such peer in network")
	}
	nc.network.clients[p].PeerConnected(nc.local)
	nc.Receiver.PeerConnected(p)
	return nil
}