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

Java decoder decodes pragma

parent dcbea31a
Branches
No related tags found
No related merge requests found
...@@ -2,34 +2,35 @@ import java.io.File; ...@@ -2,34 +2,35 @@ import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.OutputStream; import java.io.OutputStream;
import se.lth.control.labcomm.Encoder;
import se.lth.control.labcomm.EncoderChannel; import se.lth.control.labcomm.EncoderChannel;
/** /**
* Simple encoder * Simple encoder
*/ */
public class Encoder public class ExampleEncoder
{ {
EncoderChannel encoder; EncoderChannel encoder;
public Encoder(OutputStream out) public ExampleEncoder(OutputStream out)
throws Exception throws Exception
{ {
encoder = new EncoderChannel(out); encoder = new EncoderChannel(out);
twoLines.register(encoder); twoLines.register(encoder);
theint.register(encoder); theint.register(encoder);
//LabCommEncoder.MetaDataTransaction t = encoder.beginMetaData(twoLines.class); Encoder.PragmaPacketBuilder t = encoder.newPragma("se.lth.cs.sven.pragma");
////LabCommEncoder.MetaDataTransaction t = encoder.beginMetaData(); Comment.register(t);
//Comment.register(t, false); Comment c = new Comment();
//Comment c = new Comment();
//c.id = encoder.getTypeId(twoLines.class); //c.id = encoder.getTypeId(twoLines.class);
//c.comment = "Test comment"; c.id = 42;
//Comment.encode(t,c); c.comment = "Test comment";
//t.commit(); Comment.encode(t,c);
//t = encoder.beginMetaData(null); t.send();
//afoo.register(t, false); t = encoder.newPragma(null);
//afoo.encode(t, new foo()); afoo.register(t);
//t.commit(); afoo.encode(t, new foo());
t.send();
} }
public void doEncode() throws java.io.IOException { public void doEncode() throws java.io.IOException {
...@@ -94,7 +95,7 @@ public class Encoder ...@@ -94,7 +95,7 @@ public class Encoder
public static void main(String[] arg) throws Exception { public static void main(String[] arg) throws Exception {
FileOutputStream fos = new FileOutputStream(arg[0]); FileOutputStream fos = new FileOutputStream(arg[0]);
Encoder example = new Encoder(fos); ExampleEncoder example = new ExampleEncoder(fos);
example.doEncode(); example.doEncode();
fos.close(); fos.close();
} }
......
...@@ -2,7 +2,7 @@ LCDIR=../.. ...@@ -2,7 +2,7 @@ LCDIR=../..
LCCJAR=${LCDIR}/compiler/labcomm_compiler.jar # the LabComm compiler LCCJAR=${LCDIR}/compiler/labcomm_compiler.jar # the LabComm compiler
LCLJAR=${LCDIR}/lib/java/labcomm.jar # the LabComm library LCLJAR=${LCDIR}/lib/java/labcomm.jar # the LabComm library
EXECUTABLES=example_encoder example_decoder Encoder.class Decoder.class Encoder.exe Decoder.exe EXECUTABLES=example_encoder example_decoder ExampleEncoder.class Decoder.class Encoder.exe Decoder.exe
include ${LCDIR}/lib/c/os_compat.mk include ${LCDIR}/lib/c/os_compat.mk
GENDIR=gen GENDIR=gen
...@@ -44,7 +44,7 @@ build : ...@@ -44,7 +44,7 @@ build :
mkdir -p ${GENDIR} mkdir -p ${GENDIR}
java -jar ${LCDIR}/compiler/labcomm_compiler.jar --java=${GENDIR} --c=${GENDIR}/test.c --h=${GENDIR}/test.h --python=${GENDIR}/test.py --cs=${GENDIR}/test.cs test.lc java -jar ${LCDIR}/compiler/labcomm_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/labcomm.jar:. ${GENDIR}/*.java Encoder.java Decoder.java javac -cp ${LCDIR}/lib/java/labcomm.jar:. ${GENDIR}/*.java ExampleEncoder.java Decoder.java
${CC} ${CFLAGS} ${LDFLAGS} -Wall -Werror -Wno-unused-function \ ${CC} ${CFLAGS} ${LDFLAGS} -Wall -Werror -Wno-unused-function \
-I. -I${LCDIR}/lib/c -L${LCDIR}/lib/c \ -I. -I${LCDIR}/lib/c -L${LCDIR}/lib/c \
...@@ -65,7 +65,7 @@ run: ...@@ -65,7 +65,7 @@ run:
@echo "********************************************" @echo "********************************************"
@echo @echo
@java -cp .:${LCDIR}/lib/java/labcomm.jar:${GENDIR} Encoder encoded_data_j @java -cp .:${LCDIR}/lib/java/labcomm.jar:${GENDIR} ExampleEncoder encoded_data_j
@echo "************ running Java decoder: *****************" @echo "************ running Java decoder: *****************"
@java -cp .:${LCDIR}/lib/java/labcomm.jar:${GENDIR} Decoder encoded_data_j @java -cp .:${LCDIR}/lib/java/labcomm.jar:${GENDIR} Decoder encoded_data_j
...@@ -108,7 +108,7 @@ runwcs: Encoder.exe Decoder.exe ...@@ -108,7 +108,7 @@ runwcs: Encoder.exe Decoder.exe
@echo "********************************************" @echo "********************************************"
@echo @echo
@java -cp .:${LCDIR}/lib/java/labcomm.jar:${GENDIR} Encoder encoded_data_j @java -cp .:${LCDIR}/lib/java/labcomm.jar:${GENDIR} ExampleEncoder encoded_data_j
@echo "************ running Java decoder: *****************" @echo "************ running Java decoder: *****************"
@java -cp .:${LCDIR}/lib/java/labcomm.jar:${GENDIR} Decoder encoded_data_j @java -cp .:${LCDIR}/lib/java/labcomm.jar:${GENDIR} Decoder encoded_data_j
......
...@@ -37,7 +37,7 @@ public class DecoderChannel implements Decoder { ...@@ -37,7 +37,7 @@ public class DecoderChannel implements Decoder {
// decodeByte(); // decodeByte();
// } // }
String type = decodeString(); String type = decodeString();
int plen = len - (type.length()+1); // XXX HERE BE DRAGONS: +1? int plen = len - Encoder.Util.sizeof_string(type);
System.out.println("[ begin pragma ("+type+") ]"); System.out.println("[ begin pragma ("+type+") ]");
//System.out.println("metadata : "+len + " bytes, ref: " + Integer.toHexString(typeRefId)+"."); //System.out.println("metadata : "+len + " bytes, ref: " + Integer.toHexString(typeRefId)+".");
byte buf[] = new byte[plen]; byte buf[] = new byte[plen];
......
package se.lth.control.labcomm; package se.lth.control.labcomm;
import java.io.IOException; import java.io.IOException;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
public interface Encoder { public interface Encoder {
public void register(SampleDispatcher dispatcher) throws IOException; public void register(SampleDispatcher dispatcher) throws IOException;
//HERE BE DRAGONS: for internal use, but needs to be public
//in Java interfaces...
public void begin(int tag) throws IOException;
public void begin(Class<? extends Sample> c) throws IOException; public void begin(Class<? extends Sample> c) throws IOException;
public void end(Class<? extends Sample> c) throws IOException; public void end(Class<? extends Sample> c) throws IOException;
public void encodeBoolean(boolean value) throws IOException; public void encodeBoolean(boolean value) throws IOException;
...@@ -17,4 +22,93 @@ public interface Encoder { ...@@ -17,4 +22,93 @@ public interface Encoder {
public void encodeString(String value) throws IOException; public void encodeString(String value) throws IOException;
public void encodePacked32(long value) throws IOException; public void encodePacked32(long value) throws IOException;
public PragmaPacketBuilder newPragma(String type) throws IOException;
static class Util {
/* Aux method to get encoded size of packed32 variable */
//static int sizeofPacked32(int... vars)
static int sizeof_packed32(int data){
int result = 0;
//for(int data : vars) {
//XXX fixme... define number range for packed32
if(data < 0) throw new Error("TODO: emulate unsigned int...");
int i;
for (i = 0 ; i == 0 || data != 0 ; i++, data = (data >> 7)) {
result++;
}
// }
return result;
}
static int sizeof_string(String s){
int len = s.length();
return len+sizeof_packed32(len);
}
}
public static class PragmaPacketBuilder implements Encoder {
private final ByteArrayOutputStream bytes ;
private final DataOutputStream data ;
private final Encoder encoder, tempEncoder ;
private final EncoderRegistry reg;
private final String type;
private boolean sent = false;
//private final int typeRef;
//private Class<? extends LabCommSample> typeRef;
PragmaPacketBuilder(Encoder e, EncoderRegistry reg, String type) throws IOException {
bytes = new ByteArrayOutputStream();
data = new DataOutputStream(bytes);
encoder = e;
this.reg = reg;
this.type = (type==null)?"undefined":type;
System.out.println("PragmaPacketBuilder: "+this.type);
tempEncoder = new EncoderChannel(data,e,false);
}
public void send() throws IOException {
if(sent) throw new IOException("Already sent.");
tempEncoder.end(null);
data.close();
byte[] b = bytes.toByteArray();
//System.out.println("LabCommEncoder.MetaDataTransaction committing "+b.length+" bytes");
//encoder.encodePacked32(Constant.PRAGMA);
//encoder.encodePacked32(b.length);
encoder.begin(Constant.PRAGMA);
encoder.encodeString(this.type);
for (int i = 0 ; i < b.length ; i++) {
encoder.encodeByte(b[i]);
}
encoder.end(null);
}
// forward everything to tempEncoder
public void register(SampleDispatcher dispatcher) throws IOException {tempEncoder.register(dispatcher);}
//XXX hack for registration
public void begin(Class<? extends Sample> c) throws IOException{
System.err.println("begin...");
try {
tempEncoder.begin(c);
} catch (IOException e) {
System.err.println("trying lookup in enclosing encoder...");
tempEncoder.encodePacked32(reg.getTag(c));
}
}
public void begin(int c) throws IOException{
tempEncoder.begin(c);
}
public void end(Class<? extends Sample> c) throws IOException{tempEncoder.end(c);}
public void encodeBoolean(boolean value) throws IOException{tempEncoder.encodeBoolean(value);}
public void encodeByte(byte value) throws IOException{tempEncoder.encodeByte(value);}
public void encodeShort(short value) throws IOException{tempEncoder.encodeShort(value);}
public void encodeInt(int value) throws IOException{tempEncoder.encodeInt(value);}
public void encodeLong(long value) throws IOException{tempEncoder.encodeLong(value);}
public void encodeFloat(float value) throws IOException{tempEncoder.encodeFloat(value);}
public void encodeDouble(double value) throws IOException{tempEncoder.encodeDouble(value);}
public void encodeString(String value) throws IOException{tempEncoder.encodeString(value);}
public void encodePacked32(long value) throws IOException{tempEncoder.encodePacked32(value);}
public PragmaPacketBuilder newPragma(String type) throws IOException {throw new IOException("PragmaPacketBuilders do not nest");}
}
} }
...@@ -14,20 +14,44 @@ public class EncoderChannel implements Encoder { ...@@ -14,20 +14,44 @@ public class EncoderChannel implements Encoder {
private int current_tag; private int current_tag;
public EncoderChannel(Writer writer) throws IOException { public EncoderChannel(Writer writer) throws IOException {
this(writer, true);
}
public EncoderChannel(OutputStream writer) throws IOException {
this(writer, true);
}
EncoderChannel(Writer writer, boolean emitVersion) throws IOException {
this(writer, new EncoderRegistry(), emitVersion);
}
private EncoderChannel(Writer writer,
EncoderRegistry registry,
boolean emitVersion) throws IOException {
this.writer = writer; this.writer = writer;
bytes = new ByteArrayOutputStream(); this.bytes = new ByteArrayOutputStream();
data = new DataOutputStream(bytes); this.data = new DataOutputStream(bytes);
registry = new EncoderRegistry(); this.registry = registry;
if(emitVersion) {
begin(Constant.VERSION); begin(Constant.VERSION);
encodeString(Constant.CURRENT_VERSION); encodeString(Constant.CURRENT_VERSION);
end(null); end(null);
} }
}
public EncoderChannel(OutputStream writer) throws IOException { EncoderChannel(OutputStream writer, boolean emitVersion) throws IOException {
this(new WriterWrapper(writer)); this(new WriterWrapper(writer), emitVersion);
}
/* for sharing registry with PragmaPacketBuilder */
EncoderChannel(OutputStream writer,
Encoder e,
boolean emitVersion) throws IOException {
this(new WriterWrapper(writer), (e instanceof EncoderChannel) ? ((EncoderChannel)e).registry : new EncoderRegistry(), emitVersion);
} }
public void register(SampleDispatcher dispatcher) throws IOException { public void register(SampleDispatcher dispatcher) throws IOException {
int index = registry.add(dispatcher); int index = registry.add(dispatcher);
begin(Constant.SAMPLE_DEF); begin(Constant.SAMPLE_DEF);
...@@ -41,7 +65,7 @@ public class EncoderChannel implements Encoder { ...@@ -41,7 +65,7 @@ public class EncoderChannel implements Encoder {
end(null); end(null);
} }
private void begin(int tag) { public void begin(int tag) {
current_tag = tag; current_tag = tag;
bytes.reset(); bytes.reset();
} }
...@@ -131,5 +155,9 @@ public class EncoderChannel implements Encoder { ...@@ -131,5 +155,9 @@ public class EncoderChannel implements Encoder {
encodeByte((byte)(tmp[i] | (i!=0?0x80:0x00))); encodeByte((byte)(tmp[i] | (i!=0?0x80:0x00)));
} }
} }
public PragmaPacketBuilder newPragma(String type) throws IOException{
return new PragmaPacketBuilder(this, registry, type);
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment