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.networkhandler.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  import org.waarp.openr66.protocol.localhandler.packet.AbstractLocalPacket;
29  import org.waarp.openr66.protocol.localhandler.packet.LocalPacketFactory;
30  
31  /**
32   * Network Packet A Packet is composed of one global length field, two Id (4
33   * bytes x 2) and a buffer. The
34   * first Id is the localId on receive operation and the remoteId on send
35   * operation. The second Id is the
36   * reverse.
37   */
38  public class NetworkPacket {
39    public static final int NETWORK_HEADER_SIZE = 4 * 3 + 1;
40    private ByteBuf buffer;
41  
42    private final int remoteId;
43  
44    private final int localId;
45  
46    private final byte code;
47  
48    private boolean uniqueBuffer;
49  
50    /**
51     * @param localId
52     * @param remoteId
53     * @param code
54     * @param buffer
55     */
56    public NetworkPacket(final int localId, final int remoteId, final byte code,
57                         final ByteBuf buffer) {
58      this.remoteId = remoteId;
59      this.localId = localId;
60      this.code = code;
61      this.buffer = buffer;
62      this.uniqueBuffer = false;
63    }
64  
65    /**
66     * @param localId
67     * @param remoteId
68     * @param packet
69     * @param lcr the LocalChannelReference in use
70     *
71     * @throws OpenR66ProtocolPacketException
72     */
73    public NetworkPacket(final int localId, final int remoteId,
74                         final AbstractLocalPacket packet,
75                         final LocalChannelReference lcr)
76        throws OpenR66ProtocolPacketException {
77      this.remoteId = remoteId;
78      this.localId = localId;
79      code = packet.getType();
80      uniqueBuffer = true;
81      buffer = packet.getLocalPacketForNetworkPacket(lcr, this);
82    }
83  
84    /**
85     * @return the buffer
86     */
87    public final ByteBuf getBuffer() {
88      return buffer;
89    }
90  
91    /**
92     * @return the remoteId
93     */
94    public final int getRemoteId() {
95      return remoteId;
96    }
97  
98    /**
99     * @return the localId
100    */
101   public final int getLocalId() {
102     return localId;
103   }
104 
105   /**
106    * @return the code
107    */
108   public final byte getCode() {
109     return code;
110   }
111 
112   public final void writeNetworkHeader(final ByteBuf buf, final int capacity) {
113     buf.writerIndex(0);
114     buf.writeInt(capacity + NETWORK_HEADER_SIZE - 4);
115     buf.writeInt(remoteId);
116     buf.writeInt(localId);
117     buf.writeByte(code);
118   }
119 
120   /**
121    * @return The corresponding ByteBuf
122    */
123   public ByteBuf getNetworkPacket() {
124     if (uniqueBuffer) {
125       writeNetworkHeader(buffer, buffer.capacity() - NETWORK_HEADER_SIZE);
126       buffer.writerIndex(buffer.capacity());
127       return buffer;
128     }
129     final ByteBuf buf = ByteBufAllocator.DEFAULT.ioBuffer(NETWORK_HEADER_SIZE,
130                                                           NETWORK_HEADER_SIZE);
131     writeNetworkHeader(buf, buffer.capacity());
132     buffer = ByteBufAllocator.DEFAULT.compositeDirectBuffer(2)
133                                      .addComponents(buf, buffer);
134     uniqueBuffer = true;
135     return buffer;
136   }
137 
138   @Override
139   public String toString() {
140     return "RId: " + remoteId + " LId: " + localId + " Code: " + code +
141            " Length: " + (buffer != null? (buffer.readableBytes() + (code ==
142                                                                      LocalPacketFactory.REQUESTPACKET?
143         buffer.toString(buffer.readerIndex(), buffer.readableBytes(),
144                         WaarpStringUtils.UTF8) : "")) : "no buffer");
145   }
146 
147   public final void clear() {
148     if (WaarpNettyUtil.release(buffer)) {
149       buffer = null;
150     }
151   }
152 }