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 org.umu.cops.stack.COPSHeader.Flag;
10 import org.umu.cops.stack.COPSHeader.OPCode;
12 import java.io.IOException;
13 import java.io.OutputStream;
14 import java.net.Socket;
17 * COPS Client Close Message (RFC 2748 pg. 27)
19 * The Client-Close message can be issued by either the PDP or PEP to
20 * notify the other that a particular type of client is no longer being
23 * <Client-Close> ::= <Common Header>
28 * The Error object is included to describe the reason for the close
29 * (e.g. the requested client-type is not supported by the remote PDP or
32 * A PDP MAY optionally include a PDP Redirect Address object in order
33 * to inform the PEP of the alternate PDP it SHOULD use for the client-
34 * type specified in the common header.
36 public class COPSClientCloseMsg extends COPSMsg {
39 private final COPSError _error;
42 private final COPSPdpAddress _redirAddr;
43 private final COPSIntegrity _integrity;
46 * Constructor (generally used for sending messages) which probably should not be used as the PCMM version and
47 * Flag values on the header are being hardcoded to 1 and UNSOLICITED respectively. Use the next one below instead
48 * @param clientType - the type of client that created the message (required)
49 * @param error - the error (required)
50 * @param redirAddr - the redirect address (optional)
51 * @param integrity - the integrity (optional)
52 * @throws java.lang.IllegalArgumentException
55 public COPSClientCloseMsg(final short clientType, final COPSError error, final COPSPdpAddress redirAddr,
56 final COPSIntegrity integrity) {
57 this(new COPSHeader(OPCode.CC, clientType), error, redirAddr, integrity);
61 * Constructor (generally used for sending messages).
62 * @param version - the supported PCMM Version
63 * @param flag - the flag...
64 * @param clientType - the type of client that created the message (required)
65 * @param error - the error (required)
66 * @param redirAddr - the redirect address (optional)
67 * @param integrity - the integrity (optional)
68 * @throws java.lang.IllegalArgumentException
70 public COPSClientCloseMsg(final int version, final Flag flag, final short clientType, final COPSError error,
71 final COPSPdpAddress redirAddr, final COPSIntegrity integrity) {
72 this(new COPSHeader(version, flag, OPCode.CC, clientType), error, redirAddr, integrity);
76 * Constructor generally used when parsing the bytes of an inbound COPS message but can also be used when the
77 * COPSHeader information is known.
78 * @param hdr - COPS Header
79 * @param error - the error (required)
80 * @param redirAddr - the redirect address (optional)
81 * @param integrity - the integrity (optional)
82 * @throws java.lang.IllegalArgumentException
84 public COPSClientCloseMsg(final COPSHeader hdr, final COPSError error, final COPSPdpAddress redirAddr,
85 final COPSIntegrity integrity) {
87 if (!hdr.getOpCode().equals(OPCode.CC))
88 throw new IllegalArgumentException("OPCode must be of type - " + OPCode.CAT);
89 if (error == null) throw new IllegalArgumentException("Error object must not be null");
91 this._redirAddr = redirAddr;
92 this._integrity = integrity;
96 public COPSError getError() {
99 public COPSPdpAddress getRedirAddr() {
102 public COPSIntegrity getIntegrity() {
107 protected void writeBody(final Socket socket) throws IOException {
108 _error.writeData(socket);
109 if (_redirAddr != null) _redirAddr.writeData(socket);
110 if (_integrity != null) _integrity.writeData(socket);
114 protected int getDataLength() {
115 int out = _error.getDataLength() + _error.getHeader().getHdrLength();
116 if (_redirAddr != null) out += _redirAddr.getDataLength() + _redirAddr.getHeader().getHdrLength();
117 if (_integrity != null) out += _integrity.getDataLength() + _integrity.getHeader().getHdrLength();
122 protected void dumpBody(OutputStream os) throws IOException {
124 if (_redirAddr != null) _redirAddr.dump(os);
125 if (_integrity != null) _integrity.dump(os);
129 public boolean equals(final Object o) {
133 if (!(o instanceof COPSClientCloseMsg)) {
136 if (!super.equals(o)) {
140 final COPSClientCloseMsg closeMsg = (COPSClientCloseMsg) o;
142 return _error.equals(closeMsg._error) &&
143 !(_integrity != null ? !_integrity.equals(closeMsg._integrity) : closeMsg._integrity != null) &&
144 !(_redirAddr != null ? !_redirAddr.equals(closeMsg._redirAddr) : closeMsg._redirAddr != null);
149 public int hashCode() {
150 int result = super.hashCode();
151 result = 31 * result + _error.hashCode();
152 result = 31 * result + (_redirAddr != null ? _redirAddr.hashCode() : 0);
153 result = 31 * result + (_integrity != null ? _integrity.hashCode() : 0);
158 * Responsible for parsing a byte array to create a COPSDecisionMsg object
159 * @param hdrData - the object's header data
160 * @param data - the byte array to parse
161 * @return - the message object
162 * @throws COPSException
164 public static COPSClientCloseMsg parse(final COPSHeaderData hdrData, final byte[] data) throws COPSException {
165 // Variables for constructor
166 COPSError error = null;
167 COPSPdpAddress redirAddr = null;
168 COPSIntegrity integrity = null;
171 while (dataStart < data.length) {
172 final byte[] buf = new byte[data.length - dataStart];
173 System.arraycopy(data, dataStart, buf, 0, data.length - dataStart);
175 final COPSObjHeaderData objHdrData = COPSObjectParser.parseObjHeader(buf);
176 switch (objHdrData.header.getCNum()) {
178 error = COPSError.parse(objHdrData, buf);
181 redirAddr = COPSPdpAddress.parse(objHdrData, buf);
184 integrity = COPSIntegrity.parse(objHdrData, buf);
187 throw new COPSException("Bad Message format, unknown object type");
189 dataStart += objHdrData.msgByteCount;
192 return new COPSClientCloseMsg(hdrData.header, error, redirAddr, integrity);