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   * Test class for packet
30   * 
31   * 3 strings: sheader,smiddle,send
32   * 
33   * @author frederic bregier
34   */
35  public class TestPacket extends AbstractLocalPacket {
36      public static final int pingpong = 100;
37  
38      private final String sheader;
39  
40      private final String smiddle;
41  
42      private int code = 0;
43  
44      public static TestPacket createFromBuffer(int headerLength,
45              int middleLength, int endLength, ChannelBuffer buf) {
46          final byte[] bheader = new byte[headerLength - 1];
47          final byte[] bmiddle = new byte[middleLength];
48          if (headerLength - 1 > 0) {
49              buf.readBytes(bheader);
50          }
51          if (middleLength > 0) {
52              buf.readBytes(bmiddle);
53          }
54          return new TestPacket(new String(bheader), new String(bmiddle), buf
55                  .readInt());
56      }
57  
58      public TestPacket(String header, String middle, int code) {
59          sheader = header;
60          smiddle = middle;
61          this.code = code;
62      }
63  
64      /*
65       * (non-Javadoc)
66       * 
67       * @see openr66.protocol.localhandler.packet.AbstractLocalPacket#createEnd()
68       */
69      @Override
70      public void createEnd() throws OpenR66ProtocolPacketException {
71          end = ChannelBuffers.buffer(4);
72          end.writeInt(code);
73      }
74  
75      /*
76       * (non-Javadoc)
77       * 
78       * @see
79       * openr66.protocol.localhandler.packet.AbstractLocalPacket#createHeader()
80       */
81      @Override
82      public void createHeader() throws OpenR66ProtocolPacketException {
83          header = ChannelBuffers.wrappedBuffer(sheader.getBytes());
84      }
85  
86      /*
87       * (non-Javadoc)
88       * 
89       * @see
90       * openr66.protocol.localhandler.packet.AbstractLocalPacket#createMiddle()
91       */
92      @Override
93      public void createMiddle() throws OpenR66ProtocolPacketException {
94          middle = ChannelBuffers.wrappedBuffer(smiddle.getBytes());
95      }
96  
97      @Override
98      public byte getType() {
99          if (code > pingpong) {
100             return LocalPacketFactory.VALIDPACKET;
101         }
102         return LocalPacketFactory.TESTPACKET;
103     }
104 
105     /*
106      * (non-Javadoc)
107      * 
108      * @see openr66.protocol.localhandler.packet.AbstractLocalPacket#toString()
109      */
110     @Override
111     public String toString() {
112         return "TestPacket: " + sheader + ":" + smiddle + ":" + code;
113     }
114 
115     public void update() {
116         code ++;
117         end = null;
118     }
119 }