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 org.waarp.openr66.protocol.exception.OpenR66ProtocolPacketException;
24  
25  import static org.waarp.openr66.protocol.localhandler.packet.AbstractLocalPacket.*;
26  
27  /**
28   * Factory to create Packet according to type from a buffer
29   */
30  public final class LocalPacketFactory {
31    public static final byte AUTHENTPACKET = 1;
32  
33    public static final byte STARTUPPACKET = 2;
34  
35    public static final byte DATAPACKET = 3;
36  
37    public static final byte VALIDPACKET = 4;
38  
39    public static final byte ERRORPACKET = 5;
40  
41    public static final byte CONNECTERRORPACKET = 6;
42  
43    public static final byte REQUESTPACKET = 7;
44  
45    public static final byte SHUTDOWNPACKET = 8;
46  
47    public static final byte STOPPACKET = 9;
48  
49    public static final byte CANCELPACKET = 10;
50  
51    public static final byte CONFEXPORTPACKET = 11;
52  
53    public static final byte CONFIMPORTPACKET = 12;
54  
55    public static final byte TESTPACKET = 13;
56  
57    public static final byte ENDTRANSFERPACKET = 14;
58  
59    public static final byte REQUESTUSERPACKET = 15;
60  
61    public static final byte LOGPACKET = 16;
62  
63    public static final byte LOGPURGEPACKET = 17;
64  
65    public static final byte INFORMATIONPACKET = 18;
66  
67    public static final byte BANDWIDTHPACKET = 19;
68  
69    public static final byte ENDREQUESTPACKET = 20;
70  
71    public static final byte KEEPALIVEPACKET = 21;
72    // New Protocol message => Extended protocol
73    public static final byte BUSINESSREQUESTPACKET = 22;
74  
75    public static final byte NOOPPACKET = 23;
76  
77    public static final byte BLOCKREQUESTPACKET = 24;
78  
79    public static final byte JSONREQUESTPACKET = 25;
80  
81    private LocalPacketFactory() {
82    }
83  
84    /**
85     * This method create a Packet from the ByteBuf.
86     *
87     * @param headerLength length of the header from the current
88     *     position of
89     *     the buffer
90     * @param middleLength
91     * @param endLength
92     * @param buf
93     *
94     * @return the newly created Packet
95     *
96     * @throws OpenR66ProtocolPacketException
97     */
98    public static AbstractLocalPacket createPacketFromByteBuf(
99        final int headerLength, final int middleLength, final int endLength,
100       final ByteBuf buf) throws OpenR66ProtocolPacketException {
101     final byte packetType = buf.readByte();
102     switch (packetType) {
103       case AUTHENTPACKET:
104         return AuthentPacket.createFromBuffer(headerLength, middleLength,
105                                               endLength, buf);
106       case STARTUPPACKET:
107         return StartupPacket.createFromBuffer(headerLength, middleLength,
108                                               endLength, buf);
109       case DATAPACKET:
110         return DataPacket.createFromBuffer(headerLength, middleLength,
111                                            endLength, buf);
112       case VALIDPACKET:
113         return ValidPacket.createFromBuffer(headerLength, middleLength,
114                                             endLength, buf);
115       case ERRORPACKET:
116         return ErrorPacket.createFromBuffer(headerLength, middleLength,
117                                             endLength, buf);
118       case CONNECTERRORPACKET:
119         return ConnectionErrorPacket.createFromBuffer(headerLength,
120                                                       middleLength, endLength,
121                                                       buf);
122       case REQUESTPACKET:
123         return RequestPacket.createFromBuffer(headerLength, middleLength,
124                                               endLength, buf);
125       case SHUTDOWNPACKET:
126         return ShutdownPacket.createFromBuffer(headerLength, middleLength,
127                                                endLength, buf);
128       case STOPPACKET:
129       case CANCELPACKET:
130       case REQUESTUSERPACKET:
131       case LOGPACKET:
132       case LOGPURGEPACKET:
133       case CONFEXPORTPACKET:
134       case CONFIMPORTPACKET:
135       case BANDWIDTHPACKET:
136         throw new OpenR66ProtocolPacketException(
137             "Unimplemented Packet Type received: " + packetType);
138       case TESTPACKET:
139         return TestPacket.createFromBuffer(headerLength, middleLength,
140                                            endLength, buf);
141       case ENDTRANSFERPACKET:
142         return EndTransferPacket.createFromBuffer(headerLength, middleLength,
143                                                   endLength, buf);
144       case INFORMATIONPACKET:
145         return InformationPacket.createFromBuffer(headerLength, middleLength,
146                                                   endLength, buf);
147       case ENDREQUESTPACKET:
148         return EndRequestPacket.createFromBuffer(headerLength, middleLength,
149                                                  endLength, buf);
150       case KEEPALIVEPACKET:
151         return KeepAlivePacket.createFromBuffer(headerLength, middleLength,
152                                                 endLength, buf);
153       case BUSINESSREQUESTPACKET:
154         return BusinessRequestPacket.createFromBuffer(headerLength,
155                                                       middleLength, endLength,
156                                                       buf);
157       case NOOPPACKET:
158         return NoOpPacket.createFromBuffer(headerLength, middleLength,
159                                            endLength, buf);
160       case BLOCKREQUESTPACKET:
161         return BlockRequestPacket.createFromBuffer(headerLength, middleLength,
162                                                    endLength, buf);
163       case JSONREQUESTPACKET:
164         return JsonCommandPacket.createFromBuffer(headerLength, middleLength,
165                                                   endLength, buf);
166       default:
167         throw new OpenR66ProtocolPacketException(
168             "Unvalid Packet Type received: " + packetType);
169     }
170   }
171 
172   public static int estimateSize(final Object o) {
173     if (!(o instanceof AbstractLocalPacket)) {
174       // Type unimplemented
175       return -1;
176     }
177     final AbstractLocalPacket packet = (AbstractLocalPacket) o;
178     return packet.header.readableBytes() + packet.middle.readableBytes() +
179            packet.end.readableBytes() + LOCAL_HEADER_SIZE;
180   }
181 
182 }