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

import (
4 5 6 7 8 9 10
	ds "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
	context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
	bsnet "github.com/ipfs/go-ipfs/exchange/bitswap/network"
	mockpeernet "github.com/ipfs/go-ipfs/p2p/net/mock"
	peer "github.com/ipfs/go-ipfs/p2p/peer"
	mockrouting "github.com/ipfs/go-ipfs/routing/mock"
	testutil "github.com/ipfs/go-ipfs/util/testutil"
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{}