Skip to content
Snippets Groups Projects
Commit 266c6d58 authored by Sven Gestegård Robertz's avatar Sven Gestegård Robertz
Browse files

handling typedefs implemented in Java

parent 14c35d7c
No related branches found
No related tags found
No related merge requests found
......@@ -3,12 +3,14 @@ import java.io.FileInputStream;
import java.io.InputStream;
import se.lth.control.labcomm.DecoderChannel;
import se.lth.control.labcomm.Typedef;
public class Decoder
implements twoLines.Handler,
twoInts.Handler,
theFirstInt.Handler,
theSecondInt.Handler
theSecondInt.Handler,
Typedef.Handler
{
......@@ -22,6 +24,7 @@ public class Decoder
twoLines.register(decoder, this);
theFirstInt.register(decoder, this);
theSecondInt.register(decoder, this);
Typedef.register(decoder, this);
try {
System.out.println("Running decoder.");
......@@ -39,6 +42,10 @@ public class Decoder
return "Line from "+genPoint(l.start)+" to "+genPoint(l.end);
}
public void handle_Typedef(Typedef d) throws java.io.IOException {
System.out.println("Got Typedef: "+d.getName()+"("+d.getIndex()+")");
}
public void handle_twoInts(twoInts d) throws java.io.IOException {
System.out.print("Got twoInts: ");
System.out.println(d.a +", "+d.b);
......
......@@ -35,10 +35,14 @@ public class DecoderChannel implements Decoder {
}
private void processTypeDef(int len) throws IOException {
try {
processSample(Constant.TYPE_DEF);
} catch(Exception ex) {
System.out.println(ex.getMessage());
//System.err.println("Got TypeDef: skipping "+len+" bytes");
int idx = decodePacked32();
String name = decodeString();
//System.err.println("Got TypeDef: "+idx+" "+name);
System.err.println("Ignoring (unhandled) TypeDef: "+idx+" "+name);
int siglen = decodePacked32();
//System.err.println("siglen="+siglen);
for(int i=0; i<siglen; i++) {
......@@ -48,6 +52,7 @@ public class DecoderChannel implements Decoder {
}
//System.out.println();
}
}
private void processTypeBinding(int len) throws IOException {
//System.err.println("Got TypeBinding: skipping "+len+" bytes");
......@@ -63,6 +68,29 @@ public class DecoderChannel implements Decoder {
}
}
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();
}
}
public void runOne() throws Exception {
boolean done = false;
while (!done) {
......@@ -92,20 +120,9 @@ public class DecoderChannel implements Decoder {
processPragma(length);
} break;
default: {
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() +"'");
}
d.decodeAndHandle(this, h);
processSample(tag);
done = true;
}
}
}
......
......@@ -96,6 +96,14 @@ public class DecoderRegistry {
public synchronized void add(SampleDispatcher dispatcher,
SampleHandler handler) throws IOException{
//XXX kludge: special handling of predefined types
if(dispatcher.getSampleClass() == Typedef.class){
Entry e = new Entry(dispatcher, handler);
e.setIndex(Constant.TYPE_DEF);
byClass.put(dispatcher.getSampleClass(), e);
byIndex.put(Integer.valueOf(Constant.TYPE_DEF), e);
//System.out.println("LCDecoderRegistry.add("+e.getName()+", "+e.getIndex()+")");
} else {
Entry e = byClass.get(dispatcher.getSampleClass());
if (e != null) {
e.check(dispatcher.getName(), dispatcher.getSignature());
......@@ -115,6 +123,7 @@ public class DecoderRegistry {
}
}
}
}
public synchronized void add(int index,
String name,
......
......@@ -21,74 +21,6 @@ public class Typedef implements SampleType {
return name;
}
static void collectFlatSignature(Decoder in,
DecoderRegistry registry,
Encoder out,
boolean recurse) throws IOException {
int type = in.decodePacked32();
//System.out.println("cFS..."+in+" --- type = "+String.format("0x%02X ", type));
switch (type) {
case Constant.ARRAY: {
out.encodePacked32(type);
int dimensions = in.decodePacked32();
out.encodePacked32(dimensions);
for (int i = 0 ; i < dimensions ; i++) {
out.encodePacked32(in.decodePacked32());
}
collectFlatSignature(in, registry, out, recurse);
} break;
case Constant.STRUCT: {
out.encodePacked32(type);
int fields = in.decodePacked32();
out.encodePacked32(fields);
for (int i = 0 ; i < fields ; i++) {
out.encodeString(in.decodeString());
collectFlatSignature(in, registry, out, recurse);
}
} break;
case Constant.BOOLEAN:
case Constant.BYTE:
case Constant.SHORT:
case Constant.INT:
case Constant.LONG:
case Constant.FLOAT:
case Constant.DOUBLE:
case Constant.STRING: {
out.encodePacked32(type);
} break;
default: {
if(recurse) {
DecoderRegistry.Entry entry = registry.get(type);
//System.out.println("...... ***** flattening signature for type "+type);
if(entry != null) {
//System.out.println("...... ***** entry "+entry.getName()+") != null");
byte[] sig = entry.getSignature();
//System.out.print("...... ***** sig :");
//for (byte b : sig) {
// System.out.print(String.format("0x%02X ", b));
//}
//System.out.println();
ByteArrayInputStream bis = new ByteArrayInputStream(sig);
//System.out.println("...... ***** bis.available() :"+bis.available());
try {
collectFlatSignature(new DecoderChannel(bis, registry), registry, out, recurse);
} catch(java.io.EOFException e) {
// When the "inner" signature is handled, we continue recursion on the outer level
collectFlatSignature(in, registry, out, recurse);
}
bis.close();
}else {
throw new IOException("Unknown type=" + String.format("0x%02X ", type));
}
}else {
out.encodePacked32(type);
}
}
}
}
public void dump() {
System.out.print("=== Typedef "+getName()+"( "+Integer.toHexString(getIndex())+") : ");
for (byte b : signature) {
......@@ -154,25 +86,35 @@ public class Typedef implements SampleType {
// }
public void decodeAndHandle(Decoder d,
Handler h) throws Exception {
SampleHandler h) throws Exception {
((Handler)h).handle_Typedef(Typedef.decode(d));
}
public boolean hasDependencies() {
return false;
}
}
public static void encode(Encoder e, Typedef value) throws IOException {
throw new Error("Should not be called");
}
public Typedef(int index, String name, byte sig[]) {
this.index = index;
this.name = name;
this.signature = sig;
}
public static Typedef decode(Decoder d) throws IOException {
Typedef result;
result = new Typedef();
result.index = d.decodePacked32();
result.name = d.decodeString();
ByteArrayOutputStream signature = new ByteArrayOutputStream();
((DecoderChannel )d).collectFlatSignature(new EncoderChannel(signature, false), false);
result.signature = signature.toByteArray();
int index = d.decodePacked32();
String name = d.decodeString();
int siglen= d.decodePacked32();
byte sig[] = new byte[siglen];
for(int i=0; i<siglen;i++){
sig[i] = d.decodeByte();
}
result = new Typedef(index, name, sig);
return result;
}
}
......
public class TypeDef {
public class Typedef {
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment