1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package it.jnrpe.net;
17
18 import io.netty.buffer.ByteBuf;
19 import io.netty.channel.ChannelHandlerContext;
20 import io.netty.handler.codec.ReplayingDecoder;
21
22 import java.util.List;
23
24 import org.apache.commons.lang.ArrayUtils;
25
26
27
28
29
30
31 public class JNRPERequestDecoder extends
32 ReplayingDecoder<JNRPERequestDecoder.STAGE> {
33
34
35
36
37 private static final int PACKETBUFFER_LENGTH = 1024;
38
39
40
41
42 private static final int DUMMYLENGTH = 2;
43
44
45
46
47 private JNRPEProtocolPacket packet;
48
49
50
51
52 private PacketVersion packetVersion;
53
54
55
56
57
58
59
60
61 protected enum STAGE {
62
63
64
65 PACKET_VERSION,
66
67
68
69 PACKET_TYPE_CODE,
70
71
72
73 CRC,
74
75
76
77 RESULT_CODE,
78
79
80
81 BUFFER,
82
83
84
85 DUMMY
86 };
87
88
89
90
91
92 public JNRPERequestDecoder() {
93 super(STAGE.PACKET_VERSION);
94 }
95
96 @Override
97 protected final void decode(final ChannelHandlerContext ctx,
98 final ByteBuf in, final List<Object> out) throws Exception {
99
100 switch (state()) {
101 case PACKET_VERSION:
102
103 packetVersion = PacketVersion.fromIntValue(in.readShort());
104 checkpoint(STAGE.PACKET_TYPE_CODE);
105 case PACKET_TYPE_CODE:
106
107 PacketType type = PacketType.fromIntValue(in.readShort());
108 switch (type) {
109 case QUERY:
110 packet = new JNRPERequest();
111 break;
112 case RESPONSE:
113 packet = new JNRPEResponse();
114 break;
115 default:
116 throw new Exception("Unknown packet type");
117 }
118
119 packet.setPacketVersion(packetVersion);
120 checkpoint(STAGE.CRC);
121 case CRC:
122 packet.setCRC(in.readInt());
123 checkpoint(STAGE.RESULT_CODE);
124 case RESULT_CODE:
125 packet.setResultCode(in.readShort());
126 checkpoint(STAGE.BUFFER);
127 case BUFFER:
128 byte[] buff = new byte[PACKETBUFFER_LENGTH];
129 in.readBytes(buff);
130 packet.setBuffer(ztString2String(buff));
131 checkpoint(STAGE.DUMMY);
132 case DUMMY:
133 byte[] dummy = new byte[DUMMYLENGTH];
134 packet.setDummy(dummy);
135 out.add(packet);
136 reset();
137 break;
138 default:
139 throw new Error("Shouldn't reach here.");
140 }
141 }
142
143
144
145
146 private void reset() {
147 checkpoint(STAGE.PACKET_VERSION);
148 }
149
150
151
152
153
154
155
156
157 private String ztString2String(final byte[] buff) {
158 return new String(buff, 0, ArrayUtils.indexOf(buff, (byte) 0));
159 }
160
161 }