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 754 additions and 10 deletions
package se.lth.control.labcomm2014;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class EncoderChannel implements Encoder {
private Writer writer;
private ByteArrayOutputStream bytes = new ByteArrayOutputStream();
private DataOutputStream data = new DataOutputStream(bytes);
private EncoderRegistry sample_def_registry = new EncoderRegistry();
private EncoderRegistry sample_ref_registry = new EncoderRegistry();
private EncoderRegistry type_def_registry = new EncoderRegistry();
private int current_tag;
private EncoderChannel(Writer writer, boolean emitVersion) throws IOException {
this.writer = writer;
if(emitVersion){
begin(Constant.VERSION);
encodeString(Constant.CURRENT_VERSION);
end(null);
}
}
public EncoderChannel(Writer writer) throws IOException {
this(writer, true);
}
public EncoderChannel(OutputStream writer) throws IOException {
this(new WriterWrapper(writer), true);
}
private EncoderChannel(OutputStream writer, boolean emitVersion) throws IOException {
this(new WriterWrapper(writer), emitVersion);
}
private void bindType(int sampleId, int typeId) throws IOException {
begin(Constant.TYPE_BINDING);
encodePacked32(sampleId);
encodePacked32(typeId);
end(null);
}
private void registerSample(SampleDispatcher dispatcher) throws IOException {
int index = sample_def_registry.add(dispatcher);
begin(dispatcher.getTypeDeclTag());
encodePacked32(index);
//HERE BE DRAGONS! numintentions does not include name
// encodePacked32(dispatcher.getNumIntentions()+1);
encodePacked32(1);
encodePacked32(0); // the empty key == name
encodeString(dispatcher.getName());
// byte[] intentions = dispatcher.getIntentionBytes();
// for (int k = 0 ; k < intentions.length ; k++) {
// encodeByte(intentions[k]);
// }
byte[] signature = dispatcher.getSignature();
encodePacked32(signature.length);
for (int i = 0 ; i < signature.length ; i++) {
encodeByte(signature[i]);
}
end(null);
int tindex;
if(dispatcher.hasDependencies()){
tindex = registerTypeDef(dispatcher);
} else {
tindex = Constant.TYPE_BIND_SELF;
}
bindType(index, tindex);
}
private static class WrappedEncoder extends EncoderChannel{
private Encoder wrapped;
public WrappedEncoder(Encoder e, OutputStream s, boolean emitVersion) throws IOException {
super(s,emitVersion);
this.wrapped = e;
}
public int getTypeId(Class<? extends SampleType> c) throws IOException{
return wrapped.getTypeId(c);
}
}
private int registerTypeDef(SampleDispatcher dispatcher) throws IOException {
// check if already registered
try {
return type_def_registry.getTag(dispatcher);
} catch (IOException e) {
//otherwise, add to the registry
int index = type_def_registry.add(dispatcher);
//wrap encoder to get encoded length of signature
ByteArrayOutputStream baos = new ByteArrayOutputStream();
EncoderChannel wrapped = new WrappedEncoder(this, baos, false);
dispatcher.encodeTypeDef(wrapped, index);
wrapped.flush();
byte b[] = baos.toByteArray();
begin(Constant.TYPE_DEF);
encodePacked32(index);
//HERE BE DRAGONS! numintentions does not include name
// encodePacked32(dispatcher.getNumIntentions()+1);
encodePacked32(1);
encodePacked32(0); // the empty key == name
encodeString(dispatcher.getName());
encodePacked32(b.length);
for(int i = 0; i<b.length; i++) {
encodeByte(b[i]);
}
end(null);
return index;
}
}
public void register(SampleDispatcher dispatcher) throws IOException {
switch (dispatcher.getTypeDeclTag()) {
case Constant.SAMPLE_DEF: {
registerSample(dispatcher);
break;
}
case Constant.TYPE_DEF: {
registerTypeDef(dispatcher);
break;
}
default:
throw new Error("Unknown typeDeclTag: "+dispatcher.getTypeDeclTag());
}
}
public void registerSampleRef(SampleDispatcher dispatcher) throws IOException {
System.err.println(dispatcher);
int index = sample_ref_registry.add(dispatcher);
begin(Constant.SAMPLE_REF);
encodePacked32(index);
//HERE BE DRAGONS! numintentions does not include name
// encodePacked32(dispatcher.getNumIntentions()+1);
encodePacked32(1);
encodePacked32(0); // the empty key == name
encodeString(dispatcher.getName());
byte[] signature = dispatcher.getSignature();
encodePacked32(signature.length);
for (int i = 0 ; i < signature.length ; i++) {
encodeByte(signature[i]);
}
end(null);
}
public void begin(int tag) {
current_tag = tag;
bytes.reset();
}
public void begin(Class<? extends SampleType> c) throws IOException {
begin(sample_def_registry.getTag(c));
}
/* aux. method used to allow nesting encoders to find encoded size */
private void flush() throws IOException{
data.flush();
writer.write(bytes.toByteArray());
bytes.reset();
}
public void end(Class<? extends SampleType> c) throws IOException {
data.flush();
WritePacked32(writer, current_tag);
WritePacked32(writer, bytes.size());
writer.write(bytes.toByteArray());
bytes.reset();
}
/**
* @return the id of a TYPE_DEF
*/
public int getTypeId(Class<? extends SampleType> c) throws IOException {
return type_def_registry.getTag(c);
}
private void WritePacked32(Writer s, long value) throws IOException {
byte[] tmp1 = new byte[5];
byte[] tmp2 = new byte[1];
long v = value & 0xffffffff;
int i;
for (i = 0 ; i == 0 || v != 0 ; i++, v = (v >> 7)) {
tmp1[i] = (byte)(v & 0x7f | (i!=0?0x80:0x00));
}
for (i = i - 1 ; i >= 0 ; i--) {
tmp2[0] = tmp1[i];
writer.write(tmp2);
}
}
public void encodeBoolean(boolean value) throws IOException{
data.writeBoolean(value);
}
public void encodeByte(byte value) throws IOException {
data.writeByte(value);
}
public void encodeShort(short value) throws IOException {
data.writeShort(value);
}
public void encodeInt(int value) throws IOException {
data.writeInt(value);
}
public void encodeLong(long value) throws IOException {
data.writeLong(value);
}
public void encodeFloat(float value) throws IOException {
data.writeFloat(value);
}
public void encodeDouble(double value) throws IOException {
data.writeDouble(value);
}
public void encodeString(String value) throws IOException {
ByteArrayOutputStream tmpb = new ByteArrayOutputStream();
DataOutputStream tmps = new DataOutputStream(tmpb);
tmps.writeUTF(value);
tmps.flush();
byte[] tmp = tmpb.toByteArray();
//the two first bytes written by writeUTF is the length of
//the string, so we skip those
encodePacked32(tmp.length-2);
for (int i = 2 ; i < tmp.length ; i++) {
encodeByte(tmp[i]);
}
}
public void encodePacked32(long value) throws IOException {
byte[] tmp = new byte[5];
long v = value & 0xffffffff;
int i;
for (i = 0 ; i == 0 || v != 0 ; i++, v = (v >> 7)) {
tmp[i] = (byte)(v & 0x7f);
}
for (i = i - 1 ; i >= 0 ; i--) {
encodeByte((byte)(tmp[i] | (i!=0?0x80:0x00)));
}
}
public void encodeSampleRef(Class value) throws IOException {
int index = 0;
try {
index = sample_ref_registry.getTag(value);
} catch (NullPointerException e) {
//we want to return 0 for unregistered ref types
}
data.writeInt(index);
}
}
package se.lth.control.labcomm;
package se.lth.control.labcomm2014;
import java.io.IOException;
import java.util.HashMap;
public class LabCommEncoderRegistry {
public class EncoderRegistry {
public static class Entry {
private LabCommDispatcher dispatcher;
private SampleDispatcher dispatcher;
private int index;
public Entry(LabCommDispatcher dispatcher, int index) {
public Entry(SampleDispatcher dispatcher, int index) {
this.dispatcher = dispatcher;
this.index = index;
}
public LabCommDispatcher getDispatcher() {
public SampleDispatcher getDispatcher() {
return dispatcher;
}
......@@ -25,14 +25,14 @@ public class LabCommEncoderRegistry {
}
private int userIndex = LabComm.FIRST_USER_INDEX;
private int userIndex = Constant.FIRST_USER_INDEX;
private HashMap<Class, Entry> byClass;
public LabCommEncoderRegistry() {
public EncoderRegistry() {
byClass = new HashMap<Class, Entry>();
}
public synchronized int add(LabCommDispatcher dispatcher) {
public synchronized int add(SampleDispatcher dispatcher) {
Entry e = byClass.get(dispatcher.getSampleClass());
if (e == null) {
e = new Entry(dispatcher, userIndex);
......@@ -42,7 +42,11 @@ public class LabCommEncoderRegistry {
return e.getIndex();
}
public int getTag(Class<? extends LabCommSample> sample) throws IOException {
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);
if (e == null) {
throw new IOException("'" +
......@@ -52,4 +56,8 @@ public class LabCommEncoderRegistry {
return e.index;
}
}
\ No newline at end of file
public boolean contains(Class<? extends SampleType> sample) {
return byClass.containsKey(sample);
}
}
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);
}