Skip to content
Snippets Groups Projects
Commit 28c454c2 authored by Sven Robertz's avatar Sven Robertz
Browse files

added simple Java example

parent 58956828
No related branches found
No related tags found
No related merge requests found
......@@ -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
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]))
);
}
}
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();
}
}
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
java -cp .:../../lib/java:gen Encoder encoded_data
java -cp .:../../lib/java:gen Decoder encoded_data
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment