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 Transfer class
33   * <p>
34   * header = "request" middle = way end = might be empty
35   */
36  public class EndTransferPacket extends AbstractLocalPacket {
37    private static final byte ASKVALIDATE = 0;
38  
39    private static final byte ANSWERVALIDATE = 1;
40  
41    private final byte request;
42  
43    private byte way;
44  
45    private String hashOptional;
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 EndTransferPacket createFromBuffer(final int headerLength,
58                                                     final int middleLength,
59                                                     final int endLength,
60                                                     final ByteBuf buf)
61        throws OpenR66ProtocolPacketException {
62      if (headerLength - 1 != 1) {
63        throw new OpenR66ProtocolPacketException("Not enough data");
64      }
65      if (middleLength != 1) {
66        throw new OpenR66ProtocolPacketException("Not enough data");
67      }
68      final byte bheader = buf.readByte();
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 EndTransferPacket(bheader, valid, optional);
76      }
77      return new EndTransferPacket(bheader, valid);
78    }
79  
80    /**
81     * @param request
82     * @param valid
83     * @param hashOptional
84     */
85    private EndTransferPacket(final byte request, final byte valid,
86                              final String hashOptional) {
87      this.request = request;
88      way = valid;
89      this.hashOptional = hashOptional;
90    }
91  
92    /**
93     * @param request
94     * @param valid
95     */
96    private EndTransferPacket(final byte request, final byte valid) {
97      this.request = request;
98      way = valid;
99    }
100 
101   /**
102    * @param request
103    */
104   public EndTransferPacket(final byte request) {
105     this.request = request;
106     way = ASKVALIDATE;
107   }
108 
109   /**
110    * @param request
111    * @param hashOptional
112    */
113   public EndTransferPacket(final byte request, final String hashOptional) {
114     this.request = request;
115     way = ASKVALIDATE;
116     this.hashOptional = hashOptional;
117   }
118 
119   @Override
120   public final boolean hasGlobalBuffer() {
121     return true;
122   }
123 
124   @Override
125   public final synchronized void createAllBuffers(
126       final LocalChannelReference lcr, final int networkHeader) {
127     final int headerSize = 1;
128     final int middleSize = 1;
129     final byte[] endBytes =
130         hashOptional != null? hashOptional.getBytes(WaarpStringUtils.UTF8) :
131             EMPTY_ARRAY;
132     final int endSize = endBytes.length;
133     final int globalSize =
134         networkHeader + LOCAL_HEADER_SIZE + headerSize + middleSize + endSize;
135     int offset = networkHeader + LOCAL_HEADER_SIZE;
136     global = ByteBufAllocator.DEFAULT.ioBuffer(globalSize, globalSize);
137     header = WaarpNettyUtil.slice(global, offset, headerSize);
138     header.writeByte(request);
139     offset += headerSize;
140     middle = WaarpNettyUtil.slice(global, offset, middleSize);
141     middle.writeByte(way);
142     offset += middleSize;
143     end = WaarpNettyUtil.slice(global, offset, endSize);
144     if (hashOptional != null) {
145       end.writeBytes(endBytes);
146     }
147   }
148 
149   @Override
150   public final byte getType() {
151     return LocalPacketFactory.ENDTRANSFERPACKET;
152   }
153 
154   @Override
155   public final String toString() {
156     return "EndTransferPacket: " + request + ' ' + way +
157            (hashOptional != null? ' ' + hashOptional : "");
158   }
159 
160   /**
161    * @return the requestId
162    */
163   public final byte getRequest() {
164     return request;
165   }
166 
167   /**
168    * @return True if this packet is to be validated
169    */
170   public final boolean isToValidate() {
171     return way == ASKVALIDATE;
172   }
173 
174   /**
175    * Validate the connection
176    */
177   public final void validate() {
178     way = ANSWERVALIDATE;
179     clear();
180   }
181 
182   /**
183    * @return the optional
184    */
185   public final String getOptional() {
186     return hashOptional;
187   }
188 
189   /**
190    * @param optional the optional to set
191    */
192   public final void setOptional(final String optional) {
193     hashOptional = optional;
194   }
195 }