1 /*
2 * $HeadURL: /cvsroot/insight/Insight/remote-protocol/src/com/mindtree/insight/remoteprotocol/PacketDataSizeOverLimitsException.java,v $
3 * $Date: 2005/08/09 18:39:17 $
4 * $Revision: 1.1 $
5 * $Author: m1001025 $
6 *
7 * Copyright (c) 2005 MindTree Consulting Ltd.
8 *
9 * This file is part of Insight Remote Protocol Library.
10 *
11 * Insight Remote Protocol Library is free software: you can redistribute it
12 * and/or modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation, either version 3 of the License,
14 * or (at your option) any later version.
15 *
16 * Insight Remote Protocol Library is distributed in the hope that it will be
17 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
19 * Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along with
22 * Insight Remote Protocol Library. If not, see <http://www.gnu.org/licenses/>.
23 */
24
25 package com.mindtree.techworks.insight.remoteprotocol.spi;
26
27 import java.io.Serializable;
28
29
30 /**
31 * Represents a data packet sent to Insight. The packet concists of a header
32 * which in the Java world is the
33 * <code>{@link com.mindtree.techworks.insight.remoteprotocol.spi.PacketHeader PacketHeader}</code>
34 * class, and some content of the packet, which are represented by the
35 * subclasses of
36 * <code>{@link com.mindtree.techworks.insight.remoteprotocol.spi.PacketDataContent PacketDataContent}</code>.
37 * <p>
38 * The format of the data packet is described in the <code>PacketHeader</code>
39 * class.
40 * </p>
41 *
42 * @see com.mindtree.techworks.insight.remoteprotocol.spi.PacketHeader
43 * @see com.mindtree.techworks.insight.remoteprotocol.spi.PacketDataContent
44 * @author <a href="mailto:bindul_bhowmik@mindtree.com">Bindul Bhowmik</a>
45 * @version $Revision: 1.2 $ $Date: 2005/08/09 18:39:16 $
46 */
47 public class DataPacket implements Serializable {
48
49 // -------------------------------------------------------------------------
50 // Class variables
51 // -------------------------------------------------------------------------
52
53 /**
54 * Maximum number of bytes a packet should have.
55 */
56 public static final int MAX_PACKET_SIZE = 5120;
57
58 /**
59 * The Serial Version UID of the class.
60 */
61 private static final long serialVersionUID = 7180441160766839296L;
62
63 // -------------------------------------------------------------------------
64 // Instance variables
65 // -------------------------------------------------------------------------
66
67 /**
68 * The header of the packet. For a format of the header, see the
69 * PacketHeader class.
70 *
71 * @see PacketHeader
72 */
73 private PacketHeader packetHeader;
74
75 /**
76 * The data content of the packet.
77 */
78 private PacketDataContent packetDataContent;
79
80 // -------------------------------------------------------------------------
81 // Constructors
82 // -------------------------------------------------------------------------
83
84 /**
85 * Creates a DataPacket instance
86 *
87 * @param content The content of the packet
88 * @param header The header of the packet
89 */
90 public DataPacket (PacketDataContent content, PacketHeader header) {
91
92 packetDataContent = content;
93 packetHeader = header;
94 recalculatePacketHeader();
95 }
96
97 /**
98 * Creates an instance of DataPacket
99 */
100 public DataPacket () {
101
102 // No Args Constructor
103 }
104
105 // -------------------------------------------------------------------------
106 // Accessor Methods
107 // -------------------------------------------------------------------------
108
109 /**
110 * Returns the content of the packet
111 *
112 * @return The <code>PacketDataContent</code>
113 */
114 public PacketDataContent getPacketDataContent () {
115
116 return packetDataContent;
117 }
118
119 /**
120 * Sets the content of the packet
121 *
122 * @param packetDataContent The <code>PacketDataContent</code> to set
123 */
124 public void setPacketDataContent (PacketDataContent packetDataContent) {
125
126 this.packetDataContent = packetDataContent;
127 }
128
129 /**
130 * Returns the packet's header
131 *
132 * @return The instance's <code>PacketHeader</code>
133 */
134 public PacketHeader getPacketHeader () {
135
136 return packetHeader;
137 }
138
139 /**
140 * Sets the packet's header
141 *
142 * @param packetHeader The <code>PacketHeader</code> to set
143 */
144 public void setPacketHeader (PacketHeader packetHeader) {
145
146 this.packetHeader = packetHeader;
147 }
148
149 /**
150 * Returns the entire packet as a <code>byte</code> array.
151 * @return The contents in this packet as a byte array.
152 */
153 public byte[] getDataPacketAsByteArray() {
154 byte[] packetContents = new byte[packetHeader.getPacketHeaderLength() + packetDataContent.getLength()];
155 System.arraycopy(packetHeader.getPacketHeaderAsByteArray(), 0, packetContents, 0, packetHeader.getPacketHeaderLength());
156
157 if (packetDataContent.getLength() > 0) {
158 System.arraycopy(packetDataContent.getPacketData(), 0, packetContents, packetHeader.getPacketHeaderLength(), packetDataContent.getLength());
159 }
160
161 return packetContents;
162 }
163
164 // -------------------------------------------------------------------------
165 // Public Methods
166 // -------------------------------------------------------------------------
167
168 /**
169 * Recalculates the packet's data relative content. In particular the
170 * following fields are recalculated and set:
171 * <ol>
172 * <li>DATA LENGTH</li>
173 * <li>HEADER CHECKSUM</li>
174 * </ol>
175 *
176 * @see PacketHeader#recalculateHeader(PacketDataContent)
177 */
178 public void recalculatePacketHeader () {
179
180 this.packetHeader.recalculateHeader(this.packetDataContent);
181 }
182
183 }