From: Cameron Dale Date: Wed, 2 Jan 2008 19:48:37 +0000 (-0800) Subject: Added a shutdown method to the khasmir DHT. X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=7b9c88b513a3dc27a1dcaf72cec7d456bf8cf051;p=quix0rs-apt-p2p.git Added a shutdown method to the khasmir DHT. --- diff --git a/apt_dht_Khashmir/actions.py b/apt_dht_Khashmir/actions.py index a3a6fc8..088bc0f 100644 --- a/apt_dht_Khashmir/actions.py +++ b/apt_dht_Khashmir/actions.py @@ -271,3 +271,9 @@ class KeyExpirer: s = "delete from kv where time < '%s';" % self.cut c.execute(s) self.next_expire = reactor.callLater(self.config['KE_DELAY'], self.doExpire) + + def shutdown(self): + try: + self.next_expire.cancel() + except: + pass diff --git a/apt_dht_Khashmir/khashmir.py b/apt_dht_Khashmir/khashmir.py index dd2bf30..1baffa1 100644 --- a/apt_dht_Khashmir/khashmir.py +++ b/apt_dht_Khashmir/khashmir.py @@ -242,6 +242,16 @@ class KhashmirBase(protocol.Factory): num_contacts = reduce(lambda a, b: a + len(b.l), self.table.buckets, 0) num_nodes = self.config['K'] * (2**(len(self.table.buckets) - 1)) return (num_contacts, num_nodes) + + def shutdown(self): + """Closes the port and cancels pending later calls.""" + self.listenport.stopListening() + try: + self.next_checkpoint.cancel() + except: + pass + self.expirer.shutdown() + self.store.close() def krpc_ping(self, id, _krpc_sender): sender = {'id' : id} @@ -376,26 +386,8 @@ class SimpleTests(unittest.TestCase): self.b = Khashmir(d) def tearDown(self): - self.a.listenport.stopListening() - self.b.listenport.stopListening() - try: - self.a.next_checkpoint.cancel() - except: - pass - try: - self.b.next_checkpoint.cancel() - except: - pass - try: - self.a.expirer.next_expire.cancel() - except: - pass - try: - self.b.expirer.next_expire.cancel() - except: - pass - self.a.store.close() - self.b.store.close() + self.a.shutdown() + self.b.shutdown() os.unlink(self.a.db) os.unlink(self.b.db) @@ -492,16 +484,7 @@ class MultiTest(unittest.TestCase): def tearDown(self): for i in self.l: - i.listenport.stopListening() - try: - i.next_checkpoint.cancel() - except: - pass - try: - i.expirer.next_expire.cancel() - except: - pass - i.store.close() + i.shutdown() os.unlink(i.db) reactor.iterate()