Remove the duplicate error logging from the knode module.
[quix0rs-apt-p2p.git] / apt_p2p_Khashmir / knode.py
1 ## Copyright 2002-2004 Andrew Loewenstern, All Rights Reserved
2 # see LICENSE.txt for license information
3
4 """Represents a khashmir node in the DHT."""
5
6 from twisted.python import log
7
8 from node import Node, NULL_ID
9
10 class KNodeBase(Node):
11     """A basic node that can only be pinged and help find other nodes."""
12     
13     def checkSender(self, dict):
14         """Check the sender's info to make sure it meets expectations."""
15         try:
16             senderid = dict['rsp']['id']
17         except KeyError:
18             log.msg("No peer id in response")
19             raise Exception, "No peer id in response."
20         else:
21             if self.id != NULL_ID and senderid != self.id:
22                 log.msg("Got response from different node than expected.")
23                 self.table.invalidateNode(self)
24                 raise Exception, "Node ID has changed"
25                 
26         return dict
27
28     def ping(self, id):
29         """Ping the node."""
30         df = self.conn.sendRequest('ping', {"id":id})
31         df.addCallback(self.checkSender)
32         return df
33     
34     def join(self, id):
35         """Use the node to bootstrap into the system."""
36         df = self.conn.sendRequest('join', {"id":id})
37         df.addCallback(self.checkSender)
38         return df
39     
40     def find_node(self, id, target):
41         """Request the nearest nodes to the target that the node knows about."""
42         df = self.conn.sendRequest('find_node', {"target" : target, "id": id})
43         df.addCallback(self.checkSender)
44         return df
45
46 class KNodeRead(KNodeBase):
47     """More advanced node that can also find and send values."""
48     
49     def find_value(self, id, key):
50         """Request the nearest nodes to the key that the node knows about."""
51         df =  self.conn.sendRequest('find_value', {"key" : key, "id" : id})
52         df.addCallback(self.checkSender)
53         return df
54
55     def get_value(self, id, key, num):
56         """Request the values that the node has for the key."""
57         df = self.conn.sendRequest('get_value', {"key" : key, "num": num, "id" : id})
58         df.addCallback(self.checkSender)
59         return df
60
61 class KNodeWrite(KNodeRead):
62     """Most advanced node that can also store values."""
63     
64     def store_value(self, id, key, value, token):
65         """Store a value in the node."""
66         df = self.conn.sendRequest('store_value', {"key" : key, "value" : value, "token" : token, "id": id})
67         df.addCallback(self.checkSender)
68         return df