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 924 additions and 179 deletions
public interface LabCommType {
}
namespace se.lth.control.labcomm {
namespace se.lth.control.labcomm2014 {
public class LabComm {
public class Constant {
public const string CURRENT_VERSION = "LabComm2014";
/*
* Allowed packet tags
*/
public const int VERSION = 0x01;
public const int SAMPLE_DEF = 0x02;
public const int SAMPLE_REF = 0x03;
public const int TYPE_DEF = 0x04;
public const int TYPE_BINDING = 0x05;
public const int PRAGMA = 0x3f;
public const int FIRST_USER_INDEX = 0x40; /* ..0xffffffff */
/*
* Predeclared aggregate type indices
* Predefined aggregate type indices
*/
public const int TYPEDEF = 0x01;
public const int SAMPLE = 0x02;
public const int ARRAY = 0x10;
public const int STRUCT = 0x11;
......@@ -21,12 +32,8 @@ namespace se.lth.control.labcomm {
public const int FLOAT = 0x25;
public const int DOUBLE = 0x26;
public const int STRING = 0x27;
public const int SAMPLE = 0x28;
/*
* start of user defined types
*/
public const int FIRST_USER_INDEX = 0x60;
}
}
using System;
namespace se.lth.control.labcomm {
namespace se.lth.control.labcomm2014 {
public interface LabCommDecoder {
public interface Decoder {
void runOne();
void run();
void register(SampleDispatcher dispatcher,
SampleHandler handler);
void registerSampleRef(SampleDispatcher dispatcher);
void register(LabCommDispatcher dispatcher,
LabCommHandler handler);
bool decodeBoolean();
byte decodeByte();
short decodeShort();
......@@ -14,6 +18,8 @@ namespace se.lth.control.labcomm {
float decodeFloat();
double decodeDouble();
String decodeString();
int decodePacked32();
SampleDispatcher decodeSampleRef();
}
......
namespace se.lth.control.labcomm {
namespace se.lth.control.labcomm2014 {
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
public class LabCommDecoderChannel : LabCommDecoder {
public class DecoderChannel : Decoder {
private Stream stream;
private LabCommDecoderRegistry registry = new LabCommDecoderRegistry();
private DecoderRegistry def_registry = new DecoderRegistry();
private DecoderRegistry ref_registry = new DecoderRegistry();
byte[] buf = new byte[8];
public LabCommDecoderChannel(Stream stream) {
public DecoderChannel(Stream stream) {
this.stream = stream;
}
......@@ -19,25 +20,47 @@ namespace se.lth.control.labcomm {
bool done = false;
while (!done) {
int tag = decodePacked32();
int length = decodePacked32();
switch (tag) {
case LabComm.TYPEDEF:
case LabComm.SAMPLE: {
case Constant.VERSION: {
String version = decodeString();
if (version != Constant.CURRENT_VERSION) {
throw new IOException("LabComm version mismatch " +
version + " != " + Constant.CURRENT_VERSION);
}
} break;
case Constant.SAMPLE_DEF: {
int index = decodePacked32();
String name = decodeIntentions();
int signature_length = decodePacked32();
byte[] signature = new byte[signature_length];
ReadBytes(signature, signature_length);
def_registry.add(index, name, signature);
} break;
case Constant.SAMPLE_REF: {
int index = decodePacked32();
String name = decodeString();
MemoryStream signature = new MemoryStream();
collectFlatSignature(new LabCommEncoderChannel(signature));
registry.add(index, name, signature.ToArray());
String name = decodeIntentions();
int signature_length = decodePacked32();
byte[] signature = new byte[signature_length];
ReadBytes(signature, signature_length);
ref_registry.add(index, name, signature);
} break;
case Constant.TYPE_DEF:
case Constant.TYPE_BINDING: {
for(int i=0; i<length;i++){
decodeByte();
}
} break;
default: {
LabCommDecoderRegistry.Entry e = registry.get(tag);
DecoderRegistry.Entry e = def_registry.get(tag);
if (e == null) {
throw new IOException("Unhandled tag " + tag);
}
LabCommDispatcher d = e.getDispatcher();
SampleDispatcher d = e.getSampleDispatcher();
if (d == null) {
throw new IOException("No dispatcher for '" + e.getName() + "'" + e.getSignature());
}
LabCommHandler h = e.getHandler();
SampleHandler h = e.getHandler();
if (h == null) {
throw new IOException("No handler for '" + e.getName() +"'");
}
......@@ -54,45 +77,13 @@ namespace se.lth.control.labcomm {
}
}
private void collectFlatSignature(LabCommEncoder e) {
int type = decodePacked32();
e.encodePacked32(type);
switch (type) {
case LabComm.ARRAY: {
int dimensions = decodePacked32();
e.encodePacked32(dimensions);
for (int i = 0 ; i < dimensions ; i++) {
e.encodePacked32(decodePacked32());
}
collectFlatSignature(e);
} break;
case LabComm.STRUCT: {
int fields = decodePacked32();
e.encodeInt(fields);
for (int i = 0 ; i < fields ; i++) {
e.encodeString(decodeString());
collectFlatSignature(e);
}
} break;
case LabComm.BOOLEAN:
case LabComm.BYTE:
case LabComm.SHORT:
case LabComm.INT:
case LabComm.LONG:
case LabComm.FLOAT:
case LabComm.DOUBLE:
case LabComm.STRING: {
} break;
default: {
throw new IOException("Unimplemented type=" + type);
}
}
e.end(null);
public void register(SampleDispatcher dispatcher,
SampleHandler handler) {
def_registry.add(dispatcher, handler);
}
public void register(LabCommDispatcher dispatcher,
LabCommHandler handler) {
registry.add(dispatcher, handler);
public void registerSampleRef(SampleDispatcher dispatcher) {
ref_registry.add(dispatcher, null);
}
private void ReadBytes(byte[] result, int length) {
......@@ -101,17 +92,18 @@ namespace se.lth.control.labcomm {
int count = stream.Read(result, offset, length - offset);
if (count <= 0)
throw new EndOfStreamException(
String.Format("End of stream reached with {0} bytes left to read",
String.Format("End of stream reached with {0} bytes left to read",
length - offset));
offset += count;
}
}
private Int64 ReadInt(int length) {
int result = 0;
Int64 result = 0;
ReadBytes(buf, length);
for (int i = 0 ; i < length ; i++) {
result = (result << 8) + buf[i];
}
return result;
}
......@@ -123,7 +115,7 @@ namespace se.lth.control.labcomm {
public byte decodeByte() {
return (byte)ReadInt(1);
}
public short decodeShort() {
return (short)ReadInt(2);
}
......@@ -131,11 +123,11 @@ namespace se.lth.control.labcomm {
public int decodeInt() {
return (int)ReadInt(4);
}
public long decodeLong() {
return (long)ReadInt(8);
}
[StructLayout(LayoutKind.Explicit)]
private struct Int32SingleUnion {
......@@ -149,13 +141,12 @@ namespace se.lth.control.labcomm {
u.AsInt = (int)ReadInt(4);
return u.AsFloat;
}
public double decodeDouble() {
return BitConverter.Int64BitsToDouble(ReadInt(8));
}
public String decodeString() {
//int length = (int)ReadInt(4);
int length = decodePacked32();
byte[] buf = new byte[length];
ReadBytes(buf, length);
......@@ -164,17 +155,51 @@ namespace se.lth.control.labcomm {
public int decodePacked32() {
Int64 res = 0;
byte i = 0;
bool cont = true;
bool cont = true;
do {
byte c = decodeByte();
res |= (uint) ((c & 0x7f) << 7*i);
Int64 c = decodeByte();
res = (res << 7) | (c & 0x7f);
cont = (c & 0x80) != 0;
i++;
} while(cont);
return (int) (res & 0xffffffff);
}
private byte[] decodeBytes() {
int len = decodePacked32();
byte[] result = new byte[len];
for(int i=0; i<len; i++) {
result[i] = decodeByte();
}
return result;
}
private String decodeIntentions() {
int numIntentions = decodePacked32();
string name = "";
for(int i = 0; i<numIntentions; i++) {
byte[] key = decodeBytes();
byte[] val = decodeBytes();
if(key.Length == 0) {
name = Encoding.UTF8.GetString(val, 0, val.Length);
}
}
return name;
}
}
public SampleDispatcher decodeSampleRef() {
int index = (int)ReadInt(4);
try {
DecoderRegistry.Entry e = ref_registry.get(index);
return e.getSampleDispatcher();
} catch (NullReferenceException) {
return null;
}
}
}
}
namespace se.lth.control.labcomm {
namespace se.lth.control.labcomm2014 {
using System;
using System.Collections.Generic;
public class LabCommDecoderRegistry {
public class DecoderRegistry {
public class Entry {
private LabCommDispatcher dispatcher;
private LabCommHandler handler;
private SampleDispatcher dispatcher;
private SampleHandler handler;
private int index;
private String name;
private byte[] signature;
public Entry(LabCommDispatcher dispatcher,
LabCommHandler handler) {
public Entry(SampleDispatcher dispatcher,
SampleHandler handler) {
this.dispatcher = dispatcher;
this.name = dispatcher.getName();
this.signature = dispatcher.getSignature();
......@@ -27,19 +27,19 @@ namespace se.lth.control.labcomm {
this.signature = signature;
}
public LabCommDispatcher getDispatcher() {
public SampleDispatcher getSampleDispatcher() {
return dispatcher;
}
public void setDispatcher(LabCommDispatcher dispatcher) {
public void setSampleDispatcher(SampleDispatcher dispatcher) {
this.dispatcher = dispatcher;
}
public LabCommHandler getHandler() {
public SampleHandler getHandler() {
return handler;
}
public void setHandler(LabCommHandler handler) {
public void setHandler(SampleHandler handler) {
this.handler = handler;
}
......@@ -90,26 +90,26 @@ namespace se.lth.control.labcomm {
}
}
private Dictionary<Type, Entry> byClass;
private Dictionary<SampleDispatcher, Entry> byDispatcher;
private Dictionary<int, Entry> byIndex;
public LabCommDecoderRegistry() {
byClass = new Dictionary<Type, Entry>();
public DecoderRegistry() {
byDispatcher = new Dictionary<SampleDispatcher, Entry>();
byIndex = new Dictionary<int, Entry>();
}
public void add(LabCommDispatcher dispatcher,
LabCommHandler handler) {
public void add(SampleDispatcher dispatcher,
SampleHandler handler) {
lock(this) {
Entry e;
byClass.TryGetValue(dispatcher.getSampleClass(), out e);
byDispatcher.TryGetValue(dispatcher, out e);
if (e != null) {
e.check(dispatcher.getName(), dispatcher.getSignature());
e.setHandler(handler);
} else {
foreach (Entry e2 in byIndex.Values) {
if (e2.match(dispatcher.getName(), dispatcher.getSignature())) {
e2.setDispatcher(dispatcher);
e2.setSampleDispatcher(dispatcher);
e2.setHandler(handler);
e = e2;
break;
......@@ -117,7 +117,7 @@ namespace se.lth.control.labcomm {
}
if (e == null) {
e = new Entry(dispatcher, handler);
byClass.Add(dispatcher.getSampleClass(), e);
byDispatcher.Add(dispatcher, e);
}
}
}
......@@ -132,7 +132,7 @@ namespace se.lth.control.labcomm {
if (e != null) {
e.check(name, signature);
} else {
foreach (Entry e2 in byClass.Values) {
foreach (Entry e2 in byDispatcher.Values) {
if (e2.match(name, signature)) {
e2.setIndex(index);
e = e2;
......
namespace se.lth.control.labcomm {
namespace se.lth.control.labcomm2014 {
using System;
public interface LabCommEncoder {
public interface Encoder {
void register(LabCommDispatcher dispatcher);
void begin(Type c);
void end(Type c);
void register(SampleDispatcher dispatcher);
void registerSampleRef(SampleDispatcher dispatcher);
void begin(SampleDispatcher dispatcher);
void end(SampleDispatcher dispatcher);
void encodeBoolean(bool value);
void encodeByte(byte value);
void encodeShort(short value);
......@@ -16,6 +18,7 @@ namespace se.lth.control.labcomm {
void encodeDouble(double value);
void encodeString(String value);
void encodePacked32(Int64 value);
void encodeSampleRef(SampleDispatcher value);
}
......
namespace se.lth.control.labcomm {
namespace se.lth.control.labcomm2014 {
using System;
using System.IO;
using System.Text;
using System.Runtime.InteropServices;
public class LabCommEncoderChannel : LabCommEncoder {
public class EncoderChannel : Encoder {
private Stream writer;
private MemoryStream bytes = new MemoryStream();
private LabCommEncoderRegistry registry = new LabCommEncoderRegistry();
private EncoderRegistry def_registry = new EncoderRegistry();
private EncoderRegistry ref_registry = new EncoderRegistry();
byte[] buf = new byte[8];
private int current_tag;
public LabCommEncoderChannel(Stream writer) {
public EncoderChannel(Stream writer) {
this.writer = writer;
begin(Constant.VERSION);
encodeString(Constant.CURRENT_VERSION);
end(null);
}
public void register(SampleDispatcher dispatcher) {
int index = def_registry.add(dispatcher);
begin(Constant.SAMPLE_DEF);
encodePacked32(index);
encodeIntentions(dispatcher.getName());
byte[] signature = dispatcher.getSignature();
encodePacked32(signature.Length);
for (int i = 0 ; i < signature.Length ; i++) {
encodeByte(signature[i]);
}
end(null);
}
public void register(LabCommDispatcher dispatcher) {
int index = registry.add(dispatcher);
encodePacked32(LabComm.SAMPLE);
public void registerSampleRef(SampleDispatcher dispatcher) {
int index = ref_registry.add(dispatcher);
begin(Constant.SAMPLE_REF);
encodePacked32(index);
encodeString(dispatcher.getName());
encodeIntentions(dispatcher.getName());
byte[] signature = dispatcher.getSignature();
encodePacked32(signature.Length);
for (int i = 0 ; i < signature.Length ; i++) {
encodeByte(signature[i]);
}
end(null);
}
public void begin(Type c) {
encodePacked32(registry.getTag(c));
private void begin(int tag) {
current_tag = tag;
bytes.SetLength(0);
}
public void begin(SampleDispatcher identity) {
begin(def_registry.getTag(identity));
}
public void end(Type c) {
public void end(SampleDispatcher identity) {
WritePacked32(writer, current_tag);
WritePacked32(writer, bytes.Length);
bytes.WriteTo(writer);
bytes.SetLength(0);
writer.Flush();
}
private void WritePacked32(Stream s, Int64 value) {
Int64 v = value & 0xffffffff;
int i;
for (i = 0 ; i == 0 || v != 0 ; i++, v = (v >> 7)) {
buf[i] = (byte)(v & 0x7f | (i!=0?0x80:0x00));
}
for (i = i - 1 ; i >= 0 ; i--) {
s.WriteByte(buf[i]);
}
}
private void WriteInt(Int64 value, int length) {
......@@ -46,7 +86,7 @@ namespace se.lth.control.labcomm {
}
public void encodeBoolean(bool value) {
WriteInt(value ? 0 : 1, 1);
WriteInt(value ? 1 : 0, 1);
}
public void encodeByte(byte value) {
......@@ -89,13 +129,23 @@ namespace se.lth.control.labcomm {
}
public void encodePacked32(Int64 value) {
Int64 tmp = value;
WritePacked32(bytes, value);
}
while(tmp >= 0x80) {
encodeByte( (byte) ((tmp & 0x7f) | 0x80 ) );
tmp >>= 7;
private void encodeIntentions(String name) {
encodePacked32(1); // one intention field
encodePacked32(0); // empty key: name
encodeString(name);
}
public void encodeSampleRef(SampleDispatcher value) {
int index = 0;
try {
index = ref_registry.getTag(value);
} catch (NullReferenceException) {
}
encodeByte( (byte) (tmp & 0x7f) );
WriteInt(index, 4);
}
}
}
namespace se.lth.control.labcomm {
namespace se.lth.control.labcomm2014 {
using System;
using System.Collections.Generic;
public class LabCommEncoderRegistry {
public class EncoderRegistry {
public class Entry {
private LabCommDispatcher dispatcher;
private SampleDispatcher dispatcher;
private int index;
public Entry(LabCommDispatcher dispatcher, int index) {
public Entry(SampleDispatcher dispatcher, int index) {
this.dispatcher = dispatcher;
this.index = index;
}
public LabCommDispatcher getDispatcher() {
public SampleDispatcher getSampleDispatcher() {
return dispatcher;
}
......@@ -25,30 +25,30 @@ namespace se.lth.control.labcomm {
}
private int userIndex = LabComm.FIRST_USER_INDEX;
private Dictionary<Type, Entry> byClass;
private int userIndex = Constant.FIRST_USER_INDEX;
private Dictionary<SampleDispatcher, Entry> byDispatcher;
public LabCommEncoderRegistry() {
byClass = new Dictionary<Type, Entry>();
public EncoderRegistry() {
byDispatcher = new Dictionary<SampleDispatcher, Entry>();
}
public int add(LabCommDispatcher dispatcher) {
public int add(SampleDispatcher dispatcher) {
lock(this) {
Entry e;
byClass.TryGetValue(dispatcher.getSampleClass(), out e);
byDispatcher.TryGetValue(dispatcher, out e);
if (e == null) {
e = new Entry(dispatcher, userIndex);
byClass.Add(dispatcher.getSampleClass(), e);
byDispatcher.Add(dispatcher, e);
userIndex++;
}
return e.getIndex();
}
}
public int getTag(Type sample) {
public int getTag(SampleDispatcher sample) {
lock(this) {
Entry e;
byClass.TryGetValue(sample, out e);
byDispatcher.TryGetValue(sample, out e);
if (e == null) {
throw new Exception("'" +
sample.ToString() +
......@@ -60,4 +60,4 @@ namespace se.lth.control.labcomm {
}
}
\ No newline at end of file
}
namespace se.lth.control.labcomm2014 {
using System;
public class RenamingDecoder : WrappingDecoder {
private Decoder decoder;
private RenamingRegistry registry;
private Func<String,String> rename;
public RenamingDecoder(Decoder decoder,
RenamingRegistry registry,
Func<String,String> rename)
: base(decoder) {
this.decoder = decoder;
this.registry = registry;
this.rename = rename;
}
public override void register(SampleDispatcher dispatcher,
SampleHandler handler) {
decoder.register(registry.add(
dispatcher, rename(dispatcher.getName())), handler);
}
public override void registerSampleRef(SampleDispatcher dispatcher) {
decoder.registerSampleRef(registry.add(
dispatcher, rename(dispatcher.getName())));
}
}
}
namespace se.lth.control.labcomm2014 {
using System;
using System.Collections.Generic;
public class RenamingEncoder : WrappingEncoder {
private Encoder encoder;
private RenamingRegistry registry;
private Func<String,String> rename;
private Dictionary<SampleDispatcher, SampleDispatcher> alias;
public RenamingEncoder(Encoder encoder,
RenamingRegistry registry,
Func<String,String> rename)
: base(encoder) {
this.encoder = encoder;
this.registry = registry;
this.rename = rename;
alias = new Dictionary<SampleDispatcher, SampleDispatcher>();
}
private SampleDispatcher add(SampleDispatcher identity) {
SampleDispatcher renamed;
lock(this) {
if (! alias.TryGetValue(identity, out renamed)) {
renamed = registry.add(identity, rename(identity.getName()));
alias.Add(identity, renamed);
}
}
get(identity);
return renamed;
}
private SampleDispatcher get(SampleDispatcher identity) {
SampleDispatcher renamed;
lock(this) {
alias.TryGetValue(identity, out renamed);
}
return renamed;
}
public override void register(SampleDispatcher identity) {
encoder.register(add(identity));
}
public override void registerSampleRef(SampleDispatcher identity) {
encoder.registerSampleRef(add(identity));
}
public override void begin(SampleDispatcher identity) {
base.begin(get(identity));
}
public override void end(SampleDispatcher identity) {
base.end(get(identity));
}
}
}
namespace se.lth.control.labcomm2014 {
using System;
using System.Collections.Generic;
public class RenamingRegistry {
public class Dispatcher : SampleDispatcher, IEquatable<Dispatcher> {
private SampleDispatcher dispatcher;
private String name;
public Dispatcher(SampleDispatcher dispatcher,
String name) {
this.dispatcher = dispatcher;
this.name = name;
}
public String getName() {
return name;
}
public byte[] getSignature() {
return dispatcher.getSignature();
}
public void decodeAndHandle(Decoder decoder,
SampleHandler handler) {
dispatcher.decodeAndHandle(decoder, handler);
}
public bool Equals(Dispatcher obj) {
Dispatcher other = obj as Dispatcher;
return (other != null &&
dispatcher == other.dispatcher &&
name.Equals(other.name));
}
public override int GetHashCode() {
return dispatcher.GetHashCode() ^ name.GetHashCode();
}
public override string ToString() {
return "RenamingRegistry.Dispatcher(" + name + ")";
}
}
private Dictionary<Dispatcher, Dispatcher> registry;
public RenamingRegistry() {
registry = new Dictionary<Dispatcher, Dispatcher>();
}
public SampleDispatcher add(SampleDispatcher dispatcher,
String newName) {
Dispatcher result;
Dispatcher tmp = new Dispatcher(dispatcher, newName);
lock(this) {
registry.TryGetValue(tmp, out result);
if (result == null) {
registry.Add(tmp, tmp);
result = tmp;
}
}
return result;
}
}
}
namespace se.lth.control.labcomm2014 {
public interface Sample {
SampleDispatcher getDispatcher();
}
}
namespace se.lth.control.labcomm {
namespace se.lth.control.labcomm2014 {
using System;
public interface LabCommDispatcher {
Type getSampleClass();
public interface SampleDispatcher {
String getName();
byte[] getSignature();
void decodeAndHandle(LabCommDecoder decoder,
LabCommHandler handler);
void decodeAndHandle(Decoder decoder,
SampleHandler handler);
}
......
namespace se.lth.control.labcomm2014 {
public interface SampleHandler {
}
}
public interface SampleType {
}
using System;
namespace se.lth.control.labcomm2014 {
public class WrappingDecoder: Decoder {
private Decoder decoder;
public WrappingDecoder(Decoder decoder) {
this.decoder = decoder;
}
public virtual void runOne() {
decoder.runOne();
}
public virtual void run() {
decoder.run();
}
public virtual void register(SampleDispatcher dispatcher,
SampleHandler handler) {
decoder.register(dispatcher, handler);
}
public virtual void registerSampleRef(SampleDispatcher dispatcher) {
decoder.registerSampleRef(dispatcher);
}
public virtual bool decodeBoolean() {
return decoder.decodeBoolean();
}
public virtual byte decodeByte() {
return decoder.decodeByte();
}
public virtual short decodeShort() {
return decoder.decodeShort();
}
public virtual int decodeInt() {
return decoder.decodeInt();
}
public virtual long decodeLong() {
return decoder.decodeLong();
}
public virtual float decodeFloat() {
return decoder.decodeFloat();
}
public virtual double decodeDouble() {
return decoder.decodeDouble();
}
public virtual String decodeString() {
return decoder.decodeString();
}
public virtual int decodePacked32() {
return decoder.decodePacked32();
}
public virtual SampleDispatcher decodeSampleRef() {
return decoder.decodeSampleRef();
}
}
}
namespace se.lth.control.labcomm2014 {
using System;
public class WrappingEncoder : Encoder {
private Encoder encoder;
public WrappingEncoder(Encoder encoder) {
this.encoder = encoder;
}
public virtual void register(SampleDispatcher dispatcher) {
encoder.register(dispatcher);
}
public virtual void registerSampleRef(SampleDispatcher dispatcher) {
encoder.registerSampleRef(dispatcher);
}
public virtual void begin(SampleDispatcher dispatcher) {
encoder.begin(dispatcher);
}
public virtual void end(SampleDispatcher dispatcher) {
encoder.end(dispatcher);
}
public virtual void encodeBoolean(bool value) {
encoder.encodeBoolean(value);
}
public virtual void encodeByte(byte value) {
encoder.encodeByte(value);
}
public virtual void encodeShort(short value) {
encoder.encodeShort(value);
}
public virtual void encodeInt(int value) {
encoder.encodeInt(value);
}
public virtual void encodeLong(long value) {
encoder.encodeLong(value);
}
public virtual void encodeFloat(float value) {
encoder.encodeFloat(value);
}
public virtual void encodeDouble(double value) {
encoder.encodeDouble(value);
}
public virtual void encodeString(String value) {
encoder.encodeString(value);
}
public virtual void encodePacked32(Int64 value) {
encoder.encodePacked32(value);
}
public virtual void encodeSampleRef(SampleDispatcher value) {
encoder.encodeSampleRef(value);
}
}
}
gen
labcomm2014.jar
labcomm2006.jar
labcomm2014.jar
MODULES=Constant \
Decoder \
DecoderChannel \
DecoderRegistry \
Encoder \
EncoderChannel \
EncoderRegistry \
Reader \
Sample \
SampleDispatcher \
SampleHandler \
SampleType \
BuiltinType \
TypeDef \
TypeBinding \
ASTbuilder \
TypeDefParser \
SigTypeDef \
SigSampleDef \
ParsedTypeDef \
ParsedSampleDef \
Writer \
WriterWrapper \
DataType \
VoidType \
SignatureSymbolVisitor \
SignatureSymbol \
TypeSymbol \
SampleSymbol \
NameSymbol \
SigPrimitiveType \
SigStructType \
SigField \
SigArrayType \
SigUserType
.PHONY: all
all: labcomm2014.jar
labcomm2014.jar: gen/JAVAC
echo $@
cd gen ; jar cf ../$@ se/lth/control/labcomm2014/*.class
gen:
mkdir gen
gen/JAVAC: $(MODULES:%=se/lth/control/labcomm2014/%.java) \
Makefile | gen
javac -cp ../../compiler/labcomm2014_compiler.jar -d gen \
$(filter %.java, $^)
touch $@
.PHONY: test
test:
.PHONY: clean
clean:
rm -rf gen
.PHONY: distclean
distclean: clean
rm -rf labcomm2014.jar
package se.lth.control.labcomm;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.InputStream;
import java.io.IOException;
public class LabCommDecoderChannel implements LabCommDecoder {
private DataInputStream in;
private LabCommDecoderRegistry registry;
public LabCommDecoderChannel(InputStream in) throws IOException {
this.in = new DataInputStream(in);
registry = new LabCommDecoderRegistry();
}
public void runOne() throws Exception {
boolean done = false;
while (!done) {
int tag = decodePacked32();
switch (tag) {
case LabComm.TYPEDEF:
case LabComm.SAMPLE: {
int index = decodePacked32();
String name = decodeString();
ByteArrayOutputStream signature = new ByteArrayOutputStream();
collectFlatSignature(new LabCommEncoderChannel(signature));
registry.add(index, name, signature.toByteArray());
} break;
default: {
LabCommDecoderRegistry.Entry e = registry.get(tag);
if (e == null) {
throw new IOException("Unhandled tag " + tag);
}
LabCommDispatcher d = e.getDispatcher();
if (d == null) {
throw new IOException("No dispatcher for '" + e.getName() + "'");
}
LabCommHandler h = e.getHandler();
if (h == null) {
throw new IOException("No handler for '" + e.getName() +"'");
}
d.decodeAndHandle(this, h);
done = true;
}
}
}
}
public void run() throws Exception {
while (true) {
runOne();
}
}
private void collectFlatSignature(LabCommEncoder out) throws IOException {
int type = decodePacked32();
out.encodePacked32(type);
switch (type) {
case LabComm.ARRAY: {
int dimensions = decodePacked32();
out.encodePacked32(dimensions);
for (int i = 0 ; i < dimensions ; i++) {
out.encodePacked32(decodePacked32());
}
collectFlatSignature(out);
} break;
case LabComm.STRUCT: {
int fields = decodePacked32();
out.encodePacked32(fields);
for (int i = 0 ; i < fields ; i++) {
out.encodeString(decodeString());
collectFlatSignature(out);
}
} break;
case LabComm.BOOLEAN:
case LabComm.BYTE:
case LabComm.SHORT:
case LabComm.INT:
case LabComm.LONG:
case LabComm.FLOAT:
case LabComm.DOUBLE:
case LabComm.STRING: {
} break;
default: {
throw new IOException("Unimplemented type=" + type);
}
}
out.end(null);
}
public void register(LabCommDispatcher dispatcher,
LabCommHandler handler) throws IOException {
registry.add(dispatcher, handler);
}
public boolean decodeBoolean() throws IOException {
return in.readBoolean();
}
public byte decodeByte() throws IOException {
return in.readByte();
}
public short decodeShort() throws IOException {
return in.readShort();
}
public int decodeInt() throws IOException {
return in.readInt();
}
public long decodeLong() throws IOException {
return in.readLong();
}
public float decodeFloat() throws IOException {
return in.readFloat();
}
public double decodeDouble() throws IOException {
return in.readDouble();
}
public String decodeString() throws IOException {
//in.readShort(); // HACK
//return in.readUTF();
int len = decodePacked32() & 0xffffffff;
byte[] chars = new byte[len];
for(int i=0; i<len; i++) {
chars[i] = in.readByte();
}
return new String(chars);
}
public int decodePacked32() throws IOException {
long res=0;
byte i=0;
boolean cont=true;
do {
byte c = in.readByte();
res |= (c & 0x7f) << 7*i;
cont = (c & 0x80) != 0;
i++;
} while(cont);
return (int) (res & 0xffffffff);
}
}