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