Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • anders_blomdell/labcomm
  • klaren/labcomm
  • tommyo/labcomm
  • erikj/labcomm
  • sven/labcomm
5 results
Show changes
Showing
with 726 additions and 55 deletions
encoded_data
encoded_data06
example_decoder
example_decoder06
example_encoder
example_encoder06
gen
gen06
......@@ -2,24 +2,25 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import se.lth.control.labcomm.LabCommDecoderChannel;
import se.lth.control.labcomm2014.DecoderChannel;
public class Decoder
implements theTwoInts.Handler, anotherTwoInts.Handler, IntString.Handler, TwoArrays.Handler, TwoFixedArrays.Handler
implements theTwoInts.Handler, anotherTwoInts.Handler, IntString.Handler, TwoArrays.Handler, TwoFixedArrays.Handler, doavoid.Handler
{
LabCommDecoderChannel decoder;
DecoderChannel decoder;
public Decoder(InputStream in)
throws Exception
{
decoder = new LabCommDecoderChannel(in);
decoder = new DecoderChannel(in);
theTwoInts.register(decoder, this);
anotherTwoInts.register(decoder, this);
IntString.register(decoder, this);
TwoArrays.register(decoder, this);
TwoFixedArrays.register(decoder, this);
doavoid.register(decoder, this);
try {
System.out.println("Running decoder.");
......@@ -72,6 +73,10 @@ public class Decoder
}
System.out.println();
}
public void handle_doavoid() throws java.io.IOException {
System.out.println("Got doavoid");
}
public static void main(String[] arg) throws Exception {
......
......@@ -2,7 +2,7 @@ import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import se.lth.control.labcomm.LabCommEncoderChannel;
import se.lth.control.labcomm2014.EncoderChannel;
/**
* Simple encoder
......@@ -10,17 +10,20 @@ import se.lth.control.labcomm.LabCommEncoderChannel;
public class Encoder
{
LabCommEncoderChannel encoder;
EncoderChannel encoder;
public Encoder(OutputStream out)
throws Exception
{
encoder = new LabCommEncoderChannel(out);
encoder = new EncoderChannel(out);
doavoid.register(encoder);
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;
......@@ -29,11 +32,27 @@ public class Encoder
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 doavoid");
doavoid.encode(encoder);
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);
}
......
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import se.lth.control.labcomm2014.EncoderChannel;
/**
* Simple encoder
*/
public class EncoderIS
{
EncoderChannel encoder;
public EncoderIS(OutputStream out)
throws Exception
{
encoder = new EncoderChannel(out);
// doavoid.register(encoder);
// theTwoInts.register(encoder);
IntString.register(encoder);
// TwoArrays.register(encoder);
}
public void doEncodeIS() throws java.io.IOException {
IntString a = new IntString();
a.x = 17;
a.s = "A string";
IntString b = new IntString();
b.x = 9;
b.s = "Hej";
IntString c = new IntString();
c.x = 133742;
c.s = "Thirteenthirtysevenfourtytwo";
System.out.println("Encoding IntStrings");
IntString.encode(encoder, a);
IntString.encode(encoder, b);
IntString.encode(encoder, c);
}
public static void main(String[] arg) throws Exception {
FileOutputStream fos = new FileOutputStream(arg[0]);
EncoderIS example = new EncoderIS(fos);
example.doEncodeIS();
fos.close();
}
}
all:
clean:
distclean:
rm -rf gen
rm -f *.class
rm -f example_encoder
rm -f example_decoder
rm -f encoded_data
(cd ../../lib/c; make -e LABCOMM_NO_EXPERIMENTAL=true)
(cd ../../compiler ; ant jar)
#!/bin/sh
set -x
set -e
### Example compile script, showing the steps required to build a labcomm application
### (including compiler and libs). Also illustrates how versions 2013 and 2006 coexist
java -jar ../../compiler/labComm.jar --java=gen --c=gen/simple.c --h=gen/simple.h --python=gen/simple.py simple.lc
# For current version (2013)
(cd ../..; make all)
javac -cp ../../lib/java:. gen/*.java Encoder.java Decoder.java
mkdir -p gen
java -jar ../../compiler/labcomm2014_compiler.jar --java=gen --c=gen/simple.c --h=gen/simple.h --python=gen/simple.py simple.lc
gcc -o example_encoder -L ../../lib/c -I . -I ../../lib/c example_encoder.c gen/simple.c -llabcomm
gcc -o example_decoder -L ../../lib/c -I . -I ../../lib/c example_decoder.c gen/simple.c -llabcomm
#gcc -o example_encoder -I . -I ../../lib/c example_encoder.c gen/simple.c ../../lib/c/labcomm.c ../../lib/c/labcomm_fd_reader_writer.c
#gcc -o example_decoder -I . -I ../../lib/c example_decoder.c gen/simple.c ../../lib/c/labcomm.c ../../lib/c/labcomm_fd_reader_writer.c
javac -cp ../../lib/java/labcomm2014.jar:. gen/*.java Encoder.java Decoder.java
gcc -Wall -Werror -Wno-unused-function \
-I. -I../../lib/c/2014 -L../../lib/c \
-o example_encoder example_encoder.c gen/simple.c \
-llabcomm2014
gcc -Wall -Werror -I . -I ../../lib/c/2014 -L../../lib/c \
-o example_decoder example_decoder.c gen/simple.c \
-llabcomm2014
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <labcomm_fd_reader_writer.h>
#include <labcomm2014_fd_reader.h>
#include <labcomm2014_default_error_handler.h>
#include <labcomm2014_default_memory.h>
#include <labcomm2014_default_scheduler.h>
#include "gen/simple.h"
#include <stdio.h>
static void handle_simple_doavoid(simple_doavoid *v, void *context) {
printf("Got a void.\n");
}
static void handle_simple_theTwoInts(simple_TwoInts *v,void *context) {
printf("Got theTwoInts. a=%d, b=%d\n", v->a, v->b);
......@@ -18,55 +26,64 @@ static void handle_simple_IntString(simple_IntString *v,void *context) {
static void handle_simple_TwoArrays(simple_TwoArrays *d,void *context) {
printf("Got TwoArrays:");
int i;
int i,j;
for(i=0; i<2; i++) {
printf("%d ",d->fixed.a[i]);
}
printf("\n");
for(i=0; i<d->variable.n_1; i++) {
printf("%d ",d->variable.a[0+2*i]);
printf("%d ",d->variable.a[1+2*i]);
for(i=0; i<2; i++) {
for(j=0; j<d->variable.n_1; j++) {
printf("%d ",d->variable.a[i *d->variable.n_1 + j]);
}
printf("\n");
}
printf("\n");
}
static void handle_simple_TwoFixedArrays(simple_TwoFixedArrays *d,void *context) {
printf("Got TwoFixedArrays:");
int i;
int i,j;
for(i=0; i<2; i++) {
printf("%d ",d->a.a[i]);
}
printf("\n");
for(i=0; i<3; i++) {
printf("%d ",d->b.a[0+2*i]);
printf("%d ",d->b.a[1+2*i]);
for(i=0; i<2; i++) {
for(j=0; j<3; j++) {
printf("%d ",d->b.a[i][j]);
}
printf("\n");
}
printf("\n");
}
int main(int argc, char *argv[]) {
int fd;
struct labcomm_decoder *decoder;
struct labcomm2014_decoder *decoder;
void *context = NULL;
int i, j;
char *filename = argv[1];
printf("C decoder reading from %s\n", filename);
fd = open(filename, O_RDONLY);
decoder = labcomm_decoder_new(labcomm_fd_reader, &fd);
decoder = labcomm2014_decoder_new(labcomm2014_fd_reader_new(
labcomm2014_default_memory, fd, 1),
labcomm2014_default_error_handler,
labcomm2014_default_memory,
labcomm2014_default_scheduler);
if (!decoder) {
printf("Failed to allocate decoder %s:%d\n", __FUNCTION__, __LINE__);
return 1;
}
labcomm_decoder_register_simple_theTwoInts(decoder, handle_simple_theTwoInts, context);
labcomm_decoder_register_simple_anotherTwoInts(decoder, handle_simple_anotherTwoInts, context);
labcomm_decoder_register_simple_IntString(decoder, handle_simple_IntString, context);
labcomm_decoder_register_simple_TwoArrays(decoder, handle_simple_TwoArrays, context);
labcomm_decoder_register_simple_TwoFixedArrays(decoder, handle_simple_TwoFixedArrays, context);
labcomm2014_decoder_register_simple_doavoid(decoder, handle_simple_doavoid, context);
labcomm2014_decoder_register_simple_theTwoInts(decoder, handle_simple_theTwoInts, context);
labcomm2014_decoder_register_simple_anotherTwoInts(decoder, handle_simple_anotherTwoInts, context);
labcomm2014_decoder_register_simple_IntString(decoder, handle_simple_IntString, context);
labcomm2014_decoder_register_simple_TwoArrays(decoder, handle_simple_TwoArrays, context);
labcomm2014_decoder_register_simple_TwoFixedArrays(decoder, handle_simple_TwoFixedArrays, context);
printf("Decoding:\n");
labcomm_decoder_run(decoder);
labcomm2014_decoder_run(decoder);
printf("--- End Of File ---:\n");
labcomm_decoder_free(decoder);
labcomm2014_decoder_free(decoder);
return 0;
}
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <labcomm_fd_reader_writer.h>
#include <labcomm2014_fd_writer.h>
#include <labcomm2014_default_error_handler.h>
#include <labcomm2014_default_memory.h>
#include <labcomm2014_default_scheduler.h>
#include "gen/simple.h"
#include <stdio.h>
int main(int argc, char *argv[]) {
int fd;
struct labcomm_encoder *encoder;
int i, j;
struct labcomm2014_encoder *encoder;
char *filename = argv[1];
printf("C encoder writing to %s\n", filename);
fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0644);
encoder = labcomm_encoder_new(labcomm_fd_writer, &fd);
labcomm_encoder_register_simple_theTwoInts(encoder);
labcomm_encoder_register_simple_anotherTwoInts(encoder);
labcomm_encoder_register_simple_IntString(encoder);
encoder = labcomm2014_encoder_new(labcomm2014_fd_writer_new(
labcomm2014_default_memory, fd, 1),
labcomm2014_default_error_handler,
labcomm2014_default_memory,
labcomm2014_default_scheduler);
labcomm2014_encoder_register_simple_doavoid(encoder);
labcomm2014_encoder_register_simple_theTwoInts(encoder);
labcomm2014_encoder_register_simple_anotherTwoInts(encoder);
labcomm2014_encoder_register_simple_IntString(encoder);
labcomm2014_encode_simple_doavoid(encoder);
simple_IntString is;
is.x = 24;
is.s = "Hello, LabComm!";
printf("Encoding IntString, x=%d, s=%s\n", is.x, is.s);
labcomm_encode_simple_IntString(encoder, &is);
labcomm2014_encode_simple_IntString(encoder, &is);
simple_theTwoInts ti;
ti.a = 13;
ti.b = 37;
printf("Encoding theTwoInts, a=%d, b=%d\n", ti.a, ti.b);
labcomm_encode_simple_theTwoInts(encoder, &ti);
labcomm2014_encode_simple_theTwoInts(encoder, &ti);
simple_anotherTwoInts ati;
ati.a = 23;
ati.b = 47;
printf("Encoding anotherTwoInts, a=%d, b=%d\n", ati.a, ati.b);
labcomm_encode_simple_anotherTwoInts(encoder, &ati);
labcomm2014_encode_simple_anotherTwoInts(encoder, &ati);
int foo[20];
labcomm_encoder_register_simple_TwoArrays(encoder);
labcomm2014_encoder_register_simple_TwoArrays(encoder);
simple_TwoArrays ta;
ta.fixed.a[0] = 17;
......@@ -50,18 +61,29 @@ int main(int argc, char *argv[]) {
}
printf("Encoding TwoArrays...\n");
labcomm_encode_simple_TwoArrays(encoder, &ta);
labcomm2014_encode_simple_TwoArrays(encoder, &ta);
ti.a = 23;
ti.b = 47;
printf("Encoding theTwoInts, a=%d, b=%d\n", ti.a, ti.b);
labcomm_encode_simple_theTwoInts(encoder, &ti);
labcomm2014_encode_simple_theTwoInts(encoder, &ti);
simple_TwoFixedArrays tfa;
tfa.a.a[0] = 41;
tfa.a.a[1] = 42;
tfa.b.a[0][0] = 51;
tfa.b.a[0][1] = 52;
tfa.b.a[0][2] = 53;
tfa.b.a[1][0] = 61;
tfa.b.a[1][1] = 62;
tfa.b.a[1][2] = 63;
printf("Encoding TwoFixedArrays...\n");
labcomm_encoder_register_simple_TwoFixedArrays(encoder);
labcomm_encode_simple_TwoFixedArrays(encoder, &tfa);
labcomm2014_encoder_register_simple_TwoFixedArrays(encoder);
labcomm2014_encode_simple_TwoFixedArrays(encoder, &tfa);
return 0;
}
#!/usr/bin/python
import labcomm2014
import sys
import simple
if __name__ == '__main__':
version = sys.argv[2] if len(sys.argv) == 3 else "LabComm2014"
encoder = labcomm2014.Encoder(labcomm2014.StreamWriter(open(sys.argv[1], 'w')), version)
encoder.add_decl(simple.theTwoInts.signature)
encoder.add_decl(simple.IntString.signature)
foo = simple.theTwoInts()
foo.a = 13
foo.b = 37
encoder.encode(foo, simple.theTwoInts.signature)
bar = simple.IntString()
bar.x = 1742
bar.s = "A string from Python"
encoder.encode(bar, simple.IntString.signature)
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <labcomm_fd_writer.h>
#include <labcomm_default_error_handler.h>
#include <labcomm_default_memory.h>
#include <labcomm_default_scheduler.h>
#include "gen06/simple.h"
#include <stdio.h>
int main(int argc, char *argv[]) {
int fd;
struct labcomm_encoder *encoder;
char *filename = argv[1];
printf("C encoder writing to %s\n", filename);
fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0644);
encoder = labcomm_encoder_new(labcomm_fd_writer_new(
labcomm_default_memory, fd, 1),
labcomm_default_error_handler,
labcomm_default_memory,
labcomm_default_scheduler);
labcomm_encoder_register_simple_theTwoInts(encoder);
labcomm_encoder_register_simple_anotherTwoInts(encoder);
labcomm_encoder_register_simple_IntString(encoder);
simple_IntString is;
is.x = 24;
is.s = "Hello, LabComm!";
printf("Encoding IntString, x=%d, s=%s\n", is.x, is.s);
labcomm_encode_simple_IntString(encoder, &is);
simple_theTwoInts ti;
ti.a = 13;
ti.b = 37;
printf("Encoding theTwoInts, a=%d, b=%d\n", ti.a, ti.b);
labcomm_encode_simple_theTwoInts(encoder, &ti);
simple_anotherTwoInts ati;
ati.a = 23;
ati.b = 47;
printf("Encoding anotherTwoInts, a=%d, b=%d\n", ati.a, ati.b);
labcomm_encode_simple_anotherTwoInts(encoder, &ati);
int foo[20];
labcomm_encoder_register_simple_TwoArrays(encoder);
simple_TwoArrays ta;
ta.fixed.a[0] = 17;
ta.fixed.a[1] = 42;
ta.variable.n_1 = 10;
ta.variable.a = foo;
int k;
for(k=0; k<20; k++) {
foo[k] = k;
}
printf("Encoding TwoArrays...\n");
labcomm_encode_simple_TwoArrays(encoder, &ta);
ti.a = 23;
ti.b = 47;
printf("Encoding theTwoInts, a=%d, b=%d\n", ti.a, ti.b);
labcomm_encode_simple_theTwoInts(encoder, &ti);
simple_TwoFixedArrays tfa;
tfa.a.a[0] = 41;
tfa.a.a[1] = 42;
tfa.b.a[0][0] = 51;
tfa.b.a[0][1] = 52;
tfa.b.a[0][2] = 53;
tfa.b.a[1][0] = 61;
tfa.b.a[1][1] = 62;
tfa.b.a[1][2] = 63;
printf("Encoding TwoFixedArrays...\n");
labcomm_encoder_register_simple_TwoFixedArrays(encoder);
labcomm_encode_simple_TwoFixedArrays(encoder, &tfa);
return 0;
}
sample int AnInt;
sample struct {
int x;
string s;
} IntString;
sample struct {
struct {
float x;
float y;
} point;
boolean b;
} nestedStruct;
java -cp .:../../lib/java:gen Encoder encoded_data
export LD_LIBRARY_PATH=../../lib/c/
echo
echo "********************************************"
echo "*** Running example for version 2013 ***"
echo "********************************************"
echo
java -cp .:../../lib/java/labcomm2014.jar:gen Encoder encoded_data
./example_decoder encoded_data
PYTHONPATH=../../lib/python:gen ./example_encoder.py encoded_data
java -cp .:../../lib/java/labcomm2014.jar:gen Decoder encoded_data
./example_encoder encoded_data
java -cp .:../../lib/java:gen Decoder encoded_data
java -cp .:../../lib/java/labcomm2014.jar:gen Decoder encoded_data
echo "running python decoder (from wiki_example):"
PYTHONPATH=../../lib/python ../wiki_example/example_decoder.py encoded_data
PYTHONPATH=../../lib/python ../wiki_example/example_decoder.py encoded_data LabComm2014
......@@ -25,3 +25,18 @@ sample struct {
int a[2];
int b[2,3];
} TwoFixedArrays;
typedef void avoid;
sample avoid doavoid;
// examples of errors: void may not be used
// in structs or arrays
//
// sample struct {
// int a;
// avoid error;
//} foo;
//
//sample void error2[2] ;
//sample avoid error3[_];
LCDIR=../..
LABCOMM_JAR=../../compiler/labcomm2014_compiler.jar
LABCOMM=java -jar $(LABCOMM_JAR)
LCLJAR=${LCDIR}/lib/java/labcomm2014.jar # the LabComm library
JAVA_PKG=labcommTCPtest
.PHONY : clean run runserver runOSserver runclient
#### The main example #########################
run : ${JAVA_PKG}/Example.class ${JAVA_PKG}/server/TestServer.class ${JAVA_PKG}/client/TestClient.class
java -cp ${CLASSPATH} $(<:.class=)
###############################################
### dependencies and parts ####################
###############################################
CLASSPATH=.:${LCLJAR}
${LABCOMM_JAR} :
cd ${LCDIR} && make make-compiler
${LCLJAR} :
cd ${LCDIR}/lib/java && make labcomm2014.jar
${JAVA_PKG}/gen/FooSample.java: test.lc ${LCCJAR}
${LABCOMM} --javapackage=${JAVA_PKG}.gen --java=${JAVA_PKG}/gen $<
${JAVA_PKG}/gen/FooSample.class: ${JAVA_PKG}/gen/FooSample.java test.lc ${LCLJAR}
javac -cp ${CLASSPATH} $<
${JAVA_PKG}/Example.class: ${JAVA_PKG}/Example.java ${JAVA_PKG}/gen/FooSample.class ${LCLJAR}
javac -cp ${CLASSPATH} $<
${JAVA_PKG}/server/TestServer.class: ${JAVA_PKG}/server/TestServer.java ${JAVA_PKG}/gen/FooSample.class ${LCLJAR}
javac -cp ${CLASSPATH} $<
${JAVA_PKG}/server/OneShotServer.class: ${JAVA_PKG}/server/OneShotServer.java ${JAVA_PKG}/gen/FooSample.class ${LCLJAR}
javac -cp ${CLASSPATH} $<
${JAVA_PKG}/client/TestClient.class: ${JAVA_PKG}/client/TestClient.java ${JAVA_PKG}/gen//FooSample.class ${LCLJAR}
javac -cp ${CLASSPATH} $<
runserver : ${JAVA_PKG}/server/TestServer.class
java -cp ${CLASSPATH} $(<:.class=)
runOSserver : ${JAVA_PKG}/server/OneShotServer.class
java -cp ${CLASSPATH} $(<:.class=)
runclient : ${JAVA_PKG}/client/TestClient.class
java -cp ${CLASSPATH} $(<:.class=)
clean :
rm -f ${JAVA_PKG}/server/*.class
rm -f ${JAVA_PKG}/client/*.class
rm -f ${JAVA_PKG}/gen/*
distclean: clean
A simple (mostly Java) example of a LabComm channel over TCP
The class labcommTCPtest/Example.java contains set up of a server
and a client, and can be run by 'make run'
Also included is a generic decoder in python, and a server that only sends
one sample to the first client that connects, and then closes the connection
and exits.
Those can be run by
* first, in one terminal: make runOSserver
* then, in another terminal: ./runpy localhost 9999
#!/usr/bin/python
import os
import sys
import socket
import rwsocket
if not any('labcomm2014' in p for p in sys.path):
sys.path.append('../../lib/python')
import labcomm2014
if __name__ == "__main__":
print "Trying to connect..."
host = sys.argv[1] #'localhost'
port = sys.argv[2] #'8081'
addr = (host,int(port))
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(addr)
print "Connected!"
d = labcomm2014.Decoder(labcomm2014.StreamReader(sock))
while True:
try:
data,decl = d.decode()
if data:
print data
except KeyError as e:
print 'KeyError : "%s"' % str(e)
except:
print 'exception...', sys.exc_info()[0]
break
package labcommTCPtest;
import labcommTCPtest.server.TestServer;
import labcommTCPtest.client.TestClient;
import java.net.Socket;
import java.net.ServerSocket;
public class Example {
public static void main(String a[]) {
String server = "localhost";
int port = 9999;
ServerThread serverThread = new ServerThread(port);
ClientThread clientThread = new ClientThread(server, port);
serverThread.start();
clientThread.start();
}
private static class ServerThread extends Thread {
private int port;
public ServerThread(int port) {
this.port = port;
}
public void run() {
try {
ServerSocket ss = new ServerSocket(port);
Socket s = ss.accept();
TestServer ts = new TestServer(s);
ts.runOne();
} catch (Exception e) {
e.printStackTrace();
}
}
}
private static class ClientThread extends Thread {
private String server;
private int port;
public ClientThread(String server, int port) {
this.server = server;
this.port = port;
}
public void run() {
try {
Socket s = new Socket(server, port);
TestClient c = new TestClient(s);
c.test();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
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.labcomm2014.DecoderChannel;
import se.lth.control.labcomm2014.EncoderChannel;
import labcommTCPtest.gen.FooSample;
import labcommTCPtest.gen.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 {
EncoderChannel e = new EncoderChannel(out );
FooSample.register(e);
FooSample sample = new FooSample();
int a[] = new int[3];
a[0] = 1;
a[1] = 2;
a[2] = 3;
sample.s = "Some random values";
sample.x = 17;
sample.y = 42;
sample.a = a;
sample.t = 1717;
sample.d = 0.42;
printSample("Client sending", sample);
FooSample.encode(e, sample);
DecoderChannel c = new DecoderChannel(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 String formatArray(int a[]) {
StringBuilder sb = new StringBuilder();
sb.append("[");
for(int i=0; i < a.length; i++) {
sb.append(a[i]);
if(i < a.length-1) {
sb.append(", ");
}
}
sb.append("]");
return sb.toString();
}
private void printSample(String header, FooSample sample2) throws Exception {
System.out.format("[TestClient] %s: (%s, %d, %d, %s, %d, %f )\n", header, sample2.s, sample2.x, sample2.y, formatArray(sample2.a), sample2.t, sample2.d);
}
public void handle_FooSample(FooSample sample2) throws Exception {
printSample("TestClient.handle_FooSample", sample2);
}
}
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.labcomm2014.DecoderChannel;
import se.lth.control.labcomm2014.EncoderChannel;
import labcommTCPtest.gen.FooSample;
import labcommTCPtest.gen.FooSample.Handler;
public class TestClientSingleshot implements Handler {
private OutputStream out;
private InputStream in;
public TestClientSingleshot(Socket server) throws IOException {
out = server.getOutputStream();
in = server.getInputStream();
}
public void test() {
try {
EncoderChannel e = new EncoderChannel(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);
DecoderChannel c = new DecoderChannel(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);
TestClientSingleshot c = new TestClientSingleshot(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("TestClientSingleshot.invoke(%d, %d, %d, %f)\n", sample2.x, sample2.y, sample2.t, sample2.d);
}
public void handle_FooSample(FooSample sample2) throws Exception {
printSample("TestClientSingleshot.handle_FooSample", sample2);
}
}
package labcommTCPtest.server;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.ServerSocket;
import se.lth.control.labcomm2014.DecoderChannel;
import se.lth.control.labcomm2014.EncoderChannel;
import labcommTCPtest.gen.FooSample;
public class OneShotServer {//implements Handler {
private OutputStream out;
public OneShotServer(Socket s) throws IOException {
out = s.getOutputStream();
}
public void test() {
try {
EncoderChannel e = new EncoderChannel(out );
FooSample.register(e);
FooSample sample = new FooSample();
sample.s = "OneShotServer message";
sample.x = 17;
sample.y = 42;
sample.a = new int[]{10,11,12};
sample.t = 1717;
sample.d = 0.42;
printSample("Server sending", sample);
FooSample.encode(e, sample);
Thread.sleep(1);
sample.x++;
sample.y--;
printSample("Server sending", sample);
FooSample.encode(e, sample);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String... args) {
String server = "localhost";
int port = 9999;
try {
ServerSocket ss = new ServerSocket(port);
Socket s = ss.accept();
OneShotServer c = new OneShotServer(s);
c.test();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception 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);
}
}