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  import org.jboss.netty.buffer.ChannelBuffers;
27  
28  /**
29   * This class represents Abstract Packet with its header, middle and end parts.
30   * A Packet is composed of one Header part, one Middle part (data), and one End
31   * part. Header: length field (4 bytes) = Middle length field (4 bytes), End
32   * length field (4 bytes), type field (1 byte), ...<br>
33   * Middle: (Middle length field bytes)<br>
34   * End: (End length field bytes) = code status field (4 bytes), ...<br>
35   *
36   * @author frederic bregier
37   */
38  public abstract class AbstractLocalPacket {
39      protected ChannelBuffer header;
40  
41      protected ChannelBuffer middle;
42  
43      protected ChannelBuffer end;
44  
45      public AbstractLocalPacket(ChannelBuffer header, ChannelBuffer middle,
46              ChannelBuffer end) {
47          this.header = header;
48          this.middle = middle;
49          this.end = end;
50      }
51  
52      public AbstractLocalPacket() {
53          header = null;
54          middle = null;
55          end = null;
56      }
57      /**
58       * Prepare the Header buffer
59       * @throws OpenR66ProtocolPacketException
60       */
61      public abstract void createHeader() throws OpenR66ProtocolPacketException;
62      /**
63       * Prepare the Middle buffer
64       * @throws OpenR66ProtocolPacketException
65       */
66      public abstract void createMiddle() throws OpenR66ProtocolPacketException;
67      /**
68       * Prepare the End buffer
69       * @throws OpenR66ProtocolPacketException
70       */
71      public abstract void createEnd() throws OpenR66ProtocolPacketException;
72      /**
73       *
74       * @return the type of Packet
75       */
76      public abstract byte getType();
77  
78      @Override
79      public abstract String toString();
80      /**
81       *
82       * @return the ChannelBuffer as LocalPacket
83       * @throws OpenR66ProtocolPacketException
84       */
85      public ChannelBuffer getLocalPacket() throws OpenR66ProtocolPacketException {
86          final ChannelBuffer buf = ChannelBuffers.buffer(4 * 3 + 1);// 3 header
87          // lengths+type
88          if (header == null) {
89              createHeader();
90          }
91          final ChannelBuffer newHeader = header != null? header
92                  : ChannelBuffers.EMPTY_BUFFER;
93          final int headerLength = 4 * 2 + 1 + newHeader.readableBytes();
94          if (middle == null) {
95              createMiddle();
96          }
97          final ChannelBuffer newMiddle = middle != null? middle
98                  : ChannelBuffers.EMPTY_BUFFER;
99          final int middleLength = newMiddle.readableBytes();
100         if (end == null) {
101             createEnd();
102         }
103         final ChannelBuffer newEnd = end != null? end
104                 : ChannelBuffers.EMPTY_BUFFER;
105         final int endLength = newEnd.readableBytes();
106         buf.writeInt(headerLength);
107         buf.writeInt(middleLength);
108         buf.writeInt(endLength);
109         buf.writeByte(getType());
110         final ChannelBuffer channelBuffer = ChannelBuffers.wrappedBuffer(
111                 buf, newHeader, newMiddle, newEnd);
112         return channelBuffer;
113     }
114 }