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

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

8
	dshelp "github.com/ipfs/go-ipfs/thirdparty/ds-help"
Steven Allen's avatar
Steven Allen committed
9
	"gx/ipfs/QmfB65MYJqaKzBiMvW47fquCRhmEeXW6AhrJSGM7TeY5eG/go-testutil"
Jeromy's avatar
Jeromy committed
10

Steven Allen's avatar
Steven Allen committed
11 12 13
	u "gx/ipfs/QmNiJuT8Ja3hMVpBHXv3Q6dwmperaQ6JjLtpMQgMCD7xvx/go-ipfs-util"
	ds "gx/ipfs/QmPpegoMqhAEqjncrzArm7KVWAkCm78rqL2DPuNjhPrshg/go-datastore"
	routing "gx/ipfs/QmRijoA6zGS98ELTDbGsLWPZbVotYsGbjp3RbXcKCYBeon/go-libp2p-routing"
Jeromy's avatar
Jeromy committed
14
	logging "gx/ipfs/QmSpJByNKFX1sCsHBEp3R73FL4NF6FnQTEGyNAXHm2GS52/go-log"
Steven Allen's avatar
Steven Allen committed
15
	ma "gx/ipfs/QmWWQ2Txc2c6tqjsBpzg5Ar652cHPGNsQQp2SejkNmkUMb/go-multiaddr"
16
	proto "gx/ipfs/QmZ4Qi3GaRbjcx28Sme5eMH7RQjGkt8wHxt2a65oLaeFEV/gogo-protobuf/proto"
Steven Allen's avatar
Steven Allen committed
17 18 19 20
	peer "gx/ipfs/Qma7H6RW8wRrfZpNSXwxYGcd1E149s42FpWNpDNieSVrnU/go-libp2p-peer"
	dhtpb "gx/ipfs/QmbsY8Pr6s3uZsKg7rzBtGDKeCtdoAhNaMTCXBUbvb1eCV/go-libp2p-record/pb"
	cid "gx/ipfs/QmcZfnkapfECQGcLZaf9B79NRg7cRa9EnZh4LSbkCzwNvY/go-cid"
	pstore "gx/ipfs/QmeZVQzUrXqaszo24DAoHfGzcmCptN9JyngLkGAiEfk2x7/go-libp2p-peerstore"
Brian Tiger Chow's avatar
Brian Tiger Chow committed
21 22
)

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

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

// FIXME(brian): is this method meant to simulate putting a value into the network?
32
func (c *client) PutValue(ctx context.Context, key string, val []byte) error {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
33
	log.Debugf("PutValue: %s", key)
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
	}

43
	return c.datastore.Put(dshelp.NewKeyFromBinary([]byte(key)), data)
Brian Tiger Chow's avatar
Brian Tiger Chow committed
44 45 46
}

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

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

59 60 61 62 63 64 65
	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
66 67
}

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

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

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

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

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

118 119
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
120 121
}

122 123 124 125
func (c *client) Bootstrap(context.Context) error {
	return nil
}

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