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 public class InformationPacket extends AbstractLocalPacket {
35
36 private static final String NOT_ENOUGH_DATA = "Not enough data";
37
38 public enum ASKENUM {
39 ASKEXIST, ASKMLSDETAIL, ASKLIST, ASKMLSLIST
40 }
41
42 private final String rulename;
43
44 private final byte requestedInfo;
45
46 private final String filename;
47
48
49
50
51
52
53
54
55
56
57
58 public static InformationPacket createFromBuffer(final int headerLength,
59 final int middleLength,
60 final int endLength,
61 final ByteBuf buf)
62 throws OpenR66ProtocolPacketException {
63 if (headerLength - 1 <= 0) {
64 throw new OpenR66ProtocolPacketException(NOT_ENOUGH_DATA);
65 }
66 if (middleLength != 1) {
67 throw new OpenR66ProtocolPacketException(NOT_ENOUGH_DATA);
68 }
69 final byte[] bheader = new byte[headerLength - 1];
70 final byte[] bend = new byte[endLength];
71 buf.readBytes(bheader);
72 final byte request = buf.readByte();
73 if (endLength > 0) {
74 buf.readBytes(bend);
75 }
76 final String sheader = new String(bheader, WaarpStringUtils.UTF8);
77 final String send = new String(bend, WaarpStringUtils.UTF8);
78 return new InformationPacket(sheader, request, send);
79 }
80
81
82
83
84
85
86 public InformationPacket(final String rulename, final byte request,
87 final String filename) {
88 this.rulename = rulename;
89 requestedInfo = request;
90 this.filename = filename;
91 }
92
93 @Override
94 public final boolean hasGlobalBuffer() {
95 return true;
96 }
97
98 @Override
99 public final synchronized void createAllBuffers(
100 final LocalChannelReference lcr, final int networkHeader)
101 throws OpenR66ProtocolPacketException {
102 if (rulename == null) {
103 throw new OpenR66ProtocolPacketException(NOT_ENOUGH_DATA);
104 }
105 final byte[] headerBytes = rulename.getBytes(WaarpStringUtils.UTF8);
106 final int headerSize = headerBytes.length;
107 final int middleSize = 1;
108 final byte[] endBytes =
109 filename != null? filename.getBytes(WaarpStringUtils.UTF8) :
110 EMPTY_ARRAY;
111 final int endSize = endBytes.length;
112 final int globalSize =
113 networkHeader + LOCAL_HEADER_SIZE + headerSize + middleSize + endSize;
114 int offset = networkHeader + LOCAL_HEADER_SIZE;
115 global = ByteBufAllocator.DEFAULT.ioBuffer(globalSize, globalSize);
116 header = WaarpNettyUtil.slice(global, offset, headerSize);
117 header.writeBytes(headerBytes);
118 offset += headerSize;
119 middle = WaarpNettyUtil.slice(global, offset, middleSize);
120 middle.writeByte(requestedInfo);
121 offset += middleSize;
122 end = WaarpNettyUtil.slice(global, offset, endSize);
123 if (filename != null) {
124 end.writeBytes(endBytes);
125 }
126 }
127
128 @Override
129 public final byte getType() {
130 return LocalPacketFactory.INFORMATIONPACKET;
131 }
132
133 @Override
134 public final String toString() {
135 return "InformationPacket: " + requestedInfo + ' ' + rulename + ' ' +
136 filename;
137 }
138
139
140
141
142 public final byte getRequest() {
143 return requestedInfo;
144 }
145
146
147
148
149 public final String getRulename() {
150 return rulename;
151 }
152
153
154
155
156 public final String getFilename() {
157 if (filename != null) {
158 return filename;
159 } else {
160 return "";
161 }
162 }
163 }