Commit 13d97334 authored by Sven Gestegård Robertz's avatar Sven Gestegård Robertz
Browse files

experimental python implementation of coexistence of 2006 and 2013

parent 6fc5eb7f
......@@ -11,7 +11,7 @@ java -cp .:../../lib/java:gen Encoder encoded_data
java -cp .:../../lib/java:gen Decoder encoded_data
echo "running python decoder (from wiki_example):"
PYTHONPATH=../../lib/python ../wiki_example/example_decoder.py encoded_data
PYTHONPATH=../../lib/python ../wiki_example/example_decoder.py encoded_data LabComm2013
echo
echo "*******************************************************"
......@@ -23,3 +23,6 @@ java -cp .:../../lib/java:gen06 Encoder06 encoded_data06
./example_decoder06 encoded_data06
./example_encoder06 encoded_data06
java -cp .:../../lib/java:gen06 Decoder06 encoded_data06
echo "running python decoder (from wiki_example):"
PYTHONPATH=../../lib/python ../wiki_example/example_decoder.py encoded_data06 LabComm2006
......@@ -4,12 +4,13 @@ import labcomm
import sys
if __name__ == "__main__":
d = labcomm.Decoder(labcomm.StreamReader(open(sys.argv[1])))
d = labcomm.Decoder(labcomm.StreamReader(open(sys.argv[1])), sys.argv[2])
while True:
try:
data,decl = d.decode()
if data:
print data
except:
except Exception, e:
print e
break
......@@ -21,4 +21,4 @@ javac -cp ../../lib/java:. *.java
# Run through all executables (c->java->Python)
./example_encoder one two
java -cp ../../lib/java:. example_decoder_encoder example.encoded example.javaencoded
PYTHONPATH=../../lib/python ./example_decoder.py example.javaencoded
PYTHONPATH=../../lib/python ./example_decoder.py example.javaencoded LabComm2013
......@@ -96,7 +96,14 @@
import types
import struct as packer
VERSION = "LabComm2013"
#VERSION = "LabComm2013"
# Version testing
def sendVersionString(version):
return version == "LabComm2013"
def usePacked32(version):
return version == "LabComm2013"
i_TYPEDEF = 0x01
i_SAMPLE = 0x02
......@@ -546,10 +553,11 @@ class Codec(object):
class Encoder(Codec):
def __init__(self, writer):
def __init__(self, writer, version):
super(Encoder, self).__init__()
self.writer = writer
self.writer.start(self, VERSION)
self.versoin = version
self.writer.start(self, self.version)
def pack(self, format, *args):
self.writer.write(packer.pack(format, *args))
......@@ -575,14 +583,21 @@ class Encoder(Codec):
decl.encode_decl(self)
def encode_packed32(self, v):
v = v & 0xffffffff
tmp = [ v & 0x7f ]
v = v >> 7
while v:
tmp.append(v & 0x7f | 0x80)
#if usePacked32(self.version) :
if self.version == "LabComm2013" :
v = v & 0xffffffff
tmp = [ v & 0x7f ]
v = v >> 7
for c in reversed(tmp):
self.encode_byte(c)
while v:
tmp.append(v & 0x7f | 0x80)
v = v >> 7
for c in reversed(tmp):
self.encode_byte(c)
elif self.version == "LabComm2006" :
v = v & 0xffffffff
self.encode_int(v)
else :
raise Exception("Unsupported labcomm version %s" % self.version)
def encode_type(self, index):
self.encode_packed32(index)
......@@ -619,10 +634,11 @@ class Encoder(Codec):
# self.pack("!i%ds" % len(s), len(s), s)
class Decoder(Codec):
def __init__(self, reader):
def __init__(self, reader, version):
super(Decoder, self).__init__()
self.reader = reader
self.reader.start(self, VERSION)
self.version = version
self.reader.start(self, version)
def unpack(self, format):
size = packer.calcsize(format)
......@@ -664,13 +680,18 @@ class Decoder(Codec):
return result
def decode_packed32(self):
result = 0
while True:
tmp = self.decode_byte()
result = (result << 7) | (tmp & 0x7f)
if (tmp & 0x80) == 0:
break
return result
if self.version == "LabComm2013" :
result = 0
while True:
tmp = self.decode_byte()
result = (result << 7) | (tmp & 0x7f)
if (tmp & 0x80) == 0:
break
return result
elif self.version == "LabComm2006" :
return self.decode_int()
else :
raise Exception("Unsupported labcomm version %s" % self.version)
def decode_type_number(self):
return self.decode_packed32()
......
import labcomm
class StreamReader:
def __init__(self, stream):
......@@ -5,10 +7,13 @@ class StreamReader:
pass
def start(self, decoder, version):
other_version = decoder.decode_string()
if version != other_version:
raise Exception("LabComm version mismatch %s != %s" %
(version, other_version))
self.version = version;
if labcomm.LabComm.sendVersionString(version):
other_version = decoder.decode_string()
if version != other_version:
raise Exception("LabComm version mismatch %s != %s" %
(version, other_version))
pass
pass
def read(self, count):
......
import labcomm
class StreamWriter:
def __init__(self, stream):
......@@ -5,8 +7,11 @@ class StreamWriter:
pass
def start(self, encoder, version):
encoder.encode_string(version)
pass
self.version = version;
if labcomm.LabComm.sendVersionString(version):
encoder.encode_string(version)
pass
def write(self, data):
self.stream.write(data)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment