Commit c8bcec78 authored by Anders Blomdell's avatar Anders Blomdell
Browse files

Renaming implemented for Java. Various cleanups.

parent 43d8a6f8
...@@ -510,36 +510,36 @@ aspect Java_Class { ...@@ -510,36 +510,36 @@ aspect Java_Class {
env.println("private static class Dispatcher implements SampleDispatcher "+genericStr+"{"); env.println("private static class Dispatcher implements SampleDispatcher "+genericStr+"{");
env.indent(); env.indent();
env.println(); env.println();
env.println("public Class"+genericStr+" getSampleClass() {"); // env.println("public Class"+genericStr+" getSampleClass() {");
env.indent(); // env.indent();
env.println("return " + getName() + ".class;"); // env.println("return " + getName() + ".class;");
env.unindent(); // env.unindent();
env.println("}"); // env.println("}");
env.println(); // env.println();
env.println("public String getName() {"); env.println("public String getName() {");
env.indent(); env.indent();
env.println("return \"" + getName() + "\";"); env.println("return \"" + getName() + "\";");
env.unindent(); env.unindent();
env.println("}"); env.println("}");
env.println(); env.println();
env.println("public byte getTypeDeclTag() {"); // env.println("public byte getTypeDeclTag() {");
env.indent(); // env.indent();
if(env.version == 2006) { // if(env.version == 2006) {
if(isSample) { // if(isSample) {
env.println("return Constant.SAMPLE;"); // env.println("return Constant.SAMPLE;");
} else { // } else {
env.println("return Constant.TYPEDEF;"); // env.println("return Constant.TYPEDEF;");
} // }
} else { // } else {
if(isSample) { // if(isSample) {
env.println("return Constant.SAMPLE_DEF;"); // env.println("return Constant.SAMPLE_DEF;");
} else { // } else {
env.println("return Constant.TYPE_DEF;"); // env.println("return Constant.TYPE_DEF;");
} // }
} // }
env.unindent(); // env.unindent();
env.println("}"); // env.println("}");
env.println(); // env.println();
// env.println("public boolean isSample() {"); // env.println("public boolean isSample() {");
// env.indent(); // env.indent();
// env.println("return "+isSample+";"); // env.println("return "+isSample+";");
...@@ -568,16 +568,16 @@ aspect Java_Class { ...@@ -568,16 +568,16 @@ aspect Java_Class {
// env.unindent(); // env.unindent();
// env.println("}"); // env.println("}");
// env.println(); // env.println();
env.println("public void encodeTypeDef(Encoder e, int index) throws IOException{"); // env.println("public void encodeTypeDef(Encoder e, int index) throws IOException{");
env.indent(); // env.indent();
if(!isSample || hasDependencies()) { // if(!isSample || hasDependencies()) {
env.println("emitSignature(e);"); // env.println("emitSignature(e);");
} else { // } else {
env.println("// the type has no dependencies, do nothing"); // env.println("// the type has no dependencies, do nothing");
} // }
env.unindent(); // env.unindent();
env.println("}"); // env.println("}");
env.println(); // env.println();
env.println("public boolean canDecodeAndHandle() {"); env.println("public boolean canDecodeAndHandle() {");
env.indent(); env.indent();
env.println("return "+isSample+";"); env.println("return "+isSample+";");
...@@ -834,7 +834,7 @@ aspect Java_Class { ...@@ -834,7 +834,7 @@ aspect Java_Class {
public void PrimType.Java_emitTypePrefix(Java_env env) { public void PrimType.Java_emitTypePrefix(Java_env env) {
switch (getToken()) { switch (getToken()) {
case LABCOMM_STRING: { env.print("String"); } break; case LABCOMM_STRING: { env.print("String"); } break;
case LABCOMM_SAMPLE: { env.print("Class"); } break; case LABCOMM_SAMPLE: { env.print("SampleDispatcher"); } break;
default: { env.print(getName()); } break; default: { env.print(getName()); } break;
} }
} }
...@@ -961,7 +961,7 @@ aspect Java_Class { ...@@ -961,7 +961,7 @@ aspect Java_Class {
public void PrimType.Java_emitType(Java_env env) { public void PrimType.Java_emitType(Java_env env) {
switch (getToken()) { switch (getToken()) {
case LABCOMM_STRING: { env.print("String"); } break; case LABCOMM_STRING: { env.print("String"); } break;
case LABCOMM_SAMPLE: { env.print("Class"); } break; case LABCOMM_SAMPLE: { env.print("SampleDispatcher"); } break;
default: { env.print(getName()); } break; default: { env.print(getName()); } break;
} }
} }
......
MODULES=Constant\ MODULES=Constant \
Decoder \ Decoder \
DecoderChannel \ DecoderChannel \
DecoderRegistry \ DecoderRegistry \
......
...@@ -50,9 +50,10 @@ namespace se.lth.control.labcomm2014 { ...@@ -50,9 +50,10 @@ namespace se.lth.control.labcomm2014 {
if (e == null) { if (e == null) {
throw new IOException("Unhandled tag " + tag); throw new IOException("Unhandled tag " + tag);
} }
SampleDispatcher d = e.getSampleDispatcher(); SampleDispatcher d = e.getDispatcher();
if (d == null) { if (d == null) {
throw new IOException("No dispatcher for '" + e.getName() + "'" + e.getSignature()); throw new IOException("No dispatcher for '" + e.getName() + "' " +
e.getSignature());
} }
SampleHandler h = e.getHandler(); SampleHandler h = e.getHandler();
if (h == null) { if (h == null) {
...@@ -164,8 +165,9 @@ namespace se.lth.control.labcomm2014 { ...@@ -164,8 +165,9 @@ namespace se.lth.control.labcomm2014 {
int index = (int)ReadInt(4); int index = (int)ReadInt(4);
try { try {
DecoderRegistry.Entry e = ref_registry.get(index); DecoderRegistry.Entry e = ref_registry.get(index);
return e.getSampleDispatcher(); return e.getDispatcher();
} catch (NullReferenceException) { } catch (NullReferenceException) {
// Handles both the case where index == 0 and unregistered sample
return null; return null;
} }
} }
......
...@@ -27,7 +27,7 @@ namespace se.lth.control.labcomm2014 { ...@@ -27,7 +27,7 @@ namespace se.lth.control.labcomm2014 {
this.signature = signature; this.signature = signature;
} }
public SampleDispatcher getSampleDispatcher() { public SampleDispatcher getDispatcher() {
return dispatcher; return dispatcher;
} }
......
...@@ -137,7 +137,7 @@ namespace se.lth.control.labcomm2014 { ...@@ -137,7 +137,7 @@ namespace se.lth.control.labcomm2014 {
try { try {
index = ref_registry.getTag(value); index = ref_registry.getTag(value);
} catch (NullReferenceException) { } catch (NullReferenceException) {
//we want to return 0 for unregistered ref types // encode 0 for unregistered ref types
} }
WriteInt(index, 4); WriteInt(index, 4);
} }
......
...@@ -15,7 +15,7 @@ namespace se.lth.control.labcomm2014 { ...@@ -15,7 +15,7 @@ namespace se.lth.control.labcomm2014 {
this.index = index; this.index = index;
} }
public SampleDispatcher getSampleDispatcher() { public SampleDispatcher getDispatcher() {
return dispatcher; return dispatcher;
} }
......
...@@ -4,7 +4,6 @@ namespace se.lth.control.labcomm2014 { ...@@ -4,7 +4,6 @@ namespace se.lth.control.labcomm2014 {
public class RenamingDecoder : WrappingDecoder { public class RenamingDecoder : WrappingDecoder {
private Decoder decoder;
private RenamingRegistry registry; private RenamingRegistry registry;
private Func<String,String> rename; private Func<String,String> rename;
...@@ -12,20 +11,22 @@ namespace se.lth.control.labcomm2014 { ...@@ -12,20 +11,22 @@ namespace se.lth.control.labcomm2014 {
RenamingRegistry registry, RenamingRegistry registry,
Func<String,String> rename) Func<String,String> rename)
: base(decoder) { : base(decoder) {
this.decoder = decoder;
this.registry = registry; this.registry = registry;
this.rename = rename; this.rename = rename;
} }
public override void register(SampleDispatcher dispatcher, public override void register(SampleDispatcher dispatcher,
SampleHandler handler) { SampleHandler handler) {
decoder.register(registry.add( base.register(registry.add(
dispatcher, rename(dispatcher.getName())), handler); dispatcher,
rename(dispatcher.getName())),
handler);
} }
public override void registerSampleRef(SampleDispatcher dispatcher) { public override void registerSampleRef(SampleDispatcher dispatcher) {
decoder.registerSampleRef(registry.add( base.registerSampleRef(registry.add(
dispatcher, rename(dispatcher.getName()))); dispatcher,
rename(dispatcher.getName())));
} }
......
...@@ -5,7 +5,6 @@ namespace se.lth.control.labcomm2014 { ...@@ -5,7 +5,6 @@ namespace se.lth.control.labcomm2014 {
public class RenamingEncoder : WrappingEncoder { public class RenamingEncoder : WrappingEncoder {
private Encoder encoder;
private RenamingRegistry registry; private RenamingRegistry registry;
private Func<String,String> rename; private Func<String,String> rename;
private Dictionary<SampleDispatcher, SampleDispatcher> alias; private Dictionary<SampleDispatcher, SampleDispatcher> alias;
...@@ -14,7 +13,6 @@ namespace se.lth.control.labcomm2014 { ...@@ -14,7 +13,6 @@ namespace se.lth.control.labcomm2014 {
RenamingRegistry registry, RenamingRegistry registry,
Func<String,String> rename) Func<String,String> rename)
: base(encoder) { : base(encoder) {
this.encoder = encoder;
this.registry = registry; this.registry = registry;
this.rename = rename; this.rename = rename;
alias = new Dictionary<SampleDispatcher, SampleDispatcher>(); alias = new Dictionary<SampleDispatcher, SampleDispatcher>();
...@@ -28,7 +26,6 @@ namespace se.lth.control.labcomm2014 { ...@@ -28,7 +26,6 @@ namespace se.lth.control.labcomm2014 {
alias.Add(identity, renamed); alias.Add(identity, renamed);
} }
} }
get(identity);
return renamed; return renamed;
} }
...@@ -40,11 +37,11 @@ namespace se.lth.control.labcomm2014 { ...@@ -40,11 +37,11 @@ namespace se.lth.control.labcomm2014 {
return renamed; return renamed;
} }
public override void register(SampleDispatcher identity) { public override void register(SampleDispatcher identity) {
encoder.register(add(identity)); base.register(add(identity));
} }
public override void registerSampleRef(SampleDispatcher identity) { public override void registerSampleRef(SampleDispatcher identity) {
encoder.registerSampleRef(add(identity)); base.registerSampleRef(add(identity));
} }
public override void begin(SampleDispatcher identity) { public override void begin(SampleDispatcher identity) {
......
...@@ -6,10 +6,15 @@ MODULES=Constant \ ...@@ -6,10 +6,15 @@ MODULES=Constant \
EncoderChannel \ EncoderChannel \
EncoderRegistry \ EncoderRegistry \
Reader \ Reader \
RenamingDecoder \
RenamingEncoder \
RenamingRegistry \
Sample \ Sample \
SampleDispatcher \ SampleDispatcher \
SampleHandler \ SampleHandler \
SampleType \ SampleType \
WrappingDecoder \
WrappingEncoder \
Writer \ Writer \
WriterWrapper WriterWrapper
......
...@@ -19,6 +19,6 @@ public interface Decoder { ...@@ -19,6 +19,6 @@ public interface Decoder {
public double decodeDouble() throws IOException; public double decodeDouble() throws IOException;
public String decodeString() throws IOException; public String decodeString() throws IOException;
public int decodePacked32() throws IOException; public int decodePacked32() throws IOException;
public Class decodeSampleRef() throws IOException; public SampleDispatcher decodeSampleRef() throws IOException;
} }
...@@ -16,42 +16,6 @@ public class DecoderChannel implements Decoder { ...@@ -16,42 +16,6 @@ public class DecoderChannel implements Decoder {
this.in = new DataInputStream(in); this.in = new DataInputStream(in);
} }
private void processSampleDef() throws IOException {
int index = decodePacked32();
String name = decodeString();
int signature_length = decodePacked32();
byte[] signature = new byte[signature_length];
ReadBytes(signature, signature_length);
def_registry.add(index, name, signature);
}
private void processSampleRef() throws IOException {
int index = decodePacked32();
String name = decodeString();
int signature_length = decodePacked32();
byte[] signature = new byte[signature_length];
ReadBytes(signature, signature_length);
ref_registry.add(index, name, signature);
}
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 { public void runOne() throws Exception {
boolean done = false; boolean done = false;
while (!done) { while (!done) {
...@@ -66,15 +30,38 @@ public class DecoderChannel implements Decoder { ...@@ -66,15 +30,38 @@ public class DecoderChannel implements Decoder {
} }
} break; } break;
case Constant.SAMPLE_DEF: { case Constant.SAMPLE_DEF: {
processSampleDef(); int index = decodePacked32();
String name = decodeString();
int signature_length = decodePacked32();
byte[] signature = new byte[signature_length];
ReadBytes(signature, signature_length);
def_registry.add(index, name, signature);
} break; } break;
case Constant.SAMPLE_REF: { case Constant.SAMPLE_REF: {
processSampleRef(); int index = decodePacked32();
String name = decodeString();
int signature_length = decodePacked32();
byte[] signature = new byte[signature_length];
ReadBytes(signature, signature_length);
ref_registry.add(index, name, signature);
} break; } break;
default: { default: {
processSample(tag); DecoderRegistry.Entry e = def_registry.get(tag);
done = true; 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);
done = true;
} }
} }
} }
...@@ -162,27 +149,17 @@ public class DecoderChannel implements Decoder { ...@@ -162,27 +149,17 @@ public class DecoderChannel implements Decoder {
return (int) (res & 0xffffffff); return (int) (res & 0xffffffff);
} }
public Class decodeSampleRef() throws IOException { public SampleDispatcher decodeSampleRef() throws IOException {
int index = in.readInt(); int index = in.readInt();
try { try {
DecoderRegistry.Entry e = ref_registry.get(index); DecoderRegistry.Entry e = ref_registry.get(index);
return e.getDispatcher().getSampleClass(); return e.getDispatcher();
} catch (NullPointerException e) { } catch (NullPointerException e) {
// Handles both the case where index == 0 and unregistered sample
return null; return null;
} }
} }
/* Package visible methods for use from TypeDefParser */
String getSampleName(int idx) {
DecoderRegistry.Entry e = def_registry.get(idx);
return e.getName();
}
byte[] getSampleSignature(int idx) {
DecoderRegistry.Entry e = def_registry.get(idx);
return e.getSignature();
}
} }
...@@ -17,7 +17,7 @@ public interface Encoder { ...@@ -17,7 +17,7 @@ public interface Encoder {
public void encodeFloat(float value) throws IOException; public void encodeFloat(float value) throws IOException;
public void encodeDouble(double value) throws IOException; public void encodeDouble(double value) throws IOException;
public void encodeString(String value) throws IOException; public void encodeString(String value) throws IOException;
public void encodePacked32(long value) throws IOException; public void encodePacked32(int value) throws IOException;
public void encodeSampleRef(Class value) throws IOException; public void encodeSampleRef(SampleDispatcher value) throws IOException;
} }
...@@ -40,7 +40,6 @@ public class EncoderChannel implements Encoder { ...@@ -40,7 +40,6 @@ public class EncoderChannel implements Encoder {
} }
public void registerSampleRef(SampleDispatcher dispatcher) throws IOException { public void registerSampleRef(SampleDispatcher dispatcher) throws IOException {
System.err.println(dispatcher);
int index = ref_registry.add(dispatcher); int index = ref_registry.add(dispatcher);
begin(Constant.SAMPLE_REF); begin(Constant.SAMPLE_REF);
encodePacked32(index); encodePacked32(index);
...@@ -129,7 +128,7 @@ public class EncoderChannel implements Encoder { ...@@ -129,7 +128,7 @@ public class EncoderChannel implements Encoder {
} }
} }
public void encodePacked32(long value) throws IOException { public void encodePacked32(int value) throws IOException {
byte[] tmp = new byte[5]; byte[] tmp = new byte[5];
long v = value & 0xffffffff; long v = value & 0xffffffff;
int i; int i;
...@@ -142,12 +141,12 @@ public class EncoderChannel implements Encoder { ...@@ -142,12 +141,12 @@ public class EncoderChannel implements Encoder {
} }
} }
public void encodeSampleRef(Class value) throws IOException { public void encodeSampleRef(SampleDispatcher value) throws IOException {
int index = 0; int index = 0;
try { try {
index = ref_registry.getTag(value); index = ref_registry.getTag(value);
} catch (NullPointerException e) { } catch (NullPointerException e) {
//we want to return 0 for unregistered ref types // encode 0 for unregistered ref types
} }
data.writeInt(index); data.writeInt(index);
} }
......
...@@ -26,38 +26,30 @@ public class EncoderRegistry { ...@@ -26,38 +26,30 @@ public class EncoderRegistry {
} }
private int userIndex = Constant.FIRST_USER_INDEX; private int userIndex = Constant.FIRST_USER_INDEX;
private HashMap<Class, Entry> byClass; private HashMap<SampleDispatcher, Entry> byDispatcher;
public EncoderRegistry() { public EncoderRegistry() {
byClass = new HashMap<Class, Entry>(); byDispatcher = new HashMap<SampleDispatcher, Entry>();
} }
public synchronized int add(SampleDispatcher dispatcher) { public synchronized int add(SampleDispatcher dispatcher) {
Entry e = byClass.get(dispatcher.getSampleClass()); Entry e = byDispatcher.get(dispatcher);
if (e == null) { if (e == null) {
e = new Entry(dispatcher, userIndex); e = new Entry(dispatcher, userIndex);
byClass.put(dispatcher.getSampleClass(), e); byDispatcher.put(dispatcher, e);
userIndex++; userIndex++;
} }
return e.getIndex(); return e.getIndex();
} }
public int getTag(SampleDispatcher d) throws IOException { public synchronized int getTag(SampleDispatcher sample) throws IOException {
return getTag(d.getSampleClass());