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
30
31
32
33
34
35
36
37 public class BusinessRequestPacket extends AbstractLocalPacket {
38 private static final byte ASKVALIDATE = 0;
39
40 private static final byte ANSWERVALIDATE = 1;
41 private static final byte ANSWERINVALIDATE = 2;
42
43 private final String sheader;
44
45 private int delay;
46
47 private byte way;
48
49 public static BusinessRequestPacket createFromBuffer(final int headerLength,
50 final int middleLength,
51 final int endLength,
52 final ByteBuf buf)
53 throws OpenR66ProtocolPacketException {
54 final byte[] bheader = new byte[headerLength - 1];
55 if (headerLength - 1 > 0) {
56 buf.readBytes(bheader);
57 }
58 if (middleLength != 4) {
59 throw new OpenR66ProtocolPacketException("Packet not correct");
60 }
61 final int delay = buf.readInt();
62 if (endLength != 1) {
63 throw new OpenR66ProtocolPacketException("Packet not correct");
64 }
65 final byte valid = buf.readByte();
66 return new BusinessRequestPacket(new String(bheader, WaarpStringUtils.UTF8),
67 delay, valid);
68 }
69
70 public BusinessRequestPacket(final String header, final int delay,
71 final byte way) {
72 sheader = header;
73 this.delay = delay;
74 this.way = way;
75 }
76
77 public BusinessRequestPacket(final String header, final int delay) {
78 sheader = header;
79 this.delay = delay;
80 way = ASKVALIDATE;
81 }
82
83 @Override
84 public final boolean hasGlobalBuffer() {
85 return true;
86 }
87
88 @Override
89 public final synchronized void createAllBuffers(
90 final LocalChannelReference lcr, final int networkHeader) {
91 final byte[] headerBytes = sheader.getBytes(WaarpStringUtils.UTF8);
92 final int headerSize = headerBytes.length;
93 final int middleSize = 4;
94 final int endSize = 1;
95 final int globalSize =
96 networkHeader + LOCAL_HEADER_SIZE + headerSize + middleSize + endSize;
97 int offset = networkHeader + LOCAL_HEADER_SIZE;
98 global = ByteBufAllocator.DEFAULT.ioBuffer(globalSize, globalSize);
99 header = WaarpNettyUtil.slice(global, offset, headerSize);
100 header.writeBytes(headerBytes);
101 offset += headerSize;
102 middle = WaarpNettyUtil.slice(global, offset, middleSize);
103 middle.writeInt(delay);
104 offset += middleSize;
105 end = WaarpNettyUtil.slice(global, offset, endSize);
106 end.writeByte(way);
107 WaarpNettyUtil.retain(global);
108 }
109
110 @Override
111 public final byte getType() {
112 return LocalPacketFactory.BUSINESSREQUESTPACKET;
113 }
114
115 @Override
116 public final String toString() {
117 return "BusinessRequestPacket: " + sheader + ':' + delay + ':' + way;
118 }
119
120
121
122
123 public final boolean isToValidate() {
124 return way == ASKVALIDATE;
125 }
126
127
128
129
130 public final void validate() {
131 way = ANSWERVALIDATE;
132 clear();
133 }
134
135
136
137
138 public final void invalidate() {
139 way = ANSWERINVALIDATE;
140 clear();
141 }
142
143
144
145
146 public final String getSheader() {
147 return sheader;
148 }
149
150
151
152
153 public final int getDelay() {
154 return delay;
155 }
156
157
158
159
160 public final void setDelay(final int delay) {
161 this.delay = delay;
162 WaarpNettyUtil.release(middle);
163 middle = null;
164 }
165 }