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 io.netty.buffer.Unpooled;
25  import org.waarp.common.utility.WaarpNettyUtil;
26  import org.waarp.openr66.protocol.exception.OpenR66ProtocolPacketException;
27  import org.waarp.openr66.protocol.localhandler.LocalChannelReference;
28  
29  /**
30   * Keep Alive class
31   * <p>
32   * header = empty middle = way end = empty
33   */
34  public class KeepAlivePacket extends AbstractLocalPacket {
35    private static final byte ASKVALIDATE = 0;
36  
37    private static final byte ANSWERVALIDATE = 1;
38  
39    private byte way;
40  
41    /**
42     * @param headerLength
43     * @param middleLength
44     * @param endLength
45     * @param buf
46     *
47     * @return the new EndTransferPacket from buffer
48     *
49     * @throws OpenR66ProtocolPacketException
50     */
51    public static KeepAlivePacket createFromBuffer(final int headerLength,
52                                                   final int middleLength,
53                                                   final int endLength,
54                                                   final ByteBuf buf)
55        throws OpenR66ProtocolPacketException {
56      if (middleLength != 1) {
57        throw new OpenR66ProtocolPacketException("Not enough data");
58      }
59      final byte valid = buf.readByte();
60      return new KeepAlivePacket(valid);
61    }
62  
63    /**
64     * @param valid
65     */
66    private KeepAlivePacket(final byte valid) {
67      way = valid;
68    }
69  
70    /**
71     *
72     */
73    public KeepAlivePacket() {
74      way = ASKVALIDATE;
75    }
76  
77    @Override
78    public final boolean hasGlobalBuffer() {
79      return true;
80    }
81  
82    @Override
83    public final synchronized void createAllBuffers(
84        final LocalChannelReference lcr, final int networkHeader) {
85      final int globalSize = networkHeader + LOCAL_HEADER_SIZE + 1;
86      final int offset = networkHeader + LOCAL_HEADER_SIZE;
87      global = ByteBufAllocator.DEFAULT.ioBuffer(globalSize, globalSize);
88      middle = WaarpNettyUtil.slice(global, offset, 1);
89      middle.writeByte(way);
90      end = Unpooled.EMPTY_BUFFER;
91      header = Unpooled.EMPTY_BUFFER;
92    }
93  
94    @Override
95    public final byte getType() {
96      return LocalPacketFactory.KEEPALIVEPACKET;
97    }
98  
99    @Override
100   public final String toString() {
101     return "KeepAlivePacket: " + way;
102   }
103 
104   /**
105    * @return True if this packet is to be validated
106    */
107   public final boolean isToValidate() {
108     return way == ASKVALIDATE;
109   }
110 
111   /**
112    * Validate the connection
113    */
114   public final void validate() {
115     way = ANSWERVALIDATE;
116     clear();
117   }
118 }