Commit 17d3b32c authored by Sven Gestegård Robertz's avatar Sven Gestegård Robertz
Browse files

added handling for type bindings in Java

parent 1d01446e
......@@ -4,13 +4,15 @@ import java.io.InputStream;
import se.lth.control.labcomm.DecoderChannel;
import se.lth.control.labcomm.Typedef;
import se.lth.control.labcomm.TypeBinding;
public class Decoder
implements twoLines.Handler,
twoInts.Handler,
theFirstInt.Handler,
theSecondInt.Handler,
Typedef.Handler
Typedef.Handler,
TypeBinding.Handler
{
......@@ -25,6 +27,7 @@ public class Decoder
theFirstInt.register(decoder, this);
theSecondInt.register(decoder, this);
Typedef.register(decoder, this);
TypeBinding.register(decoder, this);
try {
System.out.println("Running decoder.");
......@@ -46,6 +49,10 @@ public class Decoder
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_twoInts(twoInts d) throws java.io.IOException {
System.out.print("Got twoInts: ");
System.out.println(d.a +", "+d.b);
......
......@@ -11,6 +11,7 @@ MODULES=Constant \
SampleHandler \
SampleType \
Typedef \
TypeBinding \
Writer \
WriterWrapper
......
......@@ -55,10 +55,15 @@ public class DecoderChannel implements Decoder {
}
private void processTypeBinding(int len) throws IOException {
try {
processSample(Constant.TYPE_BINDING);
} catch(Exception ex) {
System.out.println(ex.getMessage());
//System.err.println("Got TypeBinding: skipping "+len+" bytes");
for(int i=0; i<len; i++) {
decodeByte();
}
for(int i=0; i<len; i++) {
decodeByte();
}
}
}
private void processPragma(int len) throws IOException {
......
......@@ -103,6 +103,12 @@ public class DecoderRegistry {
byClass.put(dispatcher.getSampleClass(), e);
byIndex.put(Integer.valueOf(Constant.TYPE_DEF), e);
//System.out.println("LCDecoderRegistry.add("+e.getName()+", "+e.getIndex()+")");
} else if(dispatcher.getSampleClass() == TypeBinding.class){
Entry e = new Entry(dispatcher, handler);
e.setIndex(Constant.TYPE_BINDING);
byClass.put(dispatcher.getSampleClass(), e);
byIndex.put(Integer.valueOf(Constant.TYPE_BINDING), e);
//System.out.println("LCDecoderRegistry.add("+e.getName()+", "+e.getIndex()+")");
} else {
Entry e = byClass.get(dispatcher.getSampleClass());
if (e != null) {
......
package se.lth.control.labcomm;
import java.io.IOException;
import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
import se.lth.control.labcomm.Decoder;
import se.lth.control.labcomm.DecoderChannel;
import se.lth.control.labcomm.SampleDispatcher;
import se.lth.control.labcomm.SampleHandler;
public class TypeBinding implements SampleType {
private int sampleIndex;
private int typeIndex;
public int getSampleIndex() {
return sampleIndex;
}
public int getTypeIndex() {
return typeIndex;
}
public TypeBinding(int sampleIndex, int typeIndex) {
this.sampleIndex = sampleIndex;
this.typeIndex = typeIndex;
}
public interface Handler extends SampleHandler {
public void handle_TypeBinding(TypeBinding value) throws Exception;
}
public static void register(Decoder d, Handler h) throws IOException {
d.register(Dispatcher.singleton(), h);
}
public static void register(Encoder e) throws IOException {
register(e,false);
}
public static void register(Encoder e, boolean sendMetaData) throws IOException {
throw new IOException("cannot send TypeDefs");
}
static class Dispatcher implements SampleDispatcher<TypeBinding> {
private static Dispatcher singleton;
public synchronized static Dispatcher singleton() {
if(singleton==null) singleton=new Dispatcher();
return singleton;
}
public Class<TypeBinding> getSampleClass() {
return TypeBinding.class;
}
public String getName() {
return "TypeBinding";
}
public byte getTypeDeclTag() {
throw new Error("Should not be called");
}
public boolean isSample() {
throw new Error("Should not be called");
}
public boolean hasStaticSignature() {
throw new Error("Should not be called");
}
/** return the flat signature. Intended use is on decoder side */
public byte[] getSignature() {
return null; // not used for matching
}
public void encodeTypeDef(Encoder e, int index) throws IOException{
throw new Error("Should not be called");
}
// public boolean canDecodeAndHandle() {
// return true;
// }
public void decodeAndHandle(Decoder d,
SampleHandler h) throws Exception {
((Handler)h).handle_TypeBinding(TypeBinding.decode(d));
}
public boolean hasDependencies() {
return false;
}
}
public static void encode(Encoder e, TypeBinding value) throws IOException {
throw new Error("Should not be called");
}
public static TypeBinding decode(Decoder d) throws IOException {
TypeBinding result;
int sampleIndex = d.decodePacked32();
int typeIndex = d.decodePacked32();
result = new TypeBinding(sampleIndex, typeIndex);
return result;
}
}
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