From 7e5d6af5074e825fb06b4a3ba4fbe450729dbecd Mon Sep 17 00:00:00 2001 From: Anders Blomdell <anders.blomdell@control.lth.se> Date: Thu, 16 May 2013 19:59:12 +0200 Subject: [PATCH] Start all streams with a labcomm encoded version string. --- examples/wiki_example/example_decoder.py | 8 ++++ lib/c/labcomm.c | 2 +- lib/c/labcomm_fd_reader.c | 32 +++++++------- lib/c/labcomm_fd_writer.c | 9 ++-- lib/csharp/se/lth/control/labcomm/LabComm.cs | 4 +- .../control/labcomm/LabCommDecoderChannel.cs | 8 +++- .../control/labcomm/LabCommEncoderChannel.cs | 8 +++- lib/java/se/lth/control/labcomm/LabComm.java | 4 +- .../labcomm/LabCommDecoderChannel.java | 8 +++- .../labcomm/LabCommEncoderChannel.java | 19 ++++++-- lib/python/labcomm/LabComm.py | 4 ++ test/Makefile | 3 +- test/test_encoder_decoder.py | 44 ++++--------------- 13 files changed, 87 insertions(+), 66 deletions(-) diff --git a/examples/wiki_example/example_decoder.py b/examples/wiki_example/example_decoder.py index 6c3828b..3d40cf4 100755 --- a/examples/wiki_example/example_decoder.py +++ b/examples/wiki_example/example_decoder.py @@ -6,6 +6,14 @@ import sys class FileReader: def __init__(self, name): self.f = open(name) + 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)) + pass def read(self, count): s = self.f.read(count) diff --git a/lib/c/labcomm.c b/lib/c/labcomm.c index 260b3c6..f58199a 100644 --- a/lib/c/labcomm.c +++ b/lib/c/labcomm.c @@ -34,7 +34,7 @@ #include "labcomm_ioctl.h" #include "labcomm_dynamic_buffer_writer.h" -#define LABCOMM_VERSION "\x0bLabComm2013" +#define LABCOMM_VERSION "LabComm2013" typedef struct labcomm_sample_entry { struct labcomm_sample_entry *next; diff --git a/lib/c/labcomm_fd_reader.c b/lib/c/labcomm_fd_reader.c index 09bfc3c..f49be47 100644 --- a/lib/c/labcomm_fd_reader.c +++ b/lib/c/labcomm_fd_reader.c @@ -2,31 +2,33 @@ #include <unistd.h> #include <stdlib.h> #include <string.h> +#include "labcomm_private.h" #include "labcomm_fd_reader.h" #define BUFFER_SIZE 2048 static int fd_alloc(struct labcomm_reader *r, char *version) { - int result; -#ifndef LABCOMM_FD_OMIT_VERSION - int *fd = r->context; - char *tmp = strdup(version); + int result = 0; - read(*fd, tmp, strlen(version)); - free(tmp); -#endif + r->count = 0; + r->pos = 0; r->data = malloc(BUFFER_SIZE); - if (r->data) { - r->data_size = BUFFER_SIZE; - result = r->data_size; - } else { + if (! r->data) { r->data_size = 0; result = -ENOMEM; - } - r->count = 0; - r->pos = 0; + } else { + char *tmp; + r->data_size = BUFFER_SIZE; + tmp = labcomm_read_string(r); + if (strcmp(tmp, version) != 0) { + result = -EINVAL; + } else { + result = r->data_size; + } + free(tmp); + } return result; } @@ -43,7 +45,7 @@ static int fd_free(struct labcomm_reader *r) static int fd_fill(struct labcomm_reader *r) { - int result; + int result = 0; int *fd = r->context; if (r->pos < r->count) { diff --git a/lib/c/labcomm_fd_writer.c b/lib/c/labcomm_fd_writer.c index 57f9248..3c67869 100644 --- a/lib/c/labcomm_fd_writer.c +++ b/lib/c/labcomm_fd_writer.c @@ -3,16 +3,15 @@ #include <string.h> #include <stdlib.h> #include <stdarg.h> +#include "labcomm_private.h" #include "labcomm_fd_writer.h" #define BUFFER_SIZE 2048 +static int fd_flush(struct labcomm_writer *w); + static int fd_alloc(struct labcomm_writer *w, char *version) { -#ifndef LABCOMM_FD_OMIT_VERSION - int *fd = w->context; - write(*fd, version, strlen(version)); -#endif w->data = malloc(BUFFER_SIZE); if (! w->data) { w->error = -ENOMEM; @@ -23,6 +22,8 @@ static int fd_alloc(struct labcomm_writer *w, char *version) w->data_size = BUFFER_SIZE; w->count = BUFFER_SIZE; w->pos = 0; + labcomm_write_string(w, version); + fd_flush(w); } return w->error; diff --git a/lib/csharp/se/lth/control/labcomm/LabComm.cs b/lib/csharp/se/lth/control/labcomm/LabComm.cs index 21f613c..93efe68 100644 --- a/lib/csharp/se/lth/control/labcomm/LabComm.cs +++ b/lib/csharp/se/lth/control/labcomm/LabComm.cs @@ -2,7 +2,9 @@ namespace se.lth.control.labcomm { public class LabComm { - /* + public const string VERSION = "LabComm2013"; + + /* * Predeclared aggregate type indices */ public const int TYPEDEF = 0x01; diff --git a/lib/csharp/se/lth/control/labcomm/LabCommDecoderChannel.cs b/lib/csharp/se/lth/control/labcomm/LabCommDecoderChannel.cs index 953c910..2d6b8d4 100644 --- a/lib/csharp/se/lth/control/labcomm/LabCommDecoderChannel.cs +++ b/lib/csharp/se/lth/control/labcomm/LabCommDecoderChannel.cs @@ -13,6 +13,11 @@ namespace se.lth.control.labcomm { public LabCommDecoderChannel(Stream stream) { this.stream = stream; + String version = decodeString(); + if (version != LabComm.VERSION) { + throw new IOException("LabComm version mismatch " + + version + " != " + LabComm.VERSION); + } } public void runOne() { @@ -25,7 +30,7 @@ namespace se.lth.control.labcomm { int index = decodePacked32(); String name = decodeString(); MemoryStream signature = new MemoryStream(); - collectFlatSignature(new LabCommEncoderChannel(signature)); + collectFlatSignature(new LabCommEncoderChannel(signature, false)); registry.add(index, name, signature.ToArray()); } break; default: { @@ -156,7 +161,6 @@ namespace se.lth.control.labcomm { } public String decodeString() { - //int length = (int)ReadInt(4); int length = decodePacked32(); byte[] buf = new byte[length]; ReadBytes(buf, length); diff --git a/lib/csharp/se/lth/control/labcomm/LabCommEncoderChannel.cs b/lib/csharp/se/lth/control/labcomm/LabCommEncoderChannel.cs index d13a236..6cabb59 100644 --- a/lib/csharp/se/lth/control/labcomm/LabCommEncoderChannel.cs +++ b/lib/csharp/se/lth/control/labcomm/LabCommEncoderChannel.cs @@ -12,8 +12,14 @@ namespace se.lth.control.labcomm { private LabCommEncoderRegistry registry = new LabCommEncoderRegistry(); byte[] buf = new byte[8]; - public LabCommEncoderChannel(Stream writer) { + public LabCommEncoderChannel(Stream writer, bool emitVersion) { this.writer = writer; + if (emitVersion) { + encodeString(LabComm.VERSION); + } + } + + public LabCommEncoderChannel(Stream writer) : this(writer, true) { } public void register(LabCommDispatcher dispatcher) { diff --git a/lib/java/se/lth/control/labcomm/LabComm.java b/lib/java/se/lth/control/labcomm/LabComm.java index 07d1705..99c74c2 100644 --- a/lib/java/se/lth/control/labcomm/LabComm.java +++ b/lib/java/se/lth/control/labcomm/LabComm.java @@ -2,6 +2,8 @@ package se.lth.control.labcomm; public class LabComm { + public static final String VERSION = "LabComm2013"; + /* * Predeclared aggregate type indices */ @@ -23,7 +25,7 @@ public class LabComm { public static final int STRING = 0x27; /* - * Start of + * Start of user declared types */ public static final int FIRST_USER_INDEX = 0x40; diff --git a/lib/java/se/lth/control/labcomm/LabCommDecoderChannel.java b/lib/java/se/lth/control/labcomm/LabCommDecoderChannel.java index c3f6984..0fe0e82 100644 --- a/lib/java/se/lth/control/labcomm/LabCommDecoderChannel.java +++ b/lib/java/se/lth/control/labcomm/LabCommDecoderChannel.java @@ -13,6 +13,12 @@ public class LabCommDecoderChannel implements LabCommDecoder { public LabCommDecoderChannel(InputStream in) throws IOException { this.in = new DataInputStream(in); registry = new LabCommDecoderRegistry(); + String version = decodeString(); + if (! version.equals(LabComm.VERSION)) { + throw new IOException("LabComm version mismatch " + + version + " != " + LabComm.VERSION); + } + System.err.println(LabComm.VERSION); } public void runOne() throws Exception { @@ -25,7 +31,7 @@ public class LabCommDecoderChannel implements LabCommDecoder { int index = decodePacked32(); String name = decodeString(); ByteArrayOutputStream signature = new ByteArrayOutputStream(); - collectFlatSignature(new LabCommEncoderChannel(signature)); + collectFlatSignature(new LabCommEncoderChannel(signature, false)); registry.add(index, name, signature.toByteArray()); } break; default: { diff --git a/lib/java/se/lth/control/labcomm/LabCommEncoderChannel.java b/lib/java/se/lth/control/labcomm/LabCommEncoderChannel.java index 204f6cc..d9b163e 100644 --- a/lib/java/se/lth/control/labcomm/LabCommEncoderChannel.java +++ b/lib/java/se/lth/control/labcomm/LabCommEncoderChannel.java @@ -12,15 +12,28 @@ public class LabCommEncoderChannel implements LabCommEncoder { private DataOutputStream data; private LabCommEncoderRegistry registry; - public LabCommEncoderChannel(LabCommWriter writer) { + public LabCommEncoderChannel(LabCommWriter writer, + boolean emitVersion) throws IOException { this.writer = writer; bytes = new ByteArrayOutputStream(); data = new DataOutputStream(bytes); registry = new LabCommEncoderRegistry(); + if (emitVersion) { + encodeString(LabComm.VERSION); + } + } + + public LabCommEncoderChannel(LabCommWriter writer) throws IOException { + this(writer, true); + } + + public LabCommEncoderChannel(OutputStream writer, + boolean emitVersion) throws IOException { + this(new WriterWrapper(writer), emitVersion); } - public LabCommEncoderChannel(OutputStream writer) { - this(new WriterWrapper(writer)); + public LabCommEncoderChannel(OutputStream writer) throws IOException { + this(new WriterWrapper(writer), true); } public void register(LabCommDispatcher dispatcher) throws IOException { diff --git a/lib/python/labcomm/LabComm.py b/lib/python/labcomm/LabComm.py index b137925..604b901 100644 --- a/lib/python/labcomm/LabComm.py +++ b/lib/python/labcomm/LabComm.py @@ -95,6 +95,8 @@ import struct as packer +VERSION = "LabComm2013" + i_TYPEDEF = 0x01 i_SAMPLE = 0x02 @@ -537,6 +539,7 @@ class Encoder(Codec): def __init__(self, writer): super(Encoder, self).__init__() self.writer = writer + self.writer.start(self, VERSION) def pack(self, format, *args): self.writer.write(packer.pack(format, *args)) @@ -609,6 +612,7 @@ class Decoder(Codec): def __init__(self, reader): super(Decoder, self).__init__() self.reader = reader + self.reader.start(self, VERSION) def unpack(self, format): size = packer.calcsize(format) diff --git a/test/Makefile b/test/Makefile index 0bce5a9..0edf460 100644 --- a/test/Makefile +++ b/test/Makefile @@ -2,7 +2,7 @@ TESTS=basic simple nested LABCOMM_JAR=../compiler/labComm.jar LABCOMM=java -jar $(LABCOMM_JAR) -CFLAGS=-O3 -Wall -Werror +CFLAGS=-O3 -g -Wall -Werror all: @@ -52,7 +52,6 @@ gen/%/c_relay.c: gen/%/typeinfo relay_gen_c.py Makefile .PRECIOUS: gen/%/c_relay gen/%/c_relay: gen/%/c_relay.c gen/%/c_code.c Makefile $(CC) $(CFLAGS) -o $@ $< -I../lib/c -I. \ - -DLABCOMM_FD_OMIT_VERSION \ -DLABCOMM_ENCODER_LINEAR_SEARCH \ gen/$*/c_code.c \ ../lib/c/labcomm.c \ diff --git a/test/test_encoder_decoder.py b/test/test_encoder_decoder.py index 96aaec8..6d8d4bc 100755 --- a/test/test_encoder_decoder.py +++ b/test/test_encoder_decoder.py @@ -12,40 +12,6 @@ import subprocess import sys import threading -class hexwriter(object): - def __init__(self, outfile): - self.pos = 0 - self.ascii = '' - self.outfile = outfile - - def write(self, data): - for c in data: - if ' ' <= c and c <= '}': - self.ascii += c - else: - self.ascii += '.' - sys.stdout.write("%2.2x " % ord(c)) - self.pos += 1 - if self.pos >= 15: - sys.stdout.write("%s\n" % self.ascii) - self.pos = 0 - self.ascii = "" - #self.outfile.write(data) - - def mark(self): - self.flush() - pass - - def flush(self): - for i in range(self.pos, 15): - sys.stdout.write(" ") - sys.stdout.write("%s\n" % self.ascii) - self.pos = 0 - self.ascii = "" - if self.outfile: - self.outfile.flush() - - def generate(decl): if decl.__class__ == labcomm.sample: result = [] @@ -162,6 +128,9 @@ class Test: decoder = threading.Thread(target=self.decode, args=(p.stdout,)) decoder.start() class Writer: + def start(self, encoder, version): + encoder.encode_string(version) + pass def write(self, data): p.stdin.write(data) pass @@ -169,7 +138,6 @@ class Test: p.stdin.flush() pass pass - h = hexwriter(p.stdin) encoder = labcomm.Encoder(Writer()) for name,signature in self.signatures: encoder.add_decl(signature) @@ -197,6 +165,12 @@ class Test: def decode(self, f): class Reader: + def start(self, decoder, version): + other_version = decoder.decode_string() + if version != other_version: + raise Exception("LabComm version mismatch %s != %s" % + (version, other_version)) + pass def read(self, count): result = f.read(count) if len(result) == 0: -- GitLab