Skip to content
Snippets Groups Projects
Commit 6f97ea58 authored by Sven Gestegård Robertz's avatar Sven Gestegård Robertz
Browse files

added user_types example (from explicit-types branch)

parent a31dae65
Branches
Tags
No related merge requests found
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import se.lth.control.labcomm.LabCommDecoderChannel;
public class Decoder
implements twoLines.Handler
{
LabCommDecoderChannel decoder;
public Decoder(InputStream in)
throws Exception
{
decoder = new LabCommDecoderChannel(in);
twoLines.register(decoder, this);
try {
System.out.println("Running decoder.");
decoder.run();
} catch (java.io.EOFException e) {
System.out.println("Decoder reached end of file.");
}
}
private String genPoint(point p) {
return "("+p.x.val+", "+p.y.val+")";
}
private String genLine(line l) {
return "Line from "+genPoint(l.start)+" to "+genPoint(l.end);
}
public void handle_twoLines(twoLines d) throws java.io.IOException {
System.out.print("Got twoLines: ");
System.out.println("Line l1: "+genLine(d.l1));
System.out.println(" Line l2: "+genLine(d.l2));
}
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;
/**
* Simple encoder
*/
public class Encoder
{
LabCommEncoderChannel encoder;
public Encoder(OutputStream out)
throws Exception
{
encoder = new LabCommEncoderChannel(out);
twoLines.register(encoder);
}
public void doEncode() throws java.io.IOException {
twoLines x = new twoLines();
line l1 = new line();
point p11 = new point();
coord c11x = new coord();
coord c11y = new coord();
c11x.val = 11;
c11y.val = 99;
p11.x = c11x;
p11.y = c11y;
l1.start = p11;
point p12 = new point();
coord c12x = new coord();
coord c12y = new coord();
c12x.val = 22;
c12y.val = 88;
p12.x = c12x;
p12.y = c12y;
l1.end = p12;
line l2 = new line();
point p21 = new point();
coord c21x = new coord();
coord c21y = new coord();
c21x.val = 17;
c21y.val = 42;
p21.x = c21x;
p21.y = c21y;
l2.start = p21;
point p22 = new point();
coord c22x = new coord();
coord c22y = new coord();
c22x.val = 13;
c22y.val = 37;
p22.x = c22x;
p22.y = c22y;
l2.end = p22;
foo f = new foo();
f.a = 10;
f.b = 20;
f.c = false;
x.l1 = l1;
x.l2 = l2;
x.f = f;
System.out.println("Encoding theTwoLines");
twoLines.encode(encoder, x);
}
public static void main(String[] arg) throws Exception {
FileOutputStream fos = new FileOutputStream(arg[0]);
Encoder example = new Encoder(fos);
example.doEncode();
fos.close();
}
}
### Example compile script, showing the steps required to build a labcomm application
### (including compiler and libs).
# For current version (2013)
(cd ../..; make all)
mkdir -p gen
java -jar ../../compiler/labComm.jar --java=gen --c=gen/test.c --h=gen/test.h --python=gen/test.py test.lc
javac -cp ../../lib/java:. gen/*.java Encoder.java Decoder.java
gcc -Wall -Werror -Wno-unused-function \
-I. -I../../lib/c -L../../lib/c \
-DLABCOMM_COMPAT=\"labcomm_compat_osx.h\" \
-llabcomm2013 \
-o example_encoder example_encoder.c gen/test.c
gcc -Wall -Werror -I . -I ../../lib/c -L../../lib/c \
-DLABCOMM_COMPAT=\"labcomm_compat_osx.h\" \
-o example_decoder example_decoder.c gen/test.c \
-llabcomm2013
#-Tlabcomm.linkscript
#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 "gen/test.h"
#include <stdio.h>
static void handle_test_twoLines(test_twoLines *v,void *context) {
printf("Got theTwoInts. (%d,%d) -> (%d,%d), (%d,%d) -> (%d,%d)\n", v->l1.start.x.val, v->l1.start.y.val,
v->l1.end.x.val, v->l1.end.y.val,
v->l2.start.x.val, v->l2.start.y.val,
v->l2.end.x.val, v->l2.end.y.val);
}
int main(int argc, char *argv[]) {
int fd;
struct labcomm_decoder *decoder;
void *context = NULL;
char *filename = argv[1];
printf("C decoder reading from %s\n", filename);
fd = open(filename, O_RDONLY);
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 1;
}
labcomm_decoder_register_test_twoLines(decoder, handle_test_twoLines, context);
printf("Decoding:\n");
labcomm_decoder_run(decoder);
printf("--- End Of File ---:\n");
labcomm_decoder_free(decoder);
return 0;
}
#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 "gen/test.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_test_twoLines(encoder);
test_twoLines tl;
tl.l1.start.x.val = 11;
tl.l1.start.y.val = 13;
tl.l1.end.x.val = 21;
tl.l1.end.y.val = 23;
tl.l2.start.x.val = 11;
tl.l2.start.y.val = 13;
tl.l2.end.x.val = 21;
tl.l2.end.y.val = 23;
printf("Encoding twoLines...\n");
labcomm_encode_test_twoLines(encoder, &tl);
return 0;
}
#!/usr/bin/python
import labcomm
import sys
import test
if __name__ == '__main__':
version = sys.argv[2] if len(sys.argv) == 3 else "LabComm2013"
encoder = labcomm.Encoder(labcomm.StreamWriter(open(sys.argv[1], 'w')), version)
encoder.add_decl(test.twoLines.signature)
l1 = test.line()
c1s = test.point()
c1sx = test.coord()
c1sx.val = 1
c1sy = test.coord()
c1sy.val = 11
c1s.x = c1sx;
c1s.y = c1sy;
c1e = test.point()
c1ex = test.coord()
c1ex.val = 2
c1ey = test.coord()
c1ey.val = 22
c1e.x = c1ex;
c1e.y = c1ey;
l1.start=c1s
l1.end = c1e
l2 = test.line()
c2s = test.point()
c2sx = test.coord()
c2sx.val = 3
c2sy = test.coord()
c2sy.val = 33
c2s.x = c2sx;
c2s.y = c2sy;
c2e = test.point()
c2ex = test.coord()
c2ex.val = 4
c2ey = test.coord()
c2ey.val = 44
c2e.x = c2ex;
c2e.y = c2ey;
l2.start=c2s
l2.end = c2e
tl = test.twoLines()
tl.l1 = l1
tl.l2 = l2
tl.f = test.foo()
tl.f.a = 10;
tl.f.b = 20;
tl.f.c = False;
encoder.encode(tl, test.twoLines.signature)
export LD_LIBRARY_PATH=../../lib/c/
echo
echo "********************************************"
echo "*** Running example for version 2013 ***"
echo "********************************************"
echo
java -cp .:../../lib/java:gen Encoder encoded_data
echo "running Java decoder:"
java -cp .:../../lib/java:gen Decoder encoded_data
echo "running C decoder:"
./example_decoder encoded_data
echo "running python decoder (from wiki_example):"
PYTHONPATH=../../lib/python ../wiki_example/example_decoder.py encoded_data LabComm2013
echo "running C encoder:"
./example_encoder encoded_data
echo "running Java decoder:"
java -cp .:../../lib/java:gen Decoder encoded_data
echo "running C decoder:"
./example_decoder encoded_data
echo "running python decoder (from wiki_example):"
PYTHONPATH=../../lib/python ../wiki_example/example_decoder.py encoded_data LabComm2013
echo "running python encoder:"
PYTHONPATH=../../lib/python:gen ./example_encoder.py encoded_data2
echo "running Java decoder:"
java -cp .:../../lib/java:gen Decoder encoded_data2
echo "running C decoder:"
./example_decoder encoded_data2
typedef struct {
int val;
} coord;
typedef struct {
coord x;
coord y;
} point;
typedef struct {
point start;
point end;
} line;
typedef struct {
int a;
int b;
boolean c;
} foo;
sample struct {
line l1;
line l2;
foo f;
} twoLines;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment