peernet.go 1.21 KB
Newer Older
1 2 3
package bitswap

import (
Jeromy's avatar
Jeromy committed
4
	ds "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/ipfs/go-datastore"
5 6
	bsnet "github.com/ipfs/go-ipfs/exchange/bitswap/network"
	mockrouting "github.com/ipfs/go-ipfs/routing/mock"
7
	testutil "github.com/ipfs/go-ipfs/thirdparty/testutil"
Jeromy's avatar
Jeromy committed
8
	mockpeernet "gx/ipfs/QmXDvxcXUYn2DDnGKJwdQPxkJgG83jBTp5UmmNzeHzqbj5/go-libp2p/p2p/net/mock"
9
	peer "gx/ipfs/QmZwZjMVGss5rqYsJVGy18gNbkTJffFyq2x1uJ4e4p3ZAt/go-libp2p-peer"
Jeromy's avatar
Jeromy committed
10
	context "gx/ipfs/QmZy2y8t9zQH2a1b8q2ZSLKp17ATuJoCNxxyMFG5qFExpt/go-net/context"
11 12 13 14 15 16 17
)

type peernet struct {
	mockpeernet.Mocknet
	routingserver mockrouting.Server
}

Brian Tiger Chow's avatar
Brian Tiger Chow committed
18
func StreamNet(ctx context.Context, net mockpeernet.Mocknet, rs mockrouting.Server) (Network, error) {
19 20 21
	return &peernet{net, rs}, nil
}

22
func (pn *peernet) Adapter(p testutil.Identity) bsnet.BitSwapNetwork {
23 24 25 26
	client, err := pn.Mocknet.AddPeer(p.PrivateKey(), p.Address())
	if err != nil {
		panic(err.Error())
	}
Brian Tiger Chow's avatar
Brian Tiger Chow committed
27
	routing := pn.routingserver.ClientWithDatastore(context.TODO(), p, ds.NewMapDatastore())
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
28
	return bsnet.NewFromIpfsHost(client, routing)
29 30 31 32 33 34 35 36 37 38 39 40
}

func (pn *peernet) HasPeer(p peer.ID) bool {
	for _, member := range pn.Mocknet.Peers() {
		if p == member {
			return true
		}
	}
	return false
}

var _ Network = &peernet{}