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 ...@@ -11,7 +11,7 @@ java -cp .:../../lib/java:gen Encoder encoded_data
java -cp .:../../lib/java:gen Decoder encoded_data java -cp .:../../lib/java:gen Decoder encoded_data
echo "running python decoder (from wiki_example):" 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
echo "*******************************************************" echo "*******************************************************"
...@@ -23,3 +23,6 @@ java -cp .:../../lib/java:gen06 Encoder06 encoded_data06 ...@@ -23,3 +23,6 @@ java -cp .:../../lib/java:gen06 Encoder06 encoded_data06
./example_decoder06 encoded_data06 ./example_decoder06 encoded_data06
./example_encoder06 encoded_data06 ./example_encoder06 encoded_data06
java -cp .:../../lib/java:gen06 Decoder06 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 ...@@ -4,12 +4,13 @@ import labcomm
import sys import sys
if __name__ == "__main__": 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: while True:
try: try:
data,decl = d.decode() data,decl = d.decode()
if data: if data:
print data print data
except: except Exception, e:
print e
break break
...@@ -21,4 +21,4 @@ javac -cp ../../lib/java:. *.java ...@@ -21,4 +21,4 @@ javac -cp ../../lib/java:. *.java
# Run through all executables (c->java->Python) # Run through all executables (c->java->Python)
./example_encoder one two ./example_encoder one two
java -cp ../../lib/java:. example_decoder_encoder example.encoded example.javaencoded 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 @@ ...@@ -96,7 +96,14 @@
import types import types
import struct as packer 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_TYPEDEF = 0x01
i_SAMPLE = 0x02 i_SAMPLE = 0x02
...@@ -546,10 +553,11 @@ class Codec(object): ...@@ -546,10 +553,11 @@ class Codec(object):
class Encoder(Codec): class Encoder(Codec):
def __init__(self, writer): def __init__(self, writer, version):
super(Encoder, self).__init__() super(Encoder, self).__init__()
self.writer = writer self.writer = writer
self.writer.start(self, VERSION) self.versoin = version
self.writer.start(self, self.version)
def pack(self, format, *args): def pack(self, format, *args):
self.writer.write(packer.pack(format, *args)) self.writer.write(packer.pack(format, *args))
...@@ -575,14 +583,21 @@ class Encoder(Codec): ...@@ -575,14 +583,21 @@ class Encoder(Codec):
decl.encode_decl(self) decl.encode_decl(self)
def encode_packed32(self, v): def encode_packed32(self, v):
v = v & 0xffffffff #if usePacked32(self.version) :
tmp = [ v & 0x7f ] if self.version == "LabComm2013" :
v = v >> 7 v = v & 0xffffffff
while v: tmp = [ v & 0x7f ]
tmp.append(v & 0x7f | 0x80)
v = v >> 7 v = v >> 7
for c in reversed(tmp): while v:
self.encode_byte(c) 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): def encode_type(self, index):
self.encode_packed32(index) self.encode_packed32(index)
...@@ -619,10 +634,11 @@ class Encoder(Codec): ...@@ -619,10 +634,11 @@ class Encoder(Codec):
# self.pack("!i%ds" % len(s), len(s), s) # self.pack("!i%ds" % len(s), len(s), s)
class Decoder(Codec): class Decoder(Codec):
def __init__(self, reader): def __init__(self, reader, version):
super(Decoder, self).__init__() super(Decoder, self).__init__()
self.reader = reader self.reader = reader
self.reader.start(self, VERSION) self.version = version
self.reader.start(self, version)
def unpack(self, format): def unpack(self, format):
size = packer.calcsize(format) size = packer.calcsize(format)
...@@ -664,13 +680,18 @@ class Decoder(Codec): ...@@ -664,13 +680,18 @@ class Decoder(Codec):
return result return result
def decode_packed32(self): def decode_packed32(self):
result = 0 if self.version == "LabComm2013" :
while True: result = 0
tmp = self.decode_byte() while True:
result = (result << 7) | (tmp & 0x7f) tmp = self.decode_byte()
if (tmp & 0x80) == 0: result = (result << 7) | (tmp & 0x7f)
break if (tmp & 0x80) == 0:
return result 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): def decode_type_number(self):
return self.decode_packed32() return self.decode_packed32()
......
import labcomm
class StreamReader: class StreamReader:
def __init__(self, stream): def __init__(self, stream):
...@@ -5,10 +7,13 @@ class StreamReader: ...@@ -5,10 +7,13 @@ class StreamReader:
pass pass
def start(self, decoder, version): def start(self, decoder, version):
other_version = decoder.decode_string() self.version = version;
if version != other_version: if labcomm.LabComm.sendVersionString(version):
raise Exception("LabComm version mismatch %s != %s" % other_version = decoder.decode_string()
(version, other_version)) if version != other_version:
raise Exception("LabComm version mismatch %s != %s" %
(version, other_version))
pass
pass pass
def read(self, count): def read(self, count):
......
import labcomm
class StreamWriter: class StreamWriter:
def __init__(self, stream): def __init__(self, stream):
...@@ -5,8 +7,11 @@ class StreamWriter: ...@@ -5,8 +7,11 @@ class StreamWriter:
pass pass
def start(self, encoder, version): def start(self, encoder, version):
encoder.encode_string(version) self.version = version;
pass
if labcomm.LabComm.sendVersionString(version):
encoder.encode_string(version)
pass
def write(self, data): def write(self, data):
self.stream.write(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