DecoderChannel.cs 4.66 KB
Newer Older
Anders Blomdell's avatar
Anders Blomdell committed
1
namespace se.lth.control.labcomm2014 {
Anders Nilsson's avatar
Anders Nilsson committed
2
3
4
5
6
7

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

Anders Blomdell's avatar
Anders Blomdell committed
8
  public class DecoderChannel : Decoder {
Anders Nilsson's avatar
Anders Nilsson committed
9
10

    private Stream stream;
11
12
    private DecoderRegistry def_registry = new DecoderRegistry();
    private DecoderRegistry ref_registry = new DecoderRegistry();
Anders Nilsson's avatar
Anders Nilsson committed
13
14
    byte[] buf = new byte[8];

Anders Blomdell's avatar
Anders Blomdell committed
15
    public DecoderChannel(Stream stream) {
Anders Nilsson's avatar
Anders Nilsson committed
16
17
18
19
20
21
      this.stream = stream;
    }

    public void runOne() {
      bool done = false;
      while (!done) {
22
	int tag = decodePacked32();
23
        int length = decodePacked32();
Anders Nilsson's avatar
Anders Nilsson committed
24
	switch (tag) {
25
26
27
28
29
30
31
        case Constant.VERSION: {
          String version = decodeString();
          if (version != Constant.CURRENT_VERSION) {
  	    throw new IOException("LabComm version mismatch " +
			          version + " != " + Constant.CURRENT_VERSION);
          }
        } break;
32
        case Constant.SAMPLE_DEF: {
33
          int index = decodePacked32();
Anders Nilsson's avatar
Anders Nilsson committed
34
          String name = decodeString();
35
36
37
          int signature_length = decodePacked32();
          byte[] signature = new byte[signature_length];
          ReadBytes(signature, signature_length);
38
39
40
41
42
43
44
45
46
	  def_registry.add(index, name, signature);
        } break;
        case Constant.SAMPLE_REF: {
          int index = decodePacked32();
          String name = decodeString();
          int signature_length = decodePacked32();
          byte[] signature = new byte[signature_length];
          ReadBytes(signature, signature_length);
	  ref_registry.add(index, name, signature);
Anders Nilsson's avatar
Anders Nilsson committed
47
        } break;
48
49
50
51
52
53
        case Constant.TYPE_DEF: 
        case Constant.TYPE_BINDING: {
            for(int i=0; i<length;i++){
                decodeByte();
            }                
        } break;
Anders Nilsson's avatar
Anders Nilsson committed
54
        default: {
55
          DecoderRegistry.Entry e = def_registry.get(tag);
Anders Nilsson's avatar
Anders Nilsson committed
56
57
58
          if (e == null) {
            throw new IOException("Unhandled tag " + tag);
          }
Anders Blomdell's avatar
Anders Blomdell committed
59
          SampleDispatcher d = e.getSampleDispatcher();
Anders Nilsson's avatar
Anders Nilsson committed
60
61
62
          if (d == null) {
            throw new IOException("No dispatcher for '" + e.getName() + "'" + e.getSignature());
          }
Anders Blomdell's avatar
Anders Blomdell committed
63
          SampleHandler h = e.getHandler();
Anders Nilsson's avatar
Anders Nilsson committed
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
          if (h == null) {
            throw new IOException("No handler for '" + e.getName() +"'");
          }
          d.decodeAndHandle(this, h);
          done = true;
        } break;
	}
      }
    }

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

Anders Blomdell's avatar
Anders Blomdell committed
80
81
    public void register(SampleDispatcher dispatcher, 
			 SampleHandler handler) {
82
83
84
85
86
      def_registry.add(dispatcher, handler);
    }

    public void registerSampleRef(SampleDispatcher dispatcher) {
      ref_registry.add(dispatcher, null);
Anders Nilsson's avatar
Anders Nilsson committed
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
    }

    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) {
102
      Int64 result = 0;
Anders Nilsson's avatar
Anders Nilsson committed
103
104
105
      ReadBytes(buf, length);
      for (int i = 0 ; i < length ; i++) {
	result = (result << 8) + buf[i];
106

Anders Nilsson's avatar
Anders Nilsson committed
107
108
109
110
111
112
113
114
115
116
117
118
119
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
      }
      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() {
150
      int length = decodePacked32();
Anders Nilsson's avatar
Anders Nilsson committed
151
152
153
154
155
      byte[] buf = new byte[length];
      ReadBytes(buf, length);
      return Encoding.UTF8.GetString(buf);
    }

156
157
158
159
160
    public int decodePacked32() {
      Int64 res = 0;
      bool cont = true; 

      do {
161
162
        Int64 c = decodeByte();
	res = (res << 7) | (c & 0x7f);
163
164
        cont = (c & 0x80) != 0;
      } while(cont);
Anders Nilsson's avatar
Anders Nilsson committed
165

166
167
      return (int) (res & 0xffffffff);
    }
168

169
    public SampleDispatcher decodeSampleRef() {
170
      int index = (int)ReadInt(4);
Anders Blomdell's avatar
Anders Blomdell committed
171
172
      try {
        DecoderRegistry.Entry e = ref_registry.get(index);
Anders Blomdell's avatar
Anders Blomdell committed
173
        return e.getSampleDispatcher();
Anders Blomdell's avatar
Anders Blomdell committed
174
      } catch (NullReferenceException) {
175
176
        return null;
      }
177
    }
Anders Blomdell's avatar
Anders Blomdell committed
178

179
  }
Anders Blomdell's avatar
Anders Blomdell committed
180

Anders Nilsson's avatar
Anders Nilsson committed
181
}