eda1b01645bc109e737c2a837063825233d535a5
[quix0rs-apt-p2p.git] / apt_dht_Khashmir / knode.py
1 ## Copyright 2002-2004 Andrew Loewenstern, All Rights Reserved
2 # see LICENSE.txt for license information
3
4 from node import Node, NULL_ID
5
6 class KNodeBase(Node):
7     def checkSender(self, dict):
8         try:
9             senderid = dict['rsp']['id']
10         except KeyError:
11             print ">>>> No peer id in response"
12             raise Exception, "No peer id in response."
13         else:
14             if self.id != NULL_ID and senderid != self.id:
15                 print "Got response from different node than expected."
16                 self.table.invalidateNode(self)
17                 
18         return dict
19
20     def errBack(self, err):
21         print ">>> ", err
22         return err
23         
24     def ping(self, id):
25         df = self.conn.sendRequest('ping', {"id":id})
26         df.addErrback(self.errBack)
27         df.addCallback(self.checkSender)
28         return df
29     
30     def join(self, id):
31         df = self.conn.sendRequest('join', {"id":id})
32         df.addErrback(self.errBack)
33         df.addCallback(self.checkSender)
34         return df
35     
36     def findNode(self, target, id):
37         df = self.conn.sendRequest('find_node', {"target" : target, "id": id})
38         df.addErrback(self.errBack)
39         df.addCallback(self.checkSender)
40         return df
41
42 class KNodeRead(KNodeBase):
43     def findValue(self, key, id):
44         df =  self.conn.sendRequest('find_value', {"key" : key, "id" : id})
45         df.addErrback(self.errBack)
46         df.addCallback(self.checkSender)
47         return df
48
49 class KNodeWrite(KNodeRead):
50     def storeValue(self, key, value, originated, id):
51         df = self.conn.sendRequest('store_value', {"key" : key, "value" : value, "originated" : originated, "id": id})
52         df.addErrback(self.errBack)
53         df.addCallback(self.checkSender)
54         return df