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   * Block Request Message class
31   * <p>
32   * 1 code (byte as 0: unblock, 1:block): block, 1 string: spassword(or key)
33   */
34  public class BlockRequestPacket extends AbstractLocalPacket {
35    private final boolean block;
36    private final byte[] key;
37  
38    /**
39     * @param headerLength
40     * @param middleLength
41     * @param endLength
42     * @param buf
43     *
44     * @return the new ValidPacket from buffer
45     *
46     * @throws OpenR66ProtocolPacketException
47     */
48    public static BlockRequestPacket createFromBuffer(final int headerLength,
49                                                      final int middleLength,
50                                                      final int endLength,
51                                                      final ByteBuf buf)
52        throws OpenR66ProtocolPacketException {
53      if (headerLength - 2 <= 0) {
54        throw new OpenR66ProtocolPacketException("Not enough data");
55      }
56      final byte isblock = buf.readByte();
57      final byte[] bpassword = new byte[headerLength - 2];
58      buf.readBytes(bpassword);
59      final boolean block = isblock == 1;
60      return new BlockRequestPacket(block, bpassword);
61    }
62  
63    /**
64     * @param block
65     * @param spassword
66     */
67    public BlockRequestPacket(final boolean block, final byte[] spassword) {
68      this.block = block;
69      key = spassword;
70    }
71  
72    @Override
73    public final boolean hasGlobalBuffer() {
74      return true;
75    }
76  
77    @Override
78    public final synchronized void createAllBuffers(
79        final LocalChannelReference lcr, final int networkHeader) {
80      end = Unpooled.EMPTY_BUFFER;
81      middle = Unpooled.EMPTY_BUFFER;
82      final int globalSize = networkHeader + LOCAL_HEADER_SIZE + 1 + key.length;
83      final int offset = networkHeader + LOCAL_HEADER_SIZE;
84      global = ByteBufAllocator.DEFAULT.ioBuffer(globalSize, globalSize);
85      header = WaarpNettyUtil.slice(global, offset, 1 + key.length);
86      header.writeByte(block? 1 : 0);
87      header.writeBytes(key);
88    }
89  
90    @Override
91    public final String toString() {
92      return "BlockRequestPacket: " + block;
93    }
94  
95    @Override
96    public final byte getType() {
97      return LocalPacketFactory.BLOCKREQUESTPACKET;
98    }
99  
100   /**
101    * @return True if the request is to block new requests, else false
102    */
103   public final boolean getBlock() {
104     return block;
105   }
106 
107   /**
108    * @return the key
109    */
110   public final byte[] getKey() {
111     return key;
112   }
113 }