viff

changeset 1163:65cce3c355a0

Rewrote sendData and stringRecived to use a simpler format. The new format includes the length of the data as well for easier unpacking.
author Martin Geisler <mg@cs.au.dk>
date Wed, 15 Apr 2009 00:07:07 +0200
parents 83282f6e20b9
children a9119cc75e11
files viff/runtime.py
diffstat 1 files changed, 9 insertions(+), 13 deletions(-) [+]
line diff
     1.1 --- a/viff/runtime.py	Tue Apr 14 23:30:34 2009 +0200
     1.2 +++ b/viff/runtime.py	Wed Apr 15 00:07:07 2009 +0200
     1.3 @@ -302,17 +302,13 @@
     1.4                      self.transport.loseConnection()
     1.5              self.factory.identify_peer(self)
     1.6          else:
     1.7 -            # TODO: we cannot handle the empty string
     1.8 -            # also note that we cannot handle pcs longer than 256
     1.9 -            pc_size = ord(string[0])
    1.10 -            fmt = "%di" % (pc_size + 1)
    1.11 -            predata_size = struct.calcsize(fmt) + 1
    1.12 -            fmt = "%s%is" % (fmt, len(string)-predata_size)
    1.13 -
    1.14 -            unpacked = struct.unpack(fmt, string[1:])
    1.15 +            # TODO: We cannot handle the empty string.
    1.16 +            pc_size, data_size, data_type = struct.unpack("!HHB", string[:5])
    1.17 +            fmt = "!%dI%ds" % (pc_size, data_size)
    1.18 +            unpacked = struct.unpack(fmt, string[5:])
    1.19  
    1.20              program_counter = unpacked[:pc_size]
    1.21 -            data_type, data = unpacked[-2:]
    1.22 +            data = unpacked[-1]
    1.23  
    1.24              key = (program_counter, data_type)
    1.25  
    1.26 @@ -327,10 +323,10 @@
    1.27  
    1.28      def sendData(self, program_counter, data_type, data):
    1.29          pc_size = len(program_counter)
    1.30 -        fmt = "%di%ds" % (pc_size + 1, len(data))
    1.31 -        data_tuple = program_counter + (data_type, data)
    1.32 -
    1.33 -        self.sendString(chr(pc_size) + struct.pack(fmt, *data_tuple))
    1.34 +        data_size = len(data)
    1.35 +        fmt = "!HHB%dI%ds" % (pc_size, data_size)
    1.36 +        t = (pc_size, data_size, data_type) + program_counter + (data,)
    1.37 +        self.sendString(struct.pack(fmt, *t))
    1.38  
    1.39      def sendShare(self, program_counter, share):
    1.40          """Send a share.