View Javadoc
1   /*
2    * This file is part of Waarp Project (named also Waarp or GG).
3    *
4    *  Copyright (c) 2019, Waarp SAS, and individual contributors by the @author
5    *  tags. See the COPYRIGHT.txt in the distribution for a full listing of
6    * individual contributors.
7    *
8    *  All Waarp Project is free software: you can redistribute it and/or
9    * modify it under the terms of the GNU General Public License as published by
10   * the Free Software Foundation, either version 3 of the License, or (at your
11   * option) any later version.
12   *
13   * Waarp is distributed in the hope that it will be useful, but WITHOUT ANY
14   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15   * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License along with
18   * Waarp . If not, see <http://www.gnu.org/licenses/>.
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   * Error Message class for packet
31   * <p>
32   * 2 strings+1 error code: sheader,smiddle,code
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     * @param headerLength
51     * @param middleLength
52     * @param endLength
53     * @param buf
54     *
55     * @return the new ErrorPacket from buffer
56     *
57     * @throws OpenR66ProtocolPacketException
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     * @param header
82     * @param middle
83     * @param code
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    * @return the sheader
136    */
137   public final String getSheader() {
138     return sheader;
139   }
140 
141   /**
142    * @return the smiddle
143    */
144   public final String getSmiddle() {
145     return smiddle;
146   }
147 
148   /**
149    * @return the code
150    */
151   public final int getCode() {
152     return code;
153   }
154 
155 }