Extended encoding update

- modified to support both encoding and decoding
- fixes
- added test for all encodings
This commit is contained in:
Peter Šurda 2016-11-12 17:20:45 +01:00
parent 4af788e963
commit 2fc2c78299
Signed by untrusted user: PeterSurda
GPG Key ID: 0C5F50C0B5F37D87
4 changed files with 80 additions and 24 deletions

View File

@ -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

View File

@ -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)

View File

@ -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))

View File

@ -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)