From ea59a72d6f6d31c53b95531dd9700bdd13a2fafc Mon Sep 17 00:00:00 2001 From: Yuval Adam Date: Tue, 17 Jan 2012 11:04:05 +0200 Subject: initial kademlia files --- kademlia/msgtypes.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 kademlia/msgtypes.py (limited to 'kademlia/msgtypes.py') 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 -- cgit v1.3.1