Commit 7e5d6af5 authored by Anders Blomdell's avatar Anders Blomdell
Browse files

Start all streams with a labcomm encoded version string.

parent 1b0d7eb4
......@@ -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)
......
......@@ -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;
......
......@@ -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) {
......
......@@ -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;
......
......@@ -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;
......
......@@ -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);
......
......@@ -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) {
......
......@@ -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;
......
......@@ -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: {
......
......@@ -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 {
......
......@@ -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)
......
......@@ -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 \
......
......@@ -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:
......
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