@cryptotaxi247 / kubo / commits / bc2618df3

dht interface beginnings

Juan Batiz-Benet committed Jul 23, 2014 at 04:48 UTC bc2618df3fc3fa52f198e12270b8db1a340f43a6
4 files changed +115 -111
dht/dht.go deleted
-111
@@ -1,111 +0,0 @@
1 -package dht
2 -
3 -import (
4 - "time"
5 - mh "github.com/jbenet/go-multihash"
6 - peer "github.com/jbenet/go-ipfs/peer"
7 - "errors"
8 - "net"
9 -)
10 -
11 -var NotFound = errors.New("Not Found")
12 -var NotAvailable = errors.New("Not Available")
13 -var TimeoutExceeded = errors.New("Timeout Exceeded")
14 -
15 -// The IPFS DHT is an implementation of Kademlia with
16 -// Coral and S/Kademlia modifications. It is used to
17 -// implement the base IPFS Routing module.
18 -
19 -// TODO. SEE https://github.com/jbenet/node-ipfs/blob/master/submodules/ipfs-dht/index.js
20 -type DHT struct {
21 - //Network
22 - Network net.Conn
23 -
24 - // DHT Configuration Settings
25 - Config DHTConfig
26 -
27 - //Republish
28 - Republish *DHTRepublish
29 -}
30 -
31 -// TODO: not call this republish
32 -type DHTRepublish struct {
33 - Strict []*DHTObject
34 - Sloppy []*DHTObject
35 -}
36 -
37 -type DHTObject struct {
38 - Key string
39 - Value *DHTValue
40 - LastPublished *time.Time
41 -}
42 -
43 -func (o *DHTObject) ShouldRepublish(interval time.Duration) bool {
44 - return (time.Now().Second() - o.LastPublished.Second()) > int(interval.Seconds())
45 -}
46 -
47 -// A struct representing a value in the DHT
48 -type DHTValue struct {}
49 -
50 -type DHTConfig struct {
51 - // Time to wait between republishing intervals
52 - RepublishInterval time.Duration
53 -
54 - // Multihash hash function
55 - HashType int
56 -}
57 -
58 -// Looks for a particular node
59 -func (dht *DHT) FindNode(id *peer.ID /* and a callback? */) error {
60 - panic("Not implemented.")
61 -}
62 -
63 -func (dht *DHT) PingNode(id *peer.ID, timeout time.Duration) error {
64 - panic("Not implemented.")
65 -}
66 -
67 -// Retrieves a value for a given key
68 -func (dht *DHT) GetValue(key string) *DHTValue {
69 - panic("Not implemented.")
70 -}
71 -
72 -// Stores a value for a given key
73 -func (dht *DHT) SetValue(key string, value *DHTValue) error {
74 - panic("Not implemented.")
75 -}
76 -
77 -// GetSloppyValues finds (at least) a number of values for given key
78 -func (dht *DHT) GetSloppyValues(key string, count int) ([]*DHTValue, error) {
79 - panic("Not implemented.")
80 -}
81 -
82 -func (dht *DHT) SetSloppyValue(key string, value *DHTValue) error {
83 - panic("Not implemented.")
84 -}
85 -
86 -func (dht *DHT) periodicRepublish() {
87 - tick := time.NewTicker(time.Second * 5)
88 - for {
89 - select {
90 - case <-tick.C:
91 - for _,v := range dht.Republish.Strict {
92 - if v.ShouldRepublish(dht.Config.RepublishInterval) {
93 - dht.SetValue(v.Key, v.Value)
94 - }
95 - }
96 -
97 - for _,v := range dht.Republish.Sloppy {
98 - if v.ShouldRepublish(dht.Config.RepublishInterval) {
99 - dht.SetSloppyValue(v.Key, v.Value)
100 - }
101 - }
102 - }
103 - }
104 -}
105 -
106 -func (dht *DHT) handleMessage(message []byte) {
107 -
108 -}
109 -
110 -func (dht *DHT) coerceMultihash(hash mh.Multihash) {
111 -}
routing/dht/dht.go new
+10
@@ -0,0 +1,10 @@
1 +package dht
2 +
3 +// TODO. SEE https://github.com/jbenet/node-ipfs/blob/master/submodules/ipfs-dht/index.js
4 +
5 +
6 +// IpfsDHT is an implementation of Kademlia with Coral and S/Kademlia modifications.
7 +// It is used to implement the base IpfsRouting module.
8 +type IpfsDHT struct {
9 + routes RoutingTable
10 +}
routing/dht/routing.go new
+44
@@ -0,0 +1,44 @@
1 +package dht
2 +
3 +import (
4 + "time"
5 + peer "github.com/jbenet/go-ipfs/peer"
6 + u "github.com/jbenet/go-ipfs/util"
7 +)
8 +
9 +
10 +// This file implements the Routing interface for the IpfsDHT struct.
11 +
12 +// Basic Put/Get
13 +
14 +// PutValue adds value corresponding to given Key.
15 +func (s *IpfsDHT) PutValue(key u.Key, value []byte) (error) {
16 + return u.ErrNotImplemented
17 +}
18 +
19 +// GetValue searches for the value corresponding to given Key.
20 +func (s *IpfsDHT) GetValue(key u.Key, timeout time.Duration) ([]byte, error) {
21 + return nil, u.ErrNotImplemented
22 +}
23 +
24 +
25 +// Value provider layer of indirection.
26 +// This is what DSHTs (Coral and MainlineDHT) do to store large values in a DHT.
27 +
28 +// Announce that this node can provide value for given key
29 +func (s *IpfsDHT) Provide(key u.Key) (error) {
30 + return u.ErrNotImplemented
31 +}
32 +
33 +// FindProviders searches for peers who can provide the value for given key.
34 +func (s *IpfsDHT) FindProviders(key u.Key, timeout time.Duration) (*peer.Peer, error) {
35 + return nil, u.ErrNotImplemented
36 +}
37 +
38 +
39 +// Find specific Peer
40 +
41 +// FindPeer searches for a peer with given ID.
42 +func (s *IpfsDHT) FindPeer(id peer.ID, timeout time.Duration) (*peer.Peer, error) {
43 + return nil, u.ErrNotImplemented
44 +}
routing/dht/table.go new
+61
@@ -0,0 +1,61 @@
1 +package dht
2 +
3 +import (
4 + "container/list"
5 +)
6 +
7 +
8 +// ID for IpfsDHT should be a byte slice, to allow for simpler operations
9 +// (xor). DHT ids are based on the peer.IDs.
10 +//
11 +// NOTE: peer.IDs are biased because they are (a) multihashes (first bytes
12 +// biased), and (b) first bits are zeroes when using the S/Kademlia PoW.
13 +// Thus, may need to re-hash keys (uniform dist). TODO(jbenet)
14 +type ID []byte
15 +
16 +// Bucket holds a list of peers.
17 +type Bucket []*list.List
18 +
19 +
20 +// RoutingTable defines the routing table.
21 +type RoutingTable struct {
22 +
23 + // kBuckets define all the fingers to other nodes.
24 + Buckets []Bucket
25 +}
26 +
27 +
28 +func (id ID) commonPrefixLen() int {
29 + for i := 0; i < len(id); i++ {
30 + for j := 0; j < 8; j++ {
31 + if (id[i] >> uint8(7 - j)) & 0x1 != 0 {
32 + return i * 8 + j;
33 + }
34 + }
35 + }
36 + return len(id) * 8 - 1;
37 +}
38 +
39 +func xor(a, b ID) ID {
40 +
41 + // ids may actually be of different sizes.
42 + var ba ID
43 + var bb ID
44 + if len(a) >= len(b) {
45 + ba = a
46 + bb = b
47 + } else {
48 + ba = b
49 + bb = a
50 + }
51 +
52 + c := make(ID, len(ba))
53 + for i := 0; i < len(ba); i++ {
54 + if len(bb) > i {
55 + c[i] = ba[i] ^ bb[i]
56 + } else {
57 + c[i] = ba[i] ^ 0
58 + }
59 + }
60 + return c
61 +}