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 {
env.println("private static class Dispatcher implements SampleDispatcher "+genericStr+"{");
env.indent();
env.println();
env.println("public Class"+genericStr+" getSampleClass() {");
env.indent();
env.println("return " + getName() + ".class;");
env.unindent();
env.println("}");
env.println();
// env.println("public Class"+genericStr+" getSampleClass() {");
// env.indent();
// env.println("return " + getName() + ".class;");
// env.unindent();
// env.println("}");
// env.println();
env.println("public String getName() {");
env.indent();
env.println("return \"" + getName() + "\";");
env.unindent();
env.println("}");
env.println();
env.println("public byte getTypeDeclTag() {");
env.indent();
if(env.version == 2006) {
if(isSample) {
env.println("return Constant.SAMPLE;");
} else {
env.println("return Constant.TYPEDEF;");
}
} else {
if(isSample) {
env.println("return Constant.SAMPLE_DEF;");
} else {
env.println("return Constant.TYPE_DEF;");
}
}
env.unindent();
env.println("}");
env.println();
// env.println("public byte getTypeDeclTag() {");
// env.indent();
// if(env.version == 2006) {
// if(isSample) {
// env.println("return Constant.SAMPLE;");
// } else {
// env.println("return Constant.TYPEDEF;");
// }
// } else {
// if(isSample) {
// env.println("return Constant.SAMPLE_DEF;");
// } else {
// env.println("return Constant.TYPE_DEF;");
// }
// }
// env.unindent();
// env.println("}");
// env.println();
// env.println("public boolean isSample() {");
// env.indent();
// env.println("return "+isSample+";");
......@@ -568,16 +568,16 @@ aspect Java_Class {
// env.unindent();
// env.println("}");
// env.println();
env.println("public void encodeTypeDef(Encoder e, int index) throws IOException{");
env.indent();
if(!isSample || hasDependencies()) {
env.println("emitSignature(e);");
} else {
env.println("// the type has no dependencies, do nothing");
}
env.unindent();
env.println("}");
env.println();
// env.println("public void encodeTypeDef(Encoder e, int index) throws IOException{");
// env.indent();
// if(!isSample || hasDependencies()) {
// env.println("emitSignature(e);");
// } else {
// env.println("// the type has no dependencies, do nothing");
// }
// env.unindent();
// env.println("}");
// env.println();
env.println("public boolean canDecodeAndHandle() {");
env.indent();
env.println("return "+isSample+";");
......@@ -834,7 +834,7 @@ aspect Java_Class {
public void PrimType.Java_emitTypePrefix(Java_env env) {
switch (getToken()) {
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;
}
}
......@@ -961,7 +961,7 @@ aspect Java_Class {
public void PrimType.Java_emitType(Java_env env) {
switch (getToken()) {
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;
}
}
......
MODULES=Constant\
MODULES=Constant \
Decoder \
DecoderChannel \
DecoderRegistry \
......
......@@ -50,9 +50,10 @@ namespace se.lth.control.labcomm2014 {
if (e == null) {
throw new IOException("Unhandled tag " + tag);
}
SampleDispatcher d = e.getSampleDispatcher();
SampleDispatcher d = e.getDispatcher();
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();
if (h == null) {
......@@ -164,8 +165,9 @@ namespace se.lth.control.labcomm2014 {
int index = (int)ReadInt(4);
try {
DecoderRegistry.Entry e = ref_registry.get(index);
return e.getSampleDispatcher();
return e.getDispatcher();
} catch (NullReferenceException) {
// Handles both the case where index == 0 and unregistered sample
return null;
}
}
......
......@@ -27,7 +27,7 @@ namespace se.lth.control.labcomm2014 {
this.signature = signature;
}
public SampleDispatcher getSampleDispatcher() {
public SampleDispatcher getDispatcher() {
return dispatcher;
}
......
......@@ -137,7 +137,7 @@ namespace se.lth.control.labcomm2014 {
try {
index = ref_registry.getTag(value);
} catch (NullReferenceException) {
//we want to return 0 for unregistered ref types
// encode 0 for unregistered ref types
}
WriteInt(index, 4);
}
......
......@@ -15,7 +15,7 @@ namespace se.lth.control.labcomm2014 {
this.index = index;
}
public SampleDispatcher getSampleDispatcher() {
public SampleDispatcher getDispatcher() {
return dispatcher;
}
......
......@@ -4,7 +4,6 @@ namespace se.lth.control.labcomm2014 {
public class RenamingDecoder : WrappingDecoder {
private Decoder decoder;
private RenamingRegistry registry;
private Func<String,String> rename;
......@@ -12,20 +11,22 @@ namespace se.lth.control.labcomm2014 {
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);
base.register(registry.add(
dispatcher,
rename(dispatcher.getName())),
handler);
}
public override void registerSampleRef(SampleDispatcher dispatcher) {
decoder.registerSampleRef(registry.add(
dispatcher, rename(dispatcher.getName())));
base.registerSampleRef(registry.add(
dispatcher,
rename(dispatcher.getName())));
}
......
......@@ -5,7 +5,6 @@ namespace se.lth.control.labcomm2014 {
public class RenamingEncoder : WrappingEncoder {
private Encoder encoder;
private RenamingRegistry registry;
private Func<String,String> rename;
private Dictionary<SampleDispatcher, SampleDispatcher> alias;
......@@ -14,7 +13,6 @@ namespace se.lth.control.labcomm2014 {
RenamingRegistry registry,
Func<String,String> rename)
: base(encoder) {
this.encoder = encoder;
this.registry = registry;
this.rename = rename;
alias = new Dictionary<SampleDispatcher, SampleDispatcher>();
......@@ -28,7 +26,6 @@ namespace se.lth.control.labcomm2014 {
alias.Add(identity, renamed);
}
}
get(identity);
return renamed;
}
......@@ -40,11 +37,11 @@ namespace se.lth.control.labcomm2014 {
return renamed;
}
public override void register(SampleDispatcher identity) {
encoder.register(add(identity));
base.register(add(identity));
}
public override void registerSampleRef(SampleDispatcher identity) {
encoder.registerSampleRef(add(identity));
base.registerSampleRef(add(identity));
}
public override void begin(SampleDispatcher identity) {
......
......@@ -6,10 +6,15 @@ MODULES=Constant \
EncoderChannel \
EncoderRegistry \
Reader \
RenamingDecoder \
RenamingEncoder \
RenamingRegistry \
Sample \
SampleDispatcher \
SampleHandler \
SampleType \
WrappingDecoder \
WrappingEncoder \
Writer \
WriterWrapper
......
......@@ -19,6 +19,6 @@ public interface Decoder {
public double decodeDouble() throws IOException;
public String decodeString() 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 {
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 {
boolean done = false;
while (!done) {
......@@ -66,15 +30,38 @@ public class DecoderChannel implements Decoder {
}
} break;
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;
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;
default: {
processSample(tag);
done = true;
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);
done = true;
}
}
}
......@@ -162,27 +149,17 @@ public class DecoderChannel implements Decoder {
return (int) (res & 0xffffffff);
}
public Class decodeSampleRef() throws IOException {
public SampleDispatcher decodeSampleRef() throws IOException {
int index = in.readInt();
try {
DecoderRegistry.Entry e = ref_registry.get(index);
return e.getDispatcher().getSampleClass();
return e.getDispatcher();
} catch (NullPointerException e) {
// Handles both the case where index == 0 and unregistered sample
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 {
public void encodeFloat(float value) throws IOException;
public void encodeDouble(double value) throws IOException;
public void encodeString(String value) throws IOException;
public void encodePacked32(long value) throws IOException;
public void encodeSampleRef(Class value) throws IOException;
public void encodePacked32(int value) throws IOException;
public void encodeSampleRef(SampleDispatcher value) throws IOException;
}
......@@ -40,7 +40,6 @@ public class EncoderChannel implements Encoder {
}
public void registerSampleRef(SampleDispatcher dispatcher) throws IOException {
System.err.println(dispatcher);
int index = ref_registry.add(dispatcher);
begin(Constant.SAMPLE_REF);
encodePacked32(index);
......@@ -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];
long v = value & 0xffffffff;
int i;
......@@ -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;
try {
index = ref_registry.getTag(value);
} catch (NullPointerException e) {
//we want to return 0 for unregistered ref types
// encode 0 for unregistered ref types
}
data.writeInt(index);
}
......
......@@ -26,38 +26,30 @@ public class EncoderRegistry {
}
private int userIndex = Constant.FIRST_USER_INDEX;
private HashMap<Class, Entry> byClass;
private HashMap<SampleDispatcher, Entry> byDispatcher;
public EncoderRegistry() {
byClass = new HashMap<Class, Entry>();
byDispatcher = new HashMap<SampleDispatcher, Entry>();
}
public synchronized int add(SampleDispatcher dispatcher) {
Entry e = byClass.get(dispatcher.getSampleClass());
Entry e = byDispatcher.get(dispatcher);
if (e == null) {
e = new Entry(dispatcher, userIndex);
byClass.put(dispatcher.getSampleClass(), e);
byDispatcher.put(dispatcher, e);
userIndex++;
}
return e.getIndex();
}
public int getTag(SampleDispatcher d) throws IOException {
return getTag(d.getSampleClass());
}
public int getTag(Class<? extends SampleType> sample) throws IOException {
Entry e = byClass.get(sample);
public synchronized int getTag(SampleDispatcher sample) throws IOException {
Entry e = byDispatcher.get(sample);
if (e == null) {
throw new IOException("'" +
sample.getSimpleName() +
sample.getName() +
"' is not registered");
}
return e.index;
}
public boolean contains(Class<? extends SampleType> sample) {
return byClass.containsKey(sample);
return e.getIndex();
}
}
package se.lth.control.labcomm2014;
import java.io.IOException;
import java.util.HashMap;
public class RenamingDecoder extends WrappingDecoder {
public interface Rename {
String rename(String name);
}
private RenamingRegistry registry;
private Rename rename;
public RenamingDecoder(Decoder decoder,
RenamingRegistry registry,
Rename rename) throws IOException {
super(decoder);
this.registry = registry;
this.rename = rename;
}
public void register(SampleDispatcher dispatcher,
SampleHandler handler) throws IOException {
super.register(registry.add(
dispatcher,
rename.rename(dispatcher.getName())),
handler);
}
public void registerSampleRef(SampleDispatcher dispatcher) throws IOException {
super.registerSampleRef(registry.add(
dispatcher,
rename.rename(dispatcher.getName())));
}
}
package se.lth.control.labcomm2014;
import java.io.IOException;
import java.util.HashMap;
public class RenamingEncoder extends WrappingEncoder {
public interface Rename {
String rename(String name);
}
private RenamingRegistry registry;
private Rename rename;
private HashMap<SampleDispatcher, SampleDispatcher> alias;
public RenamingEncoder(Encoder encoder,
RenamingRegistry registry,
Rename rename) throws IOException {
super(encoder);
this.registry = registry;
this.rename = rename;
alias = new HashMap<SampleDispatcher, SampleDispatcher>();
}
private synchronized SampleDispatcher add(SampleDispatcher identity) {
SampleDispatcher renamed = alias.get(identity);
if (renamed == null) {
renamed = registry.add(identity, rename.rename(identity.getName()));
alias.put(identity, renamed);
}
return renamed;
}
private synchronized SampleDispatcher get(SampleDispatcher identity) {
return alias.get(identity);
}
public void register(SampleDispatcher identity) throws IOException {
super.register(add(identity));
}
public void registerSampleRef(SampleDispatcher identity) throws IOException {
super.registerSampleRef(add(identity));
}
public void begin(SampleDispatcher identity) throws IOException {
super.begin(get(identity));
}
public void end(SampleDispatcher identity) throws IOException {
super.end(get(identity));
}
}
package se.lth.control.labcomm2014;
import java.util.HashMap;
public class RenamingRegistry {
public static class Dispatcher implements SampleDispatcher {
private final SampleDispatcher dispatcher;
private final 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) throws Exception {
dispatcher.decodeAndHandle(decoder, handler);
}
public boolean equals(Object obj) {
if (! (obj instanceof Dispatcher)) {
return false;
} else {
Dispatcher other = (Dispatcher)obj;