from time import time
-from const import reactor
-import const
+from twisted.internet import reactor
+import const
from khash import intify
-from ktable import KTable, K
class ActionBase:
""" base class for some long running asynchronous proccesses like finding nodes or values """
return
l = self.found.values()
l.sort(self.sort)
- for node in l[:K]:
+ for node in l[:const.K]:
if node.id == self.target:
self.finished=1
return self.callback([node])
if self.outstanding == 0:
## all done!!
self.finished=1
- reactor.callLater(0, self.callback, l[:K])
+ reactor.callLater(0, self.callback, l[:const.K])
def makeMsgFailed(self, node):
def defaultGotNodes(err, self=self, node=node):
l = self.found.values()
l.sort(self.sort)
- for node in l[:K]:
+ for node in l[:const.K]:
if (not self.queried.has_key(node.id)) and node.id != self.table.node.id:
#xxx t.timeout = time.time() + GET_VALUE_TIMEOUT
try:
# see LICENSE.txt for license information
from types import IntType, LongType, StringType, ListType, TupleType, DictType
-import re
+from re import compile
from cStringIO import StringIO
-int_filter = re.compile('(0|-?[1-9][0-9]*)e')
+int_filter = compile('(0|-?[1-9][0-9]*)e')
def decode_int(x, f):
m = int_filter.match(x, f)
raise ValueError
return (long(m.group(1)), m.end())
-string_filter = re.compile('(0|[1-9][0-9]*):')
+string_filter = compile('(0|[1-9][0-9]*):')
def decode_string(x, f):
m = string_filter.match(x, f)
except IOError:
print "no resolv.conf!"
"""
-from twisted.internet import reactor
# magic id to use before we know a peer's id
NULL_ID = 20 * '\0'
## 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 time import time
+from random import randrange
+import sqlite ## find this at http://pysqlite.sourceforge.net/
-from ktable import KTable, K
-from knode import *
+from twisted.internet.defer import Deferred
+from twisted.internet import protocol
+from twisted.internet import reactor
+import const
+from ktable import KTable
+from knode import KNodeBase, KNodeRead, KNodeWrite
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.application import service, internet
-from twisted.web import server
-import sys
-
-from random import randrange
-
-import sqlite ## find this at http://pysqlite.sourceforge.net/
-
class KhashmirDBExcept(Exception):
pass
self.udp = krpc.hostbroker(self)
self.udp.protocol = krpc.KRPC
self.listenport = reactor.listenUDP(port, self.udp)
- self.last = time.time()
+ self.last = time()
self._loadRoutingTable()
KeyExpirer(store=self.store)
self.refreshTable(force=1)
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:
+ if old and (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
pass
for bucket in self.table.buckets:
- if force or (time.time() - bucket.lastAccessed >= const.BUCKET_STALENESS):
+ if force or (time() - bucket.lastAccessed >= const.BUCKET_STALENESS):
id = newIDInRange(bucket.min, bucket.max)
self.findNode(id, callback)
self.findNode(key, _storeValueForKey)
def krpc_store_value(self, key, value, id, _krpc_sender):
- t = "%0.6f" % time.time()
+ t = "%0.6f" % time()
c = self.store.cursor()
try:
c.execute("insert into kv values (%s, %s, %s);", (sqlite.encode(key), sqlite.encode(value), t))
# create a network of khashmir nodes
# usage: knet.py <num_nodes> <start_port> <ip_address>
-from khashmir import Khashmir
-from twisted.internet import reactor
from random import randrange
import sys, os
+from twisted.internet import reactor
+
+from khashmir import Khashmir
+
class Network:
def __init__(self, size=0, startport=5555, localip='127.0.0.1'):
self.num = size
## Copyright 2002-2004 Andrew Loewenstern, All Rights Reserved
# see LICENSE.txt for license information
+from const import NULL_ID
from node import Node
-from twisted.internet.defer import Deferred
-from const import reactor, NULL_ID
-
class KNodeBase(Node):
def checkSender(self, dict):
## Copyright 2002-2003 Andrew Loewenstern, All Rights Reserved
# see LICENSE.txt for license information
-from twisted.internet.defer import Deferred
-from twisted.protocols import basic
from bencode import bencode, bdecode
-from twisted.internet import protocol
-
-from twisted.internet import reactor
-import time
-
+from time import asctime
import sys
from traceback import format_exception
-import khash as hash
+from twisted.internet.defer import Deferred
+from twisted.internet import protocol
+from twisted.internet import reactor
KRPC_TIMEOUT = 20
olen = len(out)
self.transport.write(out, addr)
if self.noisy:
- print "%s %s >>> %s - %s %s %s" % (time.asctime(), addr, self.factory.node.port,
+ print "%s %s >>> %s - %s %s %s" % (asctime(), addr, self.factory.node.port,
ilen, msg[REQ], olen)
elif msg[TYP] == RSP:
# if response
## Copyright 2002-2003 Andrew Loewenstern, All Rights Reserved
# see LICENSE.txt for license information
-import time
-from bisect import *
-from types import *
+from time import time
+from bisect import bisect_left
-import khash as hash
-import const
-from const import K, HASH_LENGTH, NULL_ID
+from const import K, HASH_LENGTH, NULL_ID, MAX_FAILURES
+import khash
from node import Node
class KTable:
"""
if isinstance(id, str):
- num = hash.intify(id)
+ num = khash.intify(id)
elif isinstance(id, Node):
num = id.num
elif isinstance(id, int) or isinstance(id, long):
except IndexError:
return None
else:
- if n.msgFailed() >= const.MAX_FAILURES:
+ if n.msgFailed() >= MAX_FAILURES:
self.invalidateNode(n)
class KBucket:
self.l = contents
self.min = min
self.max = max
- self.lastAccessed = time.time()
+ self.lastAccessed = time()
def touch(self):
- self.lastAccessed = time.time()
+ self.lastAccessed = time()
def getNodeWithInt(self, num):
if num in self.l: return num
class TestKTable(unittest.TestCase):
def setUp(self):
- self.a = Node().init(hash.newID(), 'localhost', 2002)
+ self.a = Node().init(khash.newID(), 'localhost', 2002)
self.t = KTable(self.a)
def testAddNode(self):
- self.b = Node().init(hash.newID(), 'localhost', 2003)
+ self.b = Node().init(khash.newID(), 'localhost', 2003)
self.t.insertNode(self.b)
self.assertEqual(len(self.t.buckets[0].l), 1)
self.assertEqual(self.t.buckets[0].l[0], self.b)
def testFail(self):
self.testAddNode()
- for i in range(const.MAX_FAILURES - 1):
+ for i in range(MAX_FAILURES - 1):
self.t.nodeFailed(self.b)
self.assertEqual(len(self.t.buckets[0].l), 1)
self.assertEqual(self.t.buckets[0].l[0], self.b)
## Copyright 2002-2003 Andrew Loewenstern, All Rights Reserved
# see LICENSE.txt for license information
+from time import time
+from types import InstanceType
+
import khash
-import time
-from types import *
class Node:
"""encapsulate contact info"""
return self
def updateLastSeen(self):
- self.lastSeen = time.time()
+ self.lastSeen = time()
self.fails = 0
def msgFailed(self):
#!/usr/bin/env python
-import os
-import sys
-
try:
import distutils.core
import distutils.command.build_ext
import unittest
-import ktable, khashmir
-import hash, node, knode
-import actions
-import btemplate
-import test_krpc
tests = unittest.defaultTestLoader.loadTestsFromNames(['hash', 'node', 'knode', 'actions', 'ktable', 'test_krpc'])
result = unittest.TextTestRunner().run(tests)
-from unittest import *
-from khashmir import *
-import khash
-from copy import copy
-
+from unittest import defaultTestLoader, TextTestRunner, TestCase
+from sha import sha
from random import randrange
+import os, sys
+
+from twisted.internet import reactor
-import os
+from khashmir import Khashmir
+from khash import newID
if __name__ =="__main__":
tests = defaultTestLoader.loadTestsFromNames([sys.argv[0][:-3]])
def testStoreRetrieve(self):
for i in range(10):
- K = khash.newID()
- V = khash.newID()
+ K = newID()
+ V = newID()
for a in range(3):
self.done = 0
## Copyright 2002-2003 Andrew Loewenstern, All Rights Reserved
# see LICENSE.txt for license information
-from unittest import *
-from krpc import *
+from unittest import defaultTestLoader, TestCase, TextTestRunner
+import sys
-KRPC.noisy = 0
+from twisted.internet import protocol
+from twisted.internet import reactor
-import sys
+from krpc import KRPC, hostbroker, KRPC_ERROR_METHOD_UNKNOWN
+
+KRPC.noisy = 0
if __name__ =="__main__":
tests = defaultTestLoader.loadTestsFromNames([sys.argv[0][:-3]])