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.localhandler.LocalChannelReference;
27  
28  /**
29   * Startup Message class
30   * <p>
31   * 1 localId (Integer): localId
32   */
33  public class StartupPacket extends AbstractLocalPacket {
34    private final Integer localId;
35    private final boolean fromSsl;
36  
37    /**
38     * @param headerLength
39     * @param middleLength
40     * @param endLength
41     * @param buf
42     *
43     * @return the new ValidPacket from buffer
44     */
45    public static StartupPacket createFromBuffer(final int headerLength,
46                                                 final int middleLength,
47                                                 final int endLength,
48                                                 final ByteBuf buf) {
49      final Integer newId = buf.readInt();
50      final boolean fromSsl = buf.readBoolean();
51      return new StartupPacket(newId, fromSsl);
52    }
53  
54    /**
55     * @param newId
56     */
57    public StartupPacket(final Integer newId, final boolean fromSsl) {
58      localId = newId;
59      this.fromSsl = fromSsl;
60    }
61  
62    @Override
63    public final boolean hasGlobalBuffer() {
64      return true;
65    }
66  
67    @Override
68    public final synchronized void createAllBuffers(
69        final LocalChannelReference lcr, final int networkHeader) {
70      final int headerSize = 4;
71      final int middleSize = 1;
72      final int endSize = 0;
73      final int globalSize =
74          networkHeader + LOCAL_HEADER_SIZE + headerSize + middleSize + endSize;
75      int offset = networkHeader + LOCAL_HEADER_SIZE;
76      global = ByteBufAllocator.DEFAULT.ioBuffer(globalSize, globalSize);
77      header = WaarpNettyUtil.slice(global, offset, headerSize);
78      header.writeInt(localId);
79      offset += headerSize;
80      middle = WaarpNettyUtil.slice(global, offset, middleSize);
81      middle.writeBoolean(fromSsl);
82      end = Unpooled.EMPTY_BUFFER;
83    }
84  
85    @Override
86    public final String toString() {
87      return "StartupPacket: " + localId;
88    }
89  
90    @Override
91    public final byte getType() {
92      return LocalPacketFactory.STARTUPPACKET;
93    }
94  
95    /**
96     * @return the localId
97     */
98    public final Integer getLocalId() {
99      return localId;
100   }
101 
102   public final boolean isFromSsl() {
103     return fromSsl;
104   }
105 
106 }