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.common.utility.WaarpStringUtils;
27  import org.waarp.openr66.protocol.localhandler.LocalChannelReference;
28  
29  /**
30   * Connection Error Message class for packet
31   * <p>
32   * 2 strings: sheader,smiddle
33   */
34  public class ConnectionErrorPacket extends AbstractLocalPacket {
35  
36    private final String sheader;
37  
38    private final String smiddle;
39  
40    /**
41     * @param headerLength
42     * @param middleLength
43     * @param endLength
44     * @param buf
45     *
46     * @return the new ErrorPacket from buffer
47     */
48    public static ConnectionErrorPacket createFromBuffer(final int headerLength,
49                                                         final int middleLength,
50                                                         final int endLength,
51                                                         final ByteBuf buf) {
52      final byte[] bheader = new byte[headerLength - 1];
53      final byte[] bmiddle = new byte[middleLength];
54      if (headerLength - 1 > 0) {
55        buf.readBytes(bheader);
56      }
57      if (middleLength > 0) {
58        buf.readBytes(bmiddle);
59      }
60      return new ConnectionErrorPacket(new String(bheader, WaarpStringUtils.UTF8),
61                                       new String(bmiddle,
62                                                  WaarpStringUtils.UTF8));
63    }
64  
65    /**
66     * @param header
67     * @param middle
68     */
69    public ConnectionErrorPacket(final String header, final String middle) {
70      sheader = header;
71      smiddle = middle;
72    }
73  
74    @Override
75    public final boolean hasGlobalBuffer() {
76      return true;
77    }
78  
79    @Override
80    public final synchronized void createAllBuffers(
81        final LocalChannelReference lcr, final int networkHeader) {
82      end = Unpooled.EMPTY_BUFFER;
83      final byte[] sheaderByte =
84          sheader != null? sheader.getBytes(WaarpStringUtils.UTF8) : EMPTY_ARRAY;
85      final int headerSize = sheaderByte.length;
86      final byte[] smiddleByte =
87          smiddle != null? smiddle.getBytes(WaarpStringUtils.UTF8) : EMPTY_ARRAY;
88      final int middleSize = smiddleByte.length;
89      final int globalSize =
90          networkHeader + LOCAL_HEADER_SIZE + headerSize + middleSize;
91      int offset = networkHeader + LOCAL_HEADER_SIZE;
92      global = ByteBufAllocator.DEFAULT.ioBuffer(globalSize, globalSize);
93      header = WaarpNettyUtil.slice(global, offset, headerSize);
94      if (sheader != null) {
95        header.writeBytes(sheaderByte);
96      }
97      offset += headerSize;
98      middle = WaarpNettyUtil.slice(global, offset, middleSize);
99      if (smiddle != null) {
100       middle.writeBytes(smiddleByte);
101     }
102   }
103 
104   @Override
105   public final String toString() {
106     return "ConnectionErrorPacket: " + sheader + ':' + smiddle;
107   }
108 
109   @Override
110   public final byte getType() {
111     return LocalPacketFactory.CONNECTERRORPACKET;
112   }
113 
114   /**
115    * @return the sheader
116    */
117   public final String getSheader() {
118     return sheader;
119   }
120 
121   /**
122    * @return the smiddle
123    */
124   public final String getSmiddle() {
125     return smiddle;
126   }
127 }