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   * Validation Message class for packet
30   * 
31   * 2 strings and one byte: sheader,smiddle,send
32   * 
33   * @author frederic bregier
34   */
35  public class ValidPacket extends AbstractLocalPacket {
36      private final String sheader;
37  
38      private String smiddle;
39  
40      private final byte send;
41  
42      /**
43       * @param headerLength
44       * @param middleLength
45       * @param endLength
46       * @param buf
47       * @return the new ValidPacket from buffer
48       */
49      public static ValidPacket createFromBuffer(int headerLength,
50              int middleLength, int endLength, ChannelBuffer buf) {
51          final byte[] bheader = new byte[headerLength - 1];
52          final byte[] bmiddle = new byte[middleLength];
53          final byte bend;
54          if (headerLength - 1 > 0) {
55              buf.readBytes(bheader);
56          }
57          if (middleLength > 0) {
58              buf.readBytes(bmiddle);
59          }
60          bend = buf.readByte();
61          return new ValidPacket(new String(bheader), new String(bmiddle), bend);
62      }
63  
64      /**
65       * @param header
66       * @param middle
67       * @param end
68       */
69      public ValidPacket(String header, String middle, byte end) {
70          sheader = header;
71          smiddle = middle;
72          send = end;
73      }
74  
75      /*
76       * (non-Javadoc)
77       * 
78       * @see openr66.protocol.localhandler.packet.AbstractLocalPacket#createEnd()
79       */
80      @Override
81      public void createEnd() throws OpenR66ProtocolPacketException {
82          end = ChannelBuffers.buffer(1);
83          end.writeByte(send);
84      }
85  
86      /*
87       * (non-Javadoc)
88       * 
89       * @see
90       * openr66.protocol.localhandler.packet.AbstractLocalPacket#createHeader()
91       */
92      @Override
93      public void createHeader() throws OpenR66ProtocolPacketException {
94          if (sheader != null) {
95              header = ChannelBuffers.wrappedBuffer(sheader.getBytes());
96          }
97      }
98  
99      /*
100      * (non-Javadoc)
101      * 
102      * @see
103      * openr66.protocol.localhandler.packet.AbstractLocalPacket#createMiddle()
104      */
105     @Override
106     public void createMiddle() throws OpenR66ProtocolPacketException {
107         if (smiddle != null) {
108             middle = ChannelBuffers.wrappedBuffer(smiddle.getBytes());
109         }
110     }
111 
112     /*
113      * (non-Javadoc)
114      * 
115      * @see openr66.protocol.localhandler.packet.AbstractLocalPacket#toString()
116      */
117     @Override
118     public String toString() {
119         return "ValidPacket: " + sheader + ":" + smiddle + ":" + send;
120     }
121 
122     @Override
123     public byte getType() {
124         return LocalPacketFactory.VALIDPACKET;
125     }
126 
127     /**
128      * @return the sheader
129      */
130     public String getSheader() {
131         return sheader;
132     }
133 
134     /**
135      * @return the smiddle
136      */
137     public String getSmiddle() {
138         return smiddle;
139     }
140 
141     /**
142      * 
143      * @param smiddle
144      */
145     public void setSmiddle(String smiddle) {
146         this.smiddle = smiddle;
147         middle = null;
148     }
149 
150     /**
151      * @return the type
152      */
153     public byte getTypeValid() {
154         return send;
155     }
156 
157 }