From 266c6d58d98c3f4b4388d51b4a51fc6984f34d96 Mon Sep 17 00:00:00 2001
From: Sven Gestegard Robertz <sven.robertz@cs.lth.se>
Date: Fri, 30 Jan 2015 23:46:40 +0100
Subject: [PATCH] handling typedefs implemented in Java

---
 examples/user_types/Decoder.java              |  9 +-
 .../lth/control/labcomm/DecoderChannel.java   | 47 +++++++---
 .../lth/control/labcomm/DecoderRegistry.java  |  9 ++
 lib/java/se/lth/control/labcomm/Typedef.java  | 94 ++++---------------
 .../se/lth/control/labcomm2006/Typedef.java   |  2 +-
 5 files changed, 68 insertions(+), 93 deletions(-)

diff --git a/examples/user_types/Decoder.java b/examples/user_types/Decoder.java
index 07bd7ba..75a0174 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 57175eb..3d4c9cf 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 fdf8f59..11a1e9d 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 6928d69..3b81dfe 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 32997dd..ac8edd8 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 {
 
 }
-- 
GitLab