2 * Copyright (c) 2003 University of Murcia. All rights reserved.
\r
3 * --------------------------------------------------------------
\r
4 * For more information, please see <http://www.umu.euro6ix.org/>.
\r
7 package org.umu.cops.stack;
\r
9 import java.io.IOException;
\r
10 import java.io.OutputStream;
\r
11 import java.net.Socket;
\r
14 * COPS Keep Alive Message
\r
16 * @version COPSKAMsg.java, v 1.00 2003
\r
19 public class COPSKAMsg extends COPSMsg {
\r
21 /* COPSHeader coming from base class */
\r
22 private COPSIntegrity _integrity;
\r
24 public COPSKAMsg() {
\r
28 protected COPSKAMsg(byte[] data) throws COPSException {
\r
33 /** Checks the sanity of COPS message and throw an
\r
34 * COPSBadDataException when data is bad.
\r
36 public void checkSanity() throws COPSException {
\r
37 //The client type in the header MUST always be set to 0
\r
38 //as KA is used for connection verification.RFC 2748
\r
39 if ((_hdr == null) && (_hdr.getClientType() != 0))
\r
40 throw new COPSException("Bad message format");
\r
44 * Add message header
\r
46 * @param hdr a COPSHeader
\r
48 * @throws COPSException
\r
51 public void add (COPSHeader hdr) throws COPSException {
\r
53 throw new COPSException ("Null Header");
\r
54 if (hdr.getOpCode() != COPSHeader.COPS_OP_KA)
\r
55 throw new COPSException ("Error Header (no COPS_OP_KA)");
\r
61 * Add Integrity objects
\r
63 * @param integrity a COPSIntegrity
\r
65 * @throws COPSException
\r
68 public void add (COPSIntegrity integrity) throws COPSException {
\r
69 if (integrity == null)
\r
70 throw new COPSException ("Null Integrity");
\r
71 if (!integrity.isMessageIntegrity())
\r
72 throw new COPSException ("Error Integrity");
\r
73 _integrity = integrity;
\r
78 * Returns true if it has Integrity object
\r
83 public boolean hasIntegrity() {
\r
84 return (_integrity != null);
\r
88 * Should check hasIntegrity() before calling
\r
90 * @return a COPSIntegrity
\r
93 public COPSIntegrity getIntegrity() {
\r
94 return (_integrity);
\r
98 * Writes data to given network socket
\r
100 * @param id a Socket
\r
102 * @throws IOException
\r
105 public void writeData(Socket id) throws IOException {
\r
107 if (_hdr != null) _hdr.writeData(id);
\r
108 if (_integrity != null) _integrity.writeData(id);
\r
114 * @param data a byte[]
\r
116 * @throws COPSException
\r
119 protected void parse(byte[] data) throws COPSException {
\r
120 super.parseHeader(data);
\r
122 while (_dataStart < _dataLength) {
\r
123 byte[] buf = new byte[data.length - _dataStart];
\r
124 System.arraycopy(data,_dataStart,buf,0,data.length - _dataStart);
\r
126 COPSObjHeader objHdr = new COPSObjHeader (buf);
\r
127 switch (objHdr.getCNum()) {
\r
128 case COPSObjHeader.COPS_MSG_INTEGRITY: {
\r
129 _integrity = new COPSIntegrity(buf);
\r
130 _dataStart += _integrity.getDataLength();
\r
134 throw new COPSException("Bad Message format, unknown object type");
\r
144 * @param hdr a COPSHeader
\r
145 * @param data a byte[]
\r
147 * @throws COPSException
\r
150 protected void parse(COPSHeader hdr, byte[] data) throws COPSException {
\r
151 if (hdr.getOpCode() != COPSHeader.COPS_OP_KA)
\r
152 throw new COPSException("Error Header");
\r
159 * Set the message length, base on the set of objects it contains
\r
161 * @throws COPSException
\r
164 private void setMsgLength() throws COPSException {
\r
166 if (_integrity != null) len += _integrity.getDataLength();
\r
167 _hdr.setMsgLength(len);
\r
171 * Write an object textual description in the output stream
\r
173 * @param os an OutputStream
\r
175 * @throws IOException
\r
178 public void dump(OutputStream os) throws IOException {
\r
181 if (_integrity != null) {
\r
182 _integrity.dump(os);
\r