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 java.nio.charset.Charset;
24  
25  import openr66.protocol.exception.OpenR66ProtocolPacketException;
26  
27  import org.jboss.netty.buffer.ChannelBuffer;
28  import org.jboss.netty.buffer.ChannelBuffers;
29  
30  /**
31   * End of Request class
32   *
33   * header = Error.code middle = way end = might be empty
34   *
35   * @author frederic bregier
36   */
37  public class EndRequestPacket extends AbstractLocalPacket {
38      private static final byte ASKVALIDATE = 0;
39  
40      private static final byte ANSWERVALIDATE = 1;
41  
42      private final int code;
43  
44      private byte way;
45      
46      private String optional;
47  
48      /**
49       * @param headerLength
50       * @param middleLength
51       * @param endLength
52       * @param buf
53       * @return the new EndTransferPacket from buffer
54       * @throws OpenR66ProtocolPacketException
55       */
56      public static EndRequestPacket createFromBuffer(int headerLength,
57              int middleLength, int endLength, ChannelBuffer buf)
58              throws OpenR66ProtocolPacketException {
59          if (headerLength - 1 != 4) {
60              throw new OpenR66ProtocolPacketException("Not enough data");
61          }
62          if (middleLength != 1) {
63              throw new OpenR66ProtocolPacketException("Not enough data");
64          }
65          final int bheader = buf.readInt();
66          byte valid = buf.readByte();
67          String optional;
68          if (endLength > 0) {
69              optional = buf.toString(buf.readerIndex(), endLength, Charset.defaultCharset());
70              buf.skipBytes(endLength);
71              return new EndRequestPacket(bheader, valid, optional);
72          }
73          return new EndRequestPacket(bheader, valid);
74      }
75  
76  
77      /**
78       * @param code
79       * @param valid
80       * @param optional
81       */
82      private EndRequestPacket(int code, byte valid, String optional) {
83          this.code = code;
84          way = valid;
85          this.optional = optional;
86      }
87  
88      /**
89       * @param code
90       * @param valid
91       */
92      private EndRequestPacket(int code, byte valid) {
93          this.code = code;
94          way = valid;
95      }
96  
97      /**
98       * @param code
99       */
100     public EndRequestPacket(int code) {
101         this.code = code;
102         way = ASKVALIDATE;
103     }
104 
105     /*
106      * (non-Javadoc)
107      *
108      * @see openr66.protocol.localhandler.packet.AbstractLocalPacket#createEnd()
109      */
110     @Override
111     public void createEnd() {
112         if (optional == null) {
113             end = ChannelBuffers.EMPTY_BUFFER;
114         } else {
115             end = ChannelBuffers.copiedBuffer(optional, Charset.defaultCharset());
116         }
117     }
118 
119     /*
120      * (non-Javadoc)
121      *
122      * @see
123      * openr66.protocol.localhandler.packet.AbstractLocalPacket#createHeader()
124      */
125     @Override
126     public void createHeader() {
127         header = ChannelBuffers.buffer(4);
128         header.writeInt(code);
129     }
130 
131     /*
132      * (non-Javadoc)
133      *
134      * @see
135      * openr66.protocol.localhandler.packet.AbstractLocalPacket#createMiddle()
136      */
137     @Override
138     public void createMiddle() {
139         byte[] newbytes = {
140             way };
141         middle = ChannelBuffers.wrappedBuffer(newbytes);
142     }
143 
144     @Override
145     public byte getType() {
146         return LocalPacketFactory.ENDREQUESTPACKET;
147     }
148 
149     /*
150      * (non-Javadoc)
151      *
152      * @see openr66.protocol.localhandler.packet.AbstractLocalPacket#toString()
153      */
154     @Override
155     public String toString() {
156         return "EndRequestPacket: " + code + " " + way + (optional != null ? " "+optional : "");
157     }
158 
159     /**
160      * @return the code
161      */
162     public int getCode() {
163         return code;
164     }
165 
166     /**
167      * @return True if this packet is to be validated
168      */
169     public boolean isToValidate() {
170         return way == ASKVALIDATE;
171     }
172 
173     /**
174      * Validate the connection
175      */
176     public void validate() {
177         way = ANSWERVALIDATE;
178         header = null;
179         middle = null;
180         end = null;
181     }
182 
183 
184     /**
185      * @return the optional
186      */
187     public String getOptional() {
188         return optional;
189     }
190 
191 
192     /**
193      * @param optional the optional to set
194      */
195     public void setOptional(String optional) {
196         this.optional = optional;
197     }
198     
199 }