From d6c8fa44cbe4e7cbc3bcbfd394bc3d71f935d318 Mon Sep 17 00:00:00 2001
From: Sven Gestegard Robertz <sven.robertz@cs.lth.se>
Date: Sat, 25 Oct 2014 15:45:50 +0200
Subject: [PATCH] more java decoder pragma skeleton

---
 .../lth/control/labcomm/DecoderChannel.java   | 69 +++++++++++++++----
 1 file changed, 56 insertions(+), 13 deletions(-)

diff --git a/lib/java/se/lth/control/labcomm/DecoderChannel.java b/lib/java/se/lth/control/labcomm/DecoderChannel.java
index bd2b0dd..2397a16 100644
--- a/lib/java/se/lth/control/labcomm/DecoderChannel.java
+++ b/lib/java/se/lth/control/labcomm/DecoderChannel.java
@@ -3,6 +3,7 @@ package se.lth.control.labcomm;
 import java.io.ByteArrayOutputStream;
 import java.io.DataInputStream;
 import java.io.InputStream;
+import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.EOFException;
 
@@ -11,11 +12,55 @@ public class DecoderChannel implements Decoder {
   private DataInputStream in;
   private DecoderRegistry registry;
 
-  public DecoderChannel(InputStream in) throws IOException {
+  public DecoderChannel(InputStream in, DecoderRegistry reg) throws IOException {
     this.in = new DataInputStream(in);
-    registry = new DecoderRegistry();
+    this.registry = reg;
   }
 
+  public DecoderChannel(InputStream in) throws IOException {
+    this(in, new DecoderRegistry());
+  }
+
+  private void processSampleDef() throws IOException {
+    int index = decodePacked32();
+    String name = decodeString();
+    int signature_length = decodePacked32();
+    byte[] signature = new byte[signature_length];
+    ReadBytes(signature, signature_length);
+    registry.add(index, name, signature);
+  }	   
+
+  private void processPragma(int len) throws IOException {
+       //   String pt = decodeString();
+       //   System.out.println("Pragma["+pt+"]: skipping "+plen+" bytes"); 
+       //   for(int i=0; i<plen; i++) {
+       //     decodeByte();		  
+       //   }
+    String type = decodeString();
+    int plen = len - (type.length()+1); // XXX HERE BE DRAGONS: +1?
+    System.out.println("[ begin pragma ("+type+") ]");
+    //System.out.println("metadata : "+len + " bytes, ref: " + Integer.toHexString(typeRefId)+".");
+    byte buf[] = new byte[plen];
+      for(int i=0; i<plen; i++) {
+        buf[i]=decodeByte();
+      }
+     ByteArrayInputStream bis = new ByteArrayInputStream(buf);
+     DecoderChannel pd = new DecoderChannel(bis, registry);
+     try{
+       pd.run();
+     } catch(java.io.EOFException ex) {
+       // We're done. 
+     } catch(Throwable e) {
+       // Something unexpected happened
+       // (an unregistered handler is OK, but still report it. 
+       // TODO: implement exception handling here. Aborting is fine,
+       //       that only means skipping the rest of the metadata packet.)
+       System.out.println("Exception while decoding pragma: "+e);
+     }
+    System.out.println("[ end pragma ]");
+  }
+
+
   public void runOne() throws Exception {
     boolean done = false;
     while (!done) {
@@ -30,23 +75,21 @@ public class DecoderChannel implements Decoder {
           }
         } break;
 	case Constant.SAMPLE_DEF: {
-	  int index = decodePacked32();
-	  String name = decodeString();
-          int signature_length = decodePacked32();
-          byte[] signature = new byte[signature_length];
-          ReadBytes(signature, signature_length);
-	  registry.add(index, name, signature);
+          processSampleDef();
 	} break;
 	case Constant.PRAGMA: {
-          System.out.println("Pragma: skipping "+length+" bytes"); 
-	  for(int i=0; i<length; i++) {
-            decodeByte();		  
-	  }
+          processPragma(length);
+       //   String pt = decodeString();
+       //   int plen = length - pt.length()-1;
+       //   System.out.println("Pragma["+pt+"]: skipping "+plen+" bytes"); 
+       //   for(int i=0; i<plen; i++) {
+       //     decodeByte();		  
+       //   }
 	} break;
 	default: {
 	  DecoderRegistry.Entry e = registry.get(tag);
 	  if (e == null) {
-	    throw new IOException("Unhandled tag " + tag);
+	    throw new IOException("Unhandled tag 0x" + Integer.toHexString(tag));
 	  }
 	  SampleDispatcher d = e.getDispatcher();
 	  if (d == null) {
-- 
GitLab