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   * Shutdown Message class for packet
31   * <p>
32   * 1 string: spassword(or key) + 1 byte (optional) to restart (if not null)
33   */
34  public class ShutdownPacket extends AbstractLocalPacket {
35    private final byte[] key;
36    private final byte restart;
37  
38    /**
39     * @param headerLength
40     * @param middleLength
41     * @param endLength
42     * @param buf
43     *
44     * @return the new ShutdownPacket from buffer
45     *
46     * @throws OpenR66ProtocolPacketException
47     */
48    public static ShutdownPacket createFromBuffer(final int headerLength,
49                                                  final int middleLength,
50                                                  final int endLength,
51                                                  final ByteBuf buf)
52        throws OpenR66ProtocolPacketException {
53      if (headerLength - 1 <= 0) {
54        throw new OpenR66ProtocolPacketException("Not enough data");
55      }
56      final byte[] bpassword = new byte[headerLength - 1];
57      buf.readBytes(bpassword);
58      byte torestart = 0;
59      if (middleLength > 0) {
60        torestart = buf.readByte();
61      }
62      return new ShutdownPacket(bpassword, torestart);
63    }
64  
65    /**
66     * @param spassword
67     */
68    public ShutdownPacket(final byte[] spassword) {
69      key = spassword;
70      restart = 0;
71    }
72  
73    /**
74     * @param spassword
75     * @param restart
76     */
77    public ShutdownPacket(final byte[] spassword, final byte restart) {
78      key = spassword;
79      this.restart = restart;
80    }
81  
82    @Override
83    public final boolean hasGlobalBuffer() {
84      return true;
85    }
86  
87    @Override
88    public final synchronized void createAllBuffers(
89        final LocalChannelReference lcr, final int networkHeader) {
90      end = Unpooled.EMPTY_BUFFER;
91      final int sizeKey = key != null? key.length : 0;
92      final int sizeMiddle = restart != 0? 1 : 0;
93      final int globalSize =
94          networkHeader + LOCAL_HEADER_SIZE + sizeKey + sizeMiddle;
95      int offset = networkHeader + LOCAL_HEADER_SIZE;
96      global = ByteBufAllocator.DEFAULT.ioBuffer(globalSize, globalSize);
97      if (key != null) {
98        header = WaarpNettyUtil.slice(global, offset, sizeKey);
99        header.writeBytes(key);
100     } else {
101       header = Unpooled.EMPTY_BUFFER;
102     }
103     offset += sizeKey;
104     if (restart != 0) {
105       middle = WaarpNettyUtil.slice(global, offset, sizeMiddle);
106       middle.writeByte(restart);
107     } else {
108       middle = Unpooled.EMPTY_BUFFER;
109     }
110   }
111 
112   @Override
113   public final String toString() {
114     return "ShutdownPacket" + (restart != 0? " and restart" : "");
115   }
116 
117   @Override
118   public final byte getType() {
119     return LocalPacketFactory.SHUTDOWNPACKET;
120   }
121 
122   /**
123    * @return the key
124    */
125   public final byte[] getKey() {
126     return key;
127   }
128 
129   public final boolean isRestart() {
130     return restart != 0;
131   }
132 }