github.com/keltia/go-ipfs@v0.3.8-0.20150909044612-210793031c63/routing/mock/dht.go (about)

     1  package mockrouting
     2  
     3  import (
     4  	ds "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
     5  	sync "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync"
     6  	context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
     7  	mocknet "github.com/ipfs/go-ipfs/p2p/net/mock"
     8  	dht "github.com/ipfs/go-ipfs/routing/dht"
     9  	"github.com/ipfs/go-ipfs/util/testutil"
    10  )
    11  
    12  type mocknetserver struct {
    13  	mn mocknet.Mocknet
    14  }
    15  
    16  func NewDHTNetwork(mn mocknet.Mocknet) Server {
    17  	return &mocknetserver{
    18  		mn: mn,
    19  	}
    20  }
    21  
    22  func (rs *mocknetserver) Client(p testutil.Identity) Client {
    23  	return rs.ClientWithDatastore(context.TODO(), p, ds.NewMapDatastore())
    24  }
    25  
    26  func (rs *mocknetserver) ClientWithDatastore(ctx context.Context, p testutil.Identity, ds ds.Datastore) Client {
    27  
    28  	// FIXME AddPeer doesn't appear to be idempotent
    29  
    30  	host, err := rs.mn.AddPeer(p.PrivateKey(), p.Address())
    31  	if err != nil {
    32  		panic("FIXME")
    33  	}
    34  	return dht.NewDHT(ctx, host, sync.MutexWrap(ds))
    35  }
    36  
    37  var _ Server = &mocknetserver{}