From 3ab534965f124026fda963d29a7d7310c440d395 Mon Sep 17 00:00:00 2001
From: Sven Gestegard Robertz <sven.robertz@cs.lth.se>
Date: Fri, 22 Nov 2013 16:11:42 +0100
Subject: [PATCH] tweaked minimal TCP java example

---
 examples/jgrafchart/Makefile                  |  2 +-
 examples/tcp/labcommTCPtest/FooSample.java    | 99 +++++++++++++++++++
 .../tcp/labcommTCPtest/LabCommRegister.java   |  5 +
 .../tcp/labcommTCPtest/client/TestClient.java | 69 +++++++++++++
 .../tcp/labcommTCPtest/server/TestObject.java | 21 ++++
 .../tcp/labcommTCPtest/server/TestServer.java | 59 +++++++++++
 examples/tcp/test.lc                          |  6 ++
 7 files changed, 260 insertions(+), 1 deletion(-)
 create mode 100644 examples/tcp/labcommTCPtest/FooSample.java
 create mode 100644 examples/tcp/labcommTCPtest/LabCommRegister.java
 create mode 100644 examples/tcp/labcommTCPtest/client/TestClient.java
 create mode 100644 examples/tcp/labcommTCPtest/server/TestObject.java
 create mode 100644 examples/tcp/labcommTCPtest/server/TestServer.java
 create mode 100644 examples/tcp/test.lc

diff --git a/examples/jgrafchart/Makefile b/examples/jgrafchart/Makefile
index 76b097d..1967ced 100644
--- a/examples/jgrafchart/Makefile
+++ b/examples/jgrafchart/Makefile
@@ -15,7 +15,7 @@ ${LCFILE}.c : ${LCFILE}.lc
 	${LCC} -C ${LCFILE}.lc
 
 
-.PHONY: clean, runclient
+.PHONY: clean runclient
 clean :
 	rm ${LCFILE}.c ${LCFILE}.h client testserver
 
diff --git a/examples/tcp/labcommTCPtest/FooSample.java b/examples/tcp/labcommTCPtest/FooSample.java
new file mode 100644
index 0000000..c5024e8
--- /dev/null
+++ b/examples/tcp/labcommTCPtest/FooSample.java
@@ -0,0 +1,99 @@
+/* 
+sample struct {
+  int x;
+  int y;
+  long t;
+  double d;
+} FooSample;
+*/
+package labcommTCPtest;
+import java.io.IOException;
+import se.lth.control.labcomm.LabCommDecoder;
+import se.lth.control.labcomm.LabCommDispatcher;
+import se.lth.control.labcomm.LabCommEncoder;
+import se.lth.control.labcomm.LabCommHandler;
+import se.lth.control.labcomm.LabCommSample;
+
+public class FooSample implements LabCommSample {
+
+  public int x;
+  public int y;
+  public long t;
+  public double d;
+  
+  public interface Handler extends LabCommHandler {
+    public void handle_FooSample(FooSample value) throws Exception;
+  }
+  
+  public static void register(LabCommDecoder d, Handler h) throws IOException {
+    d.register(new Dispatcher(), h);
+  }
+  
+  public static void register(LabCommEncoder e) throws IOException {
+    e.register(new Dispatcher());
+  }
+  
+  private static class Dispatcher implements LabCommDispatcher {
+    
+    public Class getSampleClass() {
+      return FooSample.class;
+    }
+    
+    public String getName() {
+      return "FooSample";
+    }
+    
+    public byte[] getSignature() {
+      return signature;
+    }
+    
+    public void decodeAndHandle(LabCommDecoder d,
+                                LabCommHandler h) throws Exception {
+      ((Handler)h).handle_FooSample(FooSample.decode(d));
+    }
+    
+  }
+  
+  public static void encode(LabCommEncoder e, FooSample value) throws IOException {
+    e.begin(FooSample.class);
+    e.encodeInt(value.x);
+    e.encodeInt(value.y);
+    e.encodeLong(value.t);
+    e.encodeDouble(value.d);
+    e.end(FooSample.class);
+  }
+  
+  public static FooSample decode(LabCommDecoder d) throws IOException {
+    FooSample result;
+    result = new FooSample();
+    result.x = d.decodeInt();
+    result.y = d.decodeInt();
+    result.t = d.decodeLong();
+    result.d = d.decodeDouble();
+    return result;
+  }
+  
+  private static byte[] signature = new byte[] {
+    // struct { 4 fields
+    17, 
+      4, 
+      // int 'x'
+      1, 
+      120, 
+      35, 
+      // int 'y'
+      1, 
+      121, 
+      35, 
+      // long 't'
+      1, 
+      116, 
+      36, 
+      // double 'd'
+      1, 
+      100, 
+      38, 
+    // }
+  };
+
+}
diff --git a/examples/tcp/labcommTCPtest/LabCommRegister.java b/examples/tcp/labcommTCPtest/LabCommRegister.java
new file mode 100644
index 0000000..94585e9
--- /dev/null
+++ b/examples/tcp/labcommTCPtest/LabCommRegister.java
@@ -0,0 +1,5 @@
+package labcommTCPtest;
+public class LabCommRegister {
+
+
+}
diff --git a/examples/tcp/labcommTCPtest/client/TestClient.java b/examples/tcp/labcommTCPtest/client/TestClient.java
new file mode 100644
index 0000000..c040266
--- /dev/null
+++ b/examples/tcp/labcommTCPtest/client/TestClient.java
@@ -0,0 +1,69 @@
+package labcommTCPtest.client;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.Socket;
+import java.net.UnknownHostException;
+
+import se.lth.control.labcomm.LabCommDecoderChannel;
+import se.lth.control.labcomm.LabCommEncoderChannel;
+import labcommTCPtest.FooSample;
+import labcommTCPtest.FooSample.Handler;
+
+public class TestClient implements Handler {
+
+
+	private OutputStream out;
+	private InputStream in;
+
+	public TestClient(Socket server) throws IOException {
+		out = server.getOutputStream();
+		in = server.getInputStream();
+	}
+
+	public void test() {
+		
+		try {
+			LabCommEncoderChannel e = new LabCommEncoderChannel(out );
+			FooSample.register(e);
+			FooSample sample = new FooSample();
+			sample.x = 17;
+			sample.y = 42;
+			sample.t = 1717;
+			sample.d = 0.42;
+			printSample("Client sending", sample);
+			FooSample.encode(e, sample);
+
+			LabCommDecoderChannel c = new LabCommDecoderChannel(in);
+			FooSample.register(c,this);
+			c.runOne();
+		} catch (Exception e) {
+			e.printStackTrace();
+		}
+	}
+
+	public static void main(String... args) {
+		String server = "localhost";
+		int port = 9999;
+		try {
+			Socket s = new Socket(server, port);
+			TestClient c = new TestClient(s);
+			c.test();
+		} catch (UnknownHostException e) {
+			e.printStackTrace();
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+	}
+
+	private void printSample(String header, FooSample sample2) throws Exception {
+		System.out.println(header);
+		System.out.format("TestClient.invoke(%d, %d, %d, %f)\n", sample2.x, sample2.y, sample2.t, sample2.d);
+	}
+
+	public void handle_FooSample(FooSample sample2) throws Exception {
+		printSample("TestClient.handle_FooSample", sample2);
+
+	}
+}
diff --git a/examples/tcp/labcommTCPtest/server/TestObject.java b/examples/tcp/labcommTCPtest/server/TestObject.java
new file mode 100644
index 0000000..944b22b
--- /dev/null
+++ b/examples/tcp/labcommTCPtest/server/TestObject.java
@@ -0,0 +1,21 @@
+package labcommTCPtest.server;
+
+/**
+ * The service object to be accessed remotely via a LabComm channel
+ * 
+ */
+public class TestObject {
+
+	/**
+	 * A test method. The matching LabComm description is in test.lc
+	 * 
+	 * @param x
+	 * @param y
+	 * @param t
+	 * @param d
+	 */
+	public void foo(int x, int y, long t, double d) {
+		System.out.format("TestObject.foo(%d, %d, %d, %f)\n", x, y, t, d);
+	}
+
+}
diff --git a/examples/tcp/labcommTCPtest/server/TestServer.java b/examples/tcp/labcommTCPtest/server/TestServer.java
new file mode 100644
index 0000000..c3511d0
--- /dev/null
+++ b/examples/tcp/labcommTCPtest/server/TestServer.java
@@ -0,0 +1,59 @@
+package labcommTCPtest.server;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.net.ServerSocket;
+import java.net.Socket;
+
+import se.lth.control.labcomm.LabCommDecoderChannel;
+import se.lth.control.labcomm.LabCommEncoderChannel;
+import labcommTCPtest.FooSample;
+import labcommTCPtest.FooSample.Handler;
+
+public class TestServer implements Handler {
+
+	private OutputStream out;
+	private InputStream in;
+
+	public static void main(String a[]) {
+		try {
+			ServerSocket ss = new ServerSocket(9999);
+			Socket s = ss.accept();
+			
+			TestServer ts = new TestServer(s);
+			ts.runOne();
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+	}
+	
+	public TestServer(Socket s) throws IOException {
+		out = s.getOutputStream();
+		in = s.getInputStream();
+	}
+	
+	public void runOne() {
+		
+		try {
+			LabCommDecoderChannel c = new LabCommDecoderChannel(in);
+			FooSample.register(c,this);
+			c.runOne();
+		} catch (Exception e) {
+			e.printStackTrace();
+		}
+	}
+
+	public void handle_FooSample(FooSample sample) throws Exception {
+		LabCommEncoderChannel e = new LabCommEncoderChannel(out );
+		FooSample.register(e);
+		System.out.println("TestServer.handle_FooSample...");
+		sample.x *= 2;
+		sample.y *= 2;
+		sample.t *= 2;
+		sample.d *= 2;
+		FooSample.encode(e, sample);
+	}
+}
diff --git a/examples/tcp/test.lc b/examples/tcp/test.lc
new file mode 100644
index 0000000..fe104e9
--- /dev/null
+++ b/examples/tcp/test.lc
@@ -0,0 +1,6 @@
+sample struct {
+    int x;
+    int y;
+    long t;
+    double d;
+} FooSample;
-- 
GitLab