View Javadoc

1   /**
2      This file is part of GoldenGate Project (named also GoldenGate or GG).
3   
4      Copyright 2009, Frederic Bregier, 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 GoldenGate Project is free software: you can redistribute it and/or 
9      modify it under the terms of the GNU General Public License as published 
10     by the Free Software Foundation, either version 3 of the License, or
11     (at your option) any later version.
12  
13     GoldenGate is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17  
18     You should have received a copy of the GNU General Public License
19     along with GoldenGate .  If not, see <http://www.gnu.org/licenses/>.
20   */
21  package openr66.protocol.localhandler.packet;
22  
23  import openr66.protocol.exception.OpenR66ProtocolPacketException;
24  
25  import org.jboss.netty.buffer.ChannelBuffer;
26  
27  /**
28   * Factory to create Packet according to type from a buffer
29   *
30   * @author Frederic Bregier
31   */
32  public class LocalPacketFactory {
33      public static final byte AUTHENTPACKET = 1;
34  
35      public static final byte STARTUPPACKET = 2;
36  
37      public static final byte DATAPACKET = 3;
38  
39      public static final byte VALIDPACKET = 4;
40  
41      public static final byte ERRORPACKET = 5;
42  
43      public static final byte CONNECTERRORPACKET = 6;
44  
45      public static final byte REQUESTPACKET = 7;
46  
47      public static final byte SHUTDOWNPACKET = 8;
48  
49      public static final byte STOPPACKET = 9;
50  
51      public static final byte CANCELPACKET = 10;
52  
53      public static final byte CONFEXPORTPACKET = 11;
54  
55      public static final byte CONFIMPORTPACKET = 12;
56  
57      public static final byte TESTPACKET = 13;
58  
59      public static final byte ENDTRANSFERPACKET = 14;
60  
61      public static final byte REQUESTUSERPACKET = 15;
62  
63      public static final byte LOGPACKET = 16;
64  
65      public static final byte LOGPURGEPACKET = 17;
66  
67      public static final byte INFORMATIONPACKET = 18;
68  
69      public static final byte BANDWIDTHPACKET = 19;
70  
71      public static final byte ENDREQUESTPACKET = 20;
72  
73      public static final byte KEEPALIVEPACKET = 21;
74      // New Protocol message => Extended protocol
75      public static final byte BUSINESSREQUESTPACKET = 22;
76      
77      public static final byte NOOPPACKET = 23;
78  
79      /**
80       * This method create a Packet from the ChannelBuffer.
81       *
82       * @param headerLength
83       *            length of the header from the current position of the buffer
84       * @param middleLength
85       * @param endLength
86       * @param buf
87       * @return the newly created Packet
88       * @throws OpenR66ProtocolPacketException
89       */
90      public static AbstractLocalPacket createPacketFromChannelBuffer(
91              int headerLength, int middleLength, int endLength, ChannelBuffer buf)
92              throws OpenR66ProtocolPacketException {
93          final byte packetType = buf.readByte();
94          switch (packetType) {
95              case AUTHENTPACKET:
96                  return AuthentPacket.createFromBuffer(headerLength,
97                          middleLength, endLength, buf);
98              case STARTUPPACKET:
99                  return StartupPacket.createFromBuffer(headerLength,
100                         middleLength, endLength, buf);
101             case DATAPACKET:
102                 return DataPacket.createFromBuffer(headerLength, middleLength,
103                         endLength, buf);
104             case VALIDPACKET:
105                 return ValidPacket.createFromBuffer(headerLength, middleLength,
106                         endLength, buf);
107             case ERRORPACKET:
108                 return ErrorPacket.createFromBuffer(headerLength, middleLength,
109                         endLength, buf);
110             case CONNECTERRORPACKET:
111                 return ConnectionErrorPacket.createFromBuffer(headerLength,
112                         middleLength, endLength, buf);
113             case REQUESTPACKET:
114                 return RequestPacket.createFromBuffer(headerLength,
115                         middleLength, endLength, buf);
116             case SHUTDOWNPACKET:
117                 return ShutdownPacket.createFromBuffer(headerLength,
118                         middleLength, endLength, buf);
119             case STOPPACKET:
120             case CANCELPACKET:
121             case REQUESTUSERPACKET:
122             case LOGPACKET:
123             case LOGPURGEPACKET:
124             case CONFEXPORTPACKET:
125             case CONFIMPORTPACKET:
126             case BANDWIDTHPACKET:
127                 throw new OpenR66ProtocolPacketException(
128                         "Unimplemented Packet Type received: " + packetType);
129             case TESTPACKET:
130                 return TestPacket.createFromBuffer(headerLength, middleLength,
131                         endLength, buf);
132             case ENDTRANSFERPACKET:
133                 return EndTransferPacket.createFromBuffer(headerLength,
134                         middleLength, endLength, buf);
135             case INFORMATIONPACKET:
136                 return InformationPacket.createFromBuffer(headerLength,
137                         middleLength, endLength, buf);
138             case ENDREQUESTPACKET:
139                 return EndRequestPacket.createFromBuffer(headerLength,
140                         middleLength, endLength, buf);
141             case KEEPALIVEPACKET:
142                 return KeepAlivePacket.createFromBuffer(headerLength,
143                         middleLength, endLength, buf);
144             case BUSINESSREQUESTPACKET:
145                 return BusinessRequestPacket.createFromBuffer(headerLength,
146                         middleLength, endLength, buf);
147             case NOOPPACKET:
148                 return NoOpPacket.createFromBuffer(headerLength, 
149                         middleLength, endLength, buf);
150             default:
151                 throw new OpenR66ProtocolPacketException(
152                         "Unvalid Packet Type received: " + packetType);
153         }
154     }
155 }