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 841 additions and 60 deletions
......@@ -2,19 +2,19 @@ package test;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import se.lth.control.labcomm.LabCommEncoderChannel;
import se.lth.control.labcomm2014.EncoderChannel;
import gen.foo;
import gen.bar;
public class StaticEncoder {
LabCommEncoderChannel encoder;
EncoderChannel encoder;
public StaticEncoder(OutputStream out)
throws Exception
{
encoder = new LabCommEncoderChannel(out);
encoder = new EncoderChannel(out);
foo.register(encoder);
bar.register(encoder);
}
......
......@@ -16,18 +16,18 @@ import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import se.lth.control.labcomm.LabCommDecoder;
import se.lth.control.labcomm.LabCommDecoderChannel;
import se.lth.control.labcomm.LabCommEncoder;
import se.lth.control.labcomm.LabCommEncoderChannel;
import AST.LabCommParser;
import AST.LabCommScanner;
import AST.Program;
import se.lth.control.labcomm2014.Decoder;
import se.lth.control.labcomm2014.DecoderChannel;
import se.lth.control.labcomm2014.Encoder;
import se.lth.control.labcomm2014.EncoderChannel;
import AST.Parser;
import AST.Scanner;
import AST.Specification;
import beaver.Parser.Exception;
public class TestLabCommCompiler {
public class TestCompiler {
private static final String BAR = "bar";
private static final String FOO = "foo";
......@@ -73,7 +73,7 @@ public class TestLabCommCompiler {
private static void decodeTest(InRAMCompiler irc, String tmpFile) {
try {
FileInputStream in = new FileInputStream(tmpFile);
LabCommDecoderChannel dec = new LabCommDecoderChannel(in);
DecoderChannel dec = new DecoderChannel(in);
Class fc = irc.load(FOO);
Class hc = irc.load("gen_"+FOO+"Handler");
......@@ -81,7 +81,7 @@ public class TestLabCommCompiler {
Object h = hc.newInstance();
Method reg = fc.getDeclaredMethod("register", LabCommDecoder.class, hi);
Method reg = fc.getDeclaredMethod("register", Decoder.class, hi);
reg.invoke(fc, dec, h);
dec.runOne();
......@@ -106,11 +106,11 @@ public class TestLabCommCompiler {
z.setInt(f, 12);
FileOutputStream out = new FileOutputStream(tmpFile);
LabCommEncoderChannel enc = new LabCommEncoderChannel(out);
Method reg = fc.getDeclaredMethod("register", LabCommEncoder.class);
EncoderChannel enc = new EncoderChannel(out);
Method reg = fc.getDeclaredMethod("register", Encoder.class);
reg.invoke(fc, enc);
Method doEncode = fc.getDeclaredMethod("encode", LabCommEncoder.class, fc);
Method doEncode = fc.getDeclaredMethod("encode", Encoder.class, fc);
doEncode.invoke(fc, enc, f);
out.close();
......@@ -121,15 +121,15 @@ public class TestLabCommCompiler {
}
public static InRAMCompiler generateCode(String lcDecl, HashMap<String, String> handlers) {
Program ast = null;
Specification ast = null;
InputStream in = new ByteArrayInputStream(lcDecl.getBytes());
LabCommScanner scanner = new LabCommScanner(in);
LabCommParser parser = new LabCommParser();
Scanner scanner = new Scanner(in);
Parser parser = new Parser();
Collection errors = new LinkedList();
InRAMCompiler irc = null;
try {
Program p = (Program)parser.parse(scanner);
Specification p = (Specification)parser.parse(scanner);
p.errorCheck(errors);
if (errors.isEmpty()) {
ast = p;
......@@ -207,10 +207,10 @@ public class TestLabCommCompiler {
* @param handlers - a map <name, source> of handlers for the types in ast
* @return an InRAMCompiler object containing the generated clases
*/
private static InRAMCompiler handleAst(Program lcAST, HashMap<String, String> handlers) {
private static InRAMCompiler handleAst(Specification lcAST, HashMap<String, String> handlers) {
Map<String, String> genCode = new HashMap<String, String>();
try {
lcAST.J_gen(genCode, "labcomm.generated");
lcAST.J_gen(genCode, "labcomm.generated", 2013);
} catch (IOException e) {
e.printStackTrace();
}
......
......@@ -18,17 +18,18 @@ import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import se.lth.control.labcomm.LabCommDecoder;
import se.lth.control.labcomm.LabCommDecoderChannel;
import se.lth.control.labcomm.LabCommEncoder;
import se.lth.control.labcomm.LabCommEncoderChannel;
import AST.LabCommParser;
import AST.LabCommScanner;
import AST.Program;
import se.lth.control.labcomm2014.Decoder;
import se.lth.control.labcomm2014.DecoderChannel;
import se.lth.control.labcomm2014.Encoder;
import se.lth.control.labcomm2014.EncoderChannel;
import se.lth.control.labcomm2014.compiler.LabCommParser;
import se.lth.control.labcomm2014.compiler.LabCommScanner;
import se.lth.control.labcomm2014.compiler.Program;
import beaver.Parser.Exception;
public class TestLabcommGen {
private static final String TYPE_NAME_FOO = "foo_t";
private static final String SAMPLE_NAME_FOO = "foo";
private static final String SAMPLE_NAME_BAR = "bar";
......@@ -79,7 +80,7 @@ public class TestLabcommGen {
if(irc != null) {
System.out.println("*** Testing instantiation and invocation of Handler ");
dummyTest(irc);
//dummyTest(irc);
String tmpFile = args[2];
System.out.println("*** Testing writing and reading file "+tmpFile);
......@@ -160,7 +161,7 @@ public class TestLabcommGen {
}
public static InRAMCompiler generateCode(String lcDecl, HashMap<String, String> handlers) {
Program ast = null;
Specification ast = null;
InputStream in = new ByteArrayInputStream(lcDecl.getBytes());
LabCommScanner scanner = new LabCommScanner(in);
LabCommParser parser = new LabCommParser();
......@@ -168,7 +169,7 @@ public class TestLabcommGen {
InRAMCompiler irc = null;
try {
Program p = (Program)parser.parse(scanner);
Specification p = (Specification)parser.parse(scanner);
p.errorCheck(errors);
if (errors.isEmpty()) {
ast = p;
......@@ -199,10 +200,10 @@ public class TestLabcommGen {
* @param handlers - a map <name, source> of handlers for the types in ast
* @return an InRAMCompiler object containing the generated clases
*/
private static InRAMCompiler handleAst(Program lcAST, HashMap<String, String> handlers) {
private static InRAMCompiler handleAst(Specification lcAST, HashMap<String, String> handlers) {
Map<String, String> genCode = new HashMap<String, String>();
try {
lcAST.J_gen(genCode, "labcomm.generated");
lcAST.J_gen(genCode, "labcomm.generated", 2014);
} catch (IOException e) {
e.printStackTrace();
}
......@@ -226,25 +227,35 @@ public class TestLabcommGen {
try {
while(i.hasNext()){
final String sampleName = i.next();
System.out.println("sample: "+sampleName);
final String src = genCode.get(sampleName);
handlerClass.append(sampleName+".Handler");
if(i.hasNext()) {
handlerClass.append(", ");
String hctmp = handlers.get(sampleName);
if(hctmp != null) {
handlerClass.append(sampleName+".Handler");
if(i.hasNext()) {
handlerClass.append(", ");
}
handlerMethods.append(hctmp);
handlerMethods.append("\n");
}
handlerMethods.append(handlers.get(sampleName));
handlerMethods.append("\n");
//System.out.println("FOOOO:"+sampleName+"++++"+hctmp);
//System.out.println("GOOO: "+sampleName+"----"+handlerMethods);
System.out.println("***"+sampleName+"\n"+src);
irc.compile(sampleName, src); // while iterating, compile the labcomm generated code
}
handlerClass.append("{\n");
handlerClass.append(handlerAttributes);
handlerClass.append(handlerConstr);
handlerClass.append(handlerConstrCtxt);
handlerClass.append(handlerMethods.toString());
if(handlerAttributes != null)
handlerClass.append(handlerAttributes);
if(handlerConstr != null)
handlerClass.append(handlerConstr);
if(handlerConstrCtxt != null)
handlerClass.append(handlerConstrCtxt);
if(handlerMethods != null)
handlerClass.append(handlerMethods.toString());
handlerClass.append("}\n");
System.out.println("-------------------------------------");
System.out.println("--- generated code to compile -------"+ handlerClassName);
final String handlerSrc = handlerClass.toString();
System.out.println(handlerSrc);
......@@ -273,10 +284,10 @@ public class TestLabcommGen {
* @param handlers - a map <name, source> of handlers for the types in ast
* @return an InRAMCompiler object containing the generated clases
*/
private static InRAMCompiler handleAstSeparate(Program lcAST, HashMap<String, String> handlers) {
private static InRAMCompiler handleAstSeparate(Specification lcAST, HashMap<String, String> handlers) {
Map<String, String> genCode = new HashMap<String, String>();
try {
lcAST.J_gen(genCode, "labcomm.generated");
lcAST.J_gen(genCode, "labcomm.generated", 2013);
} catch (IOException e) {
e.printStackTrace();
}
......@@ -294,7 +305,7 @@ public class TestLabcommGen {
sb.append("public class gen_"+sampleName+"Handler implements "+sampleName+".Handler {\n");
sb.append(handlers.get(sampleName));
sb.append("}\n");
System.out.println("-------------------------------------");
System.out.println("--- foo -----------------------------");
System.out.println(sb.toString());
try {
irc.compile(sampleName, src);
......@@ -321,7 +332,7 @@ public class TestLabcommGen {
private static void decodeTest(InRAMCompiler irc, String tmpFile, String... sampleNames) {
try {
FileInputStream in = new FileInputStream(tmpFile);
LabCommDecoderChannel dec = new LabCommDecoderChannel(in);
DecoderChannel dec = new DecoderChannel(in);
Class handlerClass = irc.load(handlerClassName);
Constructor hcc = handlerClass.getDeclaredConstructor(Object.class);
// Object handler = handlerClass.newInstance();
......@@ -333,7 +344,7 @@ public class TestLabcommGen {
Class sampleClass = irc.load(sampleName);
Class handlerInterface = irc.load(sampleName+"$Handler");
Method reg = sampleClass.getDeclaredMethod("register", LabCommDecoder.class, handlerInterface);
Method reg = sampleClass.getDeclaredMethod("register", Decoder.class, handlerInterface);
reg.invoke(sampleClass, dec, handler);
}
......@@ -356,35 +367,42 @@ public class TestLabcommGen {
*/
private static void encodeTest(InRAMCompiler irc, String tmpFile) {
try {
Class ft;
Class fc = irc.load(SAMPLE_NAME_FOO);
Class bc = irc.load(SAMPLE_NAME_BAR);
try {
ft = irc.load(TYPE_NAME_FOO);
} catch (ClassNotFoundException e) {
System.out.println("encodeTest: defaulting to ft == fc");
ft = fc;
}
/* create sample class and instance objects */
Object f = fc.newInstance();
Object f = ft.newInstance();
Field x = fc.getDeclaredField("x");
Field y = fc.getDeclaredField("y");
Field z = fc.getDeclaredField("z");
Field x = ft.getDeclaredField("x");
Field y = ft.getDeclaredField("y");
Field z = ft.getDeclaredField("z");
x.setInt(f, 10);
y.setInt(f, 11);
z.setInt(f, 12);
FileOutputStream out = new FileOutputStream(tmpFile);
LabCommEncoderChannel enc = new LabCommEncoderChannel(out);
EncoderChannel enc = new EncoderChannel(out);
/* register and send foo */
Method regFoo = fc.getDeclaredMethod("register", LabCommEncoder.class);
Method regFoo = fc.getDeclaredMethod("register", Encoder.class);
regFoo.invoke(fc, enc);
Method doEncodeFoo = fc.getDeclaredMethod("encode", LabCommEncoder.class, fc);
Method doEncodeFoo = fc.getDeclaredMethod("encode", Encoder.class, ft);
doEncodeFoo.invoke(fc, enc, f);
/* register and send bar (NB! uses primitive type int) */
Method regBar = bc.getDeclaredMethod("register", LabCommEncoder.class);
Method regBar = bc.getDeclaredMethod("register", Encoder.class);
regBar.invoke(bc, enc);
Method doEncodeBar = bc.getDeclaredMethod("encode", LabCommEncoder.class, Integer.TYPE);
Method doEncodeBar = bc.getDeclaredMethod("encode", Encoder.class, Integer.TYPE);
doEncodeBar.invoke(bc, enc, 42);
out.close();
......@@ -457,7 +475,7 @@ public class TestLabcommGen {
private static void decodeTestSeparate(InRAMCompiler irc, String tmpFile, String... sampleNames) {
try {
FileInputStream in = new FileInputStream(tmpFile);
LabCommDecoderChannel dec = new LabCommDecoderChannel(in);
DecoderChannel dec = new DecoderChannel(in);
for (String sampleName : sampleNames) {
System.out.println("registering handler for "+sampleName);
Class sampleClass = irc.load(sampleName);
......@@ -466,7 +484,7 @@ public class TestLabcommGen {
Object handler = handlerClass.newInstance();
Method reg = sampleClass.getDeclaredMethod("register", LabCommDecoder.class, handlerInterface);
Method reg = sampleClass.getDeclaredMethod("register", Decoder.class, handlerInterface);
reg.invoke(sampleClass, dec, handler);
}
......
#dummy script to test the on-the-fly compilation
javac -cp .:../../compiler/labcomm2014_compiler.jar:../../lib/java/labcomm2014.jar:../../lib/tools/beaver.jar:../../lib/tools/beaver-rt.jar:../../lib/tools/jastadd2.jar:../../lib/tools/JFlex.jar:../../lib/tools/proj.jar test/TestLabcommGen.java
javac test/HandlerContext.java
java -cp .:../../compiler/labcomm2014_compiler.jar:../../lib/java/labcomm2014.jar:../../lib/tools/beaver.jar:../../lib/tools/beaver-rt.jar:../../lib/tools/jastadd2.jar:../../lib/tools/JFlex.jar:../../lib/tools/proj.jar test.TestLabcommGen simple_type.lc handlers_type.txt encoded_data
LCDIR=../..
LABCOMM_JAR=../../compiler/labcomm2014_compiler.jar
LABCOMM=java -jar $(LABCOMM_JAR)
CLASSPATH=.:${LCDIR}/lib/java/labcomm2014.jar
JAVA_PKG=labcommTCPtest
SAMPLENAME=foo
LCLIBDIR=${LCDIR}/lib/c
LCFILE=jg
AUX=enc.c dec.c
TLCFILE=turtle1
TAUX=turtle_enc.c turtle_dec.c
${JAVA_PKG}/gen/foo.java: ${LCFILE}.lc
${LABCOMM} --javapackage=${JAVA_PKG}.gen --java=${JAVA_PKG}/gen $<
${JAVA_PKG}/gen/${SAMPLENAME}.class: ${JAVA_PKG}/gen/${SAMPLENAME}.java
javac -cp ${CLASSPATH} $<
${JAVA_PKG}/server/TestServer.class: ${JAVA_PKG}/server/TestServer.java ${JAVA_PKG}/gen/${SAMPLENAME}.class
javac -cp ${CLASSPATH} $<
${JAVA_PKG}/client/TestClient.class: ${JAVA_PKG}/client/TestClient.java ${JAVA_PKG}/gen/${SAMPLENAME}.class
javac -cp ${CLASSPATH} $<
.PHONY: runjavaserver
runjavaserver : ${JAVA_PKG}/server/TestServer.class
java -cp ${CLASSPATH} $(<:.class=)
.PHONY: runjavaclient
runjavaclient : ${JAVA_PKG}/client/TestClient.class
java -cp ${CLASSPATH} $(<:.class=)
client: client.c ${LCFILE}.c ${AUX} ${AUX:.c=.h}
${CC} -o $@ client.c ${AUX} ${LCFILE}.c -I${LCLIBDIR} -L${LCLIBDIR} -llabcomm
testserver: testserver.c ${LCFILE}.c ${AUX} ${AUX:.c=.h}
${CC} -o $@ testserver.c ${AUX} ${LCFILE}.c -I${LCLIBDIR} -L${LCLIBDIR} -llabcomm
turtleclient: turtleclient.c ${TLCFILE}.c ${TAUX} ${TAUX:.c=.h}
${CC} -o $@ turtleclient.c ${TAUX} ${TLCFILE}.c -I${LCLIBDIR} -L${LCLIBDIR} -llabcomm
${LCFILE}.c : ${LCFILE}.lc
${LABCOMM} -C ${LCFILE}.lc
${TLCFILE}.c : ${TLCFILE}.lc
${LABCOMM} -C ${TLCFILE}.lc
all: client testserver ${JAVA_PKG}/server/TestServer.class ${JAVA_PKG}/client/TestClient.class turtleclient
.PHONY: clean
clean :
rm -f ${LCFILE}.c ${LCFILE}.h client testserver turtleclient
rm -f ${JAVA_PKG}/server/*.class ${JAVA_PKG}/client/*.class
rm -f ${JAVA_PKG}/gen/*
.PHONY: distclean
distclean: clean
.PHONY: runclient
runclient : client
LD_LIBRARY_PATH=${LCLIBDIR} ./$< localhost 9999
.PHONY: runserver
runserver : testserver
LD_LIBRARY_PATH=${LCLIBDIR} ./$< 9999
.PHONY: runtclient
runtclient : turtleclient
LD_LIBRARY_PATH=${LCLIBDIR} ./$< localhost 8082
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include "dec.h"
#include "enc.h"
void error(const char *msg)
{
perror(msg);
exit(0);
}
void do_labcomm(int sockfd)
{
void *enc = enc_init(sockfd);
void *dec = dec_init(sockfd, enc);
dec_run(dec);
dec_cleanup(dec);
enc_cleanup(enc);
}
int main(int argc, char *argv[])
{
int sockfd, portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;
char buffer[256];
if (argc < 3) {
fprintf(stderr,"usage %s hostname port\n", argv[0]);
exit(0);
}
portno = atoi(argv[2]);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
server = gethostbyname(argv[1]);
if (server == NULL) {
fprintf(stderr,"ERROR, no such host\n");
exit(0);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serv_addr.sin_addr.s_addr,
server->h_length);
serv_addr.sin_port = htons(portno);
if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
error("ERROR connecting");
do_labcomm(sockfd);
close(sockfd);
return 0;
}
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<GCDocument color="-1" dimTicks="25" dpwsInterface="" dpwsPort="-1" height="599" horizontalScrollBar="1" modifiable="1" name="Client" saveVersion="7" scale="1.0" simulationMode="1" socketHost="" socketIsServer="0" socketPort="-1" socketSendMode="Changed" threadSpeed="40" tokenLuminance="0" verticalScrollBar="1" viewPositionX="0" viewPositionY="0" width="490" x="358" y="-1">
<LabCommObject height="60" isSocketServer="0" name="LabComm" socketHost="127.0.0.1" socketPort="9999" specification="sample struct {&#10; double b;&#10; int c;&#10; int d;&#10; string e;&#10; boolean f;&#10; short g;&#10; long h;&#10; float i;&#10;} foo;&#10;&#10;//sample int bar;&#10;" width="60" x="50" y="300"/>
<GCInitialStep actionBlockVisible="1" actionText="P b = LabComm.foo.b;" fileName="" height="70" id="d518e8b8-6b49-4af4-abcc-ec8024853580" name="" useIcon="0" width="200" x="140" y="65"/>
<IntegerVariable constant="0" exp="" height="45" initialValue="" name="b" updated="0" value="437" width="65" x="380" y="70"/>
</GCDocument>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <labcomm_fd_reader.h>
#include <labcomm_default_error_handler.h>
#include <labcomm_default_memory.h>
#include <labcomm_default_scheduler.h>
#include <stdio.h>
#include "jg.h"
static struct labcomm_encoder *encoder;
static void handle_foo(jg_foo *v, void *context) {
printf("got foo: b=%f, e=%s, f=%d\n", v->b, v->e, v->f);
v->d = v->d+1;
labcomm_encode_jg_foo(encoder, v);
usleep(500000);
v->d = v->d+1;
labcomm_encode_jg_foo(encoder, v);
}
struct labcomm_decoder *dec_init(int fd, struct labcomm_encoder *e) {
struct labcomm_decoder *decoder;
encoder = e;
void *context = NULL;
decoder = labcomm_decoder_new(labcomm_fd_reader_new(
labcomm_default_memory, fd, 1),
labcomm_default_error_handler,
labcomm_default_memory,
labcomm_default_scheduler);
if (!decoder) {
printf("Failed to allocate decoder %s:%d\n", __FUNCTION__, __LINE__);
return (void *)0;
}
labcomm_decoder_register_jg_foo(decoder, handle_foo, context);
return decoder;
}
void dec_run(struct labcomm_decoder *decoder) {
printf("Decoding:\n");
labcomm_decoder_run(decoder);
printf("--- End Of File ---:\n");
}
void dec_cleanup(struct labcomm_decoder *decoder) {
labcomm_decoder_free(decoder);
}
#include <labcomm_fd_reader.h>
#include <labcomm_default_error_handler.h>
#include <labcomm_default_memory.h>
#include <labcomm_default_scheduler.h>
#include <stdio.h>
struct labcomm_decoder * dec_init(int fd, struct labcomm_encoder *e) ;
void dec_run(struct labcomm_decoder *decoder) ;
void dec_cleanup(struct labcomm_decoder *decoder) ;
#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 "jg.h"
#include <stdio.h>
struct labcomm_encoder *enc_init( int fd) {
struct labcomm_encoder *encoder;
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_jg_foo(encoder);
return encoder;
}
void enc_run(struct labcomm_encoder *encoder, jg_foo *v) {
#if 0
jg_foo v;
v.b = 17.17;
v.c = 1742;
v.d = 4217;
v.e = "hello";
v.f = 17;
v.g = 42;
v.h = 2;
v.i = 42.42;
#endif
usleep(500000);
v->b += 42;
v->h += 42000000;
labcomm_encode_jg_foo(encoder, v);
}
void enc_cleanup(struct labcomm_encoder *encoder) {
labcomm_encoder_free(encoder);
}
#include <labcomm_fd_writer.h>
#include <labcomm_default_error_handler.h>
#include <labcomm_default_memory.h>
#include <labcomm_default_scheduler.h>
#include "jg.h"
struct labcomm_encoder *enc_init(int fd) ;
void enc_run(struct labcomm_encoder *encoder, jg_foo *v) ;
void enc_cleanup(struct labcomm_encoder *encoder) ;
sample struct {
double b;
int c;
int d;
string e;
boolean f;
short g;
long h;
float i;
} foo;
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.foo;
import labcommTCPtest.gen.foo.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 );
foo.register(e);
foo sample = new foo();
sample.c = 17;
sample.d = 42;
sample.h = 1717;
sample.b = 0.42;
sample.e = "Testing";
printSample("Client sending", sample);
foo.encode(e, sample);
DecoderChannel c = new DecoderChannel(in);
foo.register(c,this);
c.run();
} 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, foo sample2) throws Exception {
System.out.println(header);
System.out.format("TestClient.invoke(%f, %d, %d, %d)\n", sample2.b, sample2.c, sample2.d, sample2.h);
}
public void handle_foo(foo sample2) throws Exception {
printSample("TestClient.handle_foo", 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;
/**
* 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);
}
}
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.labcomm2014.DecoderChannel;
import se.lth.control.labcomm2014.EncoderChannel;
import labcommTCPtest.gen.foo;
import labcommTCPtest.gen.foo.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 {
DecoderChannel c = new DecoderChannel(in);
foo.register(c,this);
c.runOne();
} catch (Exception e) {
e.printStackTrace();
}
}
public void handle_foo(foo sample) throws Exception {
EncoderChannel e = new EncoderChannel(out );
foo.register(e);
System.out.println("TestServer.handle_foo...");
sample.b *= 2;
sample.c *= 2;
sample.d *= 2;
sample.h *= 2;
foo.encode(e, sample);
}
}
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<GCDocument color="-1" dimTicks="25" dpwsInterface="" dpwsPort="-1" height="600" horizontalScrollBar="1" modifiable="1" name="Server" saveVersion="7" scale="1.0" simulationMode="1" socketHost="" socketIsServer="0" socketPort="-1" socketSendMode="Changed" threadSpeed="1000" tokenLuminance="0" verticalScrollBar="1" viewPositionX="0" viewPositionY="0" width="434" x="0" y="0">
<LabCommObject height="60" isSocketServer="1" name="LabComm" socketHost="asdf" socketPort="9999" specification="sample struct {&#10; double b;&#10; int c;&#10; int d;&#10; string e;&#10; boolean f;&#10; short g;&#10; long h;&#10; float i;&#10;} foo;&#10;&#10;//sample int bar;&#10;" width="60" x="60" y="290"/>
<GCInitialStep actionBlockVisible="1" actionText="S LabComm.foo.b = LabComm.foo.b + 1.1;&#10;S LabComm.foo.c = 8;&#10;S LabComm.foo.d = 10;&#10;S LabComm.foo.e = &quot;hej&quot;;&#10;S LabComm.foo.f = !LabComm.foo.f;&#10;S LabComm.foo.g = 32767;&#10;S LabComm.foo.h = 12345678;&#10;S LabComm.foo.i = 2.4;" fileName="" height="70" id="ef14bcd4-aa1c-4970-9a6c-5e1269c53245" name="" useIcon="0" width="200" x="170" y="15"/>
<GCTransition actionText="1" conditionVisible="1" height="25" id="e580e2b4-4aa2-4311-bd7a-8dfe4d3e0cb8" width="30" x="185" y="200"/>
<GCStep actionBlockVisible="1" actionText="S lcSend(LabComm); // send all samples&#10;//S lcSend(LabComm, &quot;foo&quot;);" fileName="" height="70" id="bb65796a-2b87-4155-af9c-cb36439497d7" name="" useIcon="0" width="200" x="170" y="235"/>
<GCTransition actionText="0" conditionVisible="1" height="25" id="975b3011-87d6-4e75-9532-adb4153cad38" width="30" x="185" y="320"/>
<GCStep actionBlockVisible="1" actionText="S LabComm.foo.b = LabComm.foo.b + 1.1;&#10;S LabComm.foo.f = !LabComm.foo.f;" fileName="" height="70" id="7503a140-222a-477f-be8f-6b358e1ea6ae" name="" useIcon="0" width="200" x="170" y="125"/>
<GCTransition actionText="1" conditionVisible="1" height="25" id="26534603-8853-4a41-81ea-cb4d5cbd134a" width="30" x="185" y="90"/>
<GCLink fromObject="e580e2b4-4aa2-4311-bd7a-8dfe4d3e0cb8" manAdj="0" toObject="bb65796a-2b87-4155-af9c-cb36439497d7">
<Stroke x="200" y="226"/>
<Stroke x="200" y="236"/>
<Stroke x="200" y="236"/>
<Stroke x="200" y="224"/>
<Stroke x="200" y="224"/>
<Stroke x="200" y="234"/>
</GCLink>
<GCLink fromObject="bb65796a-2b87-4155-af9c-cb36439497d7" manAdj="0" toObject="975b3011-87d6-4e75-9532-adb4153cad38">
<Stroke x="200" y="306"/>
<Stroke x="200" y="316"/>
<Stroke x="200" y="316"/>
<Stroke x="200" y="309"/>
<Stroke x="200" y="309"/>
<Stroke x="200" y="319"/>
</GCLink>
<GCLink fromObject="7503a140-222a-477f-be8f-6b358e1ea6ae" manAdj="0" toObject="e580e2b4-4aa2-4311-bd7a-8dfe4d3e0cb8">
<Stroke x="200" y="196"/>
<Stroke x="200" y="206"/>
<Stroke x="200" y="206"/>
<Stroke x="200" y="189"/>
<Stroke x="200" y="189"/>
<Stroke x="200" y="199"/>
</GCLink>
<GCLink fromObject="ef14bcd4-aa1c-4970-9a6c-5e1269c53245" manAdj="0" toObject="26534603-8853-4a41-81ea-cb4d5cbd134a">
<Stroke x="200" y="86"/>
<Stroke x="200" y="96"/>
<Stroke x="200" y="96"/>
<Stroke x="200" y="79"/>
<Stroke x="200" y="79"/>
<Stroke x="200" y="89"/>
</GCLink>
<GCLink fromObject="26534603-8853-4a41-81ea-cb4d5cbd134a" manAdj="0" toObject="7503a140-222a-477f-be8f-6b358e1ea6ae">
<Stroke x="200" y="116"/>
<Stroke x="200" y="126"/>
<Stroke x="200" y="126"/>
<Stroke x="200" y="114"/>
<Stroke x="200" y="114"/>
<Stroke x="200" y="124"/>
</GCLink>
<GCLink fromObject="975b3011-87d6-4e75-9532-adb4153cad38" manAdj="1" toObject="7503a140-222a-477f-be8f-6b358e1ea6ae">
<Stroke x="200" y="346"/>
<Stroke x="200" y="356"/>
<Stroke x="134" y="356"/>
<Stroke x="134" y="114"/>
<Stroke x="200" y="114"/>
<Stroke x="200" y="124"/>
</GCLink>
</GCDocument>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include "jg.h"
#include "dec.h"
#include "enc.h"
void error(const char *msg)
{
perror(msg);
exit(0);
}
void do_labcomm(int sockfd, jg_foo *v)
{
void *enc = enc_init(sockfd);
int i;
for(i=0; i<10;i++) {
enc_run(enc, v);
}
enc_cleanup(enc);
}
int main(int argc, char *argv[])
{
int sockfd, portno, n;
int newsockfd, clilen;
struct sockaddr_in serv_addr, cli_addr;
struct hostent *server;
char buffer[256];
if (argc < 2) {
fprintf(stderr,"usage %s port\n", argv[0]);
exit(0);
}
portno = atoi(argv[1]);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
int so_reuseaddr = 1; //TRUE;
if(setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR, &so_reuseaddr, sizeof so_reuseaddr)) {
error("ERROR setting socket options");
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
perror("ERROR on binding");
exit(1);
}
listen(sockfd,2);
clilen = sizeof(cli_addr);
/* Accept actual connection from the client */
newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, &clilen);
if (newsockfd < 0) {
perror("ERROR on accept");
exit(1);
}
jg_foo v;
v.b = 17.17;
v.c = 1742;
v.d = 4217;
v.e = "hello";
v.f = 17;
v.g = 42;
v.h = 2;
v.i = 42.42;
do_labcomm(newsockfd, &v);
close(newsockfd);
close(sockfd);
return 0;
}
/* Topic types */
sample struct {
string turtle_name;
float linear;
float angular;
} velocity;
sample struct {
string turtle_name;
float x;
float y;
float theta;
float linear_velocity;
float angular_velocity;
} pose;
sample struct {
string turtle_name;
float r;
float g;
float b;
} color;
/* Srv types */
sample void clear; /* Empty. */
sample void clear_resp; /* Empty. */
sample void reset; /* Empty. */
sample void reset_resp; /* Empty. */
sample string kill; /* Name of turtle. */
sample void kill_resp; /* Empty. */
sample struct {
string name;
float x;
float y;
float theta;
} spawn; /* Coordinates and direction. */
sample string spawn_resp; /* Returns name of turtle */
sample struct {
string turtle_name;
byte r;
byte g;
byte b;
byte width;
byte off;
} set_pen; /* Pen properties. */
sample void set_pen_resp; /* Empty. */
/* TODO: The following two are duplicates. Change to typedefs when working. */
sample struct {
string turtle_name;
float x;
float y;
float theta;
} teleport_absolute; /* Coordinates and direction. */
sample void teleport_absolute_resp; /* Empty. */
sample struct {
string turtle_name;
float linear;
float angular;
} teleport_relative; /* Similar to velocity but instant. */
sample void teleport_relative_resp; /* Empty. */
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <labcomm_fd_reader.h>
#include <labcomm_default_error_handler.h>
#include <labcomm_default_memory.h>
#include <labcomm_default_scheduler.h>
#include <stdio.h>
#include "turtle1.h"
static struct labcomm_encoder *encoder;
static void handle_pose(turtle1_pose *v, void *context) {
#if 1
printf("got pose\n");
#else
turtle1_velocity vel;
sleep(1);
labcomm_encode_turtle1_velocity(encoder, vel);
vel.angular = -1;
sleep(1);
labcomm_encode_turtle1_velocity(encoder, vel);
sleep(1);
vel.angular = -1;
labcomm_encode_turtle1_velocity(encoder, vel);
#endif
}
struct labcomm_decoder *turtle_dec_init(int fd, struct labcomm_encoder *e) {
struct labcomm_decoder *decoder;
encoder = e;
void *context = NULL;
decoder = labcomm_decoder_new(labcomm_fd_reader_new(
labcomm_default_memory, fd, 1),
labcomm_default_error_handler,
labcomm_default_memory,
labcomm_default_scheduler);
if (!decoder) {
printf("Failed to allocate decoder %s:%d\n", __FUNCTION__, __LINE__);
return (void *)0;
}
labcomm_decoder_register_turtle1_pose(decoder, handle_pose, context);
return decoder;
}
void turtle_dec_run(struct labcomm_decoder *decoder) {
printf("Decoding:\n");
labcomm_decoder_run(decoder);
printf("--- End Of File ---:\n");
}
void turtle_dec_cleanup(struct labcomm_decoder *decoder) {
labcomm_decoder_free(decoder);
}