diff --git a/examples/user_types/Decoder.java b/examples/user_types/Decoder.java index a0e7521a08bfd41867e4371bf6651657bca0303c..9ddede113e48ba3d786c0470496a9f033a5640bc 100644 --- a/examples/user_types/Decoder.java +++ b/examples/user_types/Decoder.java @@ -34,12 +34,12 @@ public class Decoder theSecondInt.register(decoder, this); intAndRef.register(decoder, this); doavoid.registerSampleRef(decoder); - this.tdp = TypeDefParser.registerTypeDefParser(decoder); +// this.tdp = TypeDefParser.registerTypeDefParser(decoder); // TypeDef.register(decoder, this); // TypeBinding.register(decoder, this); - tdp.addListener(this); +// tdp.addListener(this); try { System.out.println("Running decoder."); diff --git a/examples/user_types/test.lc b/examples/user_types/test.lc index 05b94c9c2c8e0d9d9ef4c6c7b525d8df41e4909b..79a80754206725f7429b3f55b8bf16092cf893cd 100644 --- a/examples/user_types/test.lc +++ b/examples/user_types/test.lc @@ -1,14 +1,14 @@ typedef struct { - (foo:bar) int val; + int val; } coord; typedef int anInt; typedef void avoid; -sample (function:trigger)(foo:bar) avoid doavoid; +sample avoid doavoid; -sample (a:b) "a struct with an int and a ref" struct { - (c:d)(e:f) int x; +sample struct { + int x; sample reference; } intAndRef; diff --git a/lib/java/se/lth/control/labcomm2014/DecoderChannel.java b/lib/java/se/lth/control/labcomm2014/DecoderChannel.java index 0e3ac6e5210dd8e67499e1a48222b456c959c89e..013dc681229c6822b2cd2ad9f78f83d3f098aeb4 100644 --- a/lib/java/se/lth/control/labcomm2014/DecoderChannel.java +++ b/lib/java/se/lth/control/labcomm2014/DecoderChannel.java @@ -23,7 +23,7 @@ public class DecoderChannel implements Decoder { byte[] signature = new byte[signature_length]; ReadBytes(signature, signature_length); def_registry.add(index, name, signature); - } + } private void processSampleRef() throws IOException { int index = decodePacked32(); @@ -32,17 +32,17 @@ public class DecoderChannel implements Decoder { byte[] signature = new byte[signature_length]; ReadBytes(signature, signature_length); ref_registry.add(index, name, signature); - } + } private void processTypeDef(int len) throws IOException { try { processSample(Constant.TYPE_DEF); } catch(Exception ex) { int idx = decodePacked32(); - String name = decodeString(); + String name = decodeString(); int siglen = decodePacked32(); for(int i=0; i<siglen; i++) { - byte b = decodeByte(); + byte b = decodeByte(); } } } @@ -52,14 +52,14 @@ public class DecoderChannel implements Decoder { processSample(Constant.TYPE_BINDING); } catch(Exception ex) { for(int i=0; i<len; i++) { - decodeByte(); + decodeByte(); } - } + } } private void processPragma(int len) throws IOException { for(int i=0; i<len; i++) { - decodeByte(); + decodeByte(); } } @@ -84,7 +84,7 @@ public class DecoderChannel implements Decoder { } catch (Exception ex) { ex.printStackTrace(); } - } + } public void runOne() throws Exception { boolean done = false; @@ -129,7 +129,7 @@ public class DecoderChannel implements Decoder { } } - public void register(SampleDispatcher dispatcher, + public void register(SampleDispatcher dispatcher, SampleHandler handler) throws IOException { def_registry.add(dispatcher, handler); } @@ -158,7 +158,7 @@ public class DecoderChannel implements Decoder { public byte decodeByte() throws IOException { return in.readByte(); } - + public short decodeShort() throws IOException { return in.readShort(); } @@ -215,17 +215,17 @@ public class DecoderChannel implements Decoder { } } - + /* Package visible methods for use from TypeDefParser */ String getSampleName(int idx) { - DecoderRegistry.Entry e = def_registry.get(idx); - return e.getName(); + DecoderRegistry.Entry e = def_registry.get(idx); + return e.getName(); } byte[] getSampleSignature(int idx) { - DecoderRegistry.Entry e = def_registry.get(idx); - return e.getSignature(); + DecoderRegistry.Entry e = def_registry.get(idx); + return e.getSignature(); } } diff --git a/lib/java/se/lth/control/labcomm2014/TypeDefParser.java b/lib/java/se/lth/control/labcomm2014/TypeDefParser.java index 4175bbca93292dd924dcccc0c4b1bb7ada7c6fd9..0998b26e22f87902d4e406204f83d1dd3cf2a42a 100644 --- a/lib/java/se/lth/control/labcomm2014/TypeDefParser.java +++ b/lib/java/se/lth/control/labcomm2014/TypeDefParser.java @@ -22,7 +22,7 @@ public class TypeDefParser implements TypeDef.Handler, TypeBinding.Handler { private int sampleIndex; private Decoder decoder; private byte[] dummy = new byte[0]; - public String toString() {return "self";} + public String toString() {return "self";} public String getName() { if(decoder instanceof DecoderChannel) { DecoderChannel dc = (DecoderChannel) decoder; @@ -30,7 +30,7 @@ public class TypeDefParser implements TypeDef.Handler, TypeBinding.Handler { } else { return "self"; } - } + } public int getIndex() {return 0;} public byte[] getSignature() { if(decoder instanceof DecoderChannel) { @@ -142,7 +142,7 @@ public class TypeDefParser implements TypeDef.Handler, TypeBinding.Handler { result.add(sd.getType()); result.add(new NameSymbol(sd.getName())); - Iterator<ParsedTypeDef> di = sd.getDepIterator(); + Iterator<ParsedTypeDef> di = sd.getDepIterator(); while(di.hasNext()) { ParsedTypeDef d = di.next(); result.add(new TypeSymbol()); @@ -164,7 +164,7 @@ public class TypeDefParser implements TypeDef.Handler, TypeBinding.Handler { return sb.toString(); } - /* An interface for using Visitor pattern to traverse + /* An interface for using Visitor pattern to traverse * ParsedTypeDefs */ public interface ParsedSymbolVisitor { @@ -209,7 +209,7 @@ public class TypeDefParser implements TypeDef.Handler, TypeBinding.Handler { this.name = name; } - public String toString() { + public String toString() { return name; } public void accept(ParsedSymbolVisitor v){ @@ -226,7 +226,7 @@ public class TypeDefParser implements TypeDef.Handler, TypeBinding.Handler { // v.visit(this); // } // -// public String toString() { +// public String toString() { // return "sample";} // } @@ -272,7 +272,7 @@ public class TypeDefParser implements TypeDef.Handler, TypeBinding.Handler { this.name = "sample"; break; default: - this.name = "??? unknown tag 0x"+Integer.toHexString(tag); + this.name = "??? unknown tag 0x"+Integer.toHexString(tag); } } @@ -280,7 +280,7 @@ public class TypeDefParser implements TypeDef.Handler, TypeBinding.Handler { v.visit(this); } - public String toString() { + public String toString() { return name;} } @@ -311,7 +311,7 @@ public class TypeDefParser implements TypeDef.Handler, TypeBinding.Handler { sb.append("struct {\n"); for(ParsedField f : fields) { sb.append(f.toString()); - sb.append(";\n"); + sb.append(";\n"); } sb.append("}"); return sb.toString(); @@ -457,7 +457,7 @@ public class TypeDefParser implements TypeDef.Handler, TypeBinding.Handler { type.accept(v); } } - + public class ParsedSampleDef extends ParsedTypeDef{ private HashSet<ParsedTypeDef> deps; @@ -480,7 +480,7 @@ public class TypeDefParser implements TypeDef.Handler, TypeBinding.Handler { Iterator<ParsedTypeDef> getDepIterator() { return deps.iterator(); - } + } } private class ParserState { @@ -504,12 +504,12 @@ public class TypeDefParser implements TypeDef.Handler, TypeBinding.Handler { this(); pushType(typeIdx); } - + ParserState(TypeDef td) { this(); pushType(td); } - + ParserState(byte sig[]) { this(); bis= new ByteArrayInputStream(sig); @@ -524,7 +524,7 @@ public class TypeDefParser implements TypeDef.Handler, TypeBinding.Handler { void pushType(int typeIdx) { if(typeIdx >= 0x40 && !typeStack.contains(typeIdx)) { typeStack.push(typeDefs.get(typeIdx)); - } + } } void popType() { @@ -569,6 +569,12 @@ public class TypeDefParser implements TypeDef.Handler, TypeBinding.Handler { return (int) (res & 0xffffffff); } + + void skipBytes(int len) throws IOException { + for(int i=0; i<len; i++) { + in.readByte(); + } + } } public ParsedSampleDef parseSignature(TypeDef td) throws IOException{ @@ -586,19 +592,19 @@ public class TypeDefParser implements TypeDef.Handler, TypeBinding.Handler { System.out.println("EOF: self_binding"); } return result; - } + } private ArrayType parseArray(ParserState in) throws IOException { int numIdx = in.decodePacked32(); int idx[] = new int[numIdx]; for(int i=0; i<numIdx; i++){ - idx[i] = in.decodePacked32(); + idx[i] = in.decodePacked32(); } int type = in.decodePacked32(); - ParsedType elementType = lookupType(type, in); + ParsedType elementType = lookupType(type, in); ArrayType result = new ArrayType(idx, elementType); for(int i=0; i<numIdx; i++){ - idx[i] = in.decodePacked32(); + idx[i] = in.decodePacked32(); } return result; } @@ -625,7 +631,7 @@ public class TypeDefParser implements TypeDef.Handler, TypeBinding.Handler { in.pushType(tag); // sampleRefs are sent as primitive types, see above // } else if(tag == Constant.SAMPLE) { -// result = new SampleRefType(); +// result = new SampleRefType(); } else { result = new PrimitiveType(tag); } @@ -642,6 +648,13 @@ public class TypeDefParser implements TypeDef.Handler, TypeBinding.Handler { return result; } private ParsedType parseType(ParserState in) throws IOException { + String intentions = in.decodeString(); + if(intentions.length()>0) { + System.out.println("parseType intentions ("+intentions); + } else { + System.out.println("no intentions"); + } + int tag = in.decodePacked32(); ParsedType result = null; switch(tag) { @@ -657,7 +670,7 @@ public class TypeDefParser implements TypeDef.Handler, TypeBinding.Handler { default: result = lookupType(tag, in); break; - } + } return result; } } diff --git a/lib/python/labcomm2014/LabComm.py b/lib/python/labcomm2014/LabComm.py index abf071b742f1571105b087a95b279b3495ba6110..5a279f15563f72b1111d8a288e33d02fafe15943 100644 --- a/lib/python/labcomm2014/LabComm.py +++ b/lib/python/labcomm2014/LabComm.py @@ -88,7 +88,7 @@ # | user data # | ... # +----+-- -# +# # # LabComm2006 packets has the following layout # @@ -122,7 +122,7 @@ # | user data # | ... # +----+-- -# +# #?? | field1 name #?? | (length + UTF8)... @@ -160,12 +160,12 @@ #?? | fieldN data | #?? | ... | #?? +----+----+----+----+ -# -# -# type numbers and lengths do not have a fixed lenght, but are packed into -# sequences of 7 bit chunks, represented in bytes with the high bit meaning +# +# +# type numbers and lengths do not have a fixed lenght, but are packed into +# sequences of 7 bit chunks, represented in bytes with the high bit meaning # that more data is to come. - + import types import struct as packer @@ -175,8 +175,8 @@ DEFAULT_VERSION = "LabComm2014" i_VERSION = 0x01 i_SAMPLE_DEF = 0x02 i_SAMPLE_REF = 0x03 -i_TYPE_DEF = 0x04 -i_TYPE_BINDING= 0x05 +i_TYPE_DEF = 0x04 +i_TYPE_BINDING= 0x05 i_PRAGMA = 0x3f i_USER = 0x40 # ..0xffffffff @@ -184,7 +184,7 @@ i_USER = 0x40 # ..0xffffffff i_ARRAY = 0x10 i_STRUCT = 0x11 -i_BOOLEAN = 0x20 +i_BOOLEAN = 0x20 i_BYTE = 0x21 i_SHORT = 0x22 i_INTEGER = 0x23 @@ -237,7 +237,7 @@ class primitive(type_decl): def __ne__(self, other): return not self.__eq__(other) - + def __hash__(self): return hash(self.__class__) @@ -247,7 +247,7 @@ class BOOLEAN(primitive): def encode(self, encoder, value): return encoder.encode_boolean(value) - + def decode(self, decoder, obj=None): return decoder.decode_boolean() @@ -263,10 +263,10 @@ class BYTE(primitive): def encode(self, encoder, value): return encoder.encode_byte(value) - + def decode(self, decoder, obj=None): return decoder.decode_byte() - + def new_instance(self): return 0 @@ -279,10 +279,10 @@ class SHORT(primitive): def encode(self, encoder, value): return encoder.encode_short(value) - + def decode(self, decoder, obj=None): return decoder.decode_short() - + def new_instance(self): return 0 @@ -295,10 +295,10 @@ class INTEGER(primitive): def encode(self, encoder, value): return encoder.encode_int(value) - + def decode(self, decoder, obj=None): return decoder.decode_int() - + def new_instance(self): return 0 @@ -311,10 +311,10 @@ class LONG(primitive): def encode(self, encoder, value): return encoder.encode_long(value) - + def decode(self, decoder, obj=None): return decoder.decode_long() - + def new_instance(self): return long(0) @@ -327,10 +327,10 @@ class FLOAT(primitive): def encode(self, encoder, value): return encoder.encode_float(value) - + def decode(self, decoder, obj=None): return decoder.decode_float() - + def new_instance(self): return 0.0 @@ -343,10 +343,10 @@ class DOUBLE(primitive): def encode(self, encoder, value): return encoder.encode_double(value) - + def decode(self, decoder, obj=None): return decoder.decode_double() - + def new_instance(self): return 0.0 @@ -359,10 +359,10 @@ class STRING(primitive): def encode(self, encoder, value): return encoder.encode_string(value) - + def decode(self, decoder, obj=None): return decoder.decode_string() - + def new_instance(self): return "" @@ -379,7 +379,7 @@ class SAMPLE(primitive): # Probably trying to encode a sample class, grab signature value = value.signature return encoder.encode_int(encoder.ref_to_index.get(value, 0)) - + def decode(self, decoder, obj=None): return decoder.decode_ref() @@ -403,6 +403,7 @@ class sampledef_or_sampleref_or_typedef(type_decl): e1.encode_type(self.get_index(encoder)) e1.encode_string(self.name) with length_encoder(e1) as e2: + self.encode_empty_intentions(e2) self.decl.encode_decl(e2) def encode(self, encoder, value): @@ -413,6 +414,7 @@ class sampledef_or_sampleref_or_typedef(type_decl): name = decoder.decode_string() if usePacketLength(decoder.version): length = decoder.decode_packed32() + self.decode_intentions(decoder) decl = decoder.decode_decl() result = self.__class__.__new__(self.__class__) result.__init__(name=name, decl=decl) @@ -428,16 +430,16 @@ class sampledef_or_sampleref_or_typedef(type_decl): return self.decl.new_instance() def __eq__(self, other): - return (self.__class__ == other.__class__ and + return (self.__class__ == other.__class__ and self.name == other.name and self.decl == other.decl) - + def __ne__(self, other): return not self.__eq__(other) def __hash__(self): return hash(self.__class__) ^ hash(self.name) ^ hash(self.decl) - + def __repr__(self): return "%s('%s', %s)" % (self.type_name, self.name, self.decl) @@ -453,12 +455,17 @@ class sample_def(sampledef_or_sampleref_or_typedef): def rename(self, name): return sample_def(name=name, decl=self.decl) - - + + def decode_intentions(self, d): + return d.decode_string() + + def encode_empty_intentions(self, e): + e.encode_empty_intentions() + class sample_ref(sampledef_or_sampleref_or_typedef): type_index = i_SAMPLE_REF type_name = 'sample_ref' - + def __init__(self, name=None, decl=None, sample=None): self.name = name self.decl = decl @@ -467,6 +474,12 @@ class sample_ref(sampledef_or_sampleref_or_typedef): else: self.sample = sample + def decode_intentions(self, d): + return d.decode_string() + + def encode_empty_intentions(self, e): + e.encode_empty_intentions() + def get_index(self, encoder): return encoder.ref_to_index[self.sample] @@ -477,6 +490,12 @@ class typedef(sampledef_or_sampleref_or_typedef): type_index = i_TYPE_DEF type_name = 'typedef' + def decode_intentions(self, d): + pass + + def encode_empty_intentions(self, e): + pass + def encode_decl(self, encoder): self.decl.encode_decl(encoder) @@ -487,18 +506,18 @@ class array(type_decl): def __init__(self, indices, decl): self.indices = tuple(indices) self.decl = decl - + def __eq__(self, other): - return (self.__class__ == other.__class__ and + return (self.__class__ == other.__class__ and self.indices == other.indices and self.decl == other.decl) - + def __ne__(self, other): return not self.__eq__(other) def __hash__(self): return hash(self.__class__) ^ hash(self.indices) ^ hash(self.decl) - + def encode_decl(self, encoder): encoder.encode_type(i_ARRAY) encoder.encode_packed32(len(self.indices)) @@ -526,7 +545,7 @@ class array(type_decl): pass except TypeError: pass - return shape + return shape def shape(self, l): shape = self.min_max_shape(l, 0, []) @@ -537,7 +556,7 @@ class array(type_decl): result.append(low) return result - + def encode_indices(self, encoder, value): depth = len(self.indices) shape = self.shape(value) @@ -558,7 +577,7 @@ class array(type_decl): self.encode_value(encoder, e, depth - 1) else: self.decl.encode(encoder, value) - + def encode(self, encoder, value): depth = self.encode_indices(encoder, value) self.encode_value(encoder, value, depth) @@ -597,7 +616,7 @@ class array(type_decl): for i in range(indices[0]): result.append(self.new_instance_value(indices[1:])) return result - + def new_instance(self): indices = [] for i in self.indices: @@ -605,19 +624,19 @@ class array(type_decl): i = decoder.decode_packed32() indices.append(i) return self.new_instance_value(indices) - + def __repr__(self): return "labcomm.array(%s,\n %s)" % ( self.indices, indent(4, self.decl.__repr__())) - + class struct(type_decl): def __init__(self, field): self.field = tuple(field) def __eq__(self, other): - return (self.__class__ == other.__class__ and + return (self.__class__ == other.__class__ and self.field == other.field) - + def __ne__(self, other): return not self.__eq__(other) @@ -628,6 +647,7 @@ class struct(type_decl): encoder.encode_type(i_STRUCT) encoder.encode_packed32(len(self.field)) for (name, decl) in self.field: + encoder.encode_empty_intentions() encoder.encode_string(name) encoder.encode_type_number(decl) @@ -643,6 +663,7 @@ class struct(type_decl): n_field = decoder.decode_packed32() field = [] for i in range(n_field): + intentions = decoder.decode_intentions() name = decoder.decode_string() decl = decoder.decode_decl() field.append((name, decl)) @@ -660,7 +681,7 @@ class struct(type_decl): for (name, decl) in self.field: result.__setattr__(name, decl.new_instance()) return result - + def __repr__(self): delim = "" result = "labcomm.struct([" @@ -720,7 +741,7 @@ class Codec(object): self.add_decl(DOUBLE(), i_DOUBLE) self.add_decl(STRING(), i_STRING) self.add_decl(SAMPLE(), i_SAMPLE) - + def add_decl(self, decl, index=0): if index == 0: if decl in self.decl_to_index: @@ -734,7 +755,7 @@ class Codec(object): except: pass return True - + def add_ref(self, ref, index=0): if not isinstance(ref, type_decl): # Probably trying to register a sample class, grab signature @@ -751,7 +772,7 @@ class Codec(object): except: pass return True - + def add_binding(self, name, decl): self.type_to_name[decl] = name self.name_to_type[name] = decl @@ -765,7 +786,7 @@ class Codec(object): if i >= i_USER and isinstance(e, sample): result.append(e) return result - + class Encoder(Codec): def __init__(self, writer, version=DEFAULT_VERSION, codec=None): @@ -779,7 +800,7 @@ class Encoder(Codec): elif self.version in [ None, "LabComm2006" ]: pass else: - raise Exception("Unsupported labcomm version %s" % self.version) + raise Exception("Unsupported labcomm version %s" % self.version) def pack(self, format, *args): self.writer.write(packer.pack(format, *args)) @@ -792,7 +813,7 @@ class Encoder(Codec): if super(Encoder, self).add_decl(decl, index): decl.encode_decl(self) self.writer.mark_end(decl, None) - + def add_ref(self, ref, index=0): if not isinstance(ref, type_decl): # Trying to register a sample class @@ -803,7 +824,7 @@ class Encoder(Codec): if super(Encoder, self).add_ref(decl, index): decl.encode_decl(self) self.writer.mark_end(ref, None) - + def encode(self, object, decl=None): if decl == None: name = self.type_to_name[object.__class__] @@ -821,7 +842,7 @@ class Encoder(Codec): self.encode_type(self.decl_to_index[decl]) except KeyError: decl.encode_decl(self) - + def encode_packed32(self, v): if self.version in [ None, "LabComm2014" ]: v = v & 0xffffffff @@ -831,7 +852,7 @@ class Encoder(Codec): tmp.append(v & 0x7f | 0x80) v = v >> 7 for c in reversed(tmp): - self.encode_byte(c) + self.encode_byte(c) elif self.version == "LabComm2006" : v = v & 0xffffffff self.encode_int(v) @@ -840,7 +861,7 @@ class Encoder(Codec): def encode_type(self, index): self.encode_packed32(index) - + def encode_boolean(self, v): if v: self.pack("!b", 1) @@ -870,12 +891,16 @@ class Encoder(Codec): self.encode_packed32(len(s)); self.pack("%ds" % len(s),s) + def encode_empty_intentions(self): +# pass + self.encode_string("") + class Decoder(Codec): def __init__(self, reader, version=DEFAULT_VERSION): super(Decoder, self).__init__() self.reader = reader self.version = version - + def unpack(self, format): size = packer.calcsize(format) data = "" @@ -901,7 +926,7 @@ class Decoder(Codec): def skip_or_raise(self, length, index): if usePacketLength(self.version): self.skip(length) - else: + else: raise Exception("Invalid type index %d" % index) def decode(self): @@ -912,7 +937,7 @@ class Decoder(Codec): if index != i_VERSION: break else: - other_version = self.decode_string() + other_version = self.decode_string() if self.version != other_version: raise Exception("LabComm version mismatch %s != %s" % (version, other_version)) @@ -923,15 +948,15 @@ class Decoder(Codec): decl = self.index_to_decl[index].decode_decl(self) value = None elif index == i_TYPE_DEF: - self.skip_or_raise(length, index) + self.skip_or_raise(length, index) decl = None value = None elif index == i_TYPE_BINDING: - self.skip_or_raise(length, index) + self.skip_or_raise(length, index) decl = None value = None elif index == i_PRAGMA: - self.skip_or_raise(length, index) + self.skip_or_raise(length, index) decl = None value = None elif index < i_USER: @@ -954,7 +979,7 @@ class Decoder(Codec): result = anonymous_object() return result - + def decode_packed32(self): if self.version in [ "LabComm2013", "LabComm2014" ] : result = 0 @@ -971,28 +996,28 @@ class Decoder(Codec): def decode_type_number(self): return self.decode_packed32() - + def decode_boolean(self): return self.unpack("!b") != 0 - + def decode_byte(self): return self.unpack("!B") - + def decode_short(self): return self.unpack("!h") - + def decode_int(self): return self.unpack("!i") - + def decode_long(self): return self.unpack("!q") - + def decode_float(self): return self.unpack("!f") - + def decode_double(self): return self.unpack("!d") - + def decode_string(self): length = self.decode_packed32() return self.unpack("!%ds" % length).decode("utf8") @@ -1001,6 +1026,9 @@ class Decoder(Codec): index = self.decode_int() return self.index_to_ref.get(index, None) + def decode_intentions(self): + return self.decode_string() + class signature_reader: def __init__(self, signature): self.signature = packer.pack("!%db" % len(signature), *signature) @@ -1009,7 +1037,7 @@ class signature_reader: result = self.signature[0:count] self.signature = self.signature[count:] return result - + def decl_from_signature(signature): decoder = Decoder(signature_reader(signature)) t = decoder.decode_decl()