diff --git a/examples/user_types/Decoder.java b/examples/user_types/Decoder.java index 07bd7ba6b5784611836dd15c9b886d164c2c1bf1..75a0174e94634594a562d71cade9fde79aa35b48 100644 --- a/examples/user_types/Decoder.java +++ b/examples/user_types/Decoder.java @@ -3,12 +3,14 @@ import java.io.FileInputStream; import java.io.InputStream; import se.lth.control.labcomm.DecoderChannel; +import se.lth.control.labcomm.Typedef; public class Decoder implements twoLines.Handler, twoInts.Handler, theFirstInt.Handler, - theSecondInt.Handler + theSecondInt.Handler, + Typedef.Handler { @@ -22,6 +24,7 @@ public class Decoder twoLines.register(decoder, this); theFirstInt.register(decoder, this); theSecondInt.register(decoder, this); + Typedef.register(decoder, this); try { System.out.println("Running decoder."); @@ -39,6 +42,10 @@ public class Decoder return "Line from "+genPoint(l.start)+" to "+genPoint(l.end); } + public void handle_Typedef(Typedef d) throws java.io.IOException { + System.out.println("Got Typedef: "+d.getName()+"("+d.getIndex()+")"); + } + public void handle_twoInts(twoInts d) throws java.io.IOException { System.out.print("Got twoInts: "); System.out.println(d.a +", "+d.b); diff --git a/lib/java/se/lth/control/labcomm/DecoderChannel.java b/lib/java/se/lth/control/labcomm/DecoderChannel.java index 57175eb806346c4b7d1a7beac54299341b72ece2..3d4c9cfe0fbbdd66d89d70258e333f1eaed84e2b 100644 --- a/lib/java/se/lth/control/labcomm/DecoderChannel.java +++ b/lib/java/se/lth/control/labcomm/DecoderChannel.java @@ -35,10 +35,14 @@ public class DecoderChannel implements Decoder { } private void processTypeDef(int len) throws IOException { + try { + processSample(Constant.TYPE_DEF); + } catch(Exception ex) { + System.out.println(ex.getMessage()); //System.err.println("Got TypeDef: skipping "+len+" bytes"); int idx = decodePacked32(); String name = decodeString(); - //System.err.println("Got TypeDef: "+idx+" "+name); + System.err.println("Ignoring (unhandled) TypeDef: "+idx+" "+name); int siglen = decodePacked32(); //System.err.println("siglen="+siglen); for(int i=0; i<siglen; i++) { @@ -47,6 +51,7 @@ public class DecoderChannel implements Decoder { //System.out.print(" "); } //System.out.println(); + } } private void processTypeBinding(int len) throws IOException { @@ -63,6 +68,29 @@ public class DecoderChannel implements Decoder { } } + private void processSample(int tag) throws IOException { + DecoderRegistry.Entry e = def_registry.get(tag); + if (e == null) { + throw new IOException("Unhandled tag " + tag); + } + SampleDispatcher d = e.getDispatcher(); + if (d == null) { + throw new IOException("No dispatcher for '" + e.getName() + "'"); + } + SampleHandler h = e.getHandler(); + if (h == null) { + throw new IOException("No handler for '" + e.getName() +"'"); + } + try { + //XXX why does decodeAndHandle throw Exception and not IOException? + d.decodeAndHandle(this, h); + } catch (IOException ex) { + throw ex; + } catch (Exception ex) { + ex.printStackTrace(); + } + } + public void runOne() throws Exception { boolean done = false; while (!done) { @@ -92,20 +120,9 @@ public class DecoderChannel implements Decoder { processPragma(length); } break; default: { - DecoderRegistry.Entry e = def_registry.get(tag); - if (e == null) { - throw new IOException("Unhandled tag " + tag); - } - SampleDispatcher d = e.getDispatcher(); - if (d == null) { - throw new IOException("No dispatcher for '" + e.getName() + "'"); - } - SampleHandler h = e.getHandler(); - if (h == null) { - throw new IOException("No handler for '" + e.getName() +"'"); - } - d.decodeAndHandle(this, h); - done = true; + processSample(tag); + done = true; + } } } diff --git a/lib/java/se/lth/control/labcomm/DecoderRegistry.java b/lib/java/se/lth/control/labcomm/DecoderRegistry.java index fdf8f591d646dad3680e7fbea48e2dcb9ed2ba15..11a1e9dbc25b47c94a27734239d564da46be3f9a 100644 --- a/lib/java/se/lth/control/labcomm/DecoderRegistry.java +++ b/lib/java/se/lth/control/labcomm/DecoderRegistry.java @@ -96,6 +96,14 @@ public class DecoderRegistry { public synchronized void add(SampleDispatcher dispatcher, SampleHandler handler) throws IOException{ + //XXX kludge: special handling of predefined types + if(dispatcher.getSampleClass() == Typedef.class){ + Entry e = new Entry(dispatcher, handler); + e.setIndex(Constant.TYPE_DEF); + byClass.put(dispatcher.getSampleClass(), e); + byIndex.put(Integer.valueOf(Constant.TYPE_DEF), e); + //System.out.println("LCDecoderRegistry.add("+e.getName()+", "+e.getIndex()+")"); + } else { Entry e = byClass.get(dispatcher.getSampleClass()); if (e != null) { e.check(dispatcher.getName(), dispatcher.getSignature()); @@ -114,6 +122,7 @@ public class DecoderRegistry { byClass.put(dispatcher.getSampleClass(), e); } } + } } public synchronized void add(int index, diff --git a/lib/java/se/lth/control/labcomm/Typedef.java b/lib/java/se/lth/control/labcomm/Typedef.java index 6928d6902d547b432b5d95b1ae9da6bd8afbad9d..3b81dfefa89daffb11105d1884a869338cdc79a9 100644 --- a/lib/java/se/lth/control/labcomm/Typedef.java +++ b/lib/java/se/lth/control/labcomm/Typedef.java @@ -21,74 +21,6 @@ public class Typedef implements SampleType { return name; } - static void collectFlatSignature(Decoder in, - DecoderRegistry registry, - Encoder out, - boolean recurse) throws IOException { - int type = in.decodePacked32(); - //System.out.println("cFS..."+in+" --- type = "+String.format("0x%02X ", type)); - switch (type) { - case Constant.ARRAY: { - out.encodePacked32(type); - int dimensions = in.decodePacked32(); - out.encodePacked32(dimensions); - for (int i = 0 ; i < dimensions ; i++) { - out.encodePacked32(in.decodePacked32()); - } - collectFlatSignature(in, registry, out, recurse); - } break; - case Constant.STRUCT: { - out.encodePacked32(type); - int fields = in.decodePacked32(); - out.encodePacked32(fields); - for (int i = 0 ; i < fields ; i++) { - out.encodeString(in.decodeString()); - collectFlatSignature(in, registry, out, recurse); - } - } break; - case Constant.BOOLEAN: - case Constant.BYTE: - case Constant.SHORT: - case Constant.INT: - case Constant.LONG: - case Constant.FLOAT: - case Constant.DOUBLE: - case Constant.STRING: { - out.encodePacked32(type); - } break; - default: { - if(recurse) { - DecoderRegistry.Entry entry = registry.get(type); - //System.out.println("...... ***** flattening signature for type "+type); - if(entry != null) { - //System.out.println("...... ***** entry "+entry.getName()+") != null"); - byte[] sig = entry.getSignature(); - - //System.out.print("...... ***** sig :"); - //for (byte b : sig) { - // System.out.print(String.format("0x%02X ", b)); - //} - //System.out.println(); - - ByteArrayInputStream bis = new ByteArrayInputStream(sig); - //System.out.println("...... ***** bis.available() :"+bis.available()); - try { - collectFlatSignature(new DecoderChannel(bis, registry), registry, out, recurse); - } catch(java.io.EOFException e) { - // When the "inner" signature is handled, we continue recursion on the outer level - collectFlatSignature(in, registry, out, recurse); - } - bis.close(); - }else { - throw new IOException("Unknown type=" + String.format("0x%02X ", type)); - } - }else { - out.encodePacked32(type); - } - } - } - } - public void dump() { System.out.print("=== Typedef "+getName()+"( "+Integer.toHexString(getIndex())+") : "); for (byte b : signature) { @@ -154,25 +86,35 @@ public class Typedef implements SampleType { // } public void decodeAndHandle(Decoder d, - Handler h) throws Exception { + SampleHandler h) throws Exception { ((Handler)h).handle_Typedef(Typedef.decode(d)); } + public boolean hasDependencies() { + return false; + } } public static void encode(Encoder e, Typedef value) throws IOException { throw new Error("Should not be called"); } + public Typedef(int index, String name, byte sig[]) { + this.index = index; + this.name = name; + this.signature = sig; + } + public static Typedef decode(Decoder d) throws IOException { Typedef result; - result = new Typedef(); - result.index = d.decodePacked32(); - result.name = d.decodeString(); - ByteArrayOutputStream signature = new ByteArrayOutputStream(); - ((DecoderChannel )d).collectFlatSignature(new EncoderChannel(signature, false), false); - result.signature = signature.toByteArray(); - + int index = d.decodePacked32(); + String name = d.decodeString(); + int siglen= d.decodePacked32(); + byte sig[] = new byte[siglen]; + for(int i=0; i<siglen;i++){ + sig[i] = d.decodeByte(); + } + result = new Typedef(index, name, sig); return result; } } diff --git a/lib/java/se/lth/control/labcomm2006/Typedef.java b/lib/java/se/lth/control/labcomm2006/Typedef.java index 32997dd75bc4b149eef5d885da7b046819e47b2c..ac8edd846743c6a9e952c28f8cfe4d9d8a34ab01 100644 --- a/lib/java/se/lth/control/labcomm2006/Typedef.java +++ b/lib/java/se/lth/control/labcomm2006/Typedef.java @@ -1,3 +1,3 @@ -public class TypeDef { +public class Typedef { }