Changeset 3293

Show
Ignore:
Timestamp:
08/02/08 02:00:19 (5 years ago)
Author:
getxsick
Message:

Packet.get_raw() is done

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • branch/UMPA/umpa/packets.py

    r3193 r3293  
    2020# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA  
    2121 
     22import struct 
     23 
     24import umpa.utils.bits 
     25from umpa.protocols._consts import BYTE 
     26 
    2227class Packet(object): 
    2328    """You have to use this class to build a completely packets. 
     
    3136        self.protos = [] 
    3237        self._add_new_protocols(protos) 
     38        self.raw = None 
     39        self.bits = 0 
    3340 
    3441    def __str__(self): 
     
    4653        self._add_new_protocols(protos) 
    4754 
    48     def _add_new_protocols(self, *protos): 
     55    def _add_new_protocols(self, protos): 
    4956        for p in protos: 
    5057            self.protos.append(p) 
     
    5259    def get_raw(self): 
    5360        """Return raw packet, in bit-mode (big-endian).""" 
    54         # TODO: calling method to pack it for each protocols 
    55         print "Not implemented yet." 
     61 
     62        self.raw = 0 
     63        self.bits = 0 
     64        proto_id = 0 
     65        for proto in reversed(self.protos): 
     66            raw_proto, bit_proto = proto.get_raw(tuple(self.protos), self.bits) 
     67            self.raw |= raw_proto << self.bits 
     68            self.bits += bit_proto 
     69        # split into chunks 
     70        # we make it because we need string for socket object 
     71        # so after that we pack it by struct module.pack() 
     72        byte_chunks = umpa.utils.bits.split_into_chunks(self.raw, self.bits) 
     73        return struct.pack('!' + 'B'*(self.bits/BYTE), *byte_chunks)