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 Client Close Message
\r
16 * @version COPSClientCloseMsg.java, v 1.00 2003
\r
19 public class COPSClientCloseMsg extends COPSMsg {
\r
21 /* COPSHeader coming from base class */
\r
22 private COPSError _error;
\r
23 private COPSIntegrity _integrity;
\r
26 public COPSClientCloseMsg() {
\r
31 protected COPSClientCloseMsg(byte[] data) throws COPSException {
\r
35 /** Checks the sanity of COPS message and throw an
\r
36 * COPSBadDataException when data is bad.
\r
38 public void checkSanity() throws COPSException {
\r
39 if ((_hdr == null) || (_error == null))
\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_CC)
\r
55 throw new COPSException ("Error Header (no COPS_OP_CC)");
\r
63 * @param error a COPSError
\r
65 * @throws COPSException
\r
68 public void add (COPSError error) throws COPSException {
\r
69 //Message integrity object should be the very last one
\r
70 //If it is already added
\r
72 throw new COPSException ("No null Error");
\r
78 * Add Integrity objects
\r
80 * @param integrity a COPSIntegrity
\r
82 * @throws COPSException
\r
85 public void add (COPSIntegrity integrity) throws COPSException {
\r
86 if (integrity == null)
\r
87 throw new COPSException ("Null Integrity");
\r
88 if (!integrity.isMessageIntegrity())
\r
89 throw new COPSException ("Error Integrity");
\r
90 _integrity = integrity;
\r
97 * @return a COPSError
\r
100 public COPSError getError() {
\r
105 * Returns true If it has integrity object
\r
107 * @return a boolean
\r
110 public boolean hasIntegrity() {
\r
111 return (_integrity != null);
\r
115 * Should check hasIntegrity() before calling
\r
117 * @return a COPSIntegrity
\r
120 public COPSIntegrity getIntegrity() {
\r
121 return (_integrity);
\r
125 * Write object data to given network socket
\r
127 * @param id a Socket
\r
129 * @throws IOException
\r
132 public void writeData(Socket id) throws IOException {
\r
134 if (_hdr != null) _hdr.writeData(id);
\r
135 if (_error != null) _error.writeData(id);
\r
136 if (_integrity != null) _integrity.writeData(id);
\r
142 * @param data a byte[]
\r
144 * @throws COPSException
\r
147 protected void parse(byte[] data) throws COPSException {
\r
150 while (_dataStart < _dataLength) {
\r
151 byte[] buf = new byte[data.length - _dataStart];
\r
152 System.arraycopy(data,_dataStart,buf,0,data.length - _dataStart);
\r
154 COPSObjHeader objHdr = new COPSObjHeader (buf);
\r
155 switch (objHdr.getCNum()) {
\r
156 case COPSObjHeader.COPS_ERROR: {
\r
157 _error = new COPSError(buf);
\r
158 _dataStart += _error.getDataLength();
\r
161 case COPSObjHeader.COPS_MSG_INTEGRITY: {
\r
162 _integrity = new COPSIntegrity(buf);
\r
163 _dataStart += _integrity.getDataLength();
\r
167 throw new COPSException("Bad Message format");
\r
177 * @param hdr a COPSHeader
\r
178 * @param data a byte[]
\r
180 * @throws COPSException
\r
183 protected void parse(COPSHeader hdr, byte[] data) throws COPSException {
\r
184 if (hdr.getOpCode() != COPSHeader.COPS_OP_CC)
\r
185 throw new COPSException("Error Header");
\r
192 * Set the message length, base on the set of objects it contains
\r
194 * @throws COPSException
\r
197 protected void setMsgLength() throws COPSException {
\r
199 if (_error != null) len += _error.getDataLength();
\r
200 if (_integrity != null) len += _integrity.getDataLength();
\r
201 _hdr.setMsgLength(len);
\r
205 * Write an object textual description in the output stream
\r
207 * @param os an OutputStream
\r
209 * @throws IOException
\r
212 public void dump(OutputStream os) throws IOException {
\r
215 if (_error != null)
\r
218 if (_integrity != null) {
\r
219 _integrity.dump(os);
\r