interface.go 1.49 KB
Newer Older
1 2 3 4
// Package mockrouting 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.
Brian Tiger Chow's avatar
Brian Tiger Chow committed
5 6 7
package mockrouting

import (
8 9
	"context"

10 11 12 13 14
	ds "github.com/ipfs/go-datastore"
	delay "github.com/ipfs/go-ipfs-delay"
	peer "github.com/libp2p/go-libp2p-peer"
	routing "github.com/libp2p/go-libp2p-routing"
	"github.com/libp2p/go-testutil"
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 25 26 27 28 29
}

// Client implements IpfsRouting
type Client interface {
	routing.IpfsRouting
}

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

// NewServerWithDelay returns a mockrouting Server with a delay!
Brian Tiger Chow's avatar
Brian Tiger Chow committed
37
func NewServerWithDelay(conf DelayConfig) Server {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
38
	return &s{
39
		providers: make(map[string]map[peer.ID]providerRecord),
Brian Tiger Chow's avatar
Brian Tiger Chow committed
40
		delayConf: conf,
Brian Tiger Chow's avatar
Brian Tiger Chow committed
41 42
	}
}
Brian Tiger Chow's avatar
Brian Tiger Chow committed
43

44 45
// DelayConfig can be used to configured the fake delays of a mock server.
// Use with NewServerWithDelay().
Brian Tiger Chow's avatar
Brian Tiger Chow committed
46 47 48 49 50 51 52 53
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
}