stub out dht code to match node repo
Jeromy Johnson committed
Jul 24, 2014 at 12:30 UTC
27b665f58885d2fbe089f63d111929c023691b25
1 file changed
+104
dht/dht.go
+104
@@ -1,7 +1,111 @@
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
+}