1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
31
32
33
34
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
47
48
49
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
61
62
63
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
75
76 public ChannelBuffer getBuffer() {
77 return buffer;
78 }
79
80
81
82
83 public int getRemoteId() {
84 return remoteId;
85 }
86
87
88
89
90 public int getLocalId() {
91 return localId;
92 }
93
94
95
96
97 public byte getCode() {
98 return code;
99 }
100
101
102
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 }