From 588a5cccb5ac25a084e43f7b134b79f7c2cf6c17 Mon Sep 17 00:00:00 2001
From: Sven Gestegard Robertz <sven.robertz@cs.lth.se>
Date: Fri, 7 Feb 2014 22:35:56 +0100
Subject: [PATCH] Java version 2006 working for simple example

---
 compiler/Java_CodeGen.jrag                    | 18 ++--
 examples/simple/Decoder06.java                | 83 +++++++++++++++++++
 examples/simple/Encoder06.java                | 62 ++++++++++++++
 examples/simple/compile.sh                    |  2 +-
 .../labcomm2006/LabCommEncoderChannel.java    |  4 +-
 5 files changed, 158 insertions(+), 11 deletions(-)
 create mode 100644 examples/simple/Decoder06.java
 create mode 100644 examples/simple/Encoder06.java

diff --git a/compiler/Java_CodeGen.jrag b/compiler/Java_CodeGen.jrag
index e3bdc22..7b7aab4 100644
--- a/compiler/Java_CodeGen.jrag
+++ b/compiler/Java_CodeGen.jrag
@@ -9,6 +9,7 @@ aspect Java_CodeGenEnv {
   public class Java_env {
 
     public final int version;  //labcomm version to generate code for
+    public final String verStr;
     private int indent;
     private int depth;
     private Java_printer printer;
@@ -78,6 +79,7 @@ aspect Java_CodeGenEnv {
 
     private Java_env(int version, int indent) {
       this.version = version; 
+      this.verStr = (version == 2006) ? "2006" : "";
       this.indent = indent;
     }
 
@@ -337,9 +339,9 @@ aspect Java_Class {
       }
 
       env.println("import java.io.IOException;");
-      env.println("import se.lth.control.labcomm.LabCommType;");
-      env.println("import se.lth.control.labcomm.LabCommEncoder;");
-      env.println("import se.lth.control.labcomm.LabCommDecoder;");
+      env.println("import se.lth.control.labcomm"+env.verStr+".LabCommType;");
+      env.println("import se.lth.control.labcomm"+env.verStr+".LabCommEncoder;");
+      env.println("import se.lth.control.labcomm"+env.verStr+".LabCommDecoder;");
       env.println();
       env.println("public class " + getName() + " implements LabCommType {");
       env.println();
@@ -377,11 +379,11 @@ aspect Java_Class {
     }
 
     env.println("import java.io.IOException;");
-    env.println("import se.lth.control.labcomm.LabCommDecoder;");
-    env.println("import se.lth.control.labcomm.LabCommDispatcher;");
-    env.println("import se.lth.control.labcomm.LabCommEncoder;");
-    env.println("import se.lth.control.labcomm.LabCommHandler;");
-    env.println("import se.lth.control.labcomm.LabCommSample;");
+    env.println("import se.lth.control.labcomm"+env.verStr+".LabCommDecoder;");
+    env.println("import se.lth.control.labcomm"+env.verStr+".LabCommDispatcher;");
+    env.println("import se.lth.control.labcomm"+env.verStr+".LabCommEncoder;");
+    env.println("import se.lth.control.labcomm"+env.verStr+".LabCommHandler;");
+    env.println("import se.lth.control.labcomm"+env.verStr+".LabCommSample;");
     env.println();
     env.print("public class " + getName());
 //    if(getType().isUserType()) {
diff --git a/examples/simple/Decoder06.java b/examples/simple/Decoder06.java
new file mode 100644
index 0000000..b6185d7
--- /dev/null
+++ b/examples/simple/Decoder06.java
@@ -0,0 +1,83 @@
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.InputStream;
+
+import se.lth.control.labcomm2006.LabCommDecoderChannel;
+
+public class Decoder06
+  implements theTwoInts.Handler, anotherTwoInts.Handler, IntString.Handler, TwoArrays.Handler, TwoFixedArrays.Handler
+
+{
+
+  LabCommDecoderChannel decoder;
+
+  public Decoder06(InputStream in) 
+    throws Exception 
+  {
+    decoder = new LabCommDecoderChannel(in);
+    theTwoInts.register(decoder, this);
+    anotherTwoInts.register(decoder, this);
+    IntString.register(decoder, this);
+    TwoArrays.register(decoder, this);
+    TwoFixedArrays.register(decoder, this);
+
+    try {
+      System.out.println("Running decoder.");
+      decoder.run();
+    } catch (java.io.EOFException e) {
+	System.out.println("Decoder reached end of file.");
+    }
+  }
+
+  public void printTwoInts(TwoInts d) throws java.io.IOException {
+    System.out.println("a="+d.a+", b="+d.b);
+  }
+
+  public void handle_theTwoInts(TwoInts d) throws java.io.IOException {
+    System.out.print("Got theTwoInts: ");
+    printTwoInts(d);
+  }
+
+  public void handle_anotherTwoInts(TwoInts d) throws java.io.IOException {
+    System.out.print("Got anotherheTwoInts: ");
+    printTwoInts(d);
+  }
+
+  public void handle_IntString(IntString d) throws java.io.IOException {
+    System.out.println("Got IntString, x="+d.x+", s="+d.s);
+  }
+
+  public void handle_TwoArrays(TwoArrays d) throws java.io.IOException {
+    System.out.println("Got TwoArrays:");
+    for(int i=0; i<d.fixed.length; i++) {
+	System.out.print(d.fixed[i]+" ");
+    }
+    System.out.println();
+    for(int i=0; i<d.variable[0].length; i++) {
+	System.out.print(d.variable[0][i]+" ");
+	System.out.print(d.variable[1][i]+" ");
+    }
+    System.out.println();
+  }
+
+  public void handle_TwoFixedArrays(TwoFixedArrays d) throws java.io.IOException {
+    System.out.println("Got TwoFixedArrays:");
+    for(int i=0; i<d.a.length; i++) {
+	System.out.print(d.a[i]+" ");
+    }
+    System.out.println();
+    for(int i=0; i<d.b[0].length; i++) {
+	System.out.print(d.b[0][i]+" ");
+	System.out.print(d.b[1][i]+" ");
+    }
+    System.out.println();
+  }
+
+
+  public static void main(String[] arg) throws Exception {
+    Decoder06 example = new Decoder06(
+      new FileInputStream(new File(arg[0]))
+    );
+  }
+}
+
diff --git a/examples/simple/Encoder06.java b/examples/simple/Encoder06.java
new file mode 100644
index 0000000..5d913d9
--- /dev/null
+++ b/examples/simple/Encoder06.java
@@ -0,0 +1,62 @@
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.OutputStream;
+
+import se.lth.control.labcomm2006.LabCommEncoderChannel;
+
+/**
+ * Simple encoder 
+ */
+public class Encoder06 
+{
+
+  LabCommEncoderChannel encoder;
+
+  public Encoder06(OutputStream out) 
+    throws Exception 
+  {
+    encoder = new LabCommEncoderChannel(out);
+    theTwoInts.register(encoder);
+    IntString.register(encoder);
+    TwoArrays.register(encoder);
+  }
+
+  public void doEncode() throws java.io.IOException {
+    TwoInts x = new TwoInts();
+    x.a = 17;
+    x.b = 42;
+
+    IntString y = new IntString();
+    y.x = 37;
+    y.s = "Testing, testing";
+
+    TwoArrays ta = new TwoArrays();
+    ta.fixed = new int[] {14, 25};
+//    ta.variable = new int[][] {{1,2},{0x11,0x12},{0x21,0x22},{0x31,0x32}};
+    ta.variable = new int[][] {{1,2, 3, 4},{0x21,0x22,0x23,0x24}};
+
+    System.out.println("Encoding theTwoInts, a="+x.a+", b="+x.b);
+    theTwoInts.encode(encoder, x);
+
+    System.out.println("Encoding IntString, x="+y.x+", s="+y.s);
+    IntString.encode(encoder, y);
+
+    System.out.println("Encoding TwoArrays");
+    for(int i = 0; i < ta.variable.length; i++) {
+	for(int j=0; j < ta.variable[0].length; j++)
+		System.out.println(ta.variable[i][j]);
+	System.out.println("---");
+    }
+    TwoArrays.encode(encoder, ta);
+  }
+
+
+  public static void main(String[] arg) throws Exception {
+    FileOutputStream fos = new FileOutputStream(arg[0]);
+    Encoder06 example = new Encoder06(fos);
+    example.doEncode();
+    fos.close();
+  }
+
+}
+
diff --git a/examples/simple/compile.sh b/examples/simple/compile.sh
index 009d747..94c3e67 100644
--- a/examples/simple/compile.sh
+++ b/examples/simple/compile.sh
@@ -3,7 +3,7 @@
 mkdir -p gen
 java -jar ../../compiler/labComm.jar --ver=2006 --java=gen --c=gen/simple.c --h=gen/simple.h  --python=gen/simple.py simple.lc 
 
-javac -cp ../../lib/java:. gen/*.java Encoder.java Decoder.java
+javac -cp ../../lib/java:. gen/*.java Encoder06.java Decoder06.java
 
 # gcc -Wall -Werror -I. -I../../lib/c -L../../lib/c \
 #    -o example_encoder example_encoder.c gen/simple.c \
diff --git a/lib/java/se/lth/control/labcomm2006/LabCommEncoderChannel.java b/lib/java/se/lth/control/labcomm2006/LabCommEncoderChannel.java
index e0de18d..6cd8804 100644
--- a/lib/java/se/lth/control/labcomm2006/LabCommEncoderChannel.java
+++ b/lib/java/se/lth/control/labcomm2006/LabCommEncoderChannel.java
@@ -24,7 +24,7 @@ public class LabCommEncoderChannel implements LabCommEncoder {
   }
 
   public LabCommEncoderChannel(LabCommWriter writer) throws IOException {
-    this(writer, true);
+    this(writer, false);
   }
 
   public LabCommEncoderChannel(OutputStream writer, 
@@ -33,7 +33,7 @@ public class LabCommEncoderChannel implements LabCommEncoder {
   }
 
   public LabCommEncoderChannel(OutputStream writer) throws IOException {
-    this(new WriterWrapper(writer), true);
+    this(new WriterWrapper(writer), false);
   }
 
   public void register(LabCommDispatcher dispatcher) throws IOException {
-- 
GitLab