Commit 43d8a6f8 authored by Anders Blomdell's avatar Anders Blomdell
Browse files

Continued work on removing typedef's from runtime library for java

parent 4956b946
......@@ -631,9 +631,9 @@ aspect Java_Class {
}
env.println(") throws IOException {");
env.indent();
env.println("e.begin(" + getName() + ".class);");
env.println("e.begin(dispatcher);");
getDataType().Java_emitEncoder(env, "value");
env.println("e.end(" + getName() + ".class);");
env.println("e.end(dispatcher);");
env.unindent();
env.println("}");
env.println();
......@@ -998,8 +998,8 @@ aspect Java_Signature {
public abstract void SignatureLine.Java_emitSignature(Java_env env, boolean decl);
public void TypeRefSignatureLine.Java_emitSignature(Java_env env, boolean isDecl){
env.print(getIndentString());
env.println("e.encodePacked32(e.getTypeId("+decl.getName()+".class));");
// env.print(getIndentString());
// env.println("e.encodePacked32(e.getTypeId("+decl.getName()+".class));");
}
public void DataSignatureLine.Java_emitSignature(Java_env env, boolean decl){
......
......@@ -45,12 +45,6 @@ namespace se.lth.control.labcomm2014 {
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: {
DecoderRegistry.Entry e = def_registry.get(tag);
if (e == null) {
......
......@@ -137,6 +137,7 @@ namespace se.lth.control.labcomm2014 {
try {
index = ref_registry.getTag(value);
} catch (NullReferenceException) {
//we want to return 0 for unregistered ref types
}
WriteInt(index, 4);
}
......
......@@ -37,10 +37,4 @@ public class Constant {
public static final int STRING = 0x27;
public static final int SAMPLE = 0x28;
/*
* Other predefined symbols
*/
public static final int TYPE_BIND_SELF = 0x00;
}
......@@ -4,6 +4,8 @@ import java.io.IOException;
public interface Decoder {
public void runOne() throws Exception;
public void run() throws Exception;
public void register(SampleDispatcher dispatcher,
SampleHandler handler) throws IOException;
public void registerSampleRef(SampleDispatcher dispatcher) throws IOException;
......
......@@ -34,56 +34,22 @@ public class DecoderChannel implements Decoder {
ref_registry.add(index, name, signature);
}
private void processTypeDef(int len) throws IOException {
try {
processSample(Constant.TYPE_DEF);
} catch(Exception ex) {
int idx = decodePacked32();
String name = decodeString();
int siglen = decodePacked32();
for(int i=0; i<siglen; i++) {
byte b = decodeByte();
}
}
}
private void processTypeBinding(int len) throws IOException {
try {
processSample(Constant.TYPE_BINDING);
} catch(Exception ex) {
for(int i=0; i<len; i++) {
decodeByte();
}
}
}
private void processPragma(int len) throws IOException {
for(int i=0; i<len; i++) {
decodeByte();
}
}
private void processSample(int tag) throws IOException {
DecoderRegistry.Entry e = def_registry.get(tag);
if (e == null) {
throw new IOException("Unhandled tag " + tag);
}
SampleDispatcher d = e.getDispatcher();
if (d == null) {
throw new IOException("No dispatcher for '" + e.getName() + "'");
}
SampleHandler h = e.getHandler();
if (h == null) {
throw new IOException("No handler for '" + e.getName() +"'");
}
try {
//XXX why does decodeAndHandle throw Exception and not IOException?
d.decodeAndHandle(this, h);
} catch (IOException ex) {
throw ex;
} catch (Exception ex) {
ex.printStackTrace();
}
private void processSample(int tag) throws Exception {
DecoderRegistry.Entry e = def_registry.get(tag);
if (e == null) {
throw new IOException("Unhandled tag " + tag);
}
SampleDispatcher d = e.getDispatcher();
if (d == null) {
throw new IOException("No dispatcher for '" + e.getName() + "'");
}
SampleHandler h = e.getHandler();
if (h == null) {
throw new IOException("No handler for '" + e.getName() +"'");
}
// decodeAndHandle throws Exception and not IOException because
// the user provided handler might throw anything
d.decodeAndHandle(this, h);
}
public void runOne() throws Exception {
......@@ -105,15 +71,6 @@ public class DecoderChannel implements Decoder {
case Constant.SAMPLE_REF: {
processSampleRef();
} break;
case Constant.TYPE_DEF: {
processTypeDef(length);
} break;
case Constant.TYPE_BINDING: {
processTypeBinding(length);
} break;
case Constant.PRAGMA: {
processPragma(length);
} break;
default: {
processSample(tag);
done = true;
......
......@@ -91,17 +91,17 @@ public class DecoderRegistry {
}
}
private HashMap<Class, Entry> byClass;
private HashMap<SampleDispatcher, Entry> byDispatcher;
private HashMap<Integer, Entry> byIndex;
public DecoderRegistry() {
byClass = new HashMap<Class, Entry>();
byDispatcher = new HashMap<SampleDispatcher, Entry>();
byIndex = new HashMap<Integer, Entry>();
}
public synchronized void add(SampleDispatcher dispatcher,
SampleHandler handler) throws IOException{
Entry e = byClass.get(dispatcher.getSampleClass());
Entry e = byDispatcher.get(dispatcher);
if (e != null) {
e.check(dispatcher.getName(), dispatcher.getSignature());
e.setHandler(handler);
......@@ -116,7 +116,7 @@ public class DecoderRegistry {
}
if (e == null) {
e = new Entry(dispatcher, handler);
byClass.put(dispatcher.getSampleClass(), e);
byDispatcher.put(dispatcher, e);
}
}
}
......@@ -128,7 +128,7 @@ public class DecoderRegistry {
if (e != null) {
e.check(name, signature);
} else {
for (Entry e2 : byClass.values()) {
for (Entry e2 : byDispatcher.values()) {
if (e2.match(name, signature)) {
e2.setIndex(index);
e = e2;
......
......@@ -6,11 +6,8 @@ public interface Encoder {
public void register(SampleDispatcher dispatcher) throws IOException;
public void registerSampleRef(SampleDispatcher dispatcher) throws IOException;
public void begin(Class<? extends SampleType> c) throws IOException;
public void end(Class<? extends SampleType> c) throws IOException;
public void begin(int t) throws IOException;
public int getTypeId(Class<? extends SampleType> c) throws IOException;
public void begin(SampleDispatcher dispatcher) throws IOException;
public void end(SampleDispatcher dispatcher) throws IOException;
public void encodeBoolean(boolean value) throws IOException;
public void encodeByte(byte value) throws IOException;
......
......@@ -10,109 +10,38 @@ public class EncoderChannel implements Encoder {
private Writer writer;
private ByteArrayOutputStream bytes = new ByteArrayOutputStream();
private DataOutputStream data = new DataOutputStream(bytes);
private EncoderRegistry sample_def_registry = new EncoderRegistry();
private EncoderRegistry sample_ref_registry = new EncoderRegistry();
private EncoderRegistry type_def_registry = new EncoderRegistry();
private EncoderRegistry def_registry = new EncoderRegistry();
private EncoderRegistry ref_registry = new EncoderRegistry();
private int current_tag;
private EncoderChannel(Writer writer, boolean emitVersion) throws IOException {
private EncoderChannel(Writer writer) throws IOException {
this.writer = writer;
if(emitVersion){
begin(Constant.VERSION);
encodeString(Constant.CURRENT_VERSION);
end(null);
}
}
public EncoderChannel(Writer writer) throws IOException {
this(writer, true);
begin(Constant.VERSION);
encodeString(Constant.CURRENT_VERSION);
end(null);
}
public EncoderChannel(OutputStream writer) throws IOException {
this(new WriterWrapper(writer), true);
}
private EncoderChannel(OutputStream writer, boolean emitVersion) throws IOException {
this(new WriterWrapper(writer), emitVersion);
}
private void bindType(int sampleId, int typeId) throws IOException {
begin(Constant.TYPE_BINDING);
encodePacked32(sampleId);
encodePacked32(typeId);
end(null);
}
private void registerSample(SampleDispatcher dispatcher) throws IOException {
int index = sample_def_registry.add(dispatcher);
begin(dispatcher.getTypeDeclTag());
encodePacked32(index);
encodeString(dispatcher.getName());
byte[] signature = dispatcher.getSignature();
encodePacked32(signature.length);
for (int i = 0 ; i < signature.length ; i++) {
encodeByte(signature[i]);
}
end(null);
}
private static class WrappedEncoder extends EncoderChannel{
private Encoder wrapped;
public WrappedEncoder(Encoder e, OutputStream s, boolean emitVersion) throws IOException {
super(s,emitVersion);
this.wrapped = e;
}
public int getTypeId(Class<? extends SampleType> c) throws IOException{
return wrapped.getTypeId(c);
}
}
private int registerTypeDef(SampleDispatcher dispatcher) throws IOException {
// check if already registered
try {
return type_def_registry.getTag(dispatcher);
} catch (IOException e) {
//otherwise, add to the registry
int index = type_def_registry.add(dispatcher);
//wrap encoder to get encoded length of signature
ByteArrayOutputStream baos = new ByteArrayOutputStream();
EncoderChannel wrapped = new WrappedEncoder(this, baos, false);
dispatcher.encodeTypeDef(wrapped, index);
wrapped.flush();
byte b[] = baos.toByteArray();
begin(Constant.TYPE_DEF);
encodePacked32(index);
encodeString(dispatcher.getName());
encodePacked32(b.length);
for(int i = 0; i<b.length; i++) {
encodeByte(b[i]);
}
end(null);
return index;
}
this(new WriterWrapper(writer));
}
public void register(SampleDispatcher dispatcher) throws IOException {
switch (dispatcher.getTypeDeclTag()) {
case Constant.SAMPLE_DEF: {
registerSample(dispatcher);
break;
}
case Constant.TYPE_DEF: {
registerTypeDef(dispatcher);
break;
}
default:
throw new Error("Unknown typeDeclTag: "+dispatcher.getTypeDeclTag());
int index = def_registry.add(dispatcher);
begin(Constant.SAMPLE_DEF);
encodePacked32(index);
encodeString(dispatcher.getName());
byte[] signature = dispatcher.getSignature();
encodePacked32(signature.length);
for (int i = 0 ; i < signature.length ; i++) {
encodeByte(signature[i]);
}
end(null);
}
public void registerSampleRef(SampleDispatcher dispatcher) throws IOException {
System.err.println(dispatcher);
int index = sample_ref_registry.add(dispatcher);
int index = ref_registry.add(dispatcher);
begin(Constant.SAMPLE_REF);
encodePacked32(index);
encodeString(dispatcher.getName());
......@@ -124,22 +53,16 @@ public class EncoderChannel implements Encoder {
end(null);
}
public void begin(int tag) {
private void begin(int tag) {
current_tag = tag;
bytes.reset();
}
public void begin(Class<? extends SampleType> c) throws IOException {
begin(sample_def_registry.getTag(c));
public void begin(SampleDispatcher dispatcher) throws IOException {
begin(def_registry.getTag(dispatcher));
}
/* aux. method used to allow nesting encoders to find encoded size */
private void flush() throws IOException{
data.flush();
writer.write(bytes.toByteArray());
bytes.reset();
}
public void end(Class<? extends SampleType> c) throws IOException {
public void end(SampleDispatcher dispatcher) throws IOException {
data.flush();
WritePacked32(writer, current_tag);
WritePacked32(writer, bytes.size());
......@@ -147,13 +70,6 @@ public class EncoderChannel implements Encoder {
bytes.reset();
}
/**
* @return the id of a TYPE_DEF
*/
public int getTypeId(Class<? extends SampleType> c) throws IOException {
return type_def_registry.getTag(c);
}
private void WritePacked32(Writer s, long value) throws IOException {
byte[] tmp1 = new byte[5];
byte[] tmp2 = new byte[1];
......@@ -229,7 +145,7 @@ public class EncoderChannel implements Encoder {
public void encodeSampleRef(Class value) throws IOException {
int index = 0;
try {
index = sample_ref_registry.getTag(value);
index = ref_registry.getTag(value);
} catch (NullPointerException e) {
//we want to return 0 for unregistered ref types
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment