centralized_client.go 2.34 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
}

Łukasz Magiera's avatar
Łukasz Magiera committed
37 38 39 40 41
func (c *client) SearchValue(ctx context.Context, key string, opts ...ropts.Option) (<-chan []byte, error) {
	log.Debugf("SearchValue: %s", key)
	return c.vs.SearchValue(ctx, key, opts...)
}

Łukasz Magiera's avatar
Łukasz Magiera committed
42
func (c *client) FindProviders(ctx context.Context, key cid.Cid) ([]pstore.PeerInfo, error) {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
43 44 45
	return c.server.Providers(key), nil
}

Jeromy's avatar
Jeromy committed
46
func (c *client) FindPeer(ctx context.Context, pid peer.ID) (pstore.PeerInfo, error) {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
47
	log.Debugf("FindPeer: %s", pid)
Jeromy's avatar
Jeromy committed
48
	return pstore.PeerInfo{}, nil
Brian Tiger Chow's avatar
Brian Tiger Chow committed
49 50
}

51
func (c *client) FindProvidersAsync(ctx context.Context, k cid.Cid, max int) <-chan pstore.PeerInfo {
Jeromy's avatar
Jeromy committed
52
	out := make(chan pstore.PeerInfo)
Brian Tiger Chow's avatar
Brian Tiger Chow committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
	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
69 70
// Provide returns once the message is on the network. Value is not necessarily
// visible yet.
71
func (c *client) Provide(_ context.Context, key cid.Cid, brd bool) error {
72 73 74
	if !brd {
		return nil
	}
Jeromy's avatar
Jeromy committed
75
	info := pstore.PeerInfo{
Brian Tiger Chow's avatar
Brian Tiger Chow committed
76 77 78 79
		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
80 81
}

82 83
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
84 85
}

86 87 88 89
func (c *client) Bootstrap(context.Context) error {
	return nil
}

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