2 * Copyright (c) 2003 University of Murcia. All rights reserved.
3 * --------------------------------------------------------------
4 * For more information, please see <http://www.umu.euro6ix.org/>.
7 package org.umu.cops.stack;
9 import java.io.IOException;
10 import java.io.OutputStream;
11 import java.net.Socket;
14 * COPS Keep Alive Message
16 * @version COPSKAMsg.java, v 1.00 2003
19 public class COPSKAMsg extends COPSMsg {
21 /* COPSHeader coming from base class */
22 private COPSIntegrity _integrity;
28 protected COPSKAMsg(byte[] data) throws COPSException {
33 /** Checks the sanity of COPS message and throw an
34 * COPSBadDataException when data is bad.
36 public void checkSanity() throws COPSException {
37 //The client type in the header MUST always be set to 0
38 //as KA is used for connection verification.RFC 2748
39 if ((_hdr == null) && (_hdr.getClientType() != 0))
40 throw new COPSException("Bad message format");
46 * @param hdr a COPSHeader
48 * @throws COPSException
51 public void add (COPSHeader hdr) throws COPSException {
53 throw new COPSException ("Null Header");
54 if (hdr.getOpCode() != COPSHeader.COPS_OP_KA)
55 throw new COPSException ("Error Header (no COPS_OP_KA)");
61 * Add Integrity objects
63 * @param integrity a COPSIntegrity
65 * @throws COPSException
68 public void add (COPSIntegrity integrity) throws COPSException {
69 if (integrity == null)
70 throw new COPSException ("Null Integrity");
71 _integrity = integrity;
76 * Returns true if it has Integrity object
81 public boolean hasIntegrity() {
82 return (_integrity != null);
86 * Should check hasIntegrity() before calling
88 * @return a COPSIntegrity
91 public COPSIntegrity getIntegrity() {
96 * Writes data to given network socket
100 * @throws IOException
103 public void writeData(Socket id) throws IOException {
105 if (_hdr != null) _hdr.writeData(id);
106 if (_integrity != null) _integrity.writeData(id);
112 * @param data a byte[]
114 * @throws COPSException
117 protected void parse(byte[] data) throws COPSException {
118 super.parseHeader(data);
120 while (_dataStart < _dataLength) {
121 byte[] buf = new byte[data.length - _dataStart];
122 System.arraycopy(data,_dataStart,buf,0,data.length - _dataStart);
124 final COPSObjHeaderData objHdrData = COPSObjectParser.parseObjHeader(buf);
125 switch (objHdrData.header.getCNum()) {
127 _integrity = COPSIntegrity.parse(objHdrData, buf);
128 _dataStart += _integrity.getDataLength();
131 throw new COPSException("Bad Message format, unknown object type");
141 * @param hdr a COPSHeader
142 * @param data a byte[]
144 * @throws COPSException
147 protected void parse(COPSHeader hdr, byte[] data) throws COPSException {
148 if (hdr.getOpCode() != COPSHeader.COPS_OP_KA)
149 throw new COPSException("Error Header");
156 * Set the message length, base on the set of objects it contains
158 * @throws COPSException
161 private void setMsgLength() throws COPSException {
163 if (_integrity != null) len += _integrity.getDataLength();
164 _hdr.setMsgLength(len);
168 * Write an object textual description in the output stream
170 * @param os an OutputStream
172 * @throws IOException
175 public void dump(OutputStream os) throws IOException {
178 if (_integrity != null) {