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   * Business Request Message class for packet
30   * 
31   * 1 string and on integer and one byte:<br> 
32   * - sheader = full text with class at first place
33   * - smiddle = integer
34   * - send = byte
35   * 
36   * @author frederic bregier
37   */
38  public class BusinessRequestPacket extends AbstractLocalPacket {
39      private static final byte ASKVALIDATE = 0;
40  
41      private static final byte ANSWERVALIDATE = 1;
42      private static final byte ANSWERINVALIDATE = 2;
43  
44      private String sheader;
45  
46      private int delay = 0;
47      
48      private byte way;
49  
50      public static BusinessRequestPacket createFromBuffer(int headerLength,
51              int middleLength, int endLength, ChannelBuffer buf) throws OpenR66ProtocolPacketException {
52          final byte[] bheader = new byte[headerLength - 1];
53          if (headerLength - 1 > 0) {
54              buf.readBytes(bheader);
55          }
56          if (middleLength != 4) {
57              throw new OpenR66ProtocolPacketException("Packet not correct");
58          }
59          int delay = buf.readInt();
60          if (endLength != 1) {
61              throw new OpenR66ProtocolPacketException("Packet not correct");
62          }
63          byte valid = buf.readByte();
64          return new BusinessRequestPacket(new String(bheader), delay, valid);
65      }
66  
67      public BusinessRequestPacket(String header, int delay, byte way) {
68          this.sheader = header;
69          this.delay = delay;
70          this.way = way;
71      }
72      
73      public BusinessRequestPacket(String header, int delay) {
74          this.sheader = header;
75          this.delay = delay;
76          this.way = ASKVALIDATE;
77      }
78  
79      /*
80       * (non-Javadoc)
81       * 
82       * @see openr66.protocol.localhandler.packet.AbstractLocalPacket#createEnd()
83       */
84      @Override
85      public void createEnd() throws OpenR66ProtocolPacketException {
86          end = ChannelBuffers.buffer(1);
87          end.writeByte(way);
88      }
89  
90      /*
91       * (non-Javadoc)
92       * 
93       * @see
94       * openr66.protocol.localhandler.packet.AbstractLocalPacket#createHeader()
95       */
96      @Override
97      public void createHeader() throws OpenR66ProtocolPacketException {
98          header = ChannelBuffers.wrappedBuffer(sheader.getBytes());
99      }
100 
101     /*
102      * (non-Javadoc)
103      * 
104      * @see
105      * openr66.protocol.localhandler.packet.AbstractLocalPacket#createMiddle()
106      */
107     @Override
108     public void createMiddle() throws OpenR66ProtocolPacketException {
109         middle = ChannelBuffers.buffer(4);
110         middle.writeInt(delay);
111     }
112 
113     @Override
114     public byte getType() {
115         return LocalPacketFactory.BUSINESSREQUESTPACKET;
116     }
117 
118     /*
119      * (non-Javadoc)
120      * 
121      * @see openr66.protocol.localhandler.packet.AbstractLocalPacket#toString()
122      */
123     @Override
124     public String toString() {
125         return "BusinessRequestPacket: " + sheader + ":" + delay+":"+way;
126     }
127 
128     /**
129      * @return True if this packet is to be validated
130      */
131     public boolean isToValidate() {
132         return way == ASKVALIDATE;
133     }
134 
135     /**
136      * Validate the request
137      */
138     public void validate() {
139         way = ANSWERVALIDATE;
140         header = null;
141         middle = null;
142         end = null;
143     }
144     /**
145      * Invalidate the request
146      */
147     public void invalidate() {
148         way = ANSWERINVALIDATE;
149         header = null;
150         middle = null;
151         end = null;
152     }
153 
154     /**
155      * @return the sheader
156      */
157     public String getSheader() {
158         return sheader;
159     }
160 
161     /**
162      * @return the delay
163      */
164     public int getDelay() {
165         return delay;
166     }
167     /**
168      * @param delay the delay to set
169      */
170     public void setDelay(int delay) {
171         this.delay = delay;
172         middle = null;
173     }
174 }