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   * Test class for packet
30   * <p>
31   * 3 strings: sheader,smiddle,send
32   */
33  public class TestPacket extends AbstractLocalPacket {
34    public static final int PINGPONG = 100;
35  
36    private final String sheader;
37  
38    private final String smiddle;
39  
40    private int code;
41  
42    public static TestPacket createFromBuffer(final int headerLength,
43                                              final int middleLength,
44                                              final int endLength,
45                                              final ByteBuf buf) {
46      final byte[] bheader = new byte[headerLength - 1];
47      final byte[] bmiddle = new byte[middleLength];
48      if (headerLength - 1 > 0) {
49        buf.readBytes(bheader);
50      }
51      if (middleLength > 0) {
52        buf.readBytes(bmiddle);
53      }
54      return new TestPacket(new String(bheader, WaarpStringUtils.UTF8),
55                            new String(bmiddle, WaarpStringUtils.UTF8),
56                            buf.readInt());
57    }
58  
59    public TestPacket(final String header, final String middle, final int code) {
60      sheader = header;
61      smiddle = middle;
62      this.code = code;
63    }
64  
65    @Override
66    public final boolean hasGlobalBuffer() {
67      return true;
68    }
69  
70    @Override
71    public final synchronized void createAllBuffers(
72        final LocalChannelReference lcr, final int networkHeader) {
73      final byte[] headerBytes =
74          sheader != null? sheader.getBytes(WaarpStringUtils.UTF8) : EMPTY_ARRAY;
75      final int headerSize = headerBytes.length;
76      final byte[] middleBytes =
77          smiddle != null? smiddle.getBytes(WaarpStringUtils.UTF8) : EMPTY_ARRAY;
78      final int middleSize = middleBytes.length;
79      final int endSize = 4;
80      final int globalSize =
81          networkHeader + LOCAL_HEADER_SIZE + headerSize + middleSize + endSize;
82      int offset = networkHeader + LOCAL_HEADER_SIZE;
83      global = ByteBufAllocator.DEFAULT.ioBuffer(globalSize, globalSize);
84      header = WaarpNettyUtil.slice(global, offset, headerSize);
85      header.writeBytes(headerBytes);
86      offset += headerSize;
87      middle = WaarpNettyUtil.slice(global, offset, middleSize);
88      middle.writeBytes(middleBytes);
89      offset += middleSize;
90      end = WaarpNettyUtil.slice(global, offset, endSize);
91      end.writeInt(code);
92    }
93  
94    @Override
95    public final byte getType() {
96      if (code > PINGPONG) {
97        return LocalPacketFactory.VALIDPACKET;
98      }
99      return LocalPacketFactory.TESTPACKET;
100   }
101 
102   @Override
103   public final String toString() {
104     return "TestPacket: " + sheader + ':' + smiddle + ':' + code;
105   }
106 
107   public final synchronized void update() {
108     code++;
109     end = null;
110   }
111 }