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.networkhandler.packet;
22
23 import openr66.protocol.exception.OpenR66ProtocolPacketException;
24 import openr66.protocol.localhandler.packet.AbstractLocalPacket;
25
26 import org.jboss.netty.buffer.ChannelBuffer;
27 import org.jboss.netty.buffer.ChannelBuffers;
28
29 /**
30 * Network Packet A Packet is composed of one global length field, two Id (4
31 * bytes x 2) and a buffer. The first Id is the localId on receive operation and
32 * the remoteId on send operation. The second Id is the reverse.
33 *
34 * @author Frederic Bregier
35 */
36 public class NetworkPacket {
37 private final ChannelBuffer buffer;
38
39 private final int remoteId;
40
41 private final int localId;
42
43 private final byte code;
44
45 /**
46 * @param localId
47 * @param remoteId
48 * @param code
49 * @param buffer
50 */
51 public NetworkPacket(int localId, int remoteId, byte code,
52 ChannelBuffer buffer) {
53 this.remoteId = remoteId;
54 this.localId = localId;
55 this.code = code;
56 this.buffer = buffer;
57 }
58
59 /**
60 * @param localId
61 * @param remoteId
62 * @param packet
63 * @throws OpenR66ProtocolPacketException
64 */
65 public NetworkPacket(int localId, int remoteId, AbstractLocalPacket packet)
66 throws OpenR66ProtocolPacketException {
67 this.remoteId = remoteId;
68 this.localId = localId;
69 code = packet.getType();
70 buffer = packet.getLocalPacket();
71 }
72
73 /**
74 * @return the buffer
75 */
76 public ChannelBuffer getBuffer() {
77 return buffer;
78 }
79
80 /**
81 * @return the remoteId
82 */
83 public int getRemoteId() {
84 return remoteId;
85 }
86
87 /**
88 * @return the localId
89 */
90 public int getLocalId() {
91 return localId;
92 }
93
94 /**
95 * @return the code
96 */
97 public byte getCode() {
98 return code;
99 }
100
101 /**
102 * @return The corresponding ChannelBuffer
103 */
104 public ChannelBuffer getNetworkPacket() {
105 final ChannelBuffer buf = ChannelBuffers.dynamicBuffer(13);
106 buf.writeInt(buffer.readableBytes() + 9);
107 buf.writeInt(remoteId);
108 buf.writeInt(localId);
109 buf.writeByte(code);
110 return ChannelBuffers.wrappedBuffer(buf, buffer);
111 }
112
113 @Override
114 public String toString() {
115 return "RId: " + remoteId + " LId: " + localId + " Code: " + code +
116 " Length: " + buffer.readableBytes();
117 }
118
119 }