-/*\r
- * Copyright (c) 2003 University of Murcia. All rights reserved.\r
- * --------------------------------------------------------------\r
- * For more information, please see <http://www.umu.euro6ix.org/>.\r
- */\r
-\r
-package org.umu.cops.stack;\r
-\r
-import java.io.IOException;\r
-import java.io.OutputStream;\r
-import java.net.Socket;\r
-\r
-/**\r
- * COPS Client Close Message\r
- *\r
- * @version COPSClientCloseMsg.java, v 1.00 2003\r
- *\r
- */\r
-public class COPSClientCloseMsg extends COPSMsg {\r
-\r
- /* COPSHeader coming from base class */\r
- private COPSError _error;\r
- private COPSIntegrity _integrity;\r
-\r
-\r
- public COPSClientCloseMsg() {\r
- _error = null;\r
- _integrity = null;\r
- }\r
-\r
- protected COPSClientCloseMsg(byte[] data) throws COPSException {\r
- parse (data);\r
- }\r
-\r
- /** Checks the sanity of COPS message and throw an\r
- * COPSBadDataException when data is bad.\r
- */\r
- public void checkSanity() throws COPSException {\r
- if ((_hdr == null) || (_error == null))\r
- throw new COPSException("Bad message format");\r
- }\r
-\r
- /**\r
- * Add message header\r
- *\r
- * @param hdr a COPSHeader\r
- *\r
- * @throws COPSException\r
- *\r
- */\r
- public void add (COPSHeader hdr) throws COPSException {\r
- if (hdr == null)\r
- throw new COPSException ("Null Header");\r
- if (hdr.getOpCode() != COPSHeader.COPS_OP_CC)\r
- throw new COPSException ("Error Header (no COPS_OP_CC)");\r
- _hdr = hdr;\r
- setMsgLength();\r
- }\r
-\r
- /**\r
- * Add Error object\r
- *\r
- * @param error a COPSError\r
- *\r
- * @throws COPSException\r
- *\r
- */\r
- public void add (COPSError error) throws COPSException {\r
- //Message integrity object should be the very last one\r
- //If it is already added\r
- if (_error != null)\r
- throw new COPSException ("No null Error");\r
- _error = error;\r
- setMsgLength();\r
- }\r
-\r
- /**\r
- * Add Integrity objects\r
- *\r
- * @param integrity a COPSIntegrity\r
- *\r
- * @throws COPSException\r
- *\r
- */\r
- public void add (COPSIntegrity integrity) throws COPSException {\r
- if (integrity == null)\r
- throw new COPSException ("Null Integrity");\r
- if (!integrity.isMessageIntegrity())\r
- throw new COPSException ("Error Integrity");\r
- _integrity = integrity;\r
- setMsgLength();\r
- }\r
-\r
- /**\r
- * Method getError\r
- *\r
- * @return a COPSError\r
- *\r
- */\r
- public COPSError getError() {\r
- return (_error);\r
- }\r
-\r
- /**\r
- * Returns true If it has integrity object\r
- *\r
- * @return a boolean\r
- *\r
- */\r
- public boolean hasIntegrity() {\r
- return (_integrity != null);\r
- };\r
-\r
- /**\r
- * Should check hasIntegrity() before calling\r
- *\r
- * @return a COPSIntegrity\r
- *\r
- */\r
- public COPSIntegrity getIntegrity() {\r
- return (_integrity);\r
- }\r
-\r
- /**\r
- * Write object data to given network socket\r
- *\r
- * @param id a Socket\r
- *\r
- * @throws IOException\r
- *\r
- */\r
- public void writeData(Socket id) throws IOException {\r
- // checkSanity();\r
- if (_hdr != null) _hdr.writeData(id);\r
- if (_error != null) _error.writeData(id);\r
- if (_integrity != null) _integrity.writeData(id);\r
- }\r
-\r
- /**\r
- * Method parse\r
- *\r
- * @param data a byte[]\r
- *\r
- * @throws COPSException\r
- *\r
- */\r
- protected void parse(byte[] data) throws COPSException {\r
- parseHeader(data);\r
-\r
- while (_dataStart < _dataLength) {\r
- byte[] buf = new byte[data.length - _dataStart];\r
- System.arraycopy(data,_dataStart,buf,0,data.length - _dataStart);\r
-\r
- COPSObjHeader objHdr = COPSObjHeader.parse(buf);\r
- switch (objHdr.getCNum()) {\r
- case ERROR:\r
- _error = new COPSError(buf);\r
- _dataStart += _error.getDataLength();\r
- break;\r
- case MSG_INTEGRITY:\r
- _integrity = new COPSIntegrity(buf);\r
- _dataStart += _integrity.getDataLength();\r
- break;\r
- default:\r
- throw new COPSException("Bad Message format");\r
- }\r
- }\r
- checkSanity();\r
- }\r
-\r
- /**\r
- * Method parse\r
- *\r
- * @param hdr a COPSHeader\r
- * @param data a byte[]\r
- *\r
- * @throws COPSException\r
- *\r
- */\r
- protected void parse(COPSHeader hdr, byte[] data) throws COPSException {\r
- if (hdr.getOpCode() != COPSHeader.COPS_OP_CC)\r
- throw new COPSException("Error Header");\r
- _hdr = hdr;\r
- parse(data);\r
- setMsgLength();\r
- }\r
-\r
- /**\r
- * Set the message length, base on the set of objects it contains\r
- *\r
- * @throws COPSException\r
- *\r
- */\r
- protected void setMsgLength() throws COPSException {\r
- int len = 0;\r
- if (_error != null) len += _error.getDataLength();\r
- if (_integrity != null) len += _integrity.getDataLength();\r
- _hdr.setMsgLength(len);\r
- }\r
-\r
- /**\r
- * Write an object textual description in the output stream\r
- *\r
- * @param os an OutputStream\r
- *\r
- * @throws IOException\r
- *\r
- */\r
- public void dump(OutputStream os) throws IOException {\r
- _hdr.dump(os);\r
-\r
- if (_error != null)\r
- _error.dump(os);\r
-\r
- if (_integrity != null) {\r
- _integrity.dump(os);\r
- }\r
- }\r
-\r
-}\r
-\r
+/*
+ * Copyright (c) 2003 University of Murcia. All rights reserved.
+ * --------------------------------------------------------------
+ * For more information, please see <http://www.umu.euro6ix.org/>.
+ */
+
+package org.umu.cops.stack;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.net.Socket;
+
+/**
+ * COPS Client Close Message
+ *
+ * @version COPSClientCloseMsg.java, v 1.00 2003
+ *
+ */
+public class COPSClientCloseMsg extends COPSMsg {
+
+ /* COPSHeader coming from base class */
+ private COPSError _error;
+ private COPSIntegrity _integrity;
+
+
+ public COPSClientCloseMsg() {
+ _error = null;
+ _integrity = null;
+ }
+
+ protected COPSClientCloseMsg(byte[] data) throws COPSException {
+ parse (data);
+ }
+
+ /** Checks the sanity of COPS message and throw an
+ * COPSBadDataException when data is bad.
+ */
+ public void checkSanity() throws COPSException {
+ if ((_hdr == null) || (_error == null))
+ throw new COPSException("Bad message format");
+ }
+
+ /**
+ * Add message header
+ *
+ * @param hdr a COPSHeader
+ *
+ * @throws COPSException
+ *
+ */
+ public void add (COPSHeader hdr) throws COPSException {
+ if (hdr == null)
+ throw new COPSException ("Null Header");
+ if (hdr.getOpCode() != COPSHeader.COPS_OP_CC)
+ throw new COPSException ("Error Header (no COPS_OP_CC)");
+ _hdr = hdr;
+ setMsgLength();
+ }
+
+ /**
+ * Add Error object
+ *
+ * @param error a COPSError
+ *
+ * @throws COPSException
+ *
+ */
+ public void add (COPSError error) throws COPSException {
+ //Message integrity object should be the very last one
+ //If it is already added
+ if (_error != null)
+ throw new COPSException ("No null Error");
+ _error = error;
+ setMsgLength();
+ }
+
+ /**
+ * Add Integrity objects
+ *
+ * @param integrity a COPSIntegrity
+ *
+ * @throws COPSException
+ *
+ */
+ public void add (COPSIntegrity integrity) throws COPSException {
+ if (integrity == null)
+ throw new COPSException ("Null Integrity");
+ _integrity = integrity;
+ setMsgLength();
+ }
+
+ /**
+ * Method getError
+ *
+ * @return a COPSError
+ *
+ */
+ public COPSError getError() {
+ return (_error);
+ }
+
+ /**
+ * Returns true If it has integrity object
+ *
+ * @return a boolean
+ *
+ */
+ public boolean hasIntegrity() {
+ return (_integrity != null);
+ };
+
+ /**
+ * Should check hasIntegrity() before calling
+ *
+ * @return a COPSIntegrity
+ *
+ */
+ public COPSIntegrity getIntegrity() {
+ return (_integrity);
+ }
+
+ /**
+ * Write object data to given network socket
+ *
+ * @param id a Socket
+ *
+ * @throws IOException
+ *
+ */
+ public void writeData(Socket id) throws IOException {
+ // checkSanity();
+ if (_hdr != null) _hdr.writeData(id);
+ if (_error != null) _error.writeData(id);
+ if (_integrity != null) _integrity.writeData(id);
+ }
+
+ /**
+ * Method parse
+ *
+ * @param data a byte[]
+ *
+ * @throws COPSException
+ *
+ */
+ protected void parse(byte[] data) throws COPSException {
+ parseHeader(data);
+
+ while (_dataStart < _dataLength) {
+ byte[] buf = new byte[data.length - _dataStart];
+ System.arraycopy(data,_dataStart,buf,0,data.length - _dataStart);
+
+ final COPSObjHeaderData objHdrData = COPSObjectParser.parseObjHeader(buf);
+ switch (objHdrData.header.getCNum()) {
+ case ERROR:
+ _error = COPSError.parse(objHdrData, buf);
+ _dataStart += _error.getDataLength();
+ break;
+ case MSG_INTEGRITY:
+ _integrity = COPSIntegrity.parse(objHdrData, buf);
+ _dataStart += _integrity.getDataLength();
+ break;
+ default:
+ throw new COPSException("Bad Message format");
+ }
+ }
+ checkSanity();
+ }
+
+ /**
+ * Method parse
+ *
+ * @param hdr a COPSHeader
+ * @param data a byte[]
+ *
+ * @throws COPSException
+ *
+ */
+ protected void parse(COPSHeader hdr, byte[] data) throws COPSException {
+ if (hdr.getOpCode() != COPSHeader.COPS_OP_CC)
+ throw new COPSException("Error Header");
+ _hdr = hdr;
+ parse(data);
+ setMsgLength();
+ }
+
+ /**
+ * Set the message length, base on the set of objects it contains
+ *
+ * @throws COPSException
+ *
+ */
+ protected void setMsgLength() throws COPSException {
+ int len = 0;
+ if (_error != null) len += _error.getDataLength();
+ if (_integrity != null) len += _integrity.getDataLength();
+ _hdr.setMsgLength(len);
+ }
+
+ /**
+ * Write an object textual description in the output stream
+ *
+ * @param os an OutputStream
+ *
+ * @throws IOException
+ *
+ */
+ public void dump(OutputStream os) throws IOException {
+ _hdr.dump(os);
+
+ if (_error != null)
+ _error.dump(os);
+
+ if (_integrity != null) {
+ _integrity.dump(os);
+ }
+ }
+
+}
+