centralized_client.go 2.25 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

tavit ohanian's avatar
tavit ohanian committed
7 8
	cid "gitlab.dms3.io/dms3/public/go-cid"
	logging "gitlab.dms3.io/dms3/public/go-log"
Raúl Kripalani's avatar
Raúl Kripalani committed
9

tavit ohanian's avatar
tavit ohanian committed
10 11 12
	"gitlab.dms3.io/p2p/go-p2p-core/peer"
	"gitlab.dms3.io/p2p/go-p2p-core/routing"
	tnet "gitlab.dms3.io/p2p/go-p2p-testing/net"
Raúl Kripalani's avatar
Raúl Kripalani committed
13

14
	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
	vs     routing.ValueStore
	server server
Raúl Kripalani's avatar
Raúl Kripalani committed
22
	peer   tnet.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?
Raúl Kripalani's avatar
Raúl Kripalani committed
26
func (c *client) PutValue(ctx context.Context, key string, val []byte, opts ...routing.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?
Raúl Kripalani's avatar
Raúl Kripalani committed
32
func (c *client) GetValue(ctx context.Context, key string, opts ...routing.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
}

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

Raúl Kripalani's avatar
Raúl Kripalani committed
42
func (c *client) FindProviders(ctx context.Context, key cid.Cid) ([]peer.AddrInfo, error) {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
43 44 45
	return c.server.Providers(key), nil
}

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

Raúl Kripalani's avatar
Raúl Kripalani committed
51 52
func (c *client) FindProvidersAsync(ctx context.Context, k cid.Cid, max int) <-chan peer.AddrInfo {
	out := make(chan peer.AddrInfo)
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
	}
Raúl Kripalani's avatar
Raúl Kripalani committed
75
	info := peer.AddrInfo{
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
}

Raúl Kripalani's avatar
Raúl Kripalani committed
90
var _ routing.Routing = &client{}