-
Notifications
You must be signed in to change notification settings - Fork 1
/
khashmir.py
362 lines (312 loc) · 13.5 KB
/
khashmir.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
## Copyright 2002-2004 Andrew Loewenstern, All Rights Reserved
# see LICENSE.txt for license information
from const import reactor
import const
import time
from sha import sha
from ktable import KTable, K
from knode import *
from khash import newID, newIDInRange
from actions import FindNode, GetValue, KeyExpirer, StoreValue
import krpc
from twisted.internet.defer import Deferred
from twisted.internet import protocol
from twisted.python import threadable
from twisted.application import service, internet
from twisted.web import server
threadable.init()
import sys
from random import randrange
import sqlite ## find this at http://pysqlite.sourceforge.net/
class KhashmirDBExcept(Exception):
pass
# this is the base class, has base functionality and find node, no key-value mappings
class KhashmirBase(protocol.Factory):
__slots__ = ('listener', 'node', 'table', 'store', 'app', 'last', 'protocol')
_Node = KNodeBase
def __init__(self, host, port, db='khashmir.db'):
self.setup(host, port, db)
def setup(self, host, port, db='khashmir.db'):
self._findDB(db)
self.port = port
self.node = self._loadSelfNode(host, port)
self.table = KTable(self.node)
#self.app = service.Application("krpc")
self.udp = krpc.hostbroker(self)
self.udp.protocol = krpc.KRPC
self.listenport = reactor.listenUDP(port, self.udp)
self.last = time.time()
self._loadRoutingTable()
KeyExpirer(store=self.store)
self.refreshTable(force=1)
reactor.callLater(60, self.checkpoint, (1,))
def Node(self):
n = self._Node()
n.table = self.table
return n
def __del__(self):
self.listenport.stopListening()
def _loadSelfNode(self, host, port):
c = self.store.cursor()
c.execute('select id from self where num = 0;')
if c.rowcount > 0:
id = c.fetchone()[0]
else:
id = newID()
return self._Node().init(id, host, port)
def _saveSelfNode(self):
c = self.store.cursor()
c.execute('delete from self where num = 0;')
c.execute("insert into self values (0, %s);", sqlite.encode(self.node.id))
self.store.commit()
def checkpoint(self, auto=0):
self._saveSelfNode()
self._dumpRoutingTable()
self.refreshTable()
if auto:
reactor.callLater(randrange(int(const.CHECKPOINT_INTERVAL * .9), int(const.CHECKPOINT_INTERVAL * 1.1)), self.checkpoint, (1,))
def _findDB(self, db):
import os
try:
os.stat(db)
except OSError:
self._createNewDB(db)
else:
self._loadDB(db)
def _loadDB(self, db):
try:
self.store = sqlite.connect(db=db)
#self.store.autocommit = 0
except:
import traceback
raise KhashmirDBExcept, "Couldn't open DB", traceback.exc_traceback
def _createNewDB(self, db):
self.store = sqlite.connect(db=db)
s = """
create table kv (key binary, value binary, time timestamp, primary key (key, value));
create index kv_key on kv(key);
create index kv_timestamp on kv(time);
create table nodes (id binary primary key, host text, port number);
create table self (num number primary key, id binary);
"""
c = self.store.cursor()
c.execute(s)
self.store.commit()
def _dumpRoutingTable(self):
"""
save routing table nodes to the database
"""
c = self.store.cursor()
c.execute("delete from nodes where id not NULL;")
for bucket in self.table.buckets:
for node in bucket.l:
c.execute("insert into nodes values (%s, %s, %s);", (sqlite.encode(node.id), node.host, node.port))
self.store.commit()
def _loadRoutingTable(self):
"""
load routing table nodes from database
it's usually a good idea to call refreshTable(force=1) after loading the table
"""
c = self.store.cursor()
c.execute("select * from nodes;")
for rec in c.fetchall():
n = self.Node().initWithDict({'id':rec[0], 'host':rec[1], 'port':int(rec[2])})
n.conn = self.udp.connectionForAddr((n.host, n.port))
self.table.insertNode(n, contacted=0)
#######
####### LOCAL INTERFACE - use these methods!
def addContact(self, host, port, callback=None):
"""
ping this node and add the contact info to the table on pong!
"""
n =self.Node().init(const.NULL_ID, host, port)
n.conn = self.udp.connectionForAddr((n.host, n.port))
self.sendPing(n, callback=callback)
## this call is async!
def findNode(self, id, callback, errback=None):
""" returns the contact info for node, or the k closest nodes, from the global table """
# get K nodes out of local table/cache, or the node we want
nodes = self.table.findNodes(id)
d = Deferred()
if errback:
d.addCallbacks(callback, errback)
else:
d.addCallback(callback)
if len(nodes) == 1 and nodes[0].id == id :
d.callback(nodes)
else:
# create our search state
state = FindNode(self, id, d.callback)
reactor.callFromThread(state.goWithNodes, nodes)
def insertNode(self, n, contacted=1):
"""
insert a node in our local table, pinging oldest contact in bucket, if necessary
If all you have is a host/port, then use addContact, which calls this method after
receiving the PONG from the remote node. The reason for the seperation is we can't insert
a node into the table without it's peer-ID. That means of course the node passed into this
method needs to be a properly formed Node object with a valid ID.
"""
old = self.table.insertNode(n, contacted=contacted)
if old and (time.time() - old.lastSeen) > const.MIN_PING_INTERVAL and old.id != self.node.id:
# the bucket is full, check to see if old node is still around and if so, replace it
## these are the callbacks used when we ping the oldest node in a bucket
def _staleNodeHandler(oldnode=old, newnode = n):
""" called if the pinged node never responds """
self.table.replaceStaleNode(old, newnode)
def _notStaleNodeHandler(dict, old=old):
""" called when we get a pong from the old node """
dict = dict['rsp']
if dict['id'] == old.id:
self.table.justSeenNode(old.id)
df = old.ping(self.node.id)
df.addCallbacks(_notStaleNodeHandler, _staleNodeHandler)
def sendPing(self, node, callback=None):
"""
ping a node
"""
df = node.ping(self.node.id)
## these are the callbacks we use when we issue a PING
def _pongHandler(dict, node=node, table=self.table, callback=callback):
_krpc_sender = dict['_krpc_sender']
dict = dict['rsp']
sender = {'id' : dict['id']}
sender['host'] = _krpc_sender[0]
sender['port'] = _krpc_sender[1]
n = self.Node().initWithDict(sender)
n.conn = self.udp.connectionForAddr((n.host, n.port))
table.insertNode(n)
if callback:
callback()
def _defaultPong(err, node=node, table=self.table, callback=callback):
table.nodeFailed(node)
if callback:
callback()
df.addCallbacks(_pongHandler,_defaultPong)
def findCloseNodes(self, callback=lambda a: None):
"""
This does a findNode on the ID one away from our own.
This will allow us to populate our table with nodes on our network closest to our own.
This is called as soon as we start up with an empty table
"""
id = self.node.id[:-1] + chr((ord(self.node.id[-1]) + 1) % 256)
self.findNode(id, callback)
def refreshTable(self, force=0):
"""
force=1 will refresh table regardless of last bucket access time
"""
def callback(nodes):
pass
for bucket in self.table.buckets:
if force or (time.time() - bucket.lastAccessed >= const.BUCKET_STALENESS):
id = newIDInRange(bucket.min, bucket.max)
self.findNode(id, callback)
def stats(self):
"""
Returns (num_contacts, num_nodes)
num_contacts: number contacts in our routing table
num_nodes: number of nodes estimated in the entire dht
"""
num_contacts = reduce(lambda a, b: a + len(b.l), self.table.buckets, 0)
num_nodes = const.K * (2**(len(self.table.buckets) - 1))
return (num_contacts, num_nodes)
def krpc_ping(self, id, _krpc_sender):
sender = {'id' : id}
sender['host'] = _krpc_sender[0]
sender['port'] = _krpc_sender[1]
n = self.Node().initWithDict(sender)
n.conn = self.udp.connectionForAddr((n.host, n.port))
self.insertNode(n, contacted=0)
return {"id" : self.node.id}
def krpc_find_node(self, target, id, _krpc_sender):
nodes = self.table.findNodes(target)
nodes = map(lambda node: node.senderDict(), nodes)
sender = {'id' : id}
sender['host'] = _krpc_sender[0]
sender['port'] = _krpc_sender[1]
n = self.Node().initWithDict(sender)
n.conn = self.udp.connectionForAddr((n.host, n.port))
self.insertNode(n, contacted=0)
return {"nodes" : nodes, "id" : self.node.id}
## This class provides read-only access to the DHT, valueForKey
## you probably want to use this mixin and provide your own write methods
class KhashmirRead(KhashmirBase):
_Node = KNodeRead
def retrieveValues(self, key):
c = self.store.cursor()
c.execute("select value from kv where key = %s;", sqlite.encode(key))
t = c.fetchone()
l = []
while t:
l.append(t['value'])
t = c.fetchone()
return l
## also async
def valueForKey(self, key, callback, searchlocal = 1):
""" returns the values found for key in global table
callback will be called with a list of values for each peer that returns unique values
final callback will be an empty list - probably should change to 'more coming' arg
"""
nodes = self.table.findNodes(key)
# get locals
if searchlocal:
l = self.retrieveValues(key)
if len(l) > 0:
reactor.callLater(0, callback, (l))
else:
l = []
# create our search state
state = GetValue(self, key, callback)
reactor.callFromThread(state.goWithNodes, nodes, l)
def krpc_find_value(self, key, id, _krpc_sender):
sender = {'id' : id}
sender['host'] = _krpc_sender[0]
sender['port'] = _krpc_sender[1]
n = self.Node().initWithDict(sender)
n.conn = self.udp.connectionForAddr((n.host, n.port))
self.insertNode(n, contacted=0)
l = self.retrieveValues(key)
if len(l) > 0:
return {'values' : l, "id": self.node.id}
else:
nodes = self.table.findNodes(key)
nodes = map(lambda node: node.senderDict(), nodes)
return {'nodes' : nodes, "id": self.node.id}
### provides a generic write method, you probably don't want to deploy something that allows
### arbitrary value storage
class KhashmirWrite(KhashmirRead):
_Node = KNodeWrite
## async, callback indicates nodes we got a response from (but no guarantee they didn't drop it on the floor)
def storeValueForKey(self, key, value, callback=None):
""" stores the value for key in the global table, returns immediately, no status
in this implementation, peers respond but don't indicate status to storing values
a key can have many values
"""
def _storeValueForKey(nodes, key=key, value=value, response=callback , table=self.table):
if not response:
# default callback
def _storedValueHandler(sender):
pass
response=_storedValueHandler
action = StoreValue(self.table, key, value, response)
reactor.callFromThread(action.goWithNodes, nodes)
# this call is asynch
self.findNode(key, _storeValueForKey)
def krpc_store_value(self, key, value, id, _krpc_sender):
t = "%0.6f" % time.time()
c = self.store.cursor()
try:
c.execute("insert into kv values (%s, %s, %s);", (sqlite.encode(key), sqlite.encode(value), t))
except sqlite.IntegrityError, reason:
# update last insert time
c.execute("update kv set time = %s where key = %s and value = %s;", (t, sqlite.encode(key), sqlite.encode(value)))
self.store.commit()
sender = {'id' : id}
sender['host'] = _krpc_sender[0]
sender['port'] = _krpc_sender[1]
n = self.Node().initWithDict(sender)
n.conn = self.udp.connectionForAddr((n.host, n.port))
self.insertNode(n, contacted=0)
return {"id" : self.node.id}
# the whole shebang, for testing
class Khashmir(KhashmirWrite):
_Node = KNodeWrite