Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • anders_blomdell/labcomm
  • klaren/labcomm
  • tommyo/labcomm
  • erikj/labcomm
  • sven/labcomm
5 results
Show changes
Showing
with 803 additions and 0 deletions
package se.lth.control.labcomm2014;
public class NameSymbol implements SignatureSymbol {
private String name;
public NameSymbol(String name) {
this.name = name;
}
public String toString() {
return name;
}
public void accept(SignatureSymbolVisitor v){
v.visit(this);
}
}
package se.lth.control.labcomm2014;
public class ParsedSampleDef extends ParsedTypeDef{
int idx;
ParsedSampleDef(ParsedTypeDef td) {
super(td);
}
@Override
public boolean isSampleDef() {
return true;
}
public String defType() {
return "sample";
}
}
package se.lth.control.labcomm2014;
public class ParsedTypeDef extends SigTypeDef{
private int idx;
ParsedTypeDef(int idx, String name){
super(name);
this.idx = idx;
}
ParsedTypeDef(int idx, String name, DataType type) {
super(name, type);
this.idx = idx;
}
ParsedTypeDef(ParsedTypeDef td) {
super(td);
this.idx = td.getIndex();
}
int getIndex() {
return idx;
}
}
package se.lth.control.labcomm;
package se.lth.control.labcomm2014;
public interface LabCommReader {
public interface Reader {
public void handle(byte[] data, int begin, int end);
......
package se.lth.control.labcomm2014;
public interface Sample extends SampleType {
public SampleDispatcher getDispatcher();
}
package se.lth.control.labcomm2014;
import java.util.Iterator;
import java.io.IOException;
public interface SampleDispatcher <T extends SampleType>{
public Class<T> getSampleClass();
public String getName();
public byte[] getSignature();
public int getNumIntentions();
public byte[] getIntentionBytes();
public void decodeAndHandle(Decoder decoder,
SampleHandler handler) throws Exception;
/** @return true if the type depends on one or more typedefs
*/
public boolean hasDependencies();
public void encodeTypeDef(Encoder e, int index) throws IOException;
/** return the tag SAMPLE_DEF or TYPE_DEF, for use
* by encoder.register.
* TODO: refactor types, moving this to a super-interface
* applicable to both type and sample defs.
*/
public byte getTypeDeclTag();
public void registerTypeDeps(Encoder e) throws IOException;
public Iterator<SampleDispatcher> getDependencyIterator();
public DataType getDataType();
}
package se.lth.control.labcomm2014;
public interface SampleHandler {
}
package se.lth.control.labcomm2014;
public class SampleSymbol implements SignatureSymbol {
public String toString() {
return "sample ";
}
public void accept(SignatureSymbolVisitor v){
v.visit(this);
}
}
package se.lth.control.labcomm2014;
public interface SampleType {
}
package se.lth.control.labcomm2014;
import java.io.PrintStream;
public class SigArrayType extends DataType {
private int idx[];
private DataType type;
public SigArrayType(DataType elementType, int idx[]) {
super("array", Constant.ARRAY);
this.idx = idx;
this.type = elementType;
}
public DataType getType() {
return type;
}
public int[] getIdx() {
return idx;
}
public boolean isArray() {
return true;
}
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(type.toString());
for(int i : idx) {
sb.append("["+(i>0 ? i : "_")+"]");
}
return sb.toString();
}
public void accept(SignatureSymbolVisitor v) {
v.visit(this);
}
public void print(PrintStream out, String indent) {
type.print(out,indent);
out.print("[");
for(int i =0; i<idx.length;i++) {
int d = idx[i];
out.print( (d==-1?"-":d));
if(i<idx.length-1) {
out.print(", ");
}
}
out.print("]");
}
}
package se.lth.control.labcomm2014;
import java.io.PrintStream;
public class SigField implements SignatureSymbol{
private DataType type;
private String name;
SigField(String name, DataType type) {
this.name = name;
this.type = type;
}
public DataType getType() {
return type;
}
public String getName() {
return name;
}
public String toString() {
return type.toString() + " " + name;
}
public void accept(SignatureSymbolVisitor v) {
v.visit(this);
}
public void print(PrintStream out, String indent) {
type.print(out, indent);
out.println(" "+name+";");
}
}
package se.lth.control.labcomm2014;
public class SigPrimitiveType extends DataType {
public SigPrimitiveType(int tag) {
super(tagName(tag), tag);
}
public void accept(SignatureSymbolVisitor v) {
v.visit(this);
}
public String toString() {
return getName();}
}
package se.lth.control.labcomm2014;
public class SigSampleDef extends SigTypeDef{
SigSampleDef(SigTypeDef td) {
super(td.getName(), td.getType());
}
@Override
public boolean isSampleDef() {
return true;
}
public String defType() {
return "sample";
}
}
package se.lth.control.labcomm2014;
import java.io.PrintStream;
import java.util.List;
import java.util.LinkedList;
import java.util.Iterator;
public class SigStructType extends DataType {
private List<SigField> fields;
SigStructType(int nSigFields) {
super("struct", Constant.STRUCT);
this.fields = new LinkedList<SigField>();
}
public SigStructType() {
this("struct");
}
protected SigStructType(String name) {
super(name, Constant.STRUCT);
this.fields = new LinkedList<SigField>();
}
public SigField[] getFields() {
return fields.toArray(new SigField[0]);
}
public Iterator<SigField> getFieldIterator() {
return fields.iterator();
}
public void addField(String name, DataType type) {
fields.add(new SigField(name, type));
}
public void addField(SigField f) {
fields.add(f);
}
public boolean isStruct() {
return true;
}
public boolean isVoid() {
return fields.size() == 0;
}
public String toString() {
if(isVoid()) { //void type is empty struct
return "void";
} else {
StringBuilder sb = new StringBuilder();
sb.append("struct {\n");
for(SigField f : fields) {
sb.append(f.toString());
sb.append(";\n");
}
sb.append("}");
return sb.toString();
}
}
public void accept(SignatureSymbolVisitor v) {
v.visit(this);
}
public void print(PrintStream out, String indent) {
out.println("struct {");
String newIndent=indent+" ";
out.print(newIndent);
Iterator<SigField> it = getFieldIterator();
while(it.hasNext()) {
it.next().print(out, newIndent);
if(it.hasNext()) {
out.print(newIndent);
}
}
out.print(indent+"}");
}
}
package se.lth.control.labcomm2014;
import java.util.HashSet;
import java.util.Iterator;
public class SigTypeDef{
private String name;
private DataType type;
protected HashSet<SigTypeDef> deps;
SigTypeDef(SigTypeDef td) {
this(td.getName(), td.getType());
this.deps = new HashSet<SigTypeDef>(td.deps);
}
SigTypeDef( String name){
this.name = name;
this.deps = new HashSet<SigTypeDef>();
}
SigTypeDef(String name, DataType type) {
this(name);
this.type = type;
}
void addDependency(SigTypeDef d) {
deps.add(d);
}
HashSet<SigTypeDef> getDependencies() {
return deps;
}
Iterator<SigTypeDef> getDepIterator() {
return deps.iterator();
}
/** To be overridden in SigSampleDef
*/
public boolean isSampleDef() {
return false;
}
public String defType() {
return "typedef";
}
void setType(DataType type) {
this.type = type;
}
public DataType getType() {
if(type==null) {
System.out.println("******** WARNING! SigTypeDef.getType returning null");
}
return type;
}
int getIndex() {
return 0;
}
public String getName() {
return name;
}
public String toString() {
return type.toString();
}
public int hashCode() {
return name.hashCode();
}
public boolean equals(Object o) {
if(! (o instanceof SigTypeDef)){
return false;
} else {
SigTypeDef other = (SigTypeDef) o;
return other.getIndex() == getIndex() && other.name.equals(name);
}
}
public void accept(SignatureSymbolVisitor v) {
type.accept(v);
}
}
package se.lth.control.labcomm2014;
public class SigUserType extends DataType {
public String toString() {
return getName();
}
public SigUserType(String name) {
super(name, 0);
}
public boolean isUserType() {
return true;
}
public void accept(SignatureSymbolVisitor v) {
v.visit(this);
}
}
package se.lth.control.labcomm2014;
public interface SignatureSymbol{
public void accept(SignatureSymbolVisitor v);
}
package se.lth.control.labcomm2014;
/* An interface for using Visitor pattern to traverse
* the type tree
*/
public interface SignatureSymbolVisitor {
void visit(TypeSymbol s);
void visit(SampleSymbol s);
void visit(NameSymbol s);
void visit(SigPrimitiveType t);
//sampleRefs are sent as primitive types
//Put this back if that is changed to SampleRefType
//void visit(SampleRefType t);
void visit(SigStructType t);
void visit(SigField t);
void visit(SigArrayType t);
void visit(SigUserType t);
}
package se.lth.control.labcomm2014;
import java.io.IOException;
import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
import java.util.Iterator;
import java.util.NoSuchElementException;
import se.lth.control.labcomm2014.Decoder;
import se.lth.control.labcomm2014.DecoderChannel;
import se.lth.control.labcomm2014.SampleDispatcher;
import se.lth.control.labcomm2014.SampleHandler;
public class TypeBinding implements BuiltinType {
private int sampleIndex;
private int typeIndex;
public TypeBinding(int sampleIndex, int typeIndex) {
this.sampleIndex = sampleIndex;
this.typeIndex = typeIndex;
}
public int getSampleIndex() {
return sampleIndex;
}
public int getTypeIndex() {
return typeIndex;
}
public boolean isSelfBinding() {
return typeIndex == Constant.TYPE_BIND_SELF;
}
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");
}
public byte[] getSignature() {
return null; // not used for matching
}
public int getNumIntentions() {
return 0;
}
public byte[] getIntentionBytes() {
return null; // not used for matching
}
public void encodeTypeDef(Encoder e, int index) throws IOException{
throw new Error("Should not be called");
}
public void registerTypeDeps(Encoder e) throws IOException{
throw new Error("Should not be called");
}
public void decodeAndHandle(Decoder d,
SampleHandler h) throws Exception {
((Handler)h).handle_TypeBinding(TypeBinding.decode(d));
}
public boolean hasDependencies() {
return false;
}
public Iterator<SampleDispatcher> getDependencyIterator() {
return new Iterator<SampleDispatcher>() {
public boolean hasNext() {
return false;
}
public SampleDispatcher next() throws NoSuchElementException {
throw new NoSuchElementException();
}
public void remove() throws UnsupportedOperationException {
throw new UnsupportedOperationException();
}
};
}
public DataType getDataType() {
throw new Error("not implemented");
}
}
public static void encode(Encoder e, TypeBinding value) throws IOException {
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 {
TypeBinding result;
int sampleIndex = d.decodePacked32();
int typeIndex = d.decodePacked32();
result = new TypeBinding(sampleIndex, typeIndex);
return result;
}
}
package se.lth.control.labcomm2014;
import java.io.IOException;
import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
import java.util.Iterator;
import java.util.NoSuchElementException;
import se.lth.control.labcomm2014.Decoder;
import se.lth.control.labcomm2014.DecoderChannel;
import se.lth.control.labcomm2014.SampleDispatcher;
import se.lth.control.labcomm2014.SampleHandler;
public class TypeDef implements BuiltinType {
private int index;
private String name;
private byte signature[];
public int getIndex() {
return index;
}
public String getName() {
return name;
}
public String toString() {
return getName();
}
public byte[] getSignature() {
return signature;
}
public void dump() {
System.out.print("=== TypeDef "+getName()+"( "+Integer.toHexString(getIndex())+") : ");
for (byte b : signature) {
System.out.print(String.format("0x%02X ", b));
}
System.out.println();
}
public interface Handler extends SampleHandler {
public void handle_TypeDef(TypeDef 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<TypeDef> {
private static Dispatcher singleton;
public synchronized static Dispatcher singleton() {
if(singleton==null) singleton=new Dispatcher();
return singleton;
}
public Class<TypeDef> getSampleClass() {
return TypeDef.class;
}
public String getName() {
return "TypeDef";
}
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 int getNumIntentions() {
return 0;
}
public byte[] getIntentionBytes() {
return new byte[0];
}
public void encodeTypeDef(Encoder e, int index) throws IOException{
throw new Error("Should not be called");
}
public void registerTypeDeps(Encoder e) 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_TypeDef(TypeDef.decode(d));
}
public boolean hasDependencies() {
return false;
}
public Iterator<SampleDispatcher> getDependencyIterator() {
return new Iterator<SampleDispatcher>() {
public boolean hasNext() {
return false;
}
public SampleDispatcher next() throws NoSuchElementException {
throw new NoSuchElementException();
}
public void remove() throws UnsupportedOperationException {
throw new UnsupportedOperationException();
}
};
}
public DataType getDataType() {
throw new Error("not implemented");
}
}
public static void encode(Encoder e, TypeDef value) throws IOException {
throw new Error("Should not be called");
}
protected TypeDef() {
}
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;
int index = d.decodePacked32();
int numIntentions = d.decodePacked32();
if(numIntentions != 1) {
System.out.println("WARNING: #intentions == "+numIntentions);
}
int keylen = d.decodePacked32();
if(keylen != 0) {
System.out.println("WARNING: keylen == "+keylen);
}
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;
}
}