centralized_client.go 2.73 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 8 9
	ds "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
	ma "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
	context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
10
	key "github.com/ipfs/go-ipfs/blocks/key"
11 12 13
	peer "github.com/ipfs/go-ipfs/p2p/peer"
	routing "github.com/ipfs/go-ipfs/routing"
	"github.com/ipfs/go-ipfs/util/testutil"
Jeromy's avatar
Jeromy committed
14
	logging "github.com/ipfs/go-ipfs/vendor/go-log-v1.0.0"
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 20 21

type client struct {
	datastore ds.Datastore
	server    server
22
	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 key.Key, val []byte) error {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
27 28 29 30 31
	log.Debugf("PutValue: %s", key)
	return c.datastore.Put(key.DsKey(), val)
}

// FIXME(brian): is this method meant to simulate getting a value from the network?
32
func (c *client) GetValue(ctx context.Context, key key.Key) ([]byte, error) {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
33 34 35 36 37 38 39 40 41 42 43 44 45 46
	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")
	}

	return data, nil
}

47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
func (c *client) GetValues(ctx context.Context, key key.Key, count int) ([]routing.RecvdVal, error) {
	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")
	}

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

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

66
func (c *client) FindPeer(ctx context.Context, pid peer.ID) (peer.PeerInfo, error) {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
67
	log.Debugf("FindPeer: %s", pid)
68
	return peer.PeerInfo{}, nil
Brian Tiger Chow's avatar
Brian Tiger Chow committed
69 70
}

71
func (c *client) FindProvidersAsync(ctx context.Context, k key.Key, max int) <-chan peer.PeerInfo {
72
	out := make(chan peer.PeerInfo)
Brian Tiger Chow's avatar
Brian Tiger Chow committed
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
	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
89 90
// Provide returns once the message is on the network. Value is not necessarily
// visible yet.
91
func (c *client) Provide(_ context.Context, key key.Key) error {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
92 93 94 95 96
	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
97 98
}

99 100
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
101 102
}

103 104 105 106
func (c *client) Bootstrap(context.Context) error {
	return nil
}

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