centralized_client.go 2.15 KB
Newer Older
Brian Tiger Chow's avatar
Brian Tiger Chow committed
1 2 3
package mockrouting

import (
4
	"context"
5
	"time"
Brian Tiger Chow's avatar
Brian Tiger Chow committed
6

7 8 9 10 11
	cid "github.com/ipfs/go-cid"
	logging "github.com/ipfs/go-log"
	peer "github.com/libp2p/go-libp2p-peer"
	pstore "github.com/libp2p/go-libp2p-peerstore"
	routing "github.com/libp2p/go-libp2p-routing"
12
	ropts "github.com/libp2p/go-libp2p-routing/options"
13 14
	"github.com/libp2p/go-testutil"
	ma "github.com/multiformats/go-multiaddr"
Brian Tiger Chow's avatar
Brian Tiger Chow committed
15 16
)

Jeromy's avatar
Jeromy committed
17
var log = logging.Logger("mockrouter")
Brian Tiger Chow's avatar
Brian Tiger Chow committed
18 19

type client struct {
Steven Allen's avatar
Steven Allen committed
20 21 22
	vs     routing.ValueStore
	server server
	peer   testutil.Identity
Brian Tiger Chow's avatar
Brian Tiger Chow committed
23 24 25
}

// FIXME(brian): is this method meant to simulate putting a value into the network?
26
func (c *client) PutValue(ctx context.Context, key string, val []byte, opts ...ropts.Option) error {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
27
	log.Debugf("PutValue: %s", key)
Steven Allen's avatar
Steven Allen committed
28
	return c.vs.PutValue(ctx, key, val, opts...)
Brian Tiger Chow's avatar
Brian Tiger Chow committed
29 30 31
}

// FIXME(brian): is this method meant to simulate getting a value from the network?
32
func (c *client) GetValue(ctx context.Context, key string, opts ...ropts.Option) ([]byte, error) {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
33
	log.Debugf("GetValue: %s", key)
Steven Allen's avatar
Steven Allen committed
34
	return c.vs.GetValue(ctx, key, opts...)
Brian Tiger Chow's avatar
Brian Tiger Chow committed
35 36
}

37
func (c *client) FindProviders(ctx context.Context, key cid.Cid) ([]pstore.PeerInfo, error) {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
38 39 40
	return c.server.Providers(key), nil
}

Jeromy's avatar
Jeromy committed
41
func (c *client) FindPeer(ctx context.Context, pid peer.ID) (pstore.PeerInfo, error) {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
42
	log.Debugf("FindPeer: %s", pid)
Jeromy's avatar
Jeromy committed
43
	return pstore.PeerInfo{}, nil
Brian Tiger Chow's avatar
Brian Tiger Chow committed
44 45
}

46
func (c *client) FindProvidersAsync(ctx context.Context, k cid.Cid, max int) <-chan pstore.PeerInfo {
Jeromy's avatar
Jeromy committed
47
	out := make(chan pstore.PeerInfo)
Brian Tiger Chow's avatar
Brian Tiger Chow committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
	go func() {
		defer close(out)
		for i, p := range c.server.Providers(k) {
			if max <= i {
				return
			}
			select {
			case out <- p:
			case <-ctx.Done():
				return
			}
		}
	}()
	return out
}

Brian Tiger Chow's avatar
Brian Tiger Chow committed
64 65
// Provide returns once the message is on the network. Value is not necessarily
// visible yet.
66
func (c *client) Provide(_ context.Context, key cid.Cid, brd bool) error {
67 68 69
	if !brd {
		return nil
	}
Jeromy's avatar
Jeromy committed
70
	info := pstore.PeerInfo{
Brian Tiger Chow's avatar
Brian Tiger Chow committed
71 72 73 74
		ID:    c.peer.ID(),
		Addrs: []ma.Multiaddr{c.peer.Address()},
	}
	return c.server.Announce(info, key)
Brian Tiger Chow's avatar
Brian Tiger Chow committed
75 76
}

77 78
func (c *client) Ping(ctx context.Context, p peer.ID) (time.Duration, error) {
	return 0, nil
Brian Tiger Chow's avatar
Brian Tiger Chow committed
79 80
}

81 82 83 84
func (c *client) Bootstrap(context.Context) error {
	return nil
}

Brian Tiger Chow's avatar
Brian Tiger Chow committed
85
var _ routing.IpfsRouting = &client{}