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