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
Select Git revision
Loading items

Target

Select target project
  • anders_blomdell/labcomm
  • klaren/labcomm
  • tommyo/labcomm
  • erikj/labcomm
  • sven/labcomm
5 results
Select Git revision
Loading items
Show changes
package se.lth.control.labcomm;
import java.io.IOException;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
public interface Encoder {
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 end(Class<? extends Sample> c) throws IOException;
public void encodeBoolean(boolean value) throws IOException;
......@@ -17,4 +22,93 @@ public interface Encoder {
public void encodeString(String 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 {
private int current_tag;
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;
bytes = new ByteArrayOutputStream();
data = new DataOutputStream(bytes);
registry = new EncoderRegistry();
this.bytes = new ByteArrayOutputStream();
this.data = new DataOutputStream(bytes);
this.registry = registry;
if(emitVersion) {
begin(Constant.VERSION);
encodeString(Constant.CURRENT_VERSION);
end(null);
}
}
public EncoderChannel(OutputStream writer) throws IOException {
this(new WriterWrapper(writer));
EncoderChannel(OutputStream writer, boolean emitVersion) throws IOException {
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 {
int index = registry.add(dispatcher);
begin(Constant.SAMPLE_DEF);
......@@ -41,7 +65,7 @@ public class EncoderChannel implements Encoder {
end(null);
}
private void begin(int tag) {
public void begin(int tag) {
current_tag = tag;
bytes.reset();
}
......@@ -131,5 +155,9 @@ public class EncoderChannel implements Encoder {
encodeByte((byte)(tmp[i] | (i!=0?0x80:0x00)));
}
}
public PragmaPacketBuilder newPragma(String type) throws IOException{
return new PragmaPacketBuilder(this, registry, type);
}
}
......@@ -81,7 +81,7 @@
# | ...
# +----+--
#
# LabComm2006 SAMPLE:
# LabComm2006 SAMPLE_DEF:
#
# +----+----+----+----+
# | id = 0x00000002 |
......