From 28c454c2529f3e8a2283985336138e9c6f317668 Mon Sep 17 00:00:00 2001 From: Sven Robertz <sven@cs.lth.se> Date: Wed, 2 Nov 2011 17:50:21 +0100 Subject: [PATCH] added simple Java example --- .bzrignore | 1 + examples/simple/Decoder.java | 43 ++++++++++++++++++++++++++++++++++++ examples/simple/Encoder.java | 42 +++++++++++++++++++++++++++++++++++ examples/simple/compile.sh | 4 +++- examples/simple/run.sh | 2 ++ 5 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 examples/simple/Decoder.java create mode 100644 examples/simple/Encoder.java create mode 100644 examples/simple/run.sh diff --git a/.bzrignore b/.bzrignore index 9aefbba..791ee8e 100644 --- a/.bzrignore +++ b/.bzrignore @@ -17,3 +17,4 @@ lib/java/se/lth/control/labcomm/LabCommReader.class lib/java/se/lth/control/labcomm/LabCommSample.class lib/java/se/lth/control/labcomm/LabCommType.class lib/java/se/lth/control/labcomm/LabCommWriter.class +gen diff --git a/examples/simple/Decoder.java b/examples/simple/Decoder.java new file mode 100644 index 0000000..278c5aa --- /dev/null +++ b/examples/simple/Decoder.java @@ -0,0 +1,43 @@ +import java.io.File; +import java.io.FileInputStream; +import java.io.InputStream; + +import se.lth.control.labcomm.LabCommDecoderChannel; + +public class Decoder + implements TwoInts.Handler, IntString.Handler +{ + + LabCommDecoderChannel decoder; + + public Decoder(InputStream in) + throws Exception + { + decoder = new LabCommDecoderChannel(in); + TwoInts.register(decoder, this); + IntString.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 handle_TwoInts(TwoInts d) throws java.io.IOException { + System.out.println("Got TwoInts, a="+d.a+", b="+d.b); + } + + public void handle_IntString(IntString d) throws java.io.IOException { + System.out.println("Got IntString, x="+d.x+", s="+d.s); + } + + + public static void main(String[] arg) throws Exception { + Decoder example = new Decoder( + new FileInputStream(new File(arg[0])) + ); + } +} + diff --git a/examples/simple/Encoder.java b/examples/simple/Encoder.java new file mode 100644 index 0000000..40eb828 --- /dev/null +++ b/examples/simple/Encoder.java @@ -0,0 +1,42 @@ +import java.io.File; +import java.io.FileOutputStream; +import java.io.OutputStream; + +import se.lth.control.labcomm.LabCommEncoderChannel; + +public class Encoder +{ + + LabCommEncoderChannel encoder; + + public Encoder(OutputStream out) + throws Exception + { + encoder = new LabCommEncoderChannel(out); + TwoInts.register(encoder); + IntString.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"; + + TwoInts.encode(encoder, x); + IntString.encode(encoder, y); + } + + + public static void main(String[] arg) throws Exception { + FileOutputStream fos = new FileOutputStream(new File(arg[0])); + Encoder example = new Encoder(fos); + example.doEncode(); + fos.close(); + } + +} + diff --git a/examples/simple/compile.sh b/examples/simple/compile.sh index d6a9f67..7d455a2 100644 --- a/examples/simple/compile.sh +++ b/examples/simple/compile.sh @@ -1 +1,3 @@ -java -jar ../../labComm.jar --java=java simple.lc +java -jar ../../labComm.jar --java=gen simple.lc + +javac -cp ../../lib/java:. gen/*.java Encoder.java Decoder.java diff --git a/examples/simple/run.sh b/examples/simple/run.sh new file mode 100644 index 0000000..95a1d72 --- /dev/null +++ b/examples/simple/run.sh @@ -0,0 +1,2 @@ +java -cp .:../../lib/java:gen Encoder encoded_data +java -cp .:../../lib/java:gen Decoder encoded_data -- GitLab