From 17d3b32ce853909933bff89c673e5ffd388ca21d Mon Sep 17 00:00:00 2001
From: Sven Gestegard Robertz <sven.robertz@cs.lth.se>
Date: Sat, 31 Jan 2015 22:39:17 +0100
Subject: [PATCH] added handling for type bindings in Java

---
 examples/user_types/Decoder.java              |   9 +-
 lib/java/Makefile                             |   1 +
 .../lth/control/labcomm/DecoderChannel.java   |  11 +-
 .../lth/control/labcomm/DecoderRegistry.java  |   6 +
 .../se/lth/control/labcomm/TypeBinding.java   | 108 ++++++++++++++++++
 .../lth/control/labcomm2006/TypeBinding.java  |   3 +
 6 files changed, 134 insertions(+), 4 deletions(-)
 create mode 100644 lib/java/se/lth/control/labcomm/TypeBinding.java
 create mode 100644 lib/java/se/lth/control/labcomm2006/TypeBinding.java

diff --git a/examples/user_types/Decoder.java b/examples/user_types/Decoder.java
index 75a0174..e0c38f4 100644
--- a/examples/user_types/Decoder.java
+++ b/examples/user_types/Decoder.java
@@ -4,13 +4,15 @@ import java.io.InputStream;
 
 import se.lth.control.labcomm.DecoderChannel;
 import se.lth.control.labcomm.Typedef;
+import se.lth.control.labcomm.TypeBinding;
 
 public class Decoder
   implements twoLines.Handler,
              twoInts.Handler,
              theFirstInt.Handler,
              theSecondInt.Handler,
-             Typedef.Handler
+             Typedef.Handler,
+             TypeBinding.Handler
 
 {
 
@@ -25,6 +27,7 @@ public class Decoder
     theFirstInt.register(decoder, this);
     theSecondInt.register(decoder, this);
     Typedef.register(decoder, this);
+    TypeBinding.register(decoder, this);
 
     try {
       System.out.println("Running decoder.");
@@ -46,6 +49,10 @@ public class Decoder
     System.out.println("Got Typedef: "+d.getName()+"("+d.getIndex()+")");
   }
 
+  public void handle_TypeBinding(TypeBinding d) throws java.io.IOException {
+    System.out.println("Got TypeBinding: "+d.getSampleIndex()+" --> "+d.getTypeIndex()+"");
+  }
+
   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/Makefile b/lib/java/Makefile
index a12f50e..432df00 100644
--- a/lib/java/Makefile
+++ b/lib/java/Makefile
@@ -11,6 +11,7 @@ MODULES=Constant \
 	SampleHandler \
 	SampleType \
 	Typedef \
+	TypeBinding \
 	Writer \
 	WriterWrapper
 
diff --git a/lib/java/se/lth/control/labcomm/DecoderChannel.java b/lib/java/se/lth/control/labcomm/DecoderChannel.java
index 3d4c9cf..3f00f4f 100644
--- a/lib/java/se/lth/control/labcomm/DecoderChannel.java
+++ b/lib/java/se/lth/control/labcomm/DecoderChannel.java
@@ -55,10 +55,15 @@ public class DecoderChannel implements Decoder {
   }
 
   private void processTypeBinding(int len) throws IOException {
+      try {
+           processSample(Constant.TYPE_BINDING);
+      } catch(Exception ex) {
+       System.out.println(ex.getMessage());   
        //System.err.println("Got TypeBinding: skipping "+len+" bytes"); 
-       for(int i=0; i<len; i++) {
-           decodeByte();		  
-       }
+          for(int i=0; i<len; i++) {
+              decodeByte();		  
+          }
+      } 
   }
 
   private void processPragma(int len) throws IOException {
diff --git a/lib/java/se/lth/control/labcomm/DecoderRegistry.java b/lib/java/se/lth/control/labcomm/DecoderRegistry.java
index 11a1e9d..acde0c2 100644
--- a/lib/java/se/lth/control/labcomm/DecoderRegistry.java
+++ b/lib/java/se/lth/control/labcomm/DecoderRegistry.java
@@ -103,6 +103,12 @@ public class DecoderRegistry {
       byClass.put(dispatcher.getSampleClass(), e);
       byIndex.put(Integer.valueOf(Constant.TYPE_DEF), e);
       //System.out.println("LCDecoderRegistry.add("+e.getName()+", "+e.getIndex()+")");
+    } else if(dispatcher.getSampleClass() == TypeBinding.class){
+      Entry e = new Entry(dispatcher, handler);
+      e.setIndex(Constant.TYPE_BINDING);
+      byClass.put(dispatcher.getSampleClass(), e);
+      byIndex.put(Integer.valueOf(Constant.TYPE_BINDING), e);
+      //System.out.println("LCDecoderRegistry.add("+e.getName()+", "+e.getIndex()+")");
     } else {
     Entry e = byClass.get(dispatcher.getSampleClass());
     if (e != null) {
diff --git a/lib/java/se/lth/control/labcomm/TypeBinding.java b/lib/java/se/lth/control/labcomm/TypeBinding.java
new file mode 100644
index 0000000..36b84fb
--- /dev/null
+++ b/lib/java/se/lth/control/labcomm/TypeBinding.java
@@ -0,0 +1,108 @@
+package se.lth.control.labcomm;
+
+import java.io.IOException;
+import java.io.ByteArrayOutputStream;
+import java.io.ByteArrayInputStream;
+import se.lth.control.labcomm.Decoder;
+import se.lth.control.labcomm.DecoderChannel;
+import se.lth.control.labcomm.SampleDispatcher;
+import se.lth.control.labcomm.SampleHandler;
+
+public class TypeBinding implements SampleType {
+    private int sampleIndex;
+    private int typeIndex;
+
+  public int getSampleIndex() {
+    return sampleIndex;
+  }
+
+  public int getTypeIndex() {
+    return typeIndex;
+  }
+
+  public TypeBinding(int sampleIndex, int typeIndex) {
+      this.sampleIndex = sampleIndex;
+      this.typeIndex = typeIndex;
+  }
+
+  public interface Handler extends SampleHandler {
+    public void handle_TypeBinding(TypeBinding value) throws Exception;
+  }
+  
+  public static void register(Decoder d, Handler h) throws IOException {
+    d.register(Dispatcher.singleton(), h);
+  }
+  
+  public static void register(Encoder e) throws IOException {
+    register(e,false);
+  }
+  
+  public static void register(Encoder e, boolean sendMetaData) throws IOException {
+    throw new IOException("cannot send TypeDefs");
+  }
+  
+ static class Dispatcher implements SampleDispatcher<TypeBinding> {
+    
+    private static Dispatcher singleton;
+    
+    public synchronized static Dispatcher singleton() {
+      if(singleton==null) singleton=new Dispatcher();
+      return singleton;
+    }
+    
+    public Class<TypeBinding> getSampleClass() {
+      return TypeBinding.class;
+    }
+    
+    public String getName() {
+      return "TypeBinding";
+    }
+    
+    public byte getTypeDeclTag() {
+      throw new Error("Should not be called");
+    }
+    
+    public boolean isSample() {
+      throw new Error("Should not be called");
+    }
+    public boolean hasStaticSignature() {
+      throw new Error("Should not be called");
+    }
+    
+    /** return the flat signature. Intended use is on decoder side */
+    public byte[] getSignature() {
+      return null; // not used for matching
+    }
+    
+    public void encodeTypeDef(Encoder e, int index) throws IOException{
+      throw new Error("Should not be called");
+    }
+    
+//    public boolean canDecodeAndHandle() {
+//      return true;
+//    }
+    
+    public void decodeAndHandle(Decoder d,
+                                SampleHandler h) throws Exception {
+      ((Handler)h).handle_TypeBinding(TypeBinding.decode(d));
+    }
+    
+    public boolean hasDependencies() {
+        return false;
+    }
+  }
+  
+  public static void encode(Encoder e, TypeBinding value) throws IOException {
+    throw new Error("Should not be called");
+  }
+  
+  public static TypeBinding decode(Decoder d) throws IOException {
+    TypeBinding result;
+    int sampleIndex = d.decodePacked32();
+    int typeIndex = d.decodePacked32();
+
+    result = new TypeBinding(sampleIndex, typeIndex);
+    return result;
+  }
+}
+
diff --git a/lib/java/se/lth/control/labcomm2006/TypeBinding.java b/lib/java/se/lth/control/labcomm2006/TypeBinding.java
new file mode 100644
index 0000000..0a15ac9
--- /dev/null
+++ b/lib/java/se/lth/control/labcomm2006/TypeBinding.java
@@ -0,0 +1,3 @@
+public class TypeBinding {
+
+}
-- 
GitLab