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