Created the build/read versions of the message template, and added a rough outline of the builder

This commit is contained in:
locklainn.linden
2008-07-22 12:44:07 +00:00
committed by Salad Dais
parent 33c2f9cff8
commit 970de7f0b5
2 changed files with 135 additions and 0 deletions

View File

@@ -38,6 +38,71 @@ myreversedictionary = makereversepacketdict()
""" can construct and deconstruct packet headers. Has nothing to
do with the packet payload, yet. """
#this probably needs to implement an interface so it can be serialized
class MsgData():
""" Used as a Message that is being created that will be
serialized and sent. """
def __init__(self, name):
self.name = name
self.total_size = 0
self.blocks = {}
def add_block(self, block):
self.blocks[block.get_name()] = block
def get_blocks(self):
return self.blocks
def get_block(self, block_name):
return self.blocks[block_name]
def add_data(self, block_name, var_name, data, data_size):
get_block(block_name).add_data(var_name, data, data_size)
#this probably needs to implement an interface so it can be serialized
class MsgBlockData():
""" Used as a Message block that is being created that will be
serialized and sent. """
def __init__(self, name):
self.name = name
self.total_size = 0
self.vars = {}
def add_variable(self, var):
self.vars[var.get_name()] = var
def get_variables(self):
return self.vars
def get_variable(self, var_name):
return self.vars[var_name]
def add_data(self, var_name, data, data_size):
get_variable[var_name].add_data(data, data_size)
class MsgVariableData():
""" Used as a Message Block variable that is being created that will be
serialized and sent """
def __init__(self, name, tp):
self.name = name
self.total_size = 0
self.lltype = tp
self.data = None
#how DO we add data? What format will it be in?
def add_data(self, data, data_size):
self.data = data
def get_data(self):
return self.data
def get_total_size(self):
return self.total_size
def get_type(self):
return self.lltype
class MessageTemplateVariable():
def __init__(self, name, tp):
self.name = name
@@ -82,6 +147,9 @@ class MessageTemplate():
def __init__(self, header):
self.blocks = {}
#this is the function or object that will handle this type of message
self.handler = None
self.name = header[0]
self.frequency = header[1]
@@ -104,6 +172,13 @@ class MessageTemplate():
self.msg_deprecation = header[5]
else:
self.msg_deprecation = ''
def get_handler(self):
return self.handler
#this probably needs more arguments to pass to the func or object
def set_handler(self, handler):
self.handler = handler
def get_frequency(self):
return self.frequency

View File

@@ -0,0 +1,60 @@
class MessageTemplateBuilder():
""" This class builds messages at its high level, that is, keeping
that data in data structure form. A serializer should be used on
the message produced by this so that it can be sent over a network. """
def __init__(self, template_list):
self.template_list = template_list
#when a message is being built, uses this template
#to add blocks and variables
self.current_template = None
self.current_msg = None
self.current_block = None
self.cur_msg_name = ''
self.cur_block_name = ''
def new_message(self, message_name):
""" Creates a new packet where data can be added to it. Note, the variables
are added when they are used, or data added to them, so to make sure
no bad data is sent over the network. """
self.current_template = self.template_list[message_name]
#error check
self.current_msg = MsgData(message_name)
self.cur_msg_name = message_name
for block in self.current_template.get_blocks():
block_data = MsgBlockData(block.get_name())
self.current_msg.add_block(block_data)
def next_block(self, block_name):
self.current_block = self.current_template.get_block(block_name)
#error check
self.cur_msg_name = block_name
for variable in self.current_block.get_variables():
var_data = MsgVariableData(variable.get_name(), variable.get_type())
self.current_msg.add_variable(var_data)
def add_data(self, var_name, data, data_size):
self.current_block.add_data(var_name, data, data_size)
"""class IMessageSerializer():
implements ISerializer
adapts MessageData
#goes through MessageData and builds a byte string that can be sent over
#UDP or tcp
serialize (pack_message, build_message)
#goes through each block of the message data
pack_block
#goes through each block of the message variables, creating a byte-code
#string to return
pack_variable"""