LabCommDecoderChannel.cs 4.54 KB
Newer Older
Anders Nilsson's avatar
Anders Nilsson committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
namespace se.lth.control.labcomm {

  using System;
  using System.IO;
  using System.Runtime.InteropServices;
  using System.Text;

  public class LabCommDecoderChannel : LabCommDecoder {

    private Stream stream;
    private LabCommDecoderRegistry registry = new LabCommDecoderRegistry();
    byte[] buf = new byte[8];

    public LabCommDecoderChannel(Stream stream) {
      this.stream = stream;
16
17
18
19
20
      String version = decodeString();
      if (version != LabComm.VERSION) {
	throw new IOException("LabComm version mismatch " +
			      version + " != " + LabComm.VERSION);
      }
Anders Nilsson's avatar
Anders Nilsson committed
21
22
23
24
25
    }

    public void runOne() {
      bool done = false;
      while (!done) {
26
	int tag = decodePacked32();
Anders Nilsson's avatar
Anders Nilsson committed
27
28
	switch (tag) {
        case LabComm.SAMPLE: {
29
          int index = decodePacked32();
Anders Nilsson's avatar
Anders Nilsson committed
30
31
          String name = decodeString();
	  MemoryStream signature = new MemoryStream();
32
	  collectFlatSignature(new LabCommEncoderChannel(signature, false));
Anders Nilsson's avatar
Anders Nilsson committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
	  registry.add(index, name, signature.ToArray());
        } break;
        default: {
          LabCommDecoderRegistry.Entry e = registry.get(tag);
          if (e == null) {
            throw new IOException("Unhandled tag " + tag);
          }
          LabCommDispatcher d = e.getDispatcher();
          if (d == null) {
            throw new IOException("No dispatcher for '" + e.getName() + "'" + e.getSignature());
          }
          LabCommHandler h = e.getHandler();
          if (h == null) {
            throw new IOException("No handler for '" + e.getName() +"'");
          }
          d.decodeAndHandle(this, h);
          done = true;
        } break;
	}
      }
    }

    public void run() {
      while (true) {
	runOne();
      }
    }

    private void collectFlatSignature(LabCommEncoder e) {
62
      int type = decodePacked32();
Sven Robertz's avatar
Sven Robertz committed
63
      e.encodePacked32(type);
Anders Nilsson's avatar
Anders Nilsson committed
64
65
      switch (type) {
      case LabComm.ARRAY: {
66
67
        int dimensions = decodePacked32();
        e.encodePacked32(dimensions);
Anders Nilsson's avatar
Anders Nilsson committed
68
        for (int i = 0 ; i < dimensions ; i++) {
69
          e.encodePacked32(decodePacked32());
Anders Nilsson's avatar
Anders Nilsson committed
70
71
72
73
        }
        collectFlatSignature(e);
      } break;
      case LabComm.STRUCT: {
74
        int fields = decodePacked32();
75
        e.encodePacked32(fields);
Anders Nilsson's avatar
Anders Nilsson committed
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
        for (int i = 0 ; i < fields ; i++) {
          e.encodeString(decodeString());
          collectFlatSignature(e);
        }
      } break;
      case LabComm.BOOLEAN:
      case LabComm.BYTE:
      case LabComm.SHORT:
      case LabComm.INT:
      case LabComm.LONG:
      case LabComm.FLOAT:
      case LabComm.DOUBLE:
      case LabComm.STRING: {
      } break;
      default: {
        throw new IOException("Unimplemented type=" + type);
      }
      }
      e.end(null);
    }

    public void register(LabCommDispatcher dispatcher, 
			 LabCommHandler handler) {
      registry.add(dispatcher, handler);
    }

    private void ReadBytes(byte[] result, int length) {
      int offset = 0;
      while (offset < length) {
	int count = stream.Read(result, offset, length - offset);
	if (count <= 0)
	  throw new EndOfStreamException(
	    String.Format("End of stream reached with {0} bytes left to read", 
			  length - offset));
	offset += count;
      }
    }

    private Int64 ReadInt(int length) {
115
      Int64 result = 0;
Anders Nilsson's avatar
Anders Nilsson committed
116
117
118
      ReadBytes(buf, length);
      for (int i = 0 ; i < length ; i++) {
	result = (result << 8) + buf[i];
119

Anders Nilsson's avatar
Anders Nilsson committed
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
      }
      return result;
    }

     public bool decodeBoolean() {
      return ReadInt(1) != 0;
    }

    public byte decodeByte() {
      return (byte)ReadInt(1);
    }
  
    public short decodeShort() {
      return (short)ReadInt(2);
    }

    public int decodeInt() {
      return (int)ReadInt(4);
    }
    
    public long decodeLong() {
      return (long)ReadInt(8);
    }
    

    [StructLayout(LayoutKind.Explicit)]
    private struct Int32SingleUnion {
      [FieldOffset(0)] public int AsInt;
      [FieldOffset(0)] public float AsFloat;
    };

    public float decodeFloat() {
      Int32SingleUnion u;
      u.AsFloat = 0; // Avoid error messge
      u.AsInt = (int)ReadInt(4);
      return u.AsFloat;
    }
    
    public double decodeDouble() {
      return BitConverter.Int64BitsToDouble(ReadInt(8));
    }

    public String decodeString() {
163
      int length = decodePacked32();
Anders Nilsson's avatar
Anders Nilsson committed
164
165
166
167
168
      byte[] buf = new byte[length];
      ReadBytes(buf, length);
      return Encoding.UTF8.GetString(buf);
    }

169
170
171
172
173
    public int decodePacked32() {
      Int64 res = 0;
      bool cont = true; 

      do {
174
175
        Int64 c = decodeByte();
	res = (res << 7) | (c & 0x7f);
176
177
        cont = (c & 0x80) != 0;
      } while(cont);
Anders Nilsson's avatar
Anders Nilsson committed
178

179
180
181
      return (int) (res & 0xffffffff);
    }
  }
Anders Nilsson's avatar
Anders Nilsson committed
182
}