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 Sync State Message (RFC 2748 pag. 26 and pag. 29
\r
16 * The format of the Synchronize State Query message is as follows:
\r
18 * <Synchronize State> ::= <Common Header>
\r
22 * This message indicates that the remote PDP wishes the client (which
\r
23 * appears in the common header) to re-send its state. If the optional
\r
24 * Client Handle is present, only the state associated with this handle
\r
25 * is synchronized. If the PEP does not recognize the requested handle,
\r
26 * it MUST immediately send a DRQ message to the PDP for the handle that
\r
27 * was specified in the SSQ message. If no handle is specified in the
\r
28 * SSQ message, all the active client state MUST be synchronized with
\r
31 * The client performs state synchronization by re-issuing request
\r
32 * queries of the specified client-type for the existing state in the
\r
33 * PEP. When synchronization is complete, the PEP MUST issue a
\r
34 * synchronize state complete message to the PDP.
\r
36 * <Synchronize State Complete> ::= <Common Header>
\r
40 * The Client Handle object only needs to be included if the corresponding
\r
41 * Synchronize State Message originally referenced a specific handle.
\r
43 * @version COPSSyncStateMsg.java, v 1.00 2003
\r
46 public class COPSSyncStateMsg extends COPSMsg {
\r
48 /* COPSHeader coming from base class */
\r
49 private COPSHandle _clientHandle;
\r
50 private COPSIntegrity _integrity;
\r
52 public COPSSyncStateMsg() {
\r
53 _clientHandle = null;
\r
58 Parse data and create COPSSyncStateMsg object
\r
60 protected COPSSyncStateMsg(byte[] data) throws COPSException {
\r
61 _clientHandle = null;
\r
67 * Checks the sanity of COPS message and throw an
\r
68 * COPSException when data is bad.
\r
70 public void checkSanity() throws COPSException {
\r
72 throw new COPSException("Bad message format");
\r
77 * Add message header
\r
79 * @param hdr a COPSHeader
\r
81 * @throws COPSException
\r
84 public void add (COPSHeader hdr) throws COPSException {
\r
86 throw new COPSException ("Null Header");
\r
87 if ((hdr.getOpCode() != COPSHeader.COPS_OP_SSC) &&
\r
88 (hdr.getOpCode() != COPSHeader.COPS_OP_SSQ))
\r
89 throw new COPSException ("Error Header (no COPS_OP_SSX)");
\r
95 * Add client handle to the message
\r
97 * @param handle a COPSHandle
\r
99 * @throws COPSException
\r
102 public void add (COPSHandle handle) throws COPSException {
\r
103 if (handle == null)
\r
104 throw new COPSException ("Null Handle");
\r
106 //Message integrity object should be the very last one
\r
107 //If it is already added
\r
108 if (_integrity != null)
\r
109 throw new COPSException ("No null Handle");
\r
111 _clientHandle = handle;
\r
116 * Add integrity object
\r
118 * @param integrity a COPSIntegrity
\r
120 * @throws COPSException
\r
123 public void add (COPSIntegrity integrity) throws COPSException {
\r
124 if (integrity == null)
\r
125 throw new COPSException ("Null Integrity");
\r
126 if (!integrity.isMessageIntegrity())
\r
127 throw new COPSException ("Error Integrity");
\r
128 _integrity = integrity;
\r
133 * If the optional Client Handle is present, only the state associated
\r
134 * with this handle is synchronized. If no handle is specified in the
\r
135 * SSQ message, all the active client state MUST be synchronized with
\r
138 * @return a boolean
\r
141 public boolean hasClientHandle() {
\r
142 return (_clientHandle != null);
\r
146 * Get client Handle
\r
148 * @return a COPSHandle
\r
151 public COPSHandle getClientHandle() {
\r
152 return _clientHandle;
\r
156 * Returns true if it has integrity object
\r
158 * @return a boolean
\r
161 public boolean hasIntegrity() {
\r
162 return (_integrity != null);
\r
166 * Get Integrity. Should check hasIntegrity() before calling
\r
168 * @return a COPSIntegrity
\r
171 public COPSIntegrity getIntegrity() {
\r
172 return (_integrity);
\r
176 * Writes data to given socket
\r
178 * @param id a Socket
\r
180 * @throws IOException
\r
183 public void writeData(Socket id) throws IOException {
\r
185 if (_hdr != null) _hdr.writeData(id);
\r
186 if (_clientHandle != null) _clientHandle.writeData(id);
\r
187 if (_integrity != null) _integrity.writeData(id);
\r
194 * @param data a byte[]
\r
196 * @throws COPSException
\r
199 protected void parse(byte[] data) throws COPSException {
\r
200 super.parseHeader(data);
\r
202 while (_dataStart < _dataLength) {
\r
203 byte[] buf = new byte[data.length - _dataStart];
\r
204 System.arraycopy(data,_dataStart,buf,0,data.length - _dataStart);
\r
206 COPSObjHeader objHdr = COPSObjHeader.parse(buf);
\r
207 switch (objHdr.getCNum()) {
\r
209 _clientHandle = new COPSHandle(buf);
\r
210 _dataStart += _clientHandle.getDataLength();
\r
212 case MSG_INTEGRITY:
\r
213 _integrity = new COPSIntegrity(buf);
\r
214 _dataStart += _integrity.getDataLength();
\r
217 throw new COPSException("Bad Message format, unknown object type");
\r
226 * @param hdr a COPSHeader
\r
227 * @param data a byte[]
\r
229 * @throws COPSException
\r
232 protected void parse(COPSHeader hdr, byte[] data) throws COPSException {
\r
234 if ((hdr.getOpCode() != COPSHeader.COPS_OP_SSC) &&
\r
235 (hdr.getOpCode() != COPSHeader.COPS_OP_SSQ))
\r
236 throw new COPSException ("Error Header (no COPS_OP_SSX)");
\r
244 * Set the message length, base on the set of objects it contains
\r
246 * @throws COPSException
\r
249 protected void setMsgLength() throws COPSException {
\r
251 if (_clientHandle != null) len += _clientHandle.getDataLength();
\r
252 if (_integrity != null) len += _integrity.getDataLength();
\r
253 _hdr.setMsgLength(len);
\r
257 * Write an object textual description in the output stream
\r
259 * @param os an OutputStream
\r
261 * @throws IOException
\r
264 public void dump(OutputStream os) throws IOException {
\r
267 if (_clientHandle != null)
\r
268 _clientHandle.dump(os);
\r
270 if (_integrity != null) {
\r
271 _integrity.dump(os);
\r