summaryrefslogtreecommitdiff
path: root/kademlia/msgtypes.py
diff options
context:
space:
mode:
authorYuval Adam <yuv.adm@gmail.com>2012-01-17 11:04:05 +0200
committerYuval Adam <yuv.adm@gmail.com>2012-01-17 11:04:05 +0200
commitea59a72d6f6d31c53b95531dd9700bdd13a2fafc (patch)
tree9724c99c1747ab1aed6c88f7a1a118cfc18d4b43 /kademlia/msgtypes.py
parent984794de2eaa8de85dd91aa4a3fcdf7045226f4b (diff)
initial kademlia files
Diffstat (limited to 'kademlia/msgtypes.py')
-rw-r--r--kademlia/msgtypes.py46
1 files changed, 46 insertions, 0 deletions
diff --git a/kademlia/msgtypes.py b/kademlia/msgtypes.py
new file mode 100644
index 0000000..4e6eb18
--- /dev/null
+++ b/kademlia/msgtypes.py
@@ -0,0 +1,46 @@
+#!/usr/bin/env python
+#
+# This library is free software, distributed under the terms of
+# the GNU Lesser General Public License Version 3, or any later version.
+# See the COPYING file included in this archive
+#
+# The docstrings in this module contain epytext markup; API documentation
+# may be created by processing this file with epydoc: http://epydoc.sf.net
+
+import hashlib
+import random
+
+class Message(object):
+ """ Base class for messages - all "unknown" messages use this class """
+ def __init__(self, rpcID, nodeID):
+ self.id = rpcID
+ self.nodeID = nodeID
+
+
+class RequestMessage(Message):
+ """ Message containing an RPC request """
+ def __init__(self, nodeID, method, methodArgs, rpcID=None):
+ if rpcID == None:
+ hash = hashlib.sha1()
+ hash.update(str(random.getrandbits(255)))
+ rpcID = hash.digest()
+ Message.__init__(self, rpcID, nodeID)
+ self.request = method
+ self.args = methodArgs
+
+
+class ResponseMessage(Message):
+ """ Message containing the result from a successful RPC request """
+ def __init__(self, rpcID, nodeID, response):
+ Message.__init__(self, rpcID, nodeID)
+ self.response = response
+
+
+class ErrorMessage(ResponseMessage):
+ """ Message containing the error from an unsuccessful RPC request """
+ def __init__(self, rpcID, nodeID, exceptionType, errorMessage):
+ ResponseMessage.__init__(self, rpcID, nodeID, errorMessage)
+ if isinstance(exceptionType, type):
+ self.exceptionType = '%s.%s' % (exceptionType.__module__, exceptionType.__name__)
+ else:
+ self.exceptionType = exceptionType