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

Jeromy's avatar
Jeromy committed
7
	ds "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/ipfs/go-datastore"
8
	key "github.com/ipfs/go-ipfs/blocks/key"
9
	routing "github.com/ipfs/go-ipfs/routing"
10
	dhtpb "github.com/ipfs/go-ipfs/routing/dht/pb"
11 12 13
	"github.com/ipfs/go-ipfs/thirdparty/testutil"
	proto "gx/ipfs/QmZ4Qi3GaRbjcx28Sme5eMH7RQjGkt8wHxt2a65oLaeFEV/gogo-protobuf/proto"
	u "gx/ipfs/QmZNVWh8LLjAavuQ2JXuFmuYH3C11xo988vSgp7UQrTRj1/go-ipfs-util"
14
	peer "gx/ipfs/QmZwZjMVGss5rqYsJVGy18gNbkTJffFyq2x1uJ4e4p3ZAt/go-libp2p-peer"
Jeromy's avatar
Jeromy committed
15
	context "gx/ipfs/QmZy2y8t9zQH2a1b8q2ZSLKp17ATuJoCNxxyMFG5qFExpt/go-net/context"
Jeromy's avatar
Jeromy committed
16
	logging "gx/ipfs/Qmazh5oNUVsDZTs2g59rq8aYQqwpss8tcUWQzor5sCCEuH/go-log"
Jeromy's avatar
Jeromy committed
17
	ma "gx/ipfs/QmcobAGsCjYt5DXoq9et9L8yR8er7o7Cu3DTvpaq12jYSz/go-multiaddr"
Brian Tiger Chow's avatar
Brian Tiger Chow committed
18 19
)

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

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

// FIXME(brian): is this method meant to simulate putting a value into the network?
29
func (c *client) PutValue(ctx context.Context, key key.Key, val []byte) error {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
30
	log.Debugf("PutValue: %s", key)
31 32 33 34 35 36 37 38 39 40
	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
41 42 43
}

// FIXME(brian): is this method meant to simulate getting a value from the network?
44
func (c *client) GetValue(ctx context.Context, key key.Key) ([]byte, error) {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
45 46 47 48 49 50 51 52 53 54 55
	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")
	}

56 57 58 59 60 61 62
	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
63 64
}

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

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

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

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

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

112 113
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
114 115
}

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

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