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 org.waarp.common.utility.WaarpNettyUtil;
25  import org.waarp.common.utility.WaarpStringUtils;
26  import org.waarp.openr66.protocol.localhandler.LocalChannelReference;
27  
28  /**
29   * Validation Message class for packet
30   * <p>
31   * 2 strings and one byte: sheader,smiddle,send
32   */
33  public class ValidPacket extends AbstractLocalPacket {
34    private final String sheader;
35  
36    private String smiddle;
37  
38    private final byte send;
39  
40    /**
41     * @param headerLength
42     * @param middleLength
43     * @param endLength
44     * @param buf
45     *
46     * @return the new ValidPacket from buffer
47     */
48    public static ValidPacket 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      final byte bend;
55      if (headerLength - 1 > 0) {
56        buf.readBytes(bheader);
57      }
58      if (middleLength > 0) {
59        buf.readBytes(bmiddle);
60      }
61      bend = buf.readByte();
62      return new ValidPacket(new String(bheader, WaarpStringUtils.UTF8),
63                             new String(bmiddle, WaarpStringUtils.UTF8), bend);
64    }
65  
66    /**
67     * @param header
68     * @param middle
69     * @param end
70     */
71    public ValidPacket(final String header, final String middle, final byte end) {
72      sheader = header;
73      smiddle = middle;
74      send = end;
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 byte[] headerBytes =
86          sheader != null? sheader.getBytes(WaarpStringUtils.UTF8) : EMPTY_ARRAY;
87      final int headerSize = headerBytes.length;
88      final byte[] middleBytes =
89          smiddle != null? smiddle.getBytes(WaarpStringUtils.UTF8) : EMPTY_ARRAY;
90      final int middleSize = middleBytes.length;
91      final int endSize = 1;
92      final int globalSize =
93          networkHeader + LOCAL_HEADER_SIZE + headerSize + middleSize + endSize;
94      int offset = networkHeader + LOCAL_HEADER_SIZE;
95      global = ByteBufAllocator.DEFAULT.ioBuffer(globalSize, globalSize);
96      header = WaarpNettyUtil.slice(global, offset, headerSize);
97      header.writeBytes(headerBytes);
98      offset += headerSize;
99      middle = WaarpNettyUtil.slice(global, offset, middleSize);
100     middle.writeBytes(middleBytes);
101     offset += middleSize;
102     end = WaarpNettyUtil.slice(global, offset, endSize);
103     end.writeByte(send);
104   }
105 
106   @Override
107   public final String toString() {
108     return "ValidPacket: " + sheader + ':' + smiddle + ':' + send;
109   }
110 
111   @Override
112   public final byte getType() {
113     return LocalPacketFactory.VALIDPACKET;
114   }
115 
116   /**
117    * @return the sheader
118    */
119   public final String getSheader() {
120     return sheader;
121   }
122 
123   /**
124    * @return the smiddle
125    */
126   public final synchronized String getSmiddle() {
127     return smiddle;
128   }
129 
130   /**
131    * @param smiddle
132    */
133   public final synchronized void setSmiddle(final String smiddle) {
134     this.smiddle = smiddle;
135     middle = null;
136   }
137 
138   /**
139    * @return the type
140    */
141   public final byte getTypeValid() {
142     return send;
143   }
144 
145 }