def setup(self, host, port, data_dir, rlcount, checkpoint=True): khashmir.KhashmirBase.setup(self, host, port, data_dir, rlcount, checkpoint) self.cur_token = self.last_token = sha('') self.tcache = Cache() self.gen_token(loop=True) self.expire_cached_tokens(loop=True)
class UTKhashmir(khashmir.KhashmirBase): _Node = UTNode def setup(self, host, port, data_dir, rlcount, checkpoint=True): khashmir.KhashmirBase.setup(self, host, port, data_dir, rlcount, checkpoint) self.cur_token = self.last_token = sha('') self.tcache = Cache() self.gen_token(loop=True) self.expire_cached_tokens(loop=True) def expire_cached_tokens(self, loop=False): self.tcache.expire(time() - TOKEN_UPDATE_INTERVAL) if loop: self.rawserver.external_add_task(TOKEN_UPDATE_INTERVAL, self.expire_cached_tokens, True) def gen_token(self, loop=False): self.last_token = self.cur_token self.cur_token = sha(newID()) if loop: self.rawserver.external_add_task(TOKEN_UPDATE_INTERVAL, self.gen_token, True) def get_token(self, host, port): x = self.cur_token.copy() x.update("%s%s" % (host, port)) h = x.digest() return h def val_token(self, token, host, port): x = self.cur_token.copy() x.update("%s%s" % (host, port)) a = x.digest() if token == a: return True x = self.last_token.copy() x.update("%s%s" % (host, port)) b = x.digest() if token == b: return True return False def addContact(self, host, port, callback=None): # use dns on host, then call khashmir.addContact Thread(target=self._get_host, args=[host, port, callback]).start() def _get_host(self, host, port, callback): # this exception catch can go away once we actually fix the bug try: ip = gethostbyname(host) except TypeError, e: raise TypeError( str(e) + (": host(%s) port(%s)" % (repr(host), repr(port)))) self.rawserver.external_add_task(0, self._got_host, ip, port, callback)
class UTKhashmir(khashmir.KhashmirBase): _Node = UTNode def setup(self, host, port, data_dir, rlcount, checkpoint=True): khashmir.KhashmirBase.setup(self, host, port,data_dir, rlcount, checkpoint) self.cur_token = self.last_token = sha('') self.tcache = Cache() self.gen_token(loop=True) self.expire_cached_tokens(loop=True) def expire_cached_tokens(self, loop=False): self.tcache.expire(time() - TOKEN_UPDATE_INTERVAL) if loop: self.rawserver.external_add_task(TOKEN_UPDATE_INTERVAL, self.expire_cached_tokens, True) def gen_token(self, loop=False): self.last_token = self.cur_token self.cur_token = sha(newID()) if loop: self.rawserver.external_add_task(TOKEN_UPDATE_INTERVAL, self.gen_token, True) def get_token(self, host, port): x = self.cur_token.copy() x.update("%s%s" % (host, port)) h = x.digest() return h def val_token(self, token, host, port): x = self.cur_token.copy() x.update("%s%s" % (host, port)) a = x.digest() if token == a: return True x = self.last_token.copy() x.update("%s%s" % (host, port)) b = x.digest() if token == b: return True return False def addContact(self, host, port, callback=None): # use dns on host, then call khashmir.addContact Thread(target=self._get_host, args=[host, port, callback]).start() def _get_host(self, host, port, callback): # this exception catch can go away once we actually fix the bug try: ip = gethostbyname(host) except TypeError, e: raise TypeError(str(e) + (": host(%s) port(%s)" % (repr(host), repr(port)))) self.rawserver.external_add_task(0, self._got_host, ip, port, callback)
def __init__(self, server, addr, transport, call_later, max_ul_rate, config, rlcount): self.server = server self.addr = addr self.transport = transport self.rltransport = KRateLimiter(transport, max_ul_rate, call_later, rlcount, config['max_rate_period']) self.call_later = call_later self.connections = Cache(touch_on_access=True) self.hammerlock = Hammerlock(100, call_later) self.expire_connections(loop=True) self.config = config if not self.config.has_key('pause'): self.config['pause'] = False
def __init__(self, server, addr, transport, call_later, max_ul_rate, config, rlcount): self.server = server #khashmirbase self.addr = addr #local address self.transport = transport #listening udp socket(addr) self.rltransport = KRateLimiter(transport, max_ul_rate, call_later, rlcount, config['max_rate_period']) self.call_later = call_later #rawserver.add_task self.connections = Cache(touch_on_access=True) self.hammerlock = Hammerlock(100, call_later) self.expire_connections(loop=True) self.config = config if not self.config.has_key('pause'): self.config['pause'] = False
class hostbroker(Handler): def __init__(self, server, addr, transport, call_later, max_ul_rate, config, rlcount): self.server = server #khashmirbase self.addr = addr #local address self.transport = transport #listening udp socket(addr) self.rltransport = KRateLimiter(transport, max_ul_rate, call_later, rlcount, config['max_rate_period']) self.call_later = call_later #rawserver.add_task self.connections = Cache(touch_on_access=True) self.hammerlock = Hammerlock(100, call_later) self.expire_connections(loop=True) self.config = config if not self.config.has_key('pause'): self.config['pause'] = False def expire_connections(self, loop=False): self.connections.expire(bttime() - KRPC_CONNECTION_CACHE_TIME) if loop: self.call_later(KRPC_CONNECTION_CACHE_TIME, self.expire_connections, True) '''receive data''' def data_came_in(self, addr, datagram): #if addr != self.addr: # print "recvfrom ",addr if not self.config['pause'] and self.hammerlock.check(addr): '''c == RRPC instance''' c = self.connectionForAddr(addr) c.datagramReceived(datagram, addr) def connection_lost(self, socket): ## this is like, bad print ">>> connection lost!", socket def connectionForAddr(self, addr): if addr == self.addr: raise KRPCSelfNodeError() if not self.connections.has_key(addr): conn = KRPC(addr, self.server, self.transport, self.rltransport, self.call_later) self.connections[addr] = conn else: conn = self.connections[addr] return conn
class hostbroker(Handler): def __init__(self, server, addr, transport, call_later, max_ul_rate, config, rlcount): self.server = server self.addr = addr self.transport = transport self.rltransport = KRateLimiter(transport, max_ul_rate, call_later, rlcount, config['max_rate_period']) self.call_later = call_later self.connections = Cache(touch_on_access=True) self.hammerlock = Hammerlock(100, call_later) self.expire_connections(loop=True) self.config = config if not self.config.has_key('pause'): self.config['pause'] = False def expire_connections(self, loop=False): self.connections.expire(bttime() - KRPC_CONNECTION_CACHE_TIME) if loop: self.call_later(KRPC_CONNECTION_CACHE_TIME, self.expire_connections, True) def data_came_in(self, addr, datagram): #if addr != self.addr: if not self.config['pause'] and self.hammerlock.check(addr): c = self.connectionForAddr(addr) c.datagramReceived(datagram, addr) def connection_lost(self, socket): ## this is like, bad print ">>> connection lost!", socket def connectionForAddr(self, addr): if addr == self.addr: raise KRPCSelfNodeError() if not self.connections.has_key(addr): conn = KRPC(addr, self.server, self.transport, self.rltransport, self.call_later) self.connections[addr] = conn else: conn = self.connections[addr] return conn
def setup(self, host, port, data_dir, rlcount, checkpoint=True): khashmir.KhashmirBase.setup(self, host, port,data_dir, rlcount, checkpoint) self.cur_token = self.last_token = sha('') self.tcache = Cache() self.gen_token(loop=True) self.expire_cached_tokens(loop=True)
class UTKhashmir(khashmir.KhashmirBase): _Node = UTNode '''Just a Test''' def getPeerQuery(self,info_hash,host,port): n = self._Node(self.udp.connectionForAddr) n.table = self n = n.init(const.NULL_ID, host, port) df = n.conn().sendRequest('get_peers', {'info_hash':info_hash, 'id':self.table.node.id}) def myErrorBack(*arg): print "error" def mySuccessBack(*arg): print "getPeer" df.addErrback(myErrorBack) df.addCallback(mySuccessBack) def pingQuery(self,host,port): n = self.Node().init(const.NULL_ID, host, port) self.sendPing(n) def setup(self, host, port, data_dir, rlcount, checkpoint=True): khashmir.KhashmirBase.setup(self, host, port,data_dir, rlcount, checkpoint) self.cur_token = self.last_token = sha('') self.tcache = Cache() self.gen_token(loop=True) self.expire_cached_tokens(loop=True) '''expire tokens in tcache''' def expire_cached_tokens(self, loop=False): self.tcache.expire(time() - TOKEN_UPDATE_INTERVAL) if loop: self.rawserver.external_add_task(TOKEN_UPDATE_INTERVAL, self.expire_cached_tokens, True) def gen_token(self, loop=False): self.last_token = self.cur_token self.cur_token = sha(newID()) if loop: self.rawserver.external_add_task(TOKEN_UPDATE_INTERVAL, self.gen_token, True) '''create token called by queried nodes''' def get_token(self, host, port): x = self.cur_token.copy() x.update("%s%s" % (host, port)) h = x.digest() return h '''check token is valid or not''' def val_token(self, token, host, port): x = self.cur_token.copy() x.update("%s%s" % (host, port)) a = x.digest() if token == a: return True x = self.last_token.copy() x.update("%s%s" % (host, port)) b = x.digest() if token == b: return True return False def addContact(self, host, port, callback=None): # use dns on host, then call khashmir.addContact Thread(target=self._get_host, args=[host, port, callback]).start() # t = Thread(target=self._get_host, args=[host, port, callback]) # t.start() def _get_host(self, host, port, callback): # this exception catch can go away once we actually fix the bug try: ip = gethostbyname(host) except TypeError, e: raise TypeError(str(e) + (": host(%s) port(%s)" % (repr(host), repr(port)))) self.rawserver.external_add_task(0, self._got_host, ip, port, callback)