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  import openr66.protocol.utils.FileUtils;
25  
26  import org.jboss.netty.buffer.ChannelBuffer;
27  import org.jboss.netty.buffer.ChannelBuffers;
28  
29  /**
30   * Data packet
31   *
32   * header = packetRank middle = data
33   *
34   * @author frederic bregier
35   */
36  public class DataPacket extends AbstractLocalPacket {
37      private final int packetRank;
38  
39      private final int lengthPacket;
40  
41      private final ChannelBuffer data;
42  
43      private final ChannelBuffer key;
44  
45      /**
46       * @param headerLength
47       * @param middleLength
48       * @param endLength
49       * @param buf
50       * @return the new DataPacket from buffer
51       * @throws OpenR66ProtocolPacketException
52       */
53      public static DataPacket createFromBuffer(int headerLength,
54              int middleLength, int endLength, ChannelBuffer buf)
55              throws OpenR66ProtocolPacketException {
56          if (headerLength - 1 <= 0) {
57              throw new OpenR66ProtocolPacketException("Not enough data");
58          }
59          if (middleLength <= 0) {
60              throw new OpenR66ProtocolPacketException("Not enough data");
61          }
62          int packetRank = buf.readInt();
63  	ChannelBuffer data = buf.readBytes(middleLength);
64          int readerIndex = buf.readerIndex();
65          ChannelBuffer key;
66          if (endLength > 0) {
67              key = buf.slice(readerIndex, endLength);
68              buf.skipBytes(endLength);
69          } else {
70              key = ChannelBuffers.EMPTY_BUFFER;
71          }
72          return new DataPacket(packetRank, data, key);
73      }
74  
75      /**
76       * @param packetRank
77       * @param data
78       * @param key
79       */
80      public DataPacket(int packetRank, ChannelBuffer data, ChannelBuffer key) {
81          this.packetRank = packetRank;
82          this.data = data;
83          this.key = key == null? ChannelBuffers.EMPTY_BUFFER : key;
84          lengthPacket = data.readableBytes();
85      }
86  
87      /*
88       * (non-Javadoc)
89       *
90       * @see openr66.protocol.localhandler.packet.AbstractLocalPacket#createEnd()
91       */
92      @Override
93      public void createEnd() throws OpenR66ProtocolPacketException {
94          end = key;
95      }
96  
97      /*
98       * (non-Javadoc)
99       *
100      * @see
101      * openr66.protocol.localhandler.packet.AbstractLocalPacket#createHeader()
102      */
103     @Override
104     public void createHeader() throws OpenR66ProtocolPacketException {
105         header = ChannelBuffers.buffer(4);
106         header.writeInt(packetRank);
107     }
108 
109     /*
110      * (non-Javadoc)
111      *
112      * @see
113      * openr66.protocol.localhandler.packet.AbstractLocalPacket#createMiddle()
114      */
115     @Override
116     public void createMiddle() throws OpenR66ProtocolPacketException {
117         middle = data;
118     }
119 
120     @Override
121     public byte getType() {
122         return LocalPacketFactory.DATAPACKET;
123     }
124 
125     /*
126      * (non-Javadoc)
127      *
128      * @see openr66.protocol.localhandler.packet.AbstractLocalPacket#toString()
129      */
130     @Override
131     public String toString() {
132         return "DataPacket: " + packetRank + ":" + lengthPacket;
133     }
134 
135     /**
136      * @return the packetRank
137      */
138     public int getPacketRank() {
139         return packetRank;
140     }
141 
142     /**
143      * @return the lengthPacket
144      */
145     public int getLengthPacket() {
146         return lengthPacket;
147     }
148 
149     /**
150      * @return the data
151      */
152     public ChannelBuffer getData() {
153         return data;
154     }
155 
156     /**
157      * @return the key
158      */
159     public ChannelBuffer getKey() {
160         return key;
161     }
162 
163     /**
164      *
165      * @return True if the MD5 key is valid (or no key is set)
166      */
167     public boolean isKeyValid() {
168         if (key == null || key == ChannelBuffers.EMPTY_BUFFER) {
169             return true;
170         }
171         ChannelBuffer newbufkey = FileUtils.getHash(data);
172         return ChannelBuffers.equals(key, newbufkey);
173     }
174 }