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 Accept Message
\r
16 * @version COPSClientAcceptMsg.java, v 1.00 2003
\r
19 public class COPSClientAcceptMsg extends COPSMsg {
\r
21 /* COPSHeader coming from base class */
\r
22 private COPSKATimer _kaTimer;
\r
23 private COPSAcctTimer _acctTimer;
\r
24 private COPSIntegrity _integrity;
\r
27 public COPSClientAcceptMsg() {
\r
33 ///Create object from data
\r
34 protected COPSClientAcceptMsg(byte[] data) throws COPSException {
\r
38 /** Checks the sanity of COPS message and throw an
\r
39 * COPSBadDataException when data is bad.
\r
41 public void checkSanity() throws COPSException {
\r
42 if ((_hdr == null) || (_kaTimer == null))
\r
43 throw new COPSException("Bad message format");
\r
47 * Add message header
\r
49 * @param hdr a COPSHeader
\r
51 * @throws COPSException
\r
54 public void add (COPSHeader hdr) throws COPSException {
\r
56 throw new COPSException ("Null Header");
\r
57 if (hdr.getOpCode() != COPSHeader.COPS_OP_CAT)
\r
58 throw new COPSException ("Error Header (no COPS_OP_CAT)");
\r
64 * Add Timer object to the message
\r
66 * @param timer a COPSTimer
\r
68 * @throws COPSException
\r
71 public void add (COPSTimer timer) throws COPSException {
\r
72 if (timer.isKATimer()) {
\r
73 _kaTimer = (COPSKATimer) timer;
\r
75 _acctTimer = (COPSAcctTimer) timer;
\r
81 * Add Integrity objects
\r
83 * @param integrity a COPSIntegrity
\r
85 * @throws COPSException
\r
88 public void add (COPSIntegrity integrity) throws COPSException {
\r
89 if (integrity == null)
\r
90 throw new COPSException ("Null Integrity");
\r
91 if (!integrity.isMessageIntegrity())
\r
92 throw new COPSException ("Error Integrity");
\r
93 _integrity = integrity;
\r
100 * @return a COPSKATimer
\r
103 public COPSKATimer getKATimer() {
\r
108 * Returns true if has a account timer object
\r
110 * @return a boolean
\r
113 public boolean hasAcctTimer() {
\r
114 return (_acctTimer != null);
\r
118 * Should check hasAcctTimer() before calling
\r
120 * @return a COPSAcctTimer
\r
123 public COPSAcctTimer getAcctTimer() {
\r
124 return (_acctTimer);
\r
128 * Returns true if has a Integrity object
\r
130 * @return a boolean
\r
133 public boolean hasIntegrity() {
\r
134 return (_integrity != null);
\r
138 * Should check hasIntegrity() before calling
\r
140 * @return a COPSIntegrity
\r
143 public COPSIntegrity getIntegrity() {
\r
144 return (_integrity);
\r
148 * Writes data to a given socket id
\r
150 * @param id a Socket
\r
152 * @throws IOException
\r
155 public void writeData(Socket id) throws IOException {
\r
157 if (_hdr != null) _hdr.writeData(id);
\r
158 if (_kaTimer != null) _kaTimer.writeData(id);
\r
159 if (_acctTimer != null) _acctTimer.writeData(id);
\r
160 if (_integrity != null) _integrity.writeData(id);
\r
166 * @param data a byte[]
\r
168 * @throws COPSException
\r
171 protected void parse(byte[] data) throws COPSException {
\r
174 while (_dataStart < _dataLength) {
\r
175 byte[] buf = new byte[data.length - _dataStart];
\r
176 System.arraycopy(data,_dataStart,buf,0,data.length - _dataStart);
\r
178 COPSObjHeader objHdr = new COPSObjHeader (buf);
\r
179 switch (objHdr.getCNum()) {
\r
180 case COPSObjHeader.COPS_KA: {
\r
181 _kaTimer = new COPSKATimer(buf);
\r
182 _dataStart += _kaTimer.getDataLength();
\r
185 case COPSObjHeader.COPS_ACCT_TIMER: {
\r
186 _acctTimer = new COPSAcctTimer(buf);
\r
187 _dataStart += _acctTimer.getDataLength();
\r
190 case COPSObjHeader.COPS_MSG_INTEGRITY: {
\r
191 _integrity = new COPSIntegrity(buf);
\r
192 _dataStart += _integrity.getDataLength();
\r
196 throw new COPSException("Bad Message format");
\r
206 * @param hdr a COPSHeader
\r
207 * @param data a byte[]
\r
209 * @throws COPSException
\r
212 protected void parse(COPSHeader hdr, byte[] data) throws COPSException {
\r
213 if (hdr.getOpCode() != COPSHeader.COPS_OP_CAT)
\r
214 throw new COPSException("Error Header");
\r
221 * Set the message length, base on the set of objects it contains
\r
223 * @throws COPSException
\r
226 protected void setMsgLength() throws COPSException {
\r
228 if (_kaTimer != null) len += _kaTimer.getDataLength();
\r
229 if (_acctTimer != null) len += _acctTimer.getDataLength();
\r
230 if (_integrity != null) len += _integrity.getDataLength();
\r
231 _hdr.setMsgLength(len);
\r
235 * Write an object textual description in the output stream
\r
237 * @param os an OutputStream
\r
239 * @throws IOException
\r
242 public void dump(OutputStream os) throws IOException {
\r
245 if (_kaTimer != null)
\r
248 if (_acctTimer != null)
\r
249 _acctTimer.dump(os);
\r
251 if (_integrity != null) {
\r
252 _integrity.dump(os);
\r