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  import java.nio.charset.Charset;
30  
31  /**
32   * End of Request class
33   * <p>
34   * header = Error.code middle = way end = might be empty
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     * @param headerLength
49     * @param middleLength
50     * @param endLength
51     * @param buf
52     *
53     * @return the new EndTransferPacket from buffer
54     *
55     * @throws OpenR66ProtocolPacketException
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     * @param code
82     * @param valid
83     * @param optional
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     * @param code
94     * @param valid
95     */
96    private EndRequestPacket(final int code, final byte valid) {
97      this.code = code;
98      way = valid;
99    }
100 
101   /**
102    * @param code
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    * @return the code
152    */
153   public final int getCode() {
154     return code;
155   }
156 
157   /**
158    * @return True if this packet is to be validated
159    */
160   public final boolean isToValidate() {
161     return way == ASKVALIDATE;
162   }
163 
164   /**
165    * Validate the connection
166    */
167   public final void validate() {
168     way = ANSWERVALIDATE;
169     clear();
170   }
171 
172   /**
173    * @return the optional
174    */
175   public final String getOptional() {
176     return optional;
177   }
178 
179   /**
180    * @param optional the optional to set
181    */
182   public final void setOptional(final String optional) {
183     this.optional = optional;
184   }
185 
186 }