1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.waarp.openr66.protocol.localhandler.packet;
21
22 import io.netty.buffer.ByteBuf;
23 import io.netty.buffer.ByteBufAllocator;
24 import org.waarp.common.utility.WaarpNettyUtil;
25 import org.waarp.common.utility.WaarpStringUtils;
26 import org.waarp.openr66.protocol.exception.OpenR66ProtocolPacketException;
27 import org.waarp.openr66.protocol.localhandler.LocalChannelReference;
28
29 import java.nio.charset.Charset;
30
31
32
33
34
35
36 public class EndRequestPacket extends AbstractLocalPacket {
37 private static final byte ASKVALIDATE = 0;
38
39 private static final byte ANSWERVALIDATE = 1;
40
41 private final int code;
42
43 private byte way;
44
45 private String optional;
46
47
48
49
50
51
52
53
54
55
56
57 public static EndRequestPacket createFromBuffer(final int headerLength,
58 final int middleLength,
59 final int endLength,
60 final ByteBuf buf)
61 throws OpenR66ProtocolPacketException {
62 if (headerLength - 1 != 4) {
63 throw new OpenR66ProtocolPacketException("Not enough data");
64 }
65 if (middleLength != 1) {
66 throw new OpenR66ProtocolPacketException("Not enough data");
67 }
68 final int bheader = buf.readInt();
69 final byte valid = buf.readByte();
70 final String optional;
71 if (endLength > 0) {
72 optional =
73 buf.toString(buf.readerIndex(), endLength, Charset.defaultCharset());
74 buf.skipBytes(endLength);
75 return new EndRequestPacket(bheader, valid, optional);
76 }
77 return new EndRequestPacket(bheader, valid);
78 }
79
80
81
82
83
84
85 private EndRequestPacket(final int code, final byte valid,
86 final String optional) {
87 this.code = code;
88 way = valid;
89 this.optional = optional;
90 }
91
92
93
94
95
96 private EndRequestPacket(final int code, final byte valid) {
97 this.code = code;
98 way = valid;
99 }
100
101
102
103
104 public EndRequestPacket(final int code) {
105 this.code = code;
106 way = ASKVALIDATE;
107 }
108
109 @Override
110 public final boolean hasGlobalBuffer() {
111 return true;
112 }
113
114 @Override
115 public final synchronized void createAllBuffers(
116 final LocalChannelReference lcr, final int networkHeader) {
117 final int headerSize = 4;
118 final int middleSize = 1;
119 final byte[] endBytes =
120 optional != null? optional.getBytes(WaarpStringUtils.UTF8) :
121 EMPTY_ARRAY;
122 final int endSize = endBytes.length;
123 final int globalSize =
124 networkHeader + LOCAL_HEADER_SIZE + headerSize + middleSize + endSize;
125 int offset = networkHeader + LOCAL_HEADER_SIZE;
126 global = ByteBufAllocator.DEFAULT.ioBuffer(globalSize, globalSize);
127 header = WaarpNettyUtil.slice(global, offset, headerSize);
128 header.writeInt(code);
129 offset += headerSize;
130 middle = WaarpNettyUtil.slice(global, offset, middleSize);
131 middle.writeByte(way);
132 offset += middleSize;
133 end = WaarpNettyUtil.slice(global, offset, endSize);
134 if (optional != null) {
135 end.writeBytes(endBytes);
136 }
137 }
138
139 @Override
140 public final byte getType() {
141 return LocalPacketFactory.ENDREQUESTPACKET;
142 }
143
144 @Override
145 public final String toString() {
146 return "EndRequestPacket: " + code + ' ' + way +
147 (optional != null? ' ' + optional : "");
148 }
149
150
151
152
153 public final int getCode() {
154 return code;
155 }
156
157
158
159
160 public final boolean isToValidate() {
161 return way == ASKVALIDATE;
162 }
163
164
165
166
167 public final void validate() {
168 way = ANSWERVALIDATE;
169 clear();
170 }
171
172
173
174
175 public final String getOptional() {
176 return optional;
177 }
178
179
180
181
182 public final void setOptional(final String optional) {
183 this.optional = optional;
184 }
185
186 }