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

  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;
Anders Blomdell's avatar
Anders Blomdell committed
11
    private DecoderRegistry registry = new DecoderRegistry();
Anders Nilsson's avatar
Anders Nilsson committed
12
13
    byte[] buf = new byte[8];

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

    public void runOne() {
      bool done = false;
      while (!done) {
26
	int tag = decodePacked32();
27
        int length = decodePacked32();
Anders Nilsson's avatar
Anders Nilsson committed
28
	switch (tag) {
Anders Blomdell's avatar
Anders Blomdell committed
29
        case Constant.SAMPLE: {
30
          int index = decodePacked32();
Anders Nilsson's avatar
Anders Nilsson committed
31
          String name = decodeString();
32
33
34
35
          int signature_length = decodePacked32();
          byte[] signature = new byte[signature_length];
          ReadBytes(signature, signature_length);
	  registry.add(index, name, signature);
Anders Nilsson's avatar
Anders Nilsson committed
36
37
        } break;
        default: {
Anders Blomdell's avatar
Anders Blomdell committed
38
          DecoderRegistry.Entry e = registry.get(tag);
Anders Nilsson's avatar
Anders Nilsson committed
39
40
41
          if (e == null) {
            throw new IOException("Unhandled tag " + tag);
          }
Anders Blomdell's avatar
Anders Blomdell committed
42
          SampleDispatcher d = e.getSampleDispatcher();
Anders Nilsson's avatar
Anders Nilsson committed
43
44
45
          if (d == null) {
            throw new IOException("No dispatcher for '" + e.getName() + "'" + e.getSignature());
          }
Anders Blomdell's avatar
Anders Blomdell committed
46
          SampleHandler h = e.getHandler();
Anders Nilsson's avatar
Anders Nilsson committed
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
          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
63
64
    public void register(SampleDispatcher dispatcher, 
			 SampleHandler handler) {
Anders Nilsson's avatar
Anders Nilsson committed
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
      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) {
81
      Int64 result = 0;
Anders Nilsson's avatar
Anders Nilsson committed
82
83
84
      ReadBytes(buf, length);
      for (int i = 0 ; i < length ; i++) {
	result = (result << 8) + buf[i];
85

Anders Nilsson's avatar
Anders Nilsson committed
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
      }
      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() {
129
      int length = decodePacked32();
Anders Nilsson's avatar
Anders Nilsson committed
130
131
132
133
134
      byte[] buf = new byte[length];
      ReadBytes(buf, length);
      return Encoding.UTF8.GetString(buf);
    }

135
136
137
138
139
    public int decodePacked32() {
      Int64 res = 0;
      bool cont = true; 

      do {
140
141
        Int64 c = decodeByte();
	res = (res << 7) | (c & 0x7f);
142
143
        cont = (c & 0x80) != 0;
      } while(cont);
Anders Nilsson's avatar
Anders Nilsson committed
144

145
146
147
      return (int) (res & 0xffffffff);
    }
  }
Anders Nilsson's avatar
Anders Nilsson committed
148
}