interface.go 1.65 KB
Newer Older
Brian Tiger Chow's avatar
Brian Tiger Chow committed
1 2 3 4 5 6 7
// Package mock provides a virtual routing server. To use it, create a virtual
// routing server and use the Client() method to get a routing client
// (IpfsRouting). The server quacks like a DHT but is really a local in-memory
// hash table.
package mockrouting

import (
Jeromy's avatar
Jeromy committed
8
	ds "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/ipfs/go-datastore"
9
	context "gx/ipfs/QmZy2y8t9zQH2a1b8q2ZSLKp17ATuJoCNxxyMFG5qFExpt/go-net/context"
10
	key "github.com/ipfs/go-ipfs/blocks/key"
11 12 13
	routing "github.com/ipfs/go-ipfs/routing"
	delay "github.com/ipfs/go-ipfs/thirdparty/delay"
	"github.com/ipfs/go-ipfs/util/testutil"
14
	peer "gx/ipfs/QmY3NAw959vbE1oJooP9HchcRdBsbxhgQsEZTRhKgvoSuC/go-libp2p/p2p/peer"
Brian Tiger Chow's avatar
Brian Tiger Chow committed
15 16 17 18
)

// Server provides mockrouting Clients
type Server interface {
19 20
	Client(p testutil.Identity) Client
	ClientWithDatastore(context.Context, testutil.Identity, ds.Datastore) Client
Brian Tiger Chow's avatar
Brian Tiger Chow committed
21 22 23 24
}

// Client implements IpfsRouting
type Client interface {
25
	FindProviders(context.Context, key.Key) ([]peer.PeerInfo, error)
Brian Tiger Chow's avatar
Brian Tiger Chow committed
26 27 28 29 30
	routing.IpfsRouting
}

// NewServer returns a mockrouting Server
func NewServer() Server {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
31 32 33 34
	return NewServerWithDelay(DelayConfig{
		ValueVisibility: delay.Fixed(0),
		Query:           delay.Fixed(0),
	})
Brian Tiger Chow's avatar
Brian Tiger Chow committed
35 36 37
}

// NewServerWithDelay returns a mockrouting Server with a delay!
Brian Tiger Chow's avatar
Brian Tiger Chow committed
38
func NewServerWithDelay(conf DelayConfig) Server {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
39
	return &s{
40
		providers: make(map[key.Key]map[peer.ID]providerRecord),
Brian Tiger Chow's avatar
Brian Tiger Chow committed
41
		delayConf: conf,
Brian Tiger Chow's avatar
Brian Tiger Chow committed
42 43
	}
}
Brian Tiger Chow's avatar
Brian Tiger Chow committed
44 45 46 47 48 49 50 51 52

type DelayConfig struct {
	// ValueVisibility is the time it takes for a value to be visible in the network
	// FIXME there _must_ be a better term for this
	ValueVisibility delay.D

	// Query is the time it takes to receive a response from a routing query
	Query delay.D
}