## Copyright 2002-2004 Andrew Loewenstern, All Rights Reserved
# see LICENSE.txt for license information
-from time import time
-
from twisted.internet import reactor
from khash import intify
self.next_expire = reactor.callLater(self.config['KEINITIAL_DELAY'], self.doExpire)
def doExpire(self):
- self.store.expireValues(time() - self.config['KE_AGE'])
+ self.store.expireValues(self.config['KE_AGE'])
self.next_expire = reactor.callLater(self.config['KE_DELAY'], self.doExpire)
def shutdown(self):
-from time import time
+from datetime import datetime, timedelta
from pysqlite2 import dbapi2 as sqlite
import os
def storeValue(self, key, value):
"""Store or update a key and value."""
- t = "%0.6f" % time()
c = self.conn.cursor()
- c.execute("INSERT OR REPLACE INTO kv VALUES (?, ?, ?)", (key, value, t))
+ c.execute("INSERT OR REPLACE INTO kv VALUES (?, ?, ?)", (key, value, datetime.now()))
self.conn.commit()
- def expireValues(self, expireTime):
- """Expire older values than expireTime."""
- t = "%0.6f" % expireTime
+ def expireValues(self, expireAfter):
+ """Expire older values after expireAfter seconds."""
+ t = datetime.now() - timedelta(seconds=expireAfter)
c = self.conn.cursor()
c.execute("DELETE FROM kv WHERE time < ?", (t, ))
self.conn.commit()
import warnings
warnings.simplefilter("ignore", DeprecationWarning)
-from time import time
+from datetime import datetime, timedelta
from random import randrange
from sha import sha
import os
self.udp = krpc.hostbroker(self)
self.udp.protocol = krpc.KRPC
self.listenport = reactor.listenUDP(self.port, self.udp)
- self.last = time()
self._loadRoutingTable()
self.expirer = KeyExpirer(self.store, config)
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() - old.lastSeen) > self.config['MIN_PING_INTERVAL'] and old.id != self.node.id:
+ if (old and old.id != self.node.id and
+ (datetime.now() - old.lastSeen) >
+ timedelta(seconds=self.config['MIN_PING_INTERVAL'])):
# 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() - bucket.lastAccessed >= self.config['BUCKET_STALENESS']):
+ if force or (datetime.now() - bucket.lastAccessed >
+ timedelta(seconds=self.config['BUCKET_STALENESS'])):
id = newIDInRange(bucket.min, bucket.max)
self.findNode(id, callback)
## Copyright 2002-2003 Andrew Loewenstern, All Rights Reserved
# see LICENSE.txt for license information
-from time import time
+from datetime import datetime
from bisect import bisect_left
from twisted.trial import unittest
self.l = contents
self.min = min
self.max = max
- self.lastAccessed = time()
+ self.lastAccessed = datetime.now()
def touch(self):
- self.lastAccessed = time()
+ self.lastAccessed = datetime.now()
def getNodeWithInt(self, num):
if num in self.l: return num
## Copyright 2002-2003 Andrew Loewenstern, All Rights Reserved
# see LICENSE.txt for license information
-from time import time
+from datetime import datetime, MINYEAR
from types import InstanceType
from twisted.trial import unittest
"""encapsulate contact info"""
def __init__(self):
self.fails = 0
- self.lastSeen = 0
+ self.lastSeen = datetime(MINYEAR, 1, 1)
self.id = self.host = self.port = ''
def init(self, id, host, port):
return self
def updateLastSeen(self):
- self.lastSeen = time()
+ self.lastSeen = datetime.now()
self.fails = 0
def msgFailed(self):