offline.go 3.03 KB
Newer Older
Jeromy's avatar
Jeromy committed
1 2 3 4 5 6
package offline

import (
	"errors"
	"time"

7
	proto "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/gogo/protobuf/proto"
8 9
	ds "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
	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 14 15
	ci "github.com/ipfs/go-ipfs/p2p/crypto"
	"github.com/ipfs/go-ipfs/p2p/peer"
	routing "github.com/ipfs/go-ipfs/routing"
	pb "github.com/ipfs/go-ipfs/routing/dht/pb"
	record "github.com/ipfs/go-ipfs/routing/record"
Jeromy's avatar
Jeromy committed
16
	logging "github.com/ipfs/go-ipfs/vendor/QmQg1J6vikuXF9oDvm4wpdeAUvvkVEKW1EYDw9HhTMnP2b/go-log"
Jeromy's avatar
Jeromy committed
17 18
)

Jeromy's avatar
Jeromy committed
19
var log = logging.Logger("offlinerouting")
Jeromy's avatar
Jeromy committed
20 21 22 23 24 25 26 27 28 29

var ErrOffline = errors.New("routing system in offline mode")

func NewOfflineRouter(dstore ds.Datastore, privkey ci.PrivKey) routing.IpfsRouting {
	return &offlineRouting{
		datastore: dstore,
		sk:        privkey,
	}
}

Jeromy's avatar
Jeromy committed
30 31 32
// offlineRouting implements the IpfsRouting interface,
// but only provides the capability to Put and Get signed dht
// records to and from the local datastore.
Jeromy's avatar
Jeromy committed
33 34 35 36 37
type offlineRouting struct {
	datastore ds.Datastore
	sk        ci.PrivKey
}

38
func (c *offlineRouting) PutValue(ctx context.Context, key key.Key, val []byte) error {
39
	rec, err := record.MakePutRecord(c.sk, key, val, false)
Jeromy's avatar
Jeromy committed
40 41 42 43 44 45 46 47 48 49 50
	if err != nil {
		return err
	}
	data, err := proto.Marshal(rec)
	if err != nil {
		return err
	}

	return c.datastore.Put(key.DsKey(), data)
}

51
func (c *offlineRouting) GetValue(ctx context.Context, key key.Key) ([]byte, error) {
Jeromy's avatar
Jeromy committed
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
	v, err := c.datastore.Get(key.DsKey())
	if err != nil {
		return nil, err
	}

	byt, ok := v.([]byte)
	if !ok {
		return nil, errors.New("value stored in datastore not []byte")
	}
	rec := new(pb.Record)
	err = proto.Unmarshal(byt, rec)
	if err != nil {
		return nil, err
	}

	return rec.GetValue(), nil
}

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
func (c *offlineRouting) GetValues(ctx context.Context, key key.Key, _ int) ([]routing.RecvdVal, error) {
	v, err := c.datastore.Get(key.DsKey())
	if err != nil {
		return nil, err
	}

	byt, ok := v.([]byte)
	if !ok {
		return nil, errors.New("value stored in datastore not []byte")
	}
	rec := new(pb.Record)
	err = proto.Unmarshal(byt, rec)
	if err != nil {
		return nil, err
	}

	return []routing.RecvdVal{
		{Val: rec.GetValue()},
	}, nil
}

91
func (c *offlineRouting) FindProviders(ctx context.Context, key key.Key) ([]peer.PeerInfo, error) {
Jeromy's avatar
Jeromy committed
92 93 94 95 96 97 98
	return nil, ErrOffline
}

func (c *offlineRouting) FindPeer(ctx context.Context, pid peer.ID) (peer.PeerInfo, error) {
	return peer.PeerInfo{}, ErrOffline
}

99
func (c *offlineRouting) FindProvidersAsync(ctx context.Context, k key.Key, max int) <-chan peer.PeerInfo {
Jeromy's avatar
Jeromy committed
100 101 102 103 104
	out := make(chan peer.PeerInfo)
	close(out)
	return out
}

105
func (c *offlineRouting) Provide(_ context.Context, key key.Key) error {
Jeromy's avatar
Jeromy committed
106 107 108 109 110 111 112
	return ErrOffline
}

func (c *offlineRouting) Ping(ctx context.Context, p peer.ID) (time.Duration, error) {
	return 0, ErrOffline
}

Jeromy's avatar
Jeromy committed
113
func (c *offlineRouting) Bootstrap(context.Context) error {
114 115 116
	return nil
}

Jeromy's avatar
Jeromy committed
117
// ensure offlineRouting matches the IpfsRouting interface
Jeromy's avatar
Jeromy committed
118
var _ routing.IpfsRouting = &offlineRouting{}