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

	context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
	ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
Brian Tiger Chow's avatar
Brian Tiger Chow committed
9
	ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
10
	peer "github.com/jbenet/go-ipfs/p2p/peer"
Brian Tiger Chow's avatar
Brian Tiger Chow committed
11 12
	routing "github.com/jbenet/go-ipfs/routing"
	u "github.com/jbenet/go-ipfs/util"
Brian Tiger Chow's avatar
Brian Tiger Chow committed
13
	"github.com/jbenet/go-ipfs/util/testutil"
Brian Tiger Chow's avatar
Brian Tiger Chow committed
14 15 16 17 18 19 20
)

var log = u.Logger("mockrouter")

type client struct {
	datastore ds.Datastore
	server    server
21
	peer      testutil.Identity
Brian Tiger Chow's avatar
Brian Tiger Chow committed
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
}

// FIXME(brian): is this method meant to simulate putting a value into the network?
func (c *client) PutValue(ctx context.Context, key u.Key, val []byte) error {
	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?
func (c *client) GetValue(ctx context.Context, key u.Key) ([]byte, 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 data, nil
}

46
func (c *client) FindProviders(ctx context.Context, key u.Key) ([]peer.PeerInfo, error) {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
47 48 49
	return c.server.Providers(key), nil
}

50
func (c *client) FindPeer(ctx context.Context, pid peer.ID) (peer.PeerInfo, error) {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
51
	log.Debugf("FindPeer: %s", pid)
52
	return peer.PeerInfo{}, nil
Brian Tiger Chow's avatar
Brian Tiger Chow committed
53 54
}

55 56
func (c *client) FindProvidersAsync(ctx context.Context, k u.Key, max int) <-chan peer.PeerInfo {
	out := make(chan peer.PeerInfo)
Brian Tiger Chow's avatar
Brian Tiger Chow committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
	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
73 74
// Provide returns once the message is on the network. Value is not necessarily
// visible yet.
Brian Tiger Chow's avatar
Brian Tiger Chow committed
75
func (c *client) Provide(_ context.Context, key u.Key) error {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
76 77 78 79 80
	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
81 82
}

83 84
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
85 86
}

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