interface.go 1.8 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 (
8
	delay "github.com/ipfs/go-ipfs/thirdparty/delay"
9
	"github.com/ipfs/go-ipfs/thirdparty/testutil"
10

11
	peer "gx/ipfs/QmWXjJo15p4pzT7cayEwZi2sWgJqLnGDof6ZGMh9xBgU1p/go-libp2p-peer"
Jeromy's avatar
Jeromy committed
12
	pstore "gx/ipfs/QmYkwVGkwoPbMVQEbf6LonZg4SsCxGP3H7PBEtdNCNRyxD/go-libp2p-peerstore"
Jeromy's avatar
Jeromy committed
13
	context "gx/ipfs/QmZy2y8t9zQH2a1b8q2ZSLKp17ATuJoCNxxyMFG5qFExpt/go-net/context"
George Antoniadis's avatar
George Antoniadis committed
14 15
	ds "gx/ipfs/QmbzuUusHqaLLoNTDEVLcSF6vZDHZDLPC7p4bztRvvkXxU/go-datastore"
	key "gx/ipfs/Qmce4Y4zg3sYr7xKM5UueS67vhNni6EeWgCRnb7MbLJMew/go-key"
Jeromy's avatar
Jeromy committed
16
	routing "gx/ipfs/QmemZcG8WprPbnVX3AM43GhhSUiA3V6NjcTLAguvWzkdpQ/go-libp2p-routing"
Brian Tiger Chow's avatar
Brian Tiger Chow committed
17 18 19 20
)

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

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

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

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

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
}