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

started sketching TypeDefParser

parent ba318e3c
No related branches found
No related tags found
No related merge requests found
...@@ -4,19 +4,21 @@ import java.io.InputStream; ...@@ -4,19 +4,21 @@ import java.io.InputStream;
import se.lth.control.labcomm.DecoderChannel; import se.lth.control.labcomm.DecoderChannel;
import se.lth.control.labcomm.TypeDef; import se.lth.control.labcomm.TypeDef;
import se.lth.control.labcomm.TypeBinding; import se.lth.control.labcomm.TypeDefParser;
//import se.lth.control.labcomm.TypeBinding;
public class Decoder public class Decoder
implements twoLines.Handler, implements twoLines.Handler,
// TypeDef.Handler,
// TypeBinding.Handler,
TypeDefParser.TypeDefListener,
twoInts.Handler, twoInts.Handler,
theFirstInt.Handler, theFirstInt.Handler,
theSecondInt.Handler, theSecondInt.Handler
TypeDef.Handler,
TypeBinding.Handler
{ {
DecoderChannel decoder; private DecoderChannel decoder;
private TypeDefParser tdp;
public Decoder(InputStream in) public Decoder(InputStream in)
throws Exception throws Exception
...@@ -26,8 +28,12 @@ public class Decoder ...@@ -26,8 +28,12 @@ public class Decoder
twoLines.register(decoder, this); twoLines.register(decoder, this);
theFirstInt.register(decoder, this); theFirstInt.register(decoder, this);
theSecondInt.register(decoder, this); theSecondInt.register(decoder, this);
TypeDef.register(decoder, this); this.tdp = TypeDefParser.registerTypeDefParser(decoder);
TypeBinding.register(decoder, this); // TypeDef.register(decoder, this);
// TypeBinding.register(decoder, this);
tdp.addListener(this);
try { try {
System.out.println("Running decoder."); System.out.println("Running decoder.");
...@@ -45,12 +51,16 @@ public class Decoder ...@@ -45,12 +51,16 @@ public class Decoder
return "Line from "+genPoint(l.start)+" to "+genPoint(l.end); return "Line from "+genPoint(l.start)+" to "+genPoint(l.end);
} }
public void handle_TypeDef(TypeDef d) throws java.io.IOException { // public void handle_TypeDef(TypeDef d) throws java.io.IOException {
System.out.println("Got TypeDef: "+d.getName()+"("+d.getIndex()+")"); // System.out.println("Got TypeDef: "+d.getName()+"("+d.getIndex()+")");
} // }
//
// public void handle_TypeBinding(TypeBinding d) throws java.io.IOException {
// System.out.println("Got TypeBinding: "+d.getSampleIndex()+" --> "+d.getTypeIndex()+"");
// }
public void handle_TypeBinding(TypeBinding d) throws java.io.IOException { public void onTypeDef(TypeDef d) {
System.out.println("Got TypeBinding: "+d.getSampleIndex()+" --> "+d.getTypeIndex()+""); System.out.println("onTypeDef: "+d.getName()+"("+d.getIndex()+")");
} }
public void handle_twoInts(twoInts d) throws java.io.IOException { public void handle_twoInts(twoInts d) throws java.io.IOException {
......
...@@ -12,6 +12,7 @@ MODULES=Constant \ ...@@ -12,6 +12,7 @@ MODULES=Constant \
SampleType \ SampleType \
TypeDef \ TypeDef \
TypeBinding \ TypeBinding \
TypeDefParser \
Writer \ Writer \
WriterWrapper WriterWrapper
......
...@@ -96,6 +96,24 @@ public class TypeBinding implements SampleType { ...@@ -96,6 +96,24 @@ public class TypeBinding implements SampleType {
throw new Error("Should not be called"); throw new Error("Should not be called");
} }
/* HERE BE DRAGONS!
* This exposes (and relies on the stability of) indices that are
* internal to a decoder.
*
* The SAMPLE_DEF and TYPE_DEF must already have been received for the
* indices to be known, so this can be changed to instead return
* references to the SampleDispactcher corresponding to the sample type
* and the matching TypeDef.
*
* Sketch:
*
* SampleDispatcher sd = d.getDispatcherForId(sampleIndex);
* TypeDef td = d.getTypeDefForId(typeIndex);
*
* return new TypeBinding(sd, td);
*
* assuming that the Decoder keeps a registry for TypeDefs
*/
public static TypeBinding decode(Decoder d) throws IOException { public static TypeBinding decode(Decoder d) throws IOException {
TypeBinding result; TypeBinding result;
int sampleIndex = d.decodePacked32(); int sampleIndex = d.decodePacked32();
......
...@@ -21,6 +21,10 @@ public class TypeDef implements SampleType { ...@@ -21,6 +21,10 @@ public class TypeDef implements SampleType {
return name; return name;
} }
public byte[] getSignature() {
return signature;
}
public void dump() { public void dump() {
System.out.print("=== TypeDef "+getName()+"( "+Integer.toHexString(getIndex())+") : "); System.out.print("=== TypeDef "+getName()+"( "+Integer.toHexString(getIndex())+") : ");
for (byte b : signature) { for (byte b : signature) {
...@@ -99,6 +103,9 @@ public class TypeDef implements SampleType { ...@@ -99,6 +103,9 @@ public class TypeDef implements SampleType {
throw new Error("Should not be called"); throw new Error("Should not be called");
} }
protected TypeDef() {
}
public TypeDef(int index, String name, byte sig[]) { public TypeDef(int index, String name, byte sig[]) {
this.index = index; this.index = index;
this.name = name; this.name = name;
......
package se.lth.control.labcomm;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import se.lth.control.labcomm.Decoder;
import se.lth.control.labcomm.TypeDef;
import se.lth.control.labcomm.TypeBinding;
public class TypeDefParser implements TypeDef.Handler, TypeBinding.Handler {
static class SelfBinding extends TypeDef {
private byte[] dummy = new byte[0];
public String getName() {return "self";}
public int getIndex() {return 0;}
public byte[] getSignature() {return dummy;}
}
public interface TypeDefListener {
void onTypeDef(TypeDef d);
}
private HashMap<Integer,TypeDef> typeDefs;
private HashMap<Integer,Integer> typeBindings;
private HashSet<TypeDefListener> listeners;
protected TypeDefParser() {
typeDefs = new HashMap<Integer,TypeDef>();
typeBindings = new HashMap<Integer,Integer>();
listeners = new HashSet<TypeDefListener>();
typeDefs.put(0, new SelfBinding());
}
public void addListener(TypeDefListener l) {
listeners.add(l);
}
public void handle_TypeDef(TypeDef d) throws java.io.IOException {
//System.out.println("Got TypeDef: "+d.getName()+"("+d.getIndex()+")");
typeDefs.put(d.getIndex(), d);
}
public void handle_TypeBinding(TypeBinding d) throws java.io.IOException {
//System.out.println("TDP got TypeBinding: "+d.getSampleIndex()+" --> "+d.getTypeIndex()+"");
typeBindings.put(d.getSampleIndex(), d.getTypeIndex());
Iterator<TypeDefListener> it = listeners.iterator();
while(it.hasNext()){
//it.next().onTypeDef(getTypeDefForIndex(d.getSampleIndex()));
it.next().onTypeDef(typeDefs.get(d.getTypeIndex()));
}
}
//* temporary testing method */
public TypeDef getTypeDefForIndex(int sampleIndex) {
return typeDefs.get(typeBindings.get(sampleIndex));
}
/** Factory method
* @return a new TypeDefParser registered on d
*/
public static TypeDefParser registerTypeDefParser(Decoder d) throws java.io.IOException {
TypeDefParser res = new TypeDefParser();
TypeDef.register(d,res);
TypeBinding.register(d,res);
return res;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment