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