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

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

7
	"github.com/ipfs/go-ipfs/thirdparty/testutil"
Jeromy's avatar
Jeromy committed
8

Jeromy's avatar
Jeromy committed
9
	logging "gx/ipfs/QmSpJByNKFX1sCsHBEp3R73FL4NF6FnQTEGyNAXHm2GS52/go-log"
10
	peer "gx/ipfs/QmWXjJo15p4pzT7cayEwZi2sWgJqLnGDof6ZGMh9xBgU1p/go-libp2p-peer"
11
	ma "gx/ipfs/QmYzDkkgAEmrcNzFCiYo6L1dTX4EAG1gZkbtdbd9trL4vd/go-multiaddr"
12 13
	proto "gx/ipfs/QmZ4Qi3GaRbjcx28Sme5eMH7RQjGkt8wHxt2a65oLaeFEV/gogo-protobuf/proto"
	u "gx/ipfs/QmZNVWh8LLjAavuQ2JXuFmuYH3C11xo988vSgp7UQrTRj1/go-ipfs-util"
Jeromy's avatar
Jeromy committed
14
	context "gx/ipfs/QmZy2y8t9zQH2a1b8q2ZSLKp17ATuJoCNxxyMFG5qFExpt/go-net/context"
15 16 17
	ds "gx/ipfs/QmbzuUusHqaLLoNTDEVLcSF6vZDHZDLPC7p4bztRvvkXxU/go-datastore"
	key "gx/ipfs/Qmce4Y4zg3sYr7xKM5UueS67vhNni6EeWgCRnb7MbLJMew/go-key"
	routing "gx/ipfs/QmcoQiBzRaaVv1DZbbXoDWiEtvDN94Ca1DcwnQKK2tP92s/go-libp2p-routing"
18
	pstore "gx/ipfs/QmdMfSLMDBDYhtc4oF3NYGCZr5dy4wQb6Ji26N4D4mdxa2/go-libp2p-peerstore"
19
	dhtpb "gx/ipfs/Qme7D9iKHYxwq28p6PzCymywsYSRBx9uyGzW7qNB3s9VbC/go-libp2p-record/pb"
Brian Tiger Chow's avatar
Brian Tiger Chow committed
20 21
)

Jeromy's avatar
Jeromy committed
22
var log = logging.Logger("mockrouter")
Brian Tiger Chow's avatar
Brian Tiger Chow committed
23 24 25 26

type client struct {
	datastore ds.Datastore
	server    server
27
	peer      testutil.Identity
Brian Tiger Chow's avatar
Brian Tiger Chow committed
28 29 30
}

// FIXME(brian): is this method meant to simulate putting a value into the network?
31
func (c *client) PutValue(ctx context.Context, key key.Key, val []byte) error {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
32
	log.Debugf("PutValue: %s", key)
33 34 35 36 37 38 39 40 41 42
	rec := new(dhtpb.Record)
	rec.Value = val
	rec.Key = proto.String(string(key))
	rec.TimeReceived = proto.String(u.FormatRFC3339(time.Now()))
	data, err := proto.Marshal(rec)
	if err != nil {
		return err
	}

	return c.datastore.Put(key.DsKey(), data)
Brian Tiger Chow's avatar
Brian Tiger Chow committed
43 44 45
}

// FIXME(brian): is this method meant to simulate getting a value from the network?
46
func (c *client) GetValue(ctx context.Context, key key.Key) ([]byte, error) {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
47 48 49 50 51 52 53 54 55 56 57
	log.Debugf("GetValue: %s", key)
	v, err := c.datastore.Get(key.DsKey())
	if err != nil {
		return nil, err
	}

	data, ok := v.([]byte)
	if !ok {
		return nil, errors.New("could not cast value from datastore")
	}

58 59 60 61 62 63 64
	rec := new(dhtpb.Record)
	err = proto.Unmarshal(data, rec)
	if err != nil {
		return nil, err
	}

	return rec.GetValue(), nil
Brian Tiger Chow's avatar
Brian Tiger Chow committed
65 66
}

67
func (c *client) GetValues(ctx context.Context, key key.Key, count int) ([]routing.RecvdVal, error) {
68 69
	log.Debugf("GetValues: %s", key)
	data, err := c.GetValue(ctx, key)
70 71 72 73 74 75 76
	if err != nil {
		return nil, err
	}

	return []routing.RecvdVal{{Val: data, From: c.peer.ID()}}, nil
}

Jeromy's avatar
Jeromy committed
77
func (c *client) FindProviders(ctx context.Context, key key.Key) ([]pstore.PeerInfo, error) {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
78 79 80
	return c.server.Providers(key), nil
}

Jeromy's avatar
Jeromy committed
81
func (c *client) FindPeer(ctx context.Context, pid peer.ID) (pstore.PeerInfo, error) {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
82
	log.Debugf("FindPeer: %s", pid)
Jeromy's avatar
Jeromy committed
83
	return pstore.PeerInfo{}, nil
Brian Tiger Chow's avatar
Brian Tiger Chow committed
84 85
}

Jeromy's avatar
Jeromy committed
86 87
func (c *client) FindProvidersAsync(ctx context.Context, k key.Key, max int) <-chan pstore.PeerInfo {
	out := make(chan pstore.PeerInfo)
Brian Tiger Chow's avatar
Brian Tiger Chow committed
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
	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
104 105
// Provide returns once the message is on the network. Value is not necessarily
// visible yet.
106
func (c *client) Provide(_ context.Context, key key.Key) error {
Jeromy's avatar
Jeromy committed
107
	info := pstore.PeerInfo{
Brian Tiger Chow's avatar
Brian Tiger Chow committed
108 109 110 111
		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
112 113
}

114 115
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
116 117
}

118 119 120 121
func (c *client) Bootstrap(context.Context) error {
	return nil
}

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