virtual.go 4.57 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

12 13
	cid "gx/ipfs/QmNp85zy9RLrQ5oQD4hPyS39ezrrXpcaa7R4Y9kxdWQLLQ/go-cid"
	routing "gx/ipfs/QmPR2JzfKd9poHx9XBhzoFeBBC31ZM3W5iUPKJZWyaoZZm/go-libp2p-routing"
Steven Allen's avatar
Steven Allen committed
14
	testutil "gx/ipfs/QmQgLZP9haZheimMHqqAjJh2LhRmNfEoZDfbtkpeMhi9xK/go-testutil"
15
	logging "gx/ipfs/QmSpJByNKFX1sCsHBEp3R73FL4NF6FnQTEGyNAXHm2GS52/go-log"
Steven Allen's avatar
Steven Allen committed
16
	ifconnmgr "gx/ipfs/QmWfkNorhirGE1Qp3VwBWcnGaj4adv4hNqCYwabMrEYc21/go-libp2p-interface-connmgr"
17
	peer "gx/ipfs/QmXYjuNuxVzXKJCfWasQk1RqkhVLDM9jtUKhqc2WPQmFSB/go-libp2p-peer"
Brian Tiger Chow's avatar
Brian Tiger Chow committed
18 19
)

20 21
var log = logging.Logger("bstestnet")

22
func VirtualNetwork(rs mockrouting.Server, d delay.D) Network {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
23
	return &network{
24 25
		clients:       make(map[peer.ID]bsnet.Receiver),
		delay:         d,
26
		routingserver: rs,
27
		conns:         make(map[string]struct{}),
Brian Tiger Chow's avatar
Brian Tiger Chow committed
28 29 30 31
	}
}

type network struct {
32 33 34
	clients       map[peer.ID]bsnet.Receiver
	routingserver mockrouting.Server
	delay         delay.D
35
	conns         map[string]struct{}
Brian Tiger Chow's avatar
Brian Tiger Chow committed
36 37
}

38
func (n *network) Adapter(p testutil.Identity) bsnet.BitSwapNetwork {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
39
	client := &networkClient{
40
		local:   p.ID(),
41
		network: n,
Brian Tiger Chow's avatar
Brian Tiger Chow committed
42
		routing: n.routingserver.Client(p),
Brian Tiger Chow's avatar
Brian Tiger Chow committed
43
	}
44
	n.clients[p.ID()] = client
Brian Tiger Chow's avatar
Brian Tiger Chow committed
45 46 47
	return client
}

48 49
func (n *network) HasPeer(p peer.ID) bool {
	_, found := n.clients[p]
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
50 51 52
	return found
}

Brian Tiger Chow's avatar
Brian Tiger Chow committed
53 54 55 56
// 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,
57 58
	from peer.ID,
	to peer.ID,
Brian Tiger Chow's avatar
Brian Tiger Chow committed
59 60
	message bsmsg.BitSwapMessage) error {

61
	receiver, ok := n.clients[to]
Brian Tiger Chow's avatar
Brian Tiger Chow committed
62 63 64 65 66 67 68 69 70 71 72 73 74
	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(
75 76
	r bsnet.Receiver, from peer.ID, message bsmsg.BitSwapMessage) error {
	if message == nil || from == "" {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
77 78 79
		return errors.New("Invalid input")
	}

80 81
	n.delay.Wait()

82 83
	r.ReceiveMessage(context.TODO(), from, message)
	return nil
Brian Tiger Chow's avatar
Brian Tiger Chow committed
84 85 86
}

type networkClient struct {
87
	local peer.ID
Brian Tiger Chow's avatar
Brian Tiger Chow committed
88
	bsnet.Receiver
89
	network *network
90
	routing routing.IpfsRouting
Brian Tiger Chow's avatar
Brian Tiger Chow committed
91 92 93 94
}

func (nc *networkClient) SendMessage(
	ctx context.Context,
95
	to peer.ID,
Brian Tiger Chow's avatar
Brian Tiger Chow committed
96 97 98 99
	message bsmsg.BitSwapMessage) error {
	return nc.network.SendMessage(ctx, nc.local, to, message)
}

100
// FindProvidersAsync returns a channel of providers for the given key
101
func (nc *networkClient) FindProvidersAsync(ctx context.Context, k *cid.Cid, max int) <-chan peer.ID {
102 103 104 105 106 107 108 109 110

	// 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.

	out := make(chan peer.ID)
	go func() {
		defer close(out)
111
		providers := nc.routing.FindProvidersAsync(ctx, k, max)
112 113 114 115 116 117 118 119
		for info := range providers {
			select {
			case <-ctx.Done():
			case out <- info.ID:
			}
		}
	}()
	return out
120 121
}

122 123 124 125
func (nc *networkClient) ConnectionManager() ifconnmgr.ConnManager {
	return &ifconnmgr.NullConnMgr{}
}

Jeromy's avatar
Jeromy committed
126 127 128 129 130 131 132
type messagePasser struct {
	net    *network
	target peer.ID
	local  peer.ID
	ctx    context.Context
}

133 134
func (mp *messagePasser) SendMsg(ctx context.Context, m bsmsg.BitSwapMessage) error {
	return mp.net.SendMessage(ctx, mp.local, mp.target, m)
Jeromy's avatar
Jeromy committed
135 136 137 138 139 140
}

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

141 142 143 144
func (mp *messagePasser) Reset() error {
	return nil
}

Jeromy's avatar
Jeromy committed
145 146 147 148 149 150 151 152 153
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
}

154
// Provide provides the key to the network
155
func (nc *networkClient) Provide(ctx context.Context, k *cid.Cid) error {
156
	return nc.routing.Provide(ctx, k, true)
157 158
}

Brian Tiger Chow's avatar
Brian Tiger Chow committed
159 160 161
func (nc *networkClient) SetDelegate(r bsnet.Receiver) {
	nc.Receiver = r
}
162 163 164 165 166

func (nc *networkClient) ConnectTo(_ context.Context, p peer.ID) error {
	if !nc.network.HasPeer(p) {
		return errors.New("no such peer in network")
	}
167 168 169 170 171 172 173 174
	tag := tagForPeers(nc.local, p)
	if _, ok := nc.network.conns[tag]; ok {
		log.Warning("ALREADY CONNECTED TO PEER (is this a reconnect? test lib needs fixing)")
		return nil
	}
	nc.network.conns[tag] = struct{}{}
	// TODO: add handling for disconnects

175 176 177 178
	nc.network.clients[p].PeerConnected(nc.local)
	nc.Receiver.PeerConnected(p)
	return nil
}
179 180 181 182 183 184 185

func tagForPeers(a, b peer.ID) string {
	if a < b {
		return string(a + b)
	}
	return string(b + a)
}