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 811 additions and 402 deletions
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using se.lth.control.labcomm2014;
namespace user_types
{
public class Encoder
{
private EncoderChannel enc;
public Encoder(Stream stream)
{
enc = new EncoderChannel(stream);
twoLines.register(enc);
}
public void doEncode()
{
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;
Console.WriteLine("Encoding theTwoLines");
twoLines.encode(enc, x);
}
static void Main(string[] args)
{
FileStream stream = new FileStream(args[0],FileMode.Create);
Encoder example = new Encoder(stream);
example.doEncode();
stream.Close();
}
}
}
LCDIR=../..
LCCJAR=${LCDIR}/compiler/labcomm2014_compiler.jar# the LabComm compiler
LCLJAR=${LCDIR}/lib/java/labcomm2014.jar# the LabComm library
EXECUTABLES=example_encoder example_decoder \
Encoder.class Decoder.class TDDecoder.class \
TestDataType.class \
ExampleEncoder.exe ExampleDecoder.exe
include ${LCDIR}/lib/c/os_compat.mk
GENDIR=gen
.PHONY: all cleanbuild clean distclean build run allall buildcs runwcs runjastadd
all: cleanbuild run
allall: clean build buildcs runwcs
test: allall
###############################################
### dependencies and parts ####################
###############################################
LCC=java -jar ${LCCJAR}
CLASSPATH=.:${LCLJAR}
${LCCJAR} :
$MAKE -C ${LCDIR} make-compiler
${LCLJAR} :
$MAKE -C ${LCDIR} labcomm2014.jar
cleanbuild: clean build
labcomm2014.dll:
ln -sf ../../lib/csharp/labcomm2014.dll $@
ExampleEncoder.exe: ExampleEncoder.cs gen/test.cs labcomm2014.dll Makefile
mcs -out:$@ $(filter %.cs, $^) -lib:../../lib/csharp/ -r:labcomm2014
chmod a+x $@
ExampleDecoder.exe: ExampleDecoder.cs gen/test.cs labcomm2014.dll Makefile
mcs -out:$@ $(filter %.cs, $^) -lib:../../lib/csharp/ -r:labcomm2014
chmod a+x $@
build :
mkdir -p ${GENDIR}
java -jar ${LCDIR}/compiler/labcomm2014_compiler.jar --java=${GENDIR} --c=${GENDIR}/test.c --h=${GENDIR}/test.h --python=${GENDIR}/test.py --cs=${GENDIR}/test.cs test.lc
javac -cp ${LCDIR}/lib/java/labcomm2014.jar:. ${GENDIR}/*.java Encoder.java Decoder.java
javac -cp ${LCDIR}/lib/java/labcomm2014.jar:${LCCJAR}:${GENDIR}:. TDDecoder.java
javac -cp ${LCDIR}/lib/java/labcomm2014.jar:${LCCJAR}:${GENDIR}:. TestDataType.java
${CC} ${CFLAGS} ${LDFLAGS} -Wall -Werror -Wno-unused-function \
-I. -I${LCDIR}/lib/c/2014 -L${LCDIR}/lib/c \
-o example_encoder example_encoder.c ${GENDIR}/test.c \
-llabcomm2014
${CC} ${CFLAGS} ${LDFLAGS} -Wall -Werror -I . -I ${LCDIR}/lib/c/2014 -L${LCDIR}/lib/c \
-o example_decoder example_decoder.c ${GENDIR}/test.c \
-llabcomm2014
buildcs: ExampleEncoder.exe ExampleDecoder.exe
run:
@echo
@echo "********************************************"
@echo "*** ************ running example for version 2014 ***"
@echo "********************************************"
@echo
@java -cp .:${LCDIR}/lib/java/labcomm2014.jar:${GENDIR} Encoder encoded_data_j
@echo "************ running Java decoder: *****************"
@java -cp .:${LCDIR}/lib/java/labcomm2014.jar:${GENDIR} Decoder encoded_data_j
@echo "************ running C decoder: *****************"
@${LD_LIBRARY_PATH_NAME}=${LCDIR}/lib/c/ ./example_decoder encoded_data_j
@echo "************ running python decoder (from wiki_example):"
@PYTHONPATH=${LCDIR}/lib/python ../wiki_example/example_decoder.py encoded_data_j LabComm2014
@echo "************ running python decoder (with handlers):"
@PYTHONPATH=${LCDIR}/lib/python:gen:. ./example_handlers.py encoded_data_j LabComm2014
@echo "************ running C encoder: *****************"
@${LD_LIBRARY_PATH_NAME}=${LCDIR}/lib/c/ ./example_encoder encoded_data_c
@echo "************ running Java decoder: *****************"
@java -cp .:${LCDIR}/lib/java/labcomm2014.jar:${GENDIR} Decoder encoded_data_c
@echo "************ running C decoder: *****************"
@${LD_LIBRARY_PATH_NAME}=${LCDIR}/lib/c/ ./example_decoder encoded_data_c
@echo "************ running python decoder (from wiki_example):"
@PYTHONPATH=${LCDIR}/lib/python ../wiki_example/example_decoder.py encoded_data_c LabComm2014
@echo "************ running python decoder (with handlers):"
@PYTHONPATH=${LCDIR}/lib/python:gen:. ./example_handlers.py encoded_data_j LabComm2014
@echo "************ running python encoder: *****************"
@PYTHONPATH=${LCDIR}/lib/python:${GENDIR} ./example_encoder.py encoded_data_p LabComm2014
@echo "************ running Java decoder: *****************"
@java -cp .:${LCDIR}/lib/java/labcomm2014.jar:${GENDIR} Decoder encoded_data_p
@echo "************ running C decoder: *****************"
@${LD_LIBRARY_PATH_NAME}=${LCDIR}/lib/c/ ./example_decoder encoded_data_p
@echo "************ running python decoder (from wiki_example):"
PYTHONPATH=${LCDIR}/lib/python ../wiki_example/example_decoder.py encoded_data_p LabComm2014
@echo "************ running python decoder (with handlers):"
@PYTHONPATH=${LCDIR}/lib/python:gen:. ./example_handlers.py encoded_data_j LabComm2014
runwcs: build ExampleEncoder.exe ExampleDecoder.exe
@echo
@echo "********************************************"
@echo "*** ************ running example for version 2014 ***"
@echo "********************************************"
@echo
@java -cp .:${LCDIR}/lib/java/labcomm2014.jar:${GENDIR} Encoder encoded_data_j
@echo "************ running Java decoder: *****************"
@java -cp .:${LCDIR}/lib/java/labcomm2014.jar:${GENDIR} Decoder encoded_data_j
@echo "************ running C decoder: *****************"
@${LD_LIBRARY_PATH_NAME}=${LCDIR}/lib/c/ ./example_decoder encoded_data_j
@echo "************ running python decoder (from wiki_example):"
@PYTHONPATH=${LCDIR}/lib/python ../wiki_example/example_decoder.py encoded_data_j LabComm2014
@echo "************ running C# decoder: *****************"
@${LD_LIBRARY_PATH_NAME}=${LCDIR}/lib/c/ mono ./ExampleDecoder.exe encoded_data_j
@echo "************ running C encoder: *****************"
@${LD_LIBRARY_PATH_NAME}=${LCDIR}/lib/c/ ./example_encoder encoded_data_c
@echo "************ running Java decoder: *****************"
@java -cp .:${LCDIR}/lib/java/labcomm2014.jar:${GENDIR} Decoder encoded_data_c
@echo "************ running C decoder: *****************"
@${LD_LIBRARY_PATH_NAME}=${LCDIR}/lib/c/ ./example_decoder encoded_data_c
@echo "************ running python decoder (from wiki_example):"
@PYTHONPATH=${LCDIR}/lib/python ../wiki_example/example_decoder.py encoded_data_c LabComm2014
@echo "************ running C# decoder: *****************"
@mono ./ExampleDecoder.exe encoded_data_c
@echo "************ running python encoder: *****************"
@PYTHONPATH=${LCDIR}/lib/python:${GENDIR} ./example_encoder.py encoded_data_p LabComm2014
@echo "************ running Java decoder: *****************"
@java -cp .:${LCDIR}/lib/java/labcomm2014.jar:${GENDIR} Decoder encoded_data_p
@echo "************ running C decoder: *****************"
@${LD_LIBRARY_PATH_NAME}=${LCDIR}/lib/c/ ./example_decoder encoded_data_p
@echo "************ running C# decoder: *****************"
@mono ./ExampleDecoder.exe encoded_data_p
@echo "************ running python decoder (from wiki_example):"
PYTHONPATH=${LCDIR}/lib/python ../wiki_example/example_decoder.py encoded_data_p LabComm2014
@echo "************ running C# encoder: *****************"
@mono ./ExampleEncoder.exe encoded_data_cs
@echo "************ running Java decoder: *****************"
@java -cp .:${LCDIR}/lib/java/labcomm2014.jar:${GENDIR} Decoder encoded_data_cs
@echo "************ running C decoder: *****************"
@LD_LIBRARY_PATH=${LCDIR}/lib/c/ ./example_decoder encoded_data_cs
@echo "************ running python decoder (from wiki_example):"
@PYTHONPATH=${LCDIR}/lib/python ../wiki_example/example_decoder.py encoded_data_cs LabComm2014
@echo "************ running C# decoder: *****************"
@mono ./ExampleDecoder.exe encoded_data_cs
runjastadd: cleanbuild
@echo
@echo "********************************************"
@echo "*** ************ running example with JastAdd unparsing ***"
@echo "********************************************"
@echo
@java -cp .:${LCDIR}/lib/java/labcomm2014.jar:${GENDIR} Encoder encoded_data_j
@echo "************ running Java TypeDefdecoder: *****************"
java -cp .:${LCDIR}/lib/java/labcomm2014.jar:${LCCJAR}:${GENDIR} TDDecoder encoded_data_j
rundatatype: cleanbuild
@echo
@echo "********************************************"
@echo "*************** running datatype example ***"
@echo "********************************************"
@echo
@java -cp .:${LCDIR}/lib/java/labcomm2014.jar:${GENDIR} TestDataType
clean:
rm -rf ${GENDIR}
distclean: clean
rm -f ${EXECUTABLES}
rm -f encoded_data_c
rm -f encoded_data_cs
rm -f encoded_data_j
rm -f encoded_data_p
rm -f labcomm.dll
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.IOException;
import se.lth.control.labcomm2014.DecoderChannel;
import se.lth.control.labcomm2014.TypeDef;
import se.lth.control.labcomm2014.TypeDefParser;
import se.lth.control.labcomm2014.SigTypeDef;
import se.lth.control.labcomm2014.ParsedSampleDef;
import se.lth.control.labcomm2014.ASTbuilder;
//import se.lth.control.labcomm2014.TypeBinding;
import se.lth.control.labcomm2014.DataType;
import se.lth.control.labcomm2014.compiler.Specification;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.Vector;
import java.util.LinkedList;
import java.util.Iterator;
public class TDDecoder
implements twoLines.Handler,
// TypeDef.Handler,
// TypeBinding.Handler,
TypeDefParser.TypeDefListener,
twoInts.Handler,
theFirstInt.Handler,
theSecondInt.Handler,
doavoid.Handler,
intAndRef.Handler
{
private DecoderChannel decoder;
private TypeDefParser tdp;
public TDDecoder(InputStream in)
throws Exception
{
decoder = new DecoderChannel(in);
twoInts.register(decoder, this);
twoLines.register(decoder, this);
theFirstInt.register(decoder, this);
theSecondInt.register(decoder, this);
doavoid.register(decoder, this);
intAndRef.register(decoder, this);
doavoid.registerSampleRef(decoder);
this.tdp = TypeDefParser.registerTypeDefParser(decoder);
// TypeDef.register(decoder, this);
// TypeBinding.register(decoder, this);
tdp.addListener(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_TypeDef(TypeDef d) throws java.io.IOException {
// System.out.println("Got TypeDef: "+d.getName()+"("+d.getIndex()+")");
// }
//
// public void handle_TypeBinding(TypeBinding d) throws java.io.IOException {
// System.out.println("Got TypeBinding: "+d.getSampleIndex()+" --> "+d.getTypeIndex()+"");
// }
public void onTypeDef(SigTypeDef d) {
if(d != null && d.isSampleDef()){
System.out.println("onTypeDef (sample): ");
//------------
try {
System.out.println("==================== DataType ======");
DataType.printDataType(System.out, d);
System.out.println();
System.out.println("==================== end ======");
} catch (IOException ioe) {
ioe.printStackTrace();
}
ASTbuilder v = new ASTbuilder();
Specification p = v.makeSpecification((ParsedSampleDef) d);
try {
FileOutputStream f = new FileOutputStream("/tmp/foopp"+d.getName()+".txt");
PrintStream out = new PrintStream(f);
p.pp(System.out);
//p.C_genC(System.out, new Vector(), "lcname", "prefix", 2014);
p.J_gen(out, "testpackage", 2014);
out.close();
} catch (Throwable e) {
System.err.println("Exception: " + e);
e.printStackTrace();
}
}
//System.out.println(" "+d.getName()+";");
//for(byte b: d.getSignature()) {
// System.out.print(Integer.toHexString(b)+" ");
//}
//System.out.println();
//try {
// tdp.parseSignature(d.getIndex());
//} catch(IOException ex) { ex.printStackTrace();}
}
public void handle_twoInts(twoInts d) throws java.io.IOException {
System.out.print("Got twoInts: ");
System.out.println(d.a +", "+d.b);
}
public void handle_theFirstInt(int d) throws java.io.IOException {
System.out.println("Got theFirstInt: "+d);
}
public void handle_theSecondInt(int d) throws java.io.IOException {
System.out.println("Got theSecondInt: "+d);
}
public void handle_doavoid() throws java.io.IOException {
System.out.println("Got a void.");
}
public void handle_intAndRef(intAndRef d) throws java.io.IOException {
System.out.println("Got intAndRef: "+d.x+", "+d.reference);
}
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 {
TDDecoder example = new TDDecoder(
new FileInputStream(new File(arg[0]))
);
}
}
import java.io.PrintStream;
import se.lth.control.labcomm2014.*;
/**
* Test data type tree
*/
public class TestDataType
{
private PrintStream out;
public TestDataType(PrintStream out)
throws Exception
{
this.out = out;
}
public void doTest() throws java.io.IOException {
twoLines x = new twoLines();
DataType.printDataType(out, x.getDispatcher());
twoStructsAndInt y = new twoStructsAndInt();
DataType.printDataType(out, y.getDispatcher());
theFirstInt z = new theFirstInt();
DataType.printDataType(out, z.getDispatcher());
doavoid a = new doavoid();
DataType.printDataType(out, a.getDispatcher());
}
public static void main(String[] arg) throws Exception {
TestDataType example = new TestDataType(System.out);
example.doTest();
}
}
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <labcomm2014_fd_reader.h>
#include <labcomm2014_default_error_handler.h>
#include <labcomm2014_default_memory.h>
#include <labcomm2014_default_scheduler.h>
#include "gen/test.h"
#include <stdio.h>
static void handle_test_doavoid(test_doavoid *v,void *context) {
printf("Got a void.\n");
}
static void handle_test_intAndRef(test_intAndRef *v,void *context) {
printf("Got intAndRef. (%d : %p) \n", v->x, v->reference);
}
static void handle_test_twoInts(test_twoInts *v,void *context) {
printf("Got twoInts. (%d,%d) \n", v->a, v->b);
}
static void handle_test_theFirstInt(int *v,void *context) {
printf("Got theFirstInt. (%d) \n", *v);
}
static void handle_test_theSecondInt(int *v,void *context) {
printf("Got theSecondInt. (%d) \n", *v);
}
static void handle_type_def(struct labcomm2014_raw_type_def *v,void *context) {
printf("Got type_def. (0x%x) %s\n", v->index, v->name);
}
static void handle_type_binding(struct labcomm2014_type_binding *v,void *context) {
printf("Got type binding. 0x%x --> 0x%x\n", v->sample_index, v->type_index);
}
static void handle_test_twoLines(test_twoLines *v,void *context) {
printf("Got twoLines. (%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 labcomm2014_decoder *decoder;
void *context = NULL;
char *filename = argv[1];
printf("C decoder reading from %s\n", filename);
fd = open(filename, O_RDONLY);
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;
}
labcomm2014_decoder_register_test_doavoid(decoder, handle_test_doavoid, context);
labcomm2014_decoder_register_test_intAndRef(decoder, handle_test_intAndRef, context);
labcomm2014_decoder_sample_ref_register(decoder,labcomm2014_signature_test_doavoid );
labcomm2014_decoder_register_test_twoInts(decoder, handle_test_twoInts, context);
labcomm2014_decoder_register_test_theFirstInt(decoder, handle_test_theFirstInt, context);
labcomm2014_decoder_register_test_theSecondInt(decoder, handle_test_theSecondInt, context);
labcomm2014_decoder_register_test_twoLines(decoder, handle_test_twoLines, context);
labcomm2014_decoder_register_labcomm2014_type_def(decoder, handle_type_def, context);
labcomm2014_decoder_register_labcomm2014_type_binding(decoder, handle_type_binding, context);
printf("Decoding:\n");
labcomm2014_decoder_run(decoder);
printf("--- End Of File ---:\n");
labcomm2014_decoder_free(decoder);
return 0;
}
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <labcomm2014_fd_writer.h>
#include <labcomm2014_default_error_handler.h>
#include <labcomm2014_default_memory.h>
#include <labcomm2014_default_scheduler.h>
#include "gen/test.h"
#include <stdio.h>
int main(int argc, char *argv[]) {
int fd;
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 = 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_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");
labcomm2014_encode_test_twoLines(encoder, &tl);
return 0;
}
#!/usr/bin/python
import labcomm2014
import sys
import test
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(test.twoLines.signature)
tl = {
(('','l1'),):{
(('','start'),):{
(('','x'),):{(('','val'),):1},
(('','y'),):{(('','val'),):11}
},
(('','end'),):{
(('','x'),):{(('','val'),):2},
(('','y'),):{(('','val'),):22}
}
},
(('','l2'),):{
(('','start'),):{
(('','x'),):{(('','val'),):3},
(('','y'),):{(('','val'),):33}
},
(('','end'),):{
(('','x'),):{(('','val'),):4},
(('','y'),):{(('','val'),):44}
}
},
(('','f'),):{
(('','a'),):10,
(('','b'),):20,
(('','c'),):False
}
}
encoder.encode(tl, test.twoLines.signature)
#!/usr/bin/python
import labcomm2014
import sys,traceback
import test
def handle_twoInts(val):
print "got twoInts: %s" % val
def handle_twoLines(val):
print "got twoLines: %s" % val
if __name__ == "__main__":
version = sys.argv[2] if len(sys.argv) == 3 else "LabComm2014"
d = labcomm2014.Decoder(labcomm2014.StreamReader(open(sys.argv[1])), version)
d.register_handler(test.twoInts.signature, handle_twoInts)
d.register_handler(test.twoLines.signature, handle_twoLines)
while True:
try:
d.runOne()
except EOFError:
print "got EOF"
break
except Exception, e:
print "got Exception"
print e
traceback.print_exc()
break
typedef struct {
(foo:bar) int val;
} coord;
typedef int anInt;
typedef void avoid;
sample (function:"a trigger")(foo:bar) avoid doavoid;
sample (a:b) "A struct: an int and a ref." struct {
(e:f)(c:d) int x;
sample reference;
} intAndRef;
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 {
(name:l1)line l1;
(name:l2)line l2;
(b:"kalle anka")(c:hejdu)(a:"kalle anka")foo f;
} twoLines;
sample struct {
int a;
int b;
} twoInts;
sample anInt theFirstInt;
sample anInt theSecondInt;
sample struct {
struct {
int x;
int y;
} s1;
struct {
int a;
int b;
} s2;
int i;
double double_array[2,3,_][3][_];
} twoStructsAndInt;
data.java
example.c
example.cs
example.encoded
example.h
example.javaencoded
example.py
example_encoder
log_message.java
GENERATED=\
data.java \
example.c \
example.cs \
example.encoded \
example.h \
example.javaencoded \
example.py \
example_encoder \
log_message.java
all:
test:
./run
clean:
rm -f $(GENERATED) *.class
distclean: clean
/*
sample float data;
*/
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 data implements LabCommSample {
public interface Handler extends LabCommHandler {
public void handle_data(float 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 data.class;
}
public String getName() {
return "data";
}
public byte[] getSignature() {
return signature;
}
public void decodeAndHandle(LabCommDecoder d,
LabCommHandler h) throws Exception {
((Handler)h).handle_data(data.decode(d));
}
}
public static void encode(LabCommEncoder e, float value) throws IOException {
e.begin(data.class);
e.encodeFloat(value);
e.end(data.class);
}
public static float decode(LabCommDecoder d) throws IOException {
float result;
result = d.decodeFloat();
return result;
}
private static byte[] signature = new byte[] {
0, 0, 0, 37,
};
}
#include "labcomm.h"
#include "labcomm_private.h"
#include "example.h"
static unsigned char signature_bytes_log_message[] = {
// struct { 2 fields
0, 0, 0, 17,
0, 0, 0, 2,
// int 'sequence'
0, 0, 0, 8,
115, 101, 113, 117, 101, 110, 99, 101,
0, 0, 0, 35,
// array [_] 'line'
0, 0, 0, 4,
108, 105, 110, 101,
// array [_]
0, 0, 0, 16,
0, 0, 0, 1,
0, 0, 0, 0,
// struct { 2 fields
0, 0, 0, 17,
0, 0, 0, 2,
// boolean 'last'
0, 0, 0, 4,
108, 97, 115, 116,
0, 0, 0, 32,
// string 'data'
0, 0, 0, 4,
100, 97, 116, 97,
0, 0, 0, 39,
// }
// }
// }
};
labcomm_signature_t labcomm_signature_example_log_message = {
LABCOMM_SAMPLE, "log_message",
(int (*)(void *))labcomm_sizeof_example_log_message,
sizeof(signature_bytes_log_message),
signature_bytes_log_message
};
static unsigned char signature_bytes_data[] = {
0, 0, 0, 37,
};
labcomm_signature_t labcomm_signature_example_data = {
LABCOMM_SAMPLE, "data",
(int (*)(void *))labcomm_sizeof_example_data,
sizeof(signature_bytes_data),
signature_bytes_data
};
static void decode_log_message(
labcomm_decoder_t *d,
void (*handle)(
example_log_message *v,
void *context
),
void *context
)
{
example_log_message v;
v.sequence = labcomm_decode_int(d);
v.line.n_0 = labcomm_decode_int(d);
v.line.a = malloc(sizeof(v.line.a[0]) * v.line.n_0);
{
int i_0_0;
for (i_0_0 = 0 ; i_0_0 < v.line.n_0 ; i_0_0++) {
int i_0 = i_0_0;
v.line.a[i_0].last = labcomm_decode_boolean(d);
v.line.a[i_0].data = labcomm_decode_string(d);
}
}
handle(&v, context);
{
{
int i_0_0;
for (i_0_0 = 0 ; i_0_0 < v.line.n_0 ; i_0_0++) {
int i_0 = i_0_0;
free(v.line.a[i_0].data);
}
}
free(v.line.a);
}
}
void labcomm_decoder_register_example_log_message(
struct labcomm_decoder *d,
void (*handler)(
example_log_message *v,
void *context
),
void *context
)
{
labcomm_internal_decoder_register(
d,
&labcomm_signature_example_log_message,
(labcomm_decoder_typecast_t)decode_log_message,
(labcomm_handler_typecast_t)handler,
context
);
}
static void encode_log_message(
labcomm_encoder_t *e,
example_log_message *v
)
{
e->writer.write(&e->writer, labcomm_writer_start);
labcomm_encode_type_index(e, &labcomm_signature_example_log_message);
{
labcomm_encode_int(e, (*v).sequence);
labcomm_encode_int(e, (*v).line.n_0);
{
int i_0_0;
for (i_0_0 = 0 ; i_0_0 < (*v).line.n_0 ; i_0_0++) {
int i_0 = i_0_0;
labcomm_encode_boolean(e, (*v).line.a[i_0].last);
labcomm_encode_string(e, (*v).line.a[i_0].data);
}
}
}
e->writer.write(&e->writer, labcomm_writer_end);
}
void labcomm_encode_example_log_message(
labcomm_encoder_t *e,
example_log_message *v
)
{
labcomm_internal_encode(e, &labcomm_signature_example_log_message, v);
}
void labcomm_encoder_register_example_log_message(
struct labcomm_encoder *e
)
{
labcomm_internal_encoder_register(
e,
&labcomm_signature_example_log_message,
(labcomm_encode_typecast_t)encode_log_message
);
}
int labcomm_sizeof_example_log_message(example_log_message *v)
{
int result = 4;
{
int i_0_0;
for (i_0_0 = 0 ; i_0_0 < (*v).line.n_0 ; i_0_0++) {
int i_0 = i_0_0;
result += 4 + strlen((*v).line.a[i_0].data);
result += 1;
}
}
result += 4;
return result;
}
static void decode_data(
labcomm_decoder_t *d,
void (*handle)(
example_data *v,
void *context
),
void *context
)
{
example_data v;
v = labcomm_decode_float(d);
handle(&v, context);
}
void labcomm_decoder_register_example_data(
struct labcomm_decoder *d,
void (*handler)(
example_data *v,
void *context
),
void *context
)
{
labcomm_internal_decoder_register(
d,
&labcomm_signature_example_data,
(labcomm_decoder_typecast_t)decode_data,
(labcomm_handler_typecast_t)handler,
context
);
}
static void encode_data(
labcomm_encoder_t *e,
example_data *v
)
{
e->writer.write(&e->writer, labcomm_writer_start);
labcomm_encode_type_index(e, &labcomm_signature_example_data);
{
labcomm_encode_float(e, (*v));
}
e->writer.write(&e->writer, labcomm_writer_end);
}
void labcomm_encode_example_data(
labcomm_encoder_t *e,
example_data *v
)
{
labcomm_internal_encode(e, &labcomm_signature_example_data, v);
}
void labcomm_encoder_register_example_data(
struct labcomm_encoder *e
)
{
labcomm_internal_encoder_register(
e,
&labcomm_signature_example_data,
(labcomm_encode_typecast_t)encode_data
);
}
int labcomm_sizeof_example_data(example_data *v)
{
return 8;
}
File deleted
/* LabComm declarations:
sample struct {
int sequence;
struct {
boolean last;
string data;
} line[_];
} log_message;
sample float data;
*/
#ifndef __LABCOMM_example_H__
#define __LABCOMM_example_H__
#include "labcomm.h"
#ifndef PREDEFINED_example_log_message
typedef struct {
int sequence;
struct {
int n_0;
struct {
unsigned char last;
char* data;
} *a;
} line;
} example_log_message;
#endif
void labcomm_decoder_register_example_log_message(
struct labcomm_decoder *d,
void (*handler)(
example_log_message *v,
void *context
),
void *context
);
void labcomm_encoder_register_example_log_message(
struct labcomm_encoder *e);
void labcomm_encode_example_log_message(
struct labcomm_encoder *e,
example_log_message *v
);
extern int labcomm_sizeof_example_log_message(example_log_message *v);
#ifndef PREDEFINED_example_data
typedef float example_data;
#endif
void labcomm_decoder_register_example_data(
struct labcomm_decoder *d,
void (*handler)(
example_data *v,
void *context
),
void *context
);
void labcomm_encoder_register_example_data(
struct labcomm_encoder *e);
void labcomm_encode_example_data(
struct labcomm_encoder *e,
example_data *v
);
extern int labcomm_sizeof_example_data(example_data *v);
#define LABCOMM_FORALL_SAMPLES_example(func, sep) \
func(log_message, example_log_message) sep \
func(data, example_data)
#endif
#!/usr/bin/python
# Auto generated example
import labcomm
class log_message(object):
signature = labcomm.sample('log_message',
labcomm.struct([
('sequence', labcomm.INTEGER()),
('line', labcomm.array([0],
labcomm.struct([
('last', labcomm.BOOLEAN()),
('data', labcomm.STRING())])))]))
class data(object):
signature = labcomm.sample('data',
labcomm.FLOAT())
#!/usr/bin/python
import labcomm
import labcomm2014
import sys
class FileReader:
def __init__(self, name):
self.f = open(name)
def read(self, count):
s = self.f.read(count)
if len(s) == 0:
raise Exception("EOF")
return s
def mark(self, value, decl):
pass
if __name__ == "__main__":
d = labcomm.Decoder(FileReader(sys.argv[1]))
version = sys.argv[2] if len(sys.argv) == 3 else "LabComm2014"
d = labcomm2014.Decoder(labcomm2014.StreamReader(open(sys.argv[1])), version)
while True:
try:
data,decl = d.decode()
if data:
print data
except:
except Exception, e:
print e
break
......@@ -4,24 +4,24 @@ import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import se.lth.control.labcomm.LabCommDecoderChannel;
import se.lth.control.labcomm.LabCommEncoderChannel;
import se.lth.control.labcomm2014.DecoderChannel;
import se.lth.control.labcomm2014.EncoderChannel;
public class example_decoder_encoder
implements data.Handler, log_message.Handler
{
LabCommDecoderChannel decoder;
LabCommEncoderChannel encoder;
DecoderChannel decoder;
EncoderChannel encoder;
public example_decoder_encoder(InputStream in, OutputStream out)
throws Exception
{
decoder = new LabCommDecoderChannel(in);
decoder = new DecoderChannel(in);
log_message.register(decoder, this);
data.register(decoder, this);
encoder = new LabCommEncoderChannel(out);
encoder = new EncoderChannel(out);
log_message.register(encoder);
data.register(encoder);
......
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <labcomm_fd_reader_writer.h>
#include <labcomm2014.h>
#include <labcomm2014_default_memory.h>
#include <labcomm2014_fd_reader.h>
#include <labcomm2014_fd_writer.h>
#include "example.h"
int main(int argc, char *argv[]) {
int fd;
struct labcomm_encoder *encoder;
struct labcomm2014_encoder *encoder;
struct labcomm2014_writer *labcomm2014_fd_writer;
int i, j;
fd = open("example.encoded", O_WRONLY|O_CREAT|O_TRUNC, 0644);
encoder = labcomm_encoder_new(labcomm_fd_writer, &fd);
labcomm_encoder_register_example_log_message(encoder);
labcomm_encoder_register_example_data(encoder);
labcomm2014_fd_writer = labcomm2014_fd_writer_new(labcomm2014_default_memory, fd, 1);
encoder = labcomm2014_encoder_new(labcomm2014_fd_writer, NULL,
labcomm2014_default_memory, NULL);
labcomm2014_encoder_register_example_log_message(encoder);
labcomm2014_encoder_register_example_data(encoder);
for (i = 0 ; i < argc ; i++) {
example_log_message message;
......@@ -23,11 +29,12 @@ int main(int argc, char *argv[]) {
message.line.a[j].last = (j == message.line.n_0 - 1);
message.line.a[j].data = argv[j + 1];
}
labcomm_encode_example_log_message(encoder, &message);
labcomm2014_encode_example_log_message(encoder, &message);
free(message.line.a);
}
for (i = 0 ; i < argc ; i++) {
float f = i;
labcomm_encode_example_data(encoder, &f);
labcomm2014_encode_example_data(encoder, &f);
}
return 0;
}
#!/bin/sh
# Auto generate code from .lc file
java -jar ../../compiler/labComm.jar \
java -jar ../../compiler/labcomm2014_compiler.jar \
--c=example.c --h=example.h \
--java=. \
--cs=example.cs \
--python=example.py \
example.lc
example.lc || exit 1
# Compile executables
gcc -o example_encoder -I ../../lib/c/ \
example_encoder.c \
example.c \
../../lib/c/labcomm.c \
../../lib/c//labcomm_fd_reader_writer.c
javac -cp ../../lib/java:. *.java
(cd ../../lib/c; make all || exit 1)
gcc -Wall -Werror -o example_encoder -I../../lib/c/2014 \
example_encoder.c \
example.c \
../../lib/c/liblabcomm2014.a || exit 1
javac -cp ../../lib/java/labcomm2014.jar:. *.java || exit 1
# Run through all executables (c->java->Python)
./example_encoder one two
java -cp ../../lib/java:. example_decoder_encoder example.encoded example.javaencoded
PYTHONPATH=../../lib/python ./example_decoder.py example.javaencoded
./example_encoder one two || exit 1
java -cp ../../lib/java/labcomm2014.jar:. example_decoder_encoder example.encoded example.javaencoded || exit 1
PYTHONPATH=../../lib/python ./example_decoder.py example.javaencoded || exit 1