[sldev-commits] r1060 - in projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base: message network tests

locklainn.linden at svn.secondlife.com locklainn.linden at svn.secondlife.com
Thu Aug 21 13:39:01 PDT 2008


Author: locklainn.linden
Date: 2008-08-21 15:39:01 -0500 (Thu, 21 Aug 2008)
New Revision: 1060

Added:
   projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/message/circuit.py
   projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/message/udpdispatcher.py
Removed:
   projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/message/circuitdata.py
   projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/message/udpconnection.py
Modified:
   projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/message/__init__.py
   projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/message/interfaces.py
   projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/message/message.py
   projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/message/message_llsd_builder.py
   projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/message/packet.py
   projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/message/udpdeserializer.py
   projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/message/udpserializer.py
   projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/network/mockup_net.py
   projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/network/net.py
   projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/tests/test_circuits.py
   projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/tests/test_message_wrapper.py
   projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/tests/test_packetdata.py
   projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/tests/test_udp_deserializer.py
   projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/tests/test_udp_serializer.py
   projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/tests/test_udpconnection.py
Trac: http://svn.secondlife.com/trac/linden/changeset/1060
Log:
seems like all tests run, meaning full refactor seems to be complete

Modified: projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/message/__init__.py
===================================================================
--- projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/message/__init__.py	2008-08-21 13:45:49 UTC (rev 1059)
+++ projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/message/__init__.py	2008-08-21 20:39:01 UTC (rev 1060)
@@ -0,0 +1,3 @@
+from interfaces import IUDPDispatcher
+from udpdispatcher import UDPDispatcher
+from zope.component import provideUtility

Copied: projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/message/circuit.py (from rev 1049, projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/message/circuitdata.py)
===================================================================
--- projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/message/circuit.py	                        (rev 0)
+++ projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/message/circuit.py	2008-08-21 20:39:01 UTC (rev 1060)
@@ -0,0 +1,122 @@
+import grokcore.component as grok
+from zope.interface import implements
+
+from pyogp.lib.base.message.interfaces import IHost
+
+class Host(grok.Adapter):
+    grok.implements(IHost)
+    grok.context(tuple)
+
+    def __init__(self, context):
+        self.ip = context[0]
+        self.port = context[1]
+
+    def is_ok(self):
+        if self.ip == None or self.port == None or \
+           self.ip == 0 or self.port == 0:
+            return False
+
+        return True
+
+    def set_host_by_name(self, hostname):
+        pass
+
+class Circuit(object):
+    """ This is used to keep track of a given circuit. It keeps statistics
+        as well as circuit information. """
+    """ Some statistics things we may need: bytes/packets in, bytes/packets out,
+        unacked packet count/bytes, acked packet count/bytes"""
+    
+    def __init__(self, host, pack_in_id):
+        self.host = host
+        self.circuit_code = 0
+        self.session_id = 0
+        self.is_alive = True
+        self.is_trusted = False
+        self.is_blocked = False
+        self.allow_timeout = True
+        self.last_packet_out_id  = 0  #id of the packet we last sent
+        self.last_packet_in_id   = pack_in_id
+
+        self.acks                = [] #packets we need to ack (ids)       
+        self.unacked_packets     = {} #packets we want acked, can be resent
+        self.unack_packet_count  = 0
+        self.unack_packet_bytes  = 0
+        self.final_retry_packets = {} #packets we want acked, can't be resent
+        self.final_packet_count  = 0
+        
+    def next_packet_id(self):
+        self.last_packet_out_id += 1
+        return self.last_packet_out_id
+
+    def handle_packet(self, packet):
+        #if its a reliable packet, get all acks from packet, set them to be acked
+        for ack_packet_id in packet.acks:
+            self.ack_reliable_packet(ack_packet_id)
+
+        if packet.reliable == True:
+            self.collect_ack(packet.packet_id)
+
+
+    def ack_reliable_packet(self, packet_id):
+        #go through the packets waiting to be acked, and set them as acked
+        if packet_id in self.unacked_packets:
+            del self.unacked_packets[packet_id]
+            self.unack_packet_count -= 1
+        
+        if packet_id in self.final_retry_packets:
+            del self.final_retry_packets[packet_id]
+            self.final_packet_count -= 1
+
+    def collect_ack(self, packet_id):
+        """ set a packet_id that this circuit needs to eventually ack
+            (need to send ack out)"""
+        self.acks.append(packet_id)
+        
+    def add_reliable_packet(self, packet):
+        """ add a packet that we want to be acked
+            (want an incoming ack) """
+        self.unack_packet_count += 1
+        #self.unack_packet_bytes += buffer_length
+        #if it can be resent/retried (not final) add it to the unack list
+        #if 'retries' in params:
+        #    packet.retries = params['retries']
+        self.unacked_packets[packet.packet_id] = packet
+        #otherwise, it can't be resent to get acked
+        #else:
+        #self.final_retry_packets[packet.packet_id] = packet
+
+class CircuitManager(object):
+    """ Manages a collection of circuits and provides some higher-level
+        functionality to do so. """
+    def __init__(self):
+        self.circuit_map = {}
+        self.unacked_circuits = {}
+        
+    def get_unacked_circuits(self):
+        #go through circuits, if it has any unacked packets waiting ack, add
+        #to a list
+        pass
+
+    def get_circuit(self, host):
+        if (host.ip, host.port) in self.circuit_map:
+            return self.circuit_map[(host.ip, host.port)]
+
+        return None
+    
+    def add_circuit(self, host, packet_in_id):
+        circuit = Circuit(host, packet_in_id)
+        
+        self.circuit_map[(host.ip, host.port)] = circuit
+        return circuit
+        
+    def remove_circuit_data(self, host):
+        if (host.ip, host.port) in self.circuit_map:
+            del self.circuit_map[(host.ip, host.port)]
+
+    def is_circuit_alive(self, host):
+        if (host.ip, host.port) not in self.circuit_map:
+            return False
+
+        circuit = self.circuit_map[(host.ip, host.port)]
+        return circuit.is_alive

Deleted: projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/message/circuitdata.py
===================================================================
--- projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/message/circuitdata.py	2008-08-21 13:45:49 UTC (rev 1059)
+++ projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/message/circuitdata.py	2008-08-21 20:39:01 UTC (rev 1060)
@@ -1,108 +0,0 @@
-from pyogp.lib.base.message.packet import Packet
-
-class Host(object):
-    def __init__(self, ip_addr, port):
-        self.ip = ip_addr
-        self.port = port
-
-    def is_ok(self):
-        if self.ip == None or self.port == None or \
-           self.ip == 0 or self.port == 0:
-            return False
-
-        return True
-
-    def set_host_by_name(self, hostname):
-        pass
-
-class Circuit(object):
-    """ This is used to keep track of a given circuit. It keeps statistics
-        as well as circuit information. """
-    """ Some statistics things we may need: bytes/packets in, bytes/packets out,
-        unacked packet count/bytes, acked packet count/bytes"""
-    
-    def __init__(self, host, pack_in_id):
-        self.host = host
-        self.circuit_code = 0
-        self.session_id = 0
-        self.is_alive = True
-        self.is_trusted = False
-        self.is_blocked = False
-        self.allow_timeout = True
-        self.last_packet_out_id  = 0  #id of the packet we last sent
-        self.last_packet_in_id   = pack_in_id
-
-        self.acks                = [] #packets we need to ack (ids)       
-        self.unacked_packets     = {} #packets we want acked, can be resent
-        self.unack_packet_count  = 0
-        self.unack_packet_bytes  = 0
-        self.final_retry_packets = {} #packets we want acked, can't be resent
-        self.final_packet_count  = 0
-        
-    def next_packet_id(self):
-        self.last_packet_out_id += 1
-        return self.last_packet_out_id
-
-    def ack_reliable_packet(self, packet_id):
-        #go through the packets waiting to be acked, and set them as acked
-        if packet_id in self.unacked_packets:
-            del self.unacked_packets[packet_id]
-            self.unack_packet_count -= 1
-        
-        if packet_id in self.final_retry_packets:
-            del self.final_retry_packets[packet_id]
-            self.final_packet_count -= 1
-
-    def collect_ack(self, packet_id):
-        """ set a packet_id that this circuit needs to eventually ack
-            (need to send ack out)"""
-        self.acks.append(packet_id)
-        
-    def add_reliable_packet(self, sock, message_buffer, buffer_length, params):
-        """ add a packet that we want to be acked
-            (want an incoming ack) """
-        packet = Packet(sock, message_buffer, buffer_length, params)
-        self.unack_packet_count += 1
-        self.unack_packet_bytes += buffer_length
-        #if it can be resent/retried (not final) add it to the unack list
-        if 'retries' in params:
-            packet.retries = params['retries']
-            self.unacked_packets[packet.packet_id] = packet
-        #otherwise, it can't be resent to get acked
-        else:
-            self.final_retry_packets[packet.packet_id] = packet
-
-class CircuitManager(object):
-    """ Manages a collection of circuits and provides some higher-level
-        functionality to do so. """
-    def __init__(self):
-        self.circuit_map = {}
-        self.unacked_circuits = {}
-        
-    def get_unacked_circuits(self):
-        #go through circuits, if it has any unacked packets waiting ack, add
-        #to a list
-        pass
-
-    def get_circuit(self, host):
-        if (host.ip, host.port) in self.circuit_map:
-            return self.circuit_map[(host.ip, host.port)]
-
-        return None
-    
-    def add_circuit(self, host, packet_in_id):
-        circuit = Circuit(host, packet_in_id)
-        
-        self.circuit_map[(host.ip, host.port)] = circuit
-        return circuit
-        
-    def remove_circuit_data(self, host):
-        if (host.ip, host.port) in self.circuit_map:
-            del self.circuit_map[(host.ip, host.port)]
-
-    def is_circuit_alive(self, host):
-        if (host.ip, host.port) not in self.circuit_map:
-            return False
-
-        circuit = self.circuit_map[(host.ip, host.port)]
-        return circuit.is_alive

Modified: projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/message/interfaces.py
===================================================================
--- projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/message/interfaces.py	2008-08-21 13:45:49 UTC (rev 1059)
+++ projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/message/interfaces.py	2008-08-21 20:39:01 UTC (rev 1060)
@@ -19,8 +19,19 @@
     def __getitem__(name):
         """alias for get_template"""
 
+class IHost(Interface):
+    ip          = Attribute("""address of the host""")
+    port        = Attribute("""port of the host""")
+
+    def is_ok():
+        """ determines if the host is valid """
+    
+class IUDPDispatcher(Interface):
+    def receive_check(host, msg_buf, msg_size):
+        """ gets a raw message and tries to handle it """
+    
 class IPacket(Interface):
-    name       = Attribute("""name of the message""")
+    name            = Attribute("""name of the message""")
     send_flags         = Attribute("""name of the message""")
     reliable_params    = Attribute("""name of the message""")
     packet_id          = Attribute("""name of the message""")

Modified: projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/message/message.py
===================================================================
--- projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/message/message.py	2008-08-21 13:45:49 UTC (rev 1059)
+++ projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/message/message.py	2008-08-21 20:39:01 UTC (rev 1060)
@@ -17,7 +17,7 @@
                 self.add_block(block)                                
 
 class Block(MsgBlockData):
-    def __init__(self, name, **kwds):
+    def __init__(self, name, *args, **kwds):
         super(Block, self).__init__(name)
         self.__parse_vars(kwds)
 
@@ -26,3 +26,5 @@
             variable_data = var_list[variable_name]
             variable = MsgVariableData(variable_name, variable_data)
             self.add_variable(variable)
+
+    

Modified: projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/message/message_llsd_builder.py
===================================================================
--- projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/message/message_llsd_builder.py	2008-08-21 13:45:49 UTC (rev 1059)
+++ projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/message/message_llsd_builder.py	2008-08-21 20:39:01 UTC (rev 1060)
@@ -26,10 +26,10 @@
             will be put in standard Python form and will need to be formatted
             based on who the target is (xml? something else?) """
         msg = {}
-        for block in self.current_msg.block_map:
+        for block in self.current_msg.blocks:
             #message can have multiple of the same block names, so
             #message actually holds a block list
-            block_list = self.current_msg.block_map[block]
+            block_list = self.current_msg.blocks[block]
             
             for block_data in block_list:
                 #set up the block list
@@ -40,7 +40,7 @@
                 msg[block_data.name].append(block)                
 
                 #go through the variables for the data
-                for variable in block_data.variable_map.values():
+                for variable in block_data.vars.values():
                     #the variable holds the key-value pairs of data
                     #for the block
                     block[variable.name] = variable.data
@@ -62,7 +62,5 @@
 
     def add_data(self, var_name, data, data_type):
         self.has_been_built = False
-        var = MsgVariableData(var_name, data_type)
+        var = MsgVariableData(var_name, data)
         self.current_block.add_variable(var)
-        #size doesn't matter for llsd, formatter will take care of it
-        self.current_block.add_data(var_name, data, -1)

Modified: projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/message/packet.py
===================================================================
--- projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/message/packet.py	2008-08-21 13:45:49 UTC (rev 1059)
+++ projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/message/packet.py	2008-08-21 20:39:01 UTC (rev 1060)
@@ -22,7 +22,7 @@
         self.resent             = False
         
         self.socket             = 0
-        self.retries            = 0
+        self.retries            = 1 #by default
         self.host               = None
         self.expiration_time    = 0
 

Deleted: projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/message/udpconnection.py
===================================================================
--- projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/message/udpconnection.py	2008-08-21 13:45:49 UTC (rev 1059)
+++ projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/message/udpconnection.py	2008-08-21 20:39:01 UTC (rev 1060)
@@ -1,188 +0,0 @@
-from zope.component import getUtility
-from pyogp.lib.base.network.interfaces import IUDPClient
-
-from pyogp.lib.base.data import msg_tmpl, msg_details
-from pyogp.lib.base.message.circuitdata import CircuitManager
-from pyogp.lib.base.message.message_types import PacketLayout, PackFlags,\
-                 MsgType, EndianType, sizeof
-from pyogp.lib.base.message.data_unpacker import DataUnpacker
-from pyogp.lib.base.message.data_packer import DataPacker
-from pyogp.lib.base.interfaces import ISerialization
-
-#maybe make a global utility
-class UDPConnection(object):
-    def __init__(self):
-        #holds the details of the message, or how the messages should be sent,
-        #built, and read
-        self.reliable_msg       = False
-        self.message_details    = None
-        self.circuit_manager    = CircuitManager()
-        self.socket             = None
-        #the ID of the packet we most recently received
-        self.receive_packet_id  = -1
-
-        self.udp_client         = getUtility(IUDPClient)
-        self.socket = self.udp_client.start_udp_connection()
-        self.unpacker = DataUnpacker()
-        self.packer = DataPacker()
-
-    def find_circuit(self, host):
-        circuit = self.circuit_manager.get_circuit(host)
-        if circuit == None:
-            #there is a case where we want to return None,
-            #when the last packet was protected
-            circuit = self.circuit_manager.add_circuit(host, self.receive_packet_id)
-
-        return circuit
-
-    def receive_check(self):
-        #determine if we have any messages that can be received through UDP
-        #also, check and decode the message we have received
-        recv_packet = None
-        
-        while True:
-            msg_buf, msg_size = self.udp_client.receive_packet(self.socket)
-            
-            #we have a message
-            if msg_size > 0:
-                recv_packet = udp_deserializer.deserialize(msg_buf)
-
-                #couldn't deserialize
-                if recv_packet == None:
-                    break
-
-                #determine sender
-                host = self.udp_client.get_sender()
-                circuit = self.find_circuit(host)
-
-                if circuit == None:
-                    break
-
-                #if its a reliable packet, get all acks from packet, set them to be acked
-                for ack_packet_id in recv_packet.acks:
-                    circuit.ack_reliable_packet(ack_packet_id)
-
-                #Case - trusted packets can only come in over trusted circuits
-                if circuit.is_trusted and \
-                    recv_packet.trusted == False:
-                    break
-                
-                if packet.reliable == True:
-                    circuit.collect_ack(recv_packet.packet_id)
-
-        return recv_packet
-                                                                             
-    def send_reliable(self, packet, host, retries):
-        """ Wants to be acked """
-        #sets up the message so send_message will add the RELIABLE flag to
-        #the message
-        self.reliable_msg = True
-        packet.send_flags |= PackFlags.LL_RELIABLE_FLAG
-        packet.reliable_params = {}
-        packet.retries = retries
-        packet.reliable_params['host'] = host
-        self.send_message(packet, host)
-    
-    def send_retry(self, packet, host):
-        """ This is a retry because we didn't get acked """
-        #sets up the message so send_message will add the RETRY flag to it
-        packet.send_flags |= PackFlags.LL_RESENT_FLAG
-        self.send_message(host, packet)                
-
-    def send_message(self, packet, host):
-        """ Sends the message that is currently built to the desired host """
-        message_size = -1
-        
-        #make sure host is OK (ip and address aren't null)
-        if host.is_ok() == False:
-            return
-
-        #use circuit manager to get the circuit to send on
-        circuit = self.find_circuit(host)
-
-        send_buffer = ''
-
-        ack_count = len(circuit.acks)
-        if ack_count > 0 and packet.name != "PacketAck":
-            packet.send_flags |= PackFlags.LL_ACK_FLAG
-
-            #also, sends as many acks as we can onto the end of the packet
-            #acks are just the packet_id that we are acking
-            for packet_id in circuit.acks:
-                packet.add_ack(packet_id)
-
-        packet.packet_id = circuit.next_packet_id()
-
-        serializer = ISerialization(packet)
-        send_buffer = serializer.serialize()
-
-        if self.reliable_msg == True:
-            if circuit.unack_packet_count <= 0:
-                self.circuit_manager.unacked_circuits[host] = circuit
-
-            circuit.add_reliable_packet(packet)
-
-        #TODO: remove this when testing a network
-        self.udp_client.send_packet(self.socket, send_buffer, host)
-        self.reliable_msg = False
-
-        return send_buffer
-                        
-    def process_acks(self):
-        """ resends all of our messages that were unacked, and acks all
-            the messages that others are waiting to be acked. """
-        
-        #send the ones we didn't get acked
-        self.__resend_all_unacked()
-        #send the acks we didn't reply to
-        self.__send_acks()
-        
-    def __resend_all_unacked(self):
-        """ Resends all packets sent that haven't yet been acked. """
-        #now_time = get_time_now()
-        
-        #go through all circuits in the map
-        for circuit in self.circuit_manager.unacked_circuits.values():
-            for unacked_packet in circuit.unacked_packets.values():
-                unacked_packet.retries -= 1
-                #is this correct? should it be serialized or something?
-                #self.reset_send_buffer()
-                self.send_buffer = ''
-                self.send_buffer += unacked_packet.buffer
-                self.send_retry(unacked_packet.host, unacked_packet.buffer)
-
-                if unacked_packet.retries <= 0:
-                    circuit.final_retry_packets[unacked_packet.packet_id] = unacked_packet
-                    del circuit.unacked_packets[unacked_packet.packet_id]
-
-            #final retries aren't resent, they are just forgotten about. boo
-            #for unacked_packet in circuit.final_retry_packets.values():
-            #    if now_time > unacked_packet.expiration_time:
-            #        del circuit.final_retry_packets[unacked_packet.packet_id] 
-
-    def __send_acks(self):
-        """ Acks all packets received that we haven't acked yet. """
-        for circuit in self.circuit_manager.circuit_map.values():
-            acks_this_packet = 0
-            for packet_id in circuit.acks:
-                if acks_this_packet == 0:
-                    self.new_message("PacketAck")
-
-                self.next_block("Packets")
-                self.add_data("ID", packet_id, MsgType.MVT_U32)
-                acks_this_packet += 1
-                if acks_this_packet > 250:
-                    self.send_message(circuit.host)
-                    acks_this_packet = 0
-
-            if acks_this_packet > 0:
-                self.send_message(circuit.host)
-                
-            circuit.acks = []
-
-    def has_unacked(self):
-        for circuit in self.circuit_manager.circuit_map.values():
-            if len(circuit.acks) > 0:
-                return True
-
-        return False

Modified: projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/message/udpdeserializer.py
===================================================================
--- projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/message/udpdeserializer.py	2008-08-21 13:45:49 UTC (rev 1059)
+++ projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/message/udpdeserializer.py	2008-08-21 20:39:01 UTC (rev 1060)
@@ -136,7 +136,7 @@
         
         #RELIABLE - means the message wants to be acked by us
         if packet.send_flags & PackFlags.LL_RELIABLE_FLAG:
-            recv_reliable = True
+            packet.reliable = True
         
         #RESENT   - packet that wasn't previously acked was resent
         if packet.send_flags & PackFlags.LL_RESENT_FLAG:
@@ -201,7 +201,7 @@
                                   " from a buffer of len " + str(len(data))
                             return False
                         if data_size == 1:
-                            #print "Reading VARIABLE variable size 1 byte" 
+                            #print "Reading VARIABLE variable size 1 byte"
                             var_size = self.unpacker.unpack_data(data, \
                                                                  MsgType.MVT_U8, \
                                                                  decode_pos)
@@ -221,6 +221,7 @@
                                               of ' + str(data_size))
                         
                         decode_pos += data_size
+                        
                     #HACK: this is a slow procedure, should passed in
                     if (decode_pos + var_size) > len(data):
                         print "ERROR 2: trying to read " +  str(decode_pos + var_size) + \
@@ -231,11 +232,11 @@
                                                               decode_pos, \
                                                               var_size=var_size)
 
-                    print 'Unpacked variable: ' + str(unpacked_data)
                     var_data = MsgVariableData(variable.name, unpacked_data)
                     self.current_block.add_variable(var_data)
                     decode_pos += var_size
 
+
         if len(msg_data.blocks) <= 0 and len(self.current_template.blocks) > 0:
             raise Exception("Read message is empty")
         

Copied: projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/message/udpdispatcher.py (from rev 1050, projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/message/udpconnection.py)
===================================================================
--- projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/message/udpdispatcher.py	                        (rev 0)
+++ projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/message/udpdispatcher.py	2008-08-21 20:39:01 UTC (rev 1060)
@@ -0,0 +1,189 @@
+from zope.interface import implements
+from zope.component import getUtility
+from pyogp.lib.base.network.interfaces import IUDPClient
+
+from pyogp.lib.base.data import msg_tmpl, msg_details
+from pyogp.lib.base.message.circuit import CircuitManager
+from pyogp.lib.base.message.message_types import PacketLayout, PackFlags,\
+                 MsgType, EndianType, sizeof
+from pyogp.lib.base.message.data_unpacker import DataUnpacker
+from pyogp.lib.base.message.data_packer import DataPacker
+from pyogp.lib.base.interfaces import ISerialization, IDeserialization
+from pyogp.lib.base.message.interfaces import IUDPDispatcher, IPacket
+from pyogp.lib.base.message.message import Message, Block
+
+#maybe make a global utility
+class UDPDispatcher(object):
+    implements(IUDPDispatcher)
+
+    def __init__(self):
+        #holds the details of the message, or how the messages should be sent,
+        #built, and read
+        self.reliable_msg       = False
+        self.circuit_manager    = CircuitManager()
+        #the ID of the packet we most recently received
+        self.receive_packet_id  = -1
+
+        self.socket             = None
+        self.udp_client         = getUtility(IUDPClient)
+        self.socket = self.udp_client.start_udp_connection()
+        self.unpacker = DataUnpacker()
+        self.packer = DataPacker()
+
+    def find_circuit(self, host):
+        circuit = self.circuit_manager.get_circuit(host)
+        if circuit == None:
+            #there is a case where we want to return None,
+            #when the last packet was protected
+            circuit = self.circuit_manager.add_circuit(host, self.receive_packet_id)
+
+        return circuit
+
+    def receive_check(self, host, msg_buf, msg_size):
+        #determine if we have any messages that can be received through UDP
+        #also, check and decode the message we have received
+        recv_packet = None
+        #msg_buf, msg_size = self.udp_client.receive_packet(self.socket)
+        
+        #we have a message
+        if msg_size > 0:
+            udp_deserializer = IDeserialization(msg_buf)
+            recv_packet = udp_deserializer.deserialize()
+
+            #couldn't deserialize
+            if recv_packet == None:
+                return None
+
+            #determine sender
+            #host = self.udp_client.get_sender()
+            circuit = self.find_circuit(host)
+            if circuit == None:
+                raise Exception("No circuit found")
+
+            #Case - trusted packets can only come in over trusted circuits
+            if circuit.is_trusted and \
+                recv_packet.trusted == False:
+                return None
+
+            circuit.handle_packet(recv_packet)
+
+        return recv_packet
+                                                                             
+    def send_reliable(self, packet, host, retries):
+        """ Wants to be acked """
+        #sets up the message so send_message will add the RELIABLE flag to
+        #the message
+        self.reliable_msg = True
+        packet.send_flags |= PackFlags.LL_RELIABLE_FLAG
+        packet.reliable_params = {}
+        packet.retries = retries
+        packet.reliable_params['host'] = host
+        return self.send_message(packet, host)
+    
+    def send_retry(self, packet, host):
+        """ This is a retry because we didn't get acked """
+        #sets up the message so send_message will add the RETRY flag to it
+        packet.send_flags |= PackFlags.LL_RESENT_FLAG
+        return self.send_message(host, packet)                
+
+    def send_message(self, packet, host):
+        """ Sends the message that is currently built to the desired host """
+        message_size = -1
+        
+        #make sure host is OK (ip and address aren't null)
+        if host.is_ok() == False:
+            return
+
+        #use circuit manager to get the circuit to send on
+        circuit = self.find_circuit(host)
+
+        send_buffer = ''
+
+        ack_count = len(circuit.acks)
+        if ack_count > 0 and packet.name != "PacketAck":
+            packet.send_flags |= PackFlags.LL_ACK_FLAG
+
+            #also, sends as many acks as we can onto the end of the packet
+            #acks are just the packet_id that we are acking
+            for packet_id in circuit.acks:
+                packet.add_ack(packet_id)
+
+        packet.packet_id = circuit.next_packet_id()
+
+        serializer = ISerialization(packet)
+        send_buffer = serializer.serialize()
+
+        if self.reliable_msg == True:
+            if circuit.unack_packet_count <= 0:
+                self.circuit_manager.unacked_circuits[host] = circuit
+
+            circuit.add_reliable_packet(packet)
+
+        #TODO: remove this when testing a network
+        self.udp_client.send_packet(self.socket, send_buffer, host)
+        self.reliable_msg = False
+
+        return send_buffer
+                        
+    def process_acks(self):
+        """ resends all of our messages that were unacked, and acks all
+            the messages that others are waiting to be acked. """
+        
+        #send the ones we didn't get acked
+        self.__resend_all_unacked()
+        #send the acks we didn't reply to
+        self.__send_acks()
+        
+    def __resend_all_unacked(self):
+        """ Resends all packets sent that haven't yet been acked. """
+        #now_time = get_time_now()
+        
+        #go through all circuits in the map
+        for circuit in self.circuit_manager.unacked_circuits.values():
+            for unacked_packet in circuit.unacked_packets.values():
+                unacked_packet.retries -= 1
+                #is this correct? should it be serialized or something?
+                #self.reset_send_buffer()
+                self.send_buffer = ''
+                self.send_buffer += unacked_packet.buffer
+                self.send_retry(unacked_packet.host, unacked_packet.buffer)
+
+                if unacked_packet.retries <= 0:
+                    circuit.final_retry_packets[unacked_packet.packet_id] = unacked_packet
+                    del circuit.unacked_packets[unacked_packet.packet_id]
+
+            #final retries aren't resent, they are just forgotten about. boo
+            #for unacked_packet in circuit.final_retry_packets.values():
+            #    if now_time > unacked_packet.expiration_time:
+            #        del circuit.final_retry_packets[unacked_packet.packet_id] 
+
+    def __send_acks(self):
+        """ Acks all packets received that we haven't acked yet. """
+        for circuit in self.circuit_manager.circuit_map.values():
+            acks_this_packet = 0
+            msg = None
+            
+            for packet_id in circuit.acks:
+                if acks_this_packet == 0:
+                    msg = Message('PacketAck')
+
+                block = Block("Packets", ID=packet_id)
+                msg.add_block(block)
+                acks_this_packet += 1
+                if acks_this_packet > 250:
+                    packet = IPacket(msg)
+                    self.send_message(packet, circuit.host)
+                    acks_this_packet = 0
+
+            if acks_this_packet > 0:
+                packet = IPacket(msg)
+                self.send_message(packet, circuit.host)    
+                
+            circuit.acks = []
+
+    def has_unacked(self):
+        for circuit in self.circuit_manager.circuit_map.values():
+            if len(circuit.acks) > 0:
+                return True
+
+        return False

Modified: projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/message/udpserializer.py
===================================================================
--- projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/message/udpserializer.py	2008-08-21 13:45:49 UTC (rev 1059)
+++ projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/message/udpserializer.py	2008-08-21 20:39:01 UTC (rev 1060)
@@ -53,7 +53,6 @@
         msg_buffer += self.packer.pack_data(0, MsgType.MVT_U8)
         
         if self.current_template == None:
-            print 'no template!'
             return None
 
         #don't need to pack the frequency and message number. The template
@@ -97,12 +96,10 @@
         #mark how many blocks there are of this type that repeat, stored in
         #the data
         if template_block.type == MsgBlockType.MBT_VARIABLE:
-            print 'Variable block: ' + str(block_count)
             block_buffer += struct.pack('>B', block_count)
             bytes += 1            
 
         for block in block_list:
-            print block.name + ' ' + str(block.block_number)
             
             for v in template_block.get_variables(): #message_block.get_variables():
                 #this mapping has to occur to make sure the data is written in correct order
@@ -133,7 +130,6 @@
 
                     bytes += var_size
                 
-                print 'Variable data: ' + variable.name + '  ' + str(var_data)
                 data = self.packer.pack_data(var_data, v.type)
                 block_buffer += data
                 bytes += len(data)

Modified: projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/network/mockup_net.py
===================================================================
--- projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/network/mockup_net.py	2008-08-21 13:45:49 UTC (rev 1059)
+++ projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/network/mockup_net.py	2008-08-21 20:39:01 UTC (rev 1060)
@@ -2,15 +2,15 @@
 import random
 
 from zope.interface import implements
+from pyogp.lib.base.message.interfaces import IHost
 
 from pyogp.lib.base.network.interfaces import IUDPClient
-from pyogp.lib.base.message.circuitdata import Host
 
 class MockupUDPServer(object):
     def __init__(self):
         self.rec_buffer = ''
         self.ip = 'MockupUDPServer'
-        
+        self.port = 80
     def receive_message(self, client, receive_buffer):
         #print 'SERVER receive'
         self.rec_buffer = receive_buffer
@@ -18,7 +18,7 @@
     def send_message(self, client, send_message):
         #print 'SERVER send'
         client.rec = send_message
-        client.sender = self
+        client.sender = IHost((self, self.port))
         
 #returns true if packet was sent successfully
 class MockupUDPClient(object):
@@ -29,7 +29,7 @@
         self.sender = None
 
     def get_sender(self):
-        return Host(self.sender, 1)
+        return self.sender
     
     def set_response(self, socket, response):
         self.rec[socket] = response

Modified: projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/network/net.py
===================================================================
--- projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/network/net.py	2008-08-21 13:45:49 UTC (rev 1059)
+++ projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/network/net.py	2008-08-21 20:39:01 UTC (rev 1060)
@@ -3,14 +3,14 @@
 from zope.interface import implements
 
 from pyogp.lib.base.network.interfaces import IUDPClient
-from pyogp.lib.base.message.circuitdata import Host
+from pyogp.lib.base.message.interfaces import IHost
 
 #returns true if packet was sent successfully
 class NetUDPClient(object):
     implements(IUDPClient)
 
     def __init__(self):
-        self.sender = Host(None, None)
+        self.sender = IHost(None, None)
 
     def get_sender(self):
         return self.sender

Modified: projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/tests/test_circuits.py
===================================================================
--- projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/tests/test_circuits.py	2008-08-21 13:45:49 UTC (rev 1059)
+++ projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/tests/test_circuits.py	2008-08-21 20:39:01 UTC (rev 1060)
@@ -3,22 +3,27 @@
 import pprint
 
 #local libraries
-from pyogp.lib.base.message.circuitdata import Host, CircuitManager, Circuit
+from pyogp.lib.base.registration import init
 
+from pyogp.lib.base.message.circuit import CircuitManager, Circuit
+from pyogp.lib.base.message.interfaces import IHost
+from pyogp.lib.base.message.message import Message, Block
+from pyogp.lib.base.message.interfaces import IPacket
+
 class TestHost(unittest.TestCase):
     
     def tearDown(self):
         pass
 
     def setUp(self):
-        pass
+        init()
 
     def test(self):
-        host = Host(0x00000001, 80)
+        host = IHost((0x00000001, 80))
         assert host.is_ok() == True, "Good host thinks it is bad"
 
     def test_fail(self):
-        host = Host(None, 80)
+        host = IHost((None, None))
         assert host.is_ok() == False, "Bad host thinks it is good"
 
 class TestCircuit(unittest.TestCase):
@@ -27,7 +32,8 @@
         pass
 
     def setUp(self):
-        self.host = Host(0x00000001, 80)
+        init()
+        self.host = IHost((0x00000001, 80))
 
     def test(self):
         circuit = Circuit(self.host, 1)
@@ -39,10 +45,14 @@
         assert circuit.unack_packet_count == 0, "Has incorrect unack count"
         assert len(circuit.unacked_packets) == 0, "Has incorrect unack"
         assert len(circuit.final_retry_packets) == 0, "Has incorrect final unacked"
-        circuit.add_reliable_packet(90, 'we are only testing', 10, \
-                                    {'retries':1, 'host':self.host})
+        msg = Message('PacketAck',
+                      Block('Packets', ID=0x00000003)
+                      )
+        packet = IPacket(msg)
+        circuit.add_reliable_packet(packet)
         assert circuit.unack_packet_count == 1, "Has incorrect unack count"
-        assert len(circuit.unacked_packets) == 1, "Has incorrect unack"
+        assert len(circuit.unacked_packets) == 1, "Has incorrect unack, " + \
+               str(len(circuit.unacked_packets))
         assert len(circuit.final_retry_packets) == 0, "Has incorrect final unacked"
 
 class TestCircuitManager(unittest.TestCase):
@@ -51,14 +61,15 @@
         pass
 
     def setUp(self):
-        self.host = Host(0x00000001, 80)
+        init()
+        self.host = IHost((0x00000001, 80))
 
     def test_(self):
         manager = CircuitManager()
         assert len(manager.circuit_map) == 0, "Circuit list incorrect"
         manager.add_circuit(self.host, 1)
         assert len(manager.circuit_map) == 1, "Circuit list incorrect 2"
-        host = Host(0x00000011, 80)
+        host = IHost((0x00000011, 80))
         manager.add_circuit(host, 10)
         assert len(manager.circuit_map) == 2, "Circuit list incorrect 4"
         circuit = manager.get_circuit(self.host)

Modified: projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/tests/test_message_wrapper.py
===================================================================
--- projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/tests/test_message_wrapper.py	2008-08-21 13:45:49 UTC (rev 1059)
+++ projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/tests/test_message_wrapper.py	2008-08-21 20:39:01 UTC (rev 1060)
@@ -4,18 +4,11 @@
 
 #local libraries
 from pyogp.lib.base.data import msg_tmpl
-from pyogp.lib.base.message.message_template import MessageTemplate, MessageTemplateBlock, MessageTemplateVariable
-from pyogp.lib.base.message.message_template_parser import MessageTemplateParser
-from pyogp.lib.base.message.message_template_builder import MessageTemplateBuilder
-from pyogp.lib.base.message.message_template_dict import TemplateDictionary
 from pyogp.lib.base.message.message_types import MsgType
 from pyogp.lib.base.message.message import Message, Block
-#import pyogp.lib.base.message.udpdeserializer
 from pyogp.lib.base.interfaces import IDeserialization
 from pyogp.lib.base.registration import init
 
-#from indra.base.lluuid import UUID
-
 class TestMessage(unittest.TestCase):
     
     def tearDown(self):
@@ -23,17 +16,14 @@
 
     def setUp(self):
         init()
+
+    def test_block(self):
+        block = Block('CircuitCode', {'ID':1234},{'Code':531})
         
     def test_build(self):
         msg = Message('TestPacket',
                       Block('CircuitCode', {'ID':1234},{'Code':531})
                       )
-        print msg.name
-        for block_list in msg.blocks.values():
-            for block in block_list:
-                print "\t" + block.name
-                for var in block.vars.values():
-                    print "\t\t" + var.name
 
     def test_build_multiple(self):
         msg = Message('TestPacket',
@@ -41,13 +31,6 @@
                       Block('CircuitCode', {'ID':5678},{'Code':456}),
                       Block('Test', {'ID':9101},{'Code':123})
                       )
-        print msg.name
-        for block_list in msg.blocks.values():
-            for block in block_list:
-                print "\t" + block.name
-                for var in block.vars.values():
-                    print "\t\t" + var.name
-
             
 def test_suite():
     from unittest import TestSuite, makeSuite

Modified: projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/tests/test_packetdata.py
===================================================================
--- projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/tests/test_packetdata.py	2008-08-21 13:45:49 UTC (rev 1059)
+++ projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/tests/test_packetdata.py	2008-08-21 20:39:01 UTC (rev 1060)
@@ -3,16 +3,15 @@
 import pprint
 import binascii
 
+from zope.component import getUtility
+
 #local libraries
 from pyogp.lib.base.data import msg_tmpl
-from pyogp.lib.base.message.message_system import MessageSystem
-from pyogp.lib.base.message.message_types import MsgType
-from pyogp.lib.base.message.circuitdata import Host
-from pyogp.lib.base.message.message_template_reader import MessageTemplateReader
+from pyogp.lib.base.message.interfaces import IUDPDispatcher
+from pyogp.lib.base.interfaces import IDeserialization
 from pyogp.lib.base.message.message_template_parser import MessageTemplateParser
-from pyogp.lib.base.message.message_template_builder import MessageTemplateBuilder
-from pyogp.lib.base.message.message_template_reader import MessageTemplateReader
 from pyogp.lib.base.message.message_template_dict import TemplateDictionary
+from pyogp.lib.base.registration import init
 
 AGENT_DATA_UPDATE="C0 00 00 00 02 00 FF FF 01 83 1C 8A 77 67 E3 7B 42 2E AF B3 85 09 31 97 CA D1 03 4A 42 00 01 06 4B 72 61 66 74 00 01 01 00 1A"
 AGENT_DATA_UPDATE =  binascii.unhexlify(''.join(AGENT_DATA_UPDATE.split()))
@@ -28,35 +27,33 @@
         pass
 
     def setUp(self):
+        init()
         self.template_file = msg_tmpl
         parser = MessageTemplateParser(self.template_file)
         self.template_list = parser.message_templates        
         self.template_dict = TemplateDictionary(self.template_list)
-        self.reader = MessageTemplateReader(self.template_dict)
         
     def test_agent_data_update(self):
         """test if the agent data update packet can be decoded"""
         message = AGENT_DATA_UPDATE
-        self.reader.clear_message()
         size = len(message)
-        message = MessageSystem(80).zero_code_expand(message, size)
-        try:
-            assert self.reader.validate_message(message, size), "Validation failed for test_read"
-        except:
-            assert False, "Validation got error"
-        try:
-            assert self.reader.read_message(message), "Read failed"
-        except:
-            assert False, "Read got error"
+        deserializer = IDeserialization(message)
+        packet = deserializer.deserialize()
+        assert packet != None, "Wrong data"
+        assert packet.message_data.name == 'AgentDataUpdate', "Wrong packet"
+        assert packet.message_data.blocks['AgentData'][0].vars['LastName'].data == 'Kraft\x00', \
+               "Wrong last name for agent update"
+        assert packet.message_data.blocks['AgentData'][0].vars['FirstName'].data == 'JB\x00', \
+               "Wrong first name for agent update"
         
     def test_agent_animation(self):
         """test if the agent data update packet can be decoded"""
         message = AGENT_ANIMATION
-        self.reader.clear_message()
         size = len(message)
-        assert self.reader.validate_message(message, size), "Validation failed for test_read"
-        assert self.reader.read_message(message), "Read failed"    
-    
+        deserializer = IDeserialization(message)
+        packet = deserializer.deserialize()
+        assert packet != None, "Wrong data 2"
+
         
 def test_suite():
     from unittest import TestSuite, makeSuite

Modified: projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/tests/test_udp_deserializer.py
===================================================================
--- projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/tests/test_udp_deserializer.py	2008-08-21 13:45:49 UTC (rev 1059)
+++ projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/tests/test_udp_deserializer.py	2008-08-21 20:39:01 UTC (rev 1060)
@@ -26,11 +26,6 @@
         packet = deserializer.deserialize()
         data = packet.message_data
         assert packet.name == 'PacketAck', 'Incorrect deserialization'
-        for block_list in data.blocks.values():
-            for block in block_list:
-                print block.name + ' ' + str(block.block_number)
-                for var in block.vars.values():
-                    print var.data
         
 def test_suite():
     from unittest import TestSuite, makeSuite

Modified: projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/tests/test_udp_serializer.py
===================================================================
--- projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/tests/test_udp_serializer.py	2008-08-21 13:45:49 UTC (rev 1059)
+++ projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/tests/test_udp_serializer.py	2008-08-21 20:39:01 UTC (rev 1060)
@@ -27,12 +27,6 @@
         #print packet.send_flags
         #print packet.packet_id
         data = packet.message_data
-        print len(data.blocks)
-        for block_list in data.blocks.values():
-            for block in block_list:
-                print block.name + ' ' + str(block.block_number)
-                for var in block.vars.values():
-                    print var.data
 
         serializer = ISerialization(packet)
         packed_data = serializer.serialize()

Modified: projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/tests/test_udpconnection.py
===================================================================
--- projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/tests/test_udpconnection.py	2008-08-21 13:45:49 UTC (rev 1059)
+++ projects/2008/pyogp/pyogp.lib.base/branches/locklainn-message-refactoring/pyogp/lib/base/tests/test_udpconnection.py	2008-08-21 20:39:01 UTC (rev 1060)
@@ -2,16 +2,17 @@
 import unittest, doctest
 import pprint
 
+from zope.component import getUtility
+
 from pyogp.lib.base.registration import init
 
 #local libraries
 #from pyogp.lib.base.message.udp_connection import MessageSystem
-from pyogp.lib.base.message.udpconnection import UDPConnection
 from pyogp.lib.base.message.message_types import MsgType
-from pyogp.lib.base.message.circuitdata import Host
 from pyogp.lib.base.network.mockup_net import MockupUDPServer
 from pyogp.lib.base.message.message import Message, Block
-from pyogp.lib.base.message.interfaces import IPacket
+from pyogp.lib.base.message.interfaces import IPacket, IHost
+from pyogp.lib.base.message.udpdispatcher import UDPDispatcher
 
 class TestUDPConnection(unittest.TestCase):
     
@@ -20,13 +21,14 @@
 
     def setUp(self):
         init()
-        self.udp_connection = UDPConnection()
-        self.host = Host(MockupUDPServer(), 80)
         
+        self.udp_connection = UDPDispatcher()
+        self.host = IHost( (MockupUDPServer(), 80) )
+        
     def test_find_circuit(self):
-        host  = Host(MockupUDPServer(), 80)
-        host2 = Host(MockupUDPServer(), 80)
-        udp_connection = UDPConnection()
+        host  = IHost((MockupUDPServer(), 80))
+        host2 = IHost((MockupUDPServer(), 80))
+        udp_connection = UDPDispatcher()
         assert len(udp_connection.circuit_manager.circuit_map) == 0, \
                "Circuit map has incorrect circuits"
         circuit1 = udp_connection.find_circuit(host)
@@ -83,57 +85,57 @@
                'Received: ' + repr(ret2) + '  ' + \
                'Expected: ' + repr(test_str2)
                 
-    """def test_send_reliable(self):
-        assert udp_connection.builder == None, "Has builder already"
-        
-        udp_connection.new_message('PacketAck')
-
-        assert udp_connection.builder == \
-               udp_connection.template_builder, "Builder incorrect"
-        udp_connection.next_block('Packets')
-        udp_connection.add_data('ID', 0x00000003, MsgType.MVT_U32)
-        host = Host(MockupUDPServer(), 80)
-        udp_connection.send_reliable(host, 10)
+    def test_send_reliable(self):
+        msg = Message('PacketAck',
+                      Block('Packets', ID=0x00000003)
+                      )
+        packet = IPacket(msg)
+        host = IHost((MockupUDPServer(), 80))
+        ret_msg = self.udp_connection.send_reliable(packet, host, 10)
         test_str = '\x40' + '\x00\x00\x00\x01' + '\x00' + '\xff\xff\xff\xfb' + \
                '\x01' + '\x03\x00\x00\x00'
-        assert udp_connection.send_buffer == \
+        assert ret_msg == \
                test_str ,\
-               'Received: ' + repr(udp_connection.send_buffer) + '  ' + \
+               'Received: ' + repr(msg) + '  ' + \
                'Expected: ' + repr(test_str)
 
     def test_receive(self):
         out_message = '\x00' + '\x00\x00\x00\x01' + '\x00' + \
             '\xff\xff\xff\xfb' + '\x01' + '\x01\x00\x00\x00'
         server = MockupUDPServer()
-        server.send_message(udp_connection.udp_client, out_message)
-        
-        udp_connection.receive_check()
-        msg = udp_connection.get_received_message()
-        assert msg.name == 'PacketAck'
-        data = udp_connection.get_data('Packets', 'ID', MsgType.MVT_U32)
+        server.send_message(self.udp_connection.udp_client, out_message)
+
+        data, data_size = self.udp_connection.udp_client.receive_packet(0)
+        packet = self.udp_connection.receive_check(self.udp_connection.udp_client.sender,
+                                          data, data_size)
+        assert packet.name == 'PacketAck'
+        data = packet.message_data.blocks['Packets'][0].vars['ID'].data
         assert data == 1, "ID Data incorrect: " + str(data)
         
     def test_receive_zero(self):
         out_message = '\x80' + '\x00\x00\x00\x01' + '\x00' + \
             '\xff\xff\xff\xfb' + '\x01' + '\x01\x00\x03'
         server = MockupUDPServer()
-        server.send_message(udp_connection.udp_client, out_message)
+        server.send_message(self.udp_connection.udp_client, out_message)
 
-        udp_connection.receive_check()
-        msg = udp_connection.get_received_message()
-        assert msg.name == 'PacketAck'
-        data = udp_connection.get_data('Packets', 'ID', MsgType.MVT_U32)
+        data, data_size = self.udp_connection.udp_client.receive_packet(0)
+        packet = self.udp_connection.receive_check(self.udp_connection.udp_client.sender,
+                                          data, data_size)
+        assert packet.name == 'PacketAck'
+        data = packet.message_data.blocks['Packets'][0].vars['ID'].data
         assert data == 1, "ID Data incorrect: " + str(data)
 
     def test_receive_reliable(self):
         out_message = '\x40' + '\x00\x00\x00\x05' + '\x00' + \
             '\xff\xff\xff\xfb' + '\x01' + '\x01\x00\x00\x00'
         server = MockupUDPServer()
-        server.send_message(udp_connection.udp_client, out_message)
+        server.send_message(self.udp_connection.udp_client, out_message)
 
-        udp_connection.receive_check()
-        sender_host = udp_connection.udp_client.get_sender()
-        circuit = udp_connection.circuit_manager.get_circuit(sender_host)
+        data, data_size = self.udp_connection.udp_client.receive_packet(0)
+        packet = self.udp_connection.receive_check(self.udp_connection.udp_client.sender,
+                                          data, data_size)
+        sender_host = self.udp_connection.udp_client.get_sender()
+        circuit = self.udp_connection.circuit_manager.get_circuit(sender_host)
         assert len(circuit.acks) == 1, "Ack not collected"
         assert circuit.acks[0] == 5, "Ack ID not correct, got " + str(circuit.acks[0])
         
@@ -141,17 +143,19 @@
         out_message = '\x40' + '\x00\x00\x00\x05' + '\x00' + \
             '\xff\xff\xff\xfb' + '\x01' + '\x00\x00\x00\x01'
         server = MockupUDPServer()
-        server.send_message(udp_connection.udp_client, out_message)
+        server.send_message(self.udp_connection.udp_client, out_message)
 
-        udp_connection.receive_check()
+        data, data_size = self.udp_connection.udp_client.receive_packet(0)
+        packet = self.udp_connection.receive_check(self.udp_connection.udp_client.sender,
+                                          data, data_size)
         assert server.rec_buffer == '', "ERROR: server has message without " + \
                     "receiving one"
-        udp_connection.process_acks()
+        self.udp_connection.process_acks()
         assert server.rec_buffer != '', "Ack not sent"
         test_msg = '\x00' + '\x00\x00\x00\x01' + '\x00' + \
             '\xff\xff\xff\xfb' + '\x01' + '\x05\x00\x00\x00'
         assert server.rec_buffer == test_msg, "Ack received incorrect, got " + \
-               repr(server.rec_buffer)"""
+               repr(server.rec_buffer)
         
 def test_suite():
     from unittest import TestSuite, makeSuite



More information about the sldev-commits mailing list