From 2fc2c7829960b7e28ad8a7fb085ec82265c36e07 Mon Sep 17 00:00:00 2001 From: Peter Surda Date: Sat, 12 Nov 2016 17:20:45 +0100 Subject: [PATCH] Extended encoding update - modified to support both encoding and decoding - fixes - added test for all encodings --- src/helper_msgcoding.py | 35 ++++++++++++++++++++++++++++------- src/messagetypes/__init__.py | 23 ++++++++++++++--------- src/messagetypes/message.py | 23 +++++++++++++++++++---- src/messagetypes/vote.py | 23 +++++++++++++++++++---- 4 files changed, 80 insertions(+), 24 deletions(-) diff --git a/src/helper_msgcoding.py b/src/helper_msgcoding.py index 04c03fed..99d617f8 100644 --- a/src/helper_msgcoding.py +++ b/src/helper_msgcoding.py @@ -1,10 +1,10 @@ #!/usr/bin/python2.7 -import string import msgpack +import string import zlib -from debug import logger +import shared import messagetypes BITMESSAGE_ENCODING_IGNORE = 0 @@ -12,7 +12,6 @@ BITMESSAGE_ENCODING_TRIVIAL = 1 BITMESSAGE_ENCODING_SIMPLE = 2 BITMESSAGE_ENCODING_EXTENDED = 3 - class MsgEncode(object): def __init__(self, message, encoding=BITMESSAGE_ENCODING_SIMPLE): self.data = None @@ -27,7 +26,8 @@ class MsgEncode(object): def encodeExtended(self, message): try: - self.data = zlib.compress(msgpack.dumps({"": "message", "subject": message['subject'], "message": ['body']}), 9) + msgObj = messagetypes.message.Message() + self.data = zlib.compress(msgpack.dumps(msgObj.encode(message)), 9) except zlib.error: logger.error("Error compressing message") raise @@ -70,14 +70,14 @@ class MsgDecode(object): logger.error("Message type missing") raise - msgObj = messagetypes.constructObject(data) + msgObj = messagetypes.constructObject(tmp) if msgObj is None: raise ValueError("Malformed message") try: msgObj.process() except: raise ValueError("Malformed message") - if msgType[""] == "message": + if msgType == "message": self.subject = msgObj.subject self.body = msgObj.body @@ -96,4 +96,25 @@ class MsgDecode(object): if subject: subject = subject.splitlines()[0] self.subject = subject - self.message = body + self.body = body + +if __name__ == '__main__': + import random + messageData = { + "subject": ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(40)), + "body": ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(10000)) + } + obj1 = MsgEncode(messageData, 1) + obj2 = MsgEncode(messageData, 2) + obj3 = MsgEncode(messageData, 3) + print "1:%i 2:%i 3:%i" %(len(obj1.data), len(obj2.data), len(obj3.data)) + + obj1e = MsgDecode(1, obj1.data) + # no subject in trivial encoding + assert messageData["body"] == obj1e.body + obj2e = MsgDecode(2, obj2.data) + assert messageData["subject"] == obj2e.subject + assert messageData["body"] == obj2e.body + obj3e = MsgDecode(3, obj3.data) + assert messageData["subject"] == obj3e.subject + assert messageData["body"] == obj3e.body diff --git a/src/messagetypes/__init__.py b/src/messagetypes/__init__.py index 9b5c5a05..0b63519d 100644 --- a/src/messagetypes/__init__.py +++ b/src/messagetypes/__init__.py @@ -1,7 +1,13 @@ from importlib import import_module -from pprint import pprint from os import path, listdir -import sys +from string import lower + +from debug import logger + + +class MsgBase(object): + def encode(self): + self.data = {"": lower(type(self).__name__)} def constructObject(data): @@ -11,13 +17,13 @@ def constructObject(data): print "Don't know how to handle message type: \"%s\"" % (data[""]) return None try: - returnObj = classBase(data) + returnObj = classBase() + returnObj.decode(data) except KeyError as e: - print "Missing mandatory key %s" % (e) + logger.error("Missing mandatory key %s", e) return None except: - print "classBase fail:" - pprint(sys.exc_info()) + logger.error("classBase fail", exc_info=True) return None else: return returnObj @@ -31,7 +37,6 @@ for mod in listdir(path.dirname(__file__)): try: import_module("." + splitted[0], "messagetypes") except ImportError: - print "Error importing %s" % (mod) - pprint(sys.exc_info()) + logger.error("Error importing %s", mod, exc_info=True) else: - print "Imported %s" % (mod) + logger.debug("Imported message type module %s", mod) diff --git a/src/messagetypes/message.py b/src/messagetypes/message.py index 654e6d6b..aea621cc 100644 --- a/src/messagetypes/message.py +++ b/src/messagetypes/message.py @@ -1,8 +1,23 @@ -class Message: - def __init__(self, data): +from debug import logger +from messagetypes import MsgBase + +class Message(MsgBase): + def __init__(self): + return + + def decode(self, data): self.subject = data["subject"] self.body = data["body"] + def encode(self, data): + super(Message, self).encode() + try: + self.data["subject"] = data["subject"] + self.data["body"] = data["body"] + except KeyError as e: + logger.error("Missing key ", e.name) + return self.data + def process(self): - print "Subject: %s" % (self.subject) - print "Body: %s" % (self.body) + logger.debug("Subject: %i bytes", len(self.subject)) + logger.debug("Body: %i bytes", len(self.body)) diff --git a/src/messagetypes/vote.py b/src/messagetypes/vote.py index 5b692eb2..539aed8a 100644 --- a/src/messagetypes/vote.py +++ b/src/messagetypes/vote.py @@ -1,8 +1,23 @@ -class Vote: - def __init__(self, data): +from debug import logger +from messagetypes import MsgBase + +class Vote(MsgBase): + def __init__(self): + return + + def decode(self, data): self.msgid = data["msgid"] self.vote = data["vote"] + def encode(self, data): + super(Vote, self).encode() + try: + self.data["msgid"] = data["msgid"] + self.data["vote"] = data["vote"] + except KeyError as e: + logger.error("Missing key ", e.name) + return self.data + def process(self): - print "msgid: %s" % (self.msgid) - print "vote: %s" % (self.vote) + logger.debug("msgid: %s", self.msgid) + logger.debug("vote: %s", self.vote)