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.exception.OpenR66ProtocolPacketException;
27  import org.waarp.openr66.protocol.localhandler.LocalChannelReference;
28  
29  /**
30   * Business Request Message class for packet
31   * <p>
32   * 1 string and on integer and one byte:<br>
33   * - sheader = full text with class at first place, following (space separated)
34   * by extra arguments - smiddle =
35   * integer - send = byte
36   */
37  public class BusinessRequestPacket extends AbstractLocalPacket {
38    private static final byte ASKVALIDATE = 0;
39  
40    private static final byte ANSWERVALIDATE = 1;
41    private static final byte ANSWERINVALIDATE = 2;
42  
43    private final String sheader;
44  
45    private int delay;
46  
47    private byte way;
48  
49    public static BusinessRequestPacket createFromBuffer(final int headerLength,
50                                                         final int middleLength,
51                                                         final int endLength,
52                                                         final ByteBuf buf)
53        throws OpenR66ProtocolPacketException {
54      final byte[] bheader = new byte[headerLength - 1];
55      if (headerLength - 1 > 0) {
56        buf.readBytes(bheader);
57      }
58      if (middleLength != 4) {
59        throw new OpenR66ProtocolPacketException("Packet not correct");
60      }
61      final int delay = buf.readInt();
62      if (endLength != 1) {
63        throw new OpenR66ProtocolPacketException("Packet not correct");
64      }
65      final byte valid = buf.readByte();
66      return new BusinessRequestPacket(new String(bheader, WaarpStringUtils.UTF8),
67                                       delay, valid);
68    }
69  
70    public BusinessRequestPacket(final String header, final int delay,
71                                 final byte way) {
72      sheader = header;
73      this.delay = delay;
74      this.way = way;
75    }
76  
77    public BusinessRequestPacket(final String header, final int delay) {
78      sheader = header;
79      this.delay = delay;
80      way = ASKVALIDATE;
81    }
82  
83    @Override
84    public final boolean hasGlobalBuffer() {
85      return true;
86    }
87  
88    @Override
89    public final synchronized void createAllBuffers(
90        final LocalChannelReference lcr, final int networkHeader) {
91      final byte[] headerBytes = sheader.getBytes(WaarpStringUtils.UTF8);
92      final int headerSize = headerBytes.length;
93      final int middleSize = 4;
94      final int endSize = 1;
95      final int globalSize =
96          networkHeader + LOCAL_HEADER_SIZE + headerSize + middleSize + endSize;
97      int offset = networkHeader + LOCAL_HEADER_SIZE;
98      global = ByteBufAllocator.DEFAULT.ioBuffer(globalSize, globalSize);
99      header = WaarpNettyUtil.slice(global, offset, headerSize);
100     header.writeBytes(headerBytes);
101     offset += headerSize;
102     middle = WaarpNettyUtil.slice(global, offset, middleSize);
103     middle.writeInt(delay);
104     offset += middleSize;
105     end = WaarpNettyUtil.slice(global, offset, endSize);
106     end.writeByte(way);
107     WaarpNettyUtil.retain(global);
108   }
109 
110   @Override
111   public final byte getType() {
112     return LocalPacketFactory.BUSINESSREQUESTPACKET;
113   }
114 
115   @Override
116   public final String toString() {
117     return "BusinessRequestPacket: " + sheader + ':' + delay + ':' + way;
118   }
119 
120   /**
121    * @return True if this packet is to be validated
122    */
123   public final boolean isToValidate() {
124     return way == ASKVALIDATE;
125   }
126 
127   /**
128    * Validate the request
129    */
130   public final void validate() {
131     way = ANSWERVALIDATE;
132     clear();
133   }
134 
135   /**
136    * Invalidate the request
137    */
138   public final void invalidate() {
139     way = ANSWERINVALIDATE;
140     clear();
141   }
142 
143   /**
144    * @return the sheader
145    */
146   public final String getSheader() {
147     return sheader;
148   }
149 
150   /**
151    * @return the delay
152    */
153   public final int getDelay() {
154     return delay;
155   }
156 
157   /**
158    * @param delay the delay to set
159    */
160   public final void setDelay(final int delay) {
161     this.delay = delay;
162     WaarpNettyUtil.release(middle);
163     middle = null;
164   }
165 }