1 ## Copyright 2002-2004 Andrew Loewenstern, All Rights Reserved
2 # see LICENSE.txt for license information
4 """Represents a khashmir node in the DHT."""
6 from twisted.python import log
8 from node import Node, NULL_ID
10 class KNodeBase(Node):
11 """A basic node that can only be pinged and help find other nodes."""
13 def checkSender(self, dict):
14 """Check the sender's info to make sure it meets expectations."""
18 log.msg("No peer id in response")
19 raise Exception, "No peer id in response."
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"
30 df = self.conn.sendRequest('ping', {"id":id})
31 df.addCallback(self.checkSender)
35 """Use the node to bootstrap into the system."""
36 df = self.conn.sendRequest('join', {"id":id})
37 df.addCallback(self.checkSender)
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)
46 class KNodeRead(KNodeBase):
47 """More advanced node that can also find and send values."""
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)
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)
61 class KNodeWrite(KNodeRead):
62 """Most advanced node that can also store values."""
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)