4 package org.pcmm.messages.impl;
6 import org.umu.cops.stack.*;
8 import java.io.IOException;
9 import java.io.OutputStream;
10 import java.net.Socket;
11 import java.util.Enumeration;
12 import java.util.Hashtable;
13 import java.util.Vector;
16 * COPS Decision Message
21 public class COPSDecisionMsgEX extends COPSMsg {
23 /* COPSHeader coming from base class */
24 private COPSHandle _clientHandle;
25 private COPSError _error;
26 private Hashtable _decisions;
27 private COPSIntegrity _integrity;
28 private COPSContext _decContext;
29 private COPSClientSI clientSI;
32 public COPSDecisionMsgEX() {
35 _decisions = new Hashtable(20);
42 * Checks the sanity of COPS message and throw an COPSBadDataException when
45 public void checkSanity() throws COPSException {
46 if ((_hdr == null) || (_clientHandle == null)
47 || ((_error == null) && (_decisions.size() == 0))) {
48 throw new COPSException("Bad message format");
53 protected COPSDecisionMsgEX(byte[] data) throws COPSException {
54 _decisions = new Hashtable(20);
64 * Parses the data and fills COPSDecisionMsg with its constituents
69 * @throws COPSException
72 protected void parse(byte[] data) throws COPSException {
73 super.parseHeader(data);
75 while (_dataStart < _dataLength) {
76 byte[] buf = new byte[data.length - _dataStart];
77 System.arraycopy(data, _dataStart, buf, 0, data.length - _dataStart);
79 COPSObjHeader objHdr = COPSObjHeader.parse(buf);
80 switch (objHdr.getCNum()) {
82 _clientHandle = new COPSHandle(buf) {
84 _dataStart += _clientHandle.getDataLength();
88 _decContext = new COPSContext(buf) {
90 _dataStart += _decContext.getDataLength();
93 _error = new COPSError(buf) {
95 _dataStart += _error.getDataLength();
98 COPSDecision decs = new COPSDecision(buf) {
100 _dataStart += decs.getDataLength();
101 addDecision(decs, _decContext);
104 _integrity = new COPSIntegrity(buf);
105 _dataStart += _integrity.getDataLength();
108 clientSI = new COPSClientSI(buf) {
110 _dataStart += clientSI.getDataLength();
113 throw new COPSException(
114 "Bad Message format, unknown object type");
121 * Parses the data and fills that follows the header hdr and fills
129 * @throws COPSException
132 protected void parse(COPSHeader hdr, byte[] data) throws COPSException {
144 * @throws COPSException
147 public void add(COPSHeader hdr) throws COPSException {
149 throw new COPSException("Null Header");
150 if (hdr.getOpCode() != COPSHeader.COPS_OP_DEC)
151 throw new COPSException("Error Header (no COPS_OP_DEC)");
157 * Add client handle to the message
162 * @throws COPSException
165 public void add(COPSHandle handle) throws COPSException {
167 throw new COPSException("Null Handle");
168 _clientHandle = handle;
173 * Add an Error object
178 * @throws COPSException
181 public void add(COPSError error) throws COPSException {
182 if (_decisions.size() != 0)
183 throw new COPSException("No null decisions");
185 throw new COPSException("No null error");
186 // Message integrity object should be the very last one
187 // If it is already added
188 if (_integrity != null)
189 throw new COPSException("No null integrity");
195 * Add one or more local decision object for a given decision context the
196 * context is optional, if null all decision object are tided to message
204 * @throws COPSException
207 public void addDecision(COPSDecision decision, COPSContext context)
208 throws COPSException {
209 // Either error or decision can be added
210 // If error is aleady there assert
212 throw new COPSException("No null error");
214 if (decision.isLocalDecision())
215 throw new COPSException("Is local decision");
217 Vector v = (Vector) _decisions.get(context);
221 if (decision.isFlagSet()) {// Commented out as advised by Felix
222 // if (v.size() != 0)
224 // Only one set of decision flags is allowed
226 // throw new COPSException
227 // ("Bad Message format, only one set of decision flags is allowed.");
231 // The flags decision must precede any other
232 // decision message, since the decision is not
233 // flags throw exception
234 throw new COPSException(
235 "Bad Message format, flags decision must precede any other decision object.");
239 _decisions.put(context, v);
245 * Add integrity object
250 * @throws COPSException
253 public void add(COPSIntegrity integrity) throws COPSException {
254 if (integrity == null)
255 throw new COPSException("Null Integrity");
256 if (!integrity.isMessageIntegrity())
257 throw new COPSException("Error Integrity");
258 _integrity = integrity;
263 * Add a client specific informations
268 * @throws COPSException
271 public void add(COPSClientSI clientSI) throws COPSException {
272 if (clientSI == null)
273 throw new COPSException("Null ClientSI");
274 this.clientSI = clientSI;
279 * Writes data to given socket
284 * @throws IOException
287 public void writeData(Socket id) throws IOException {
291 if (_clientHandle != null)
292 _clientHandle.writeData(id);
294 _error.writeData(id);
297 // Display any local decisions
298 for (Enumeration e = _decisions.keys(); e.hasMoreElements();) {
300 COPSContext context = (COPSContext) e.nextElement();
301 Vector v = (Vector) _decisions.get(context);
302 context.writeData(id);
304 for (Enumeration ee = v.elements(); ee.hasMoreElements();) {
305 COPSDecision decision = (COPSDecision) ee.nextElement();
306 decision.writeData(id);
309 if (clientSI != null)
310 clientSI.writeData(id);
311 if (_integrity != null)
312 _integrity.writeData(id);
318 * @return a COPSHeader
321 public COPSHeader getHeader() {
326 * Method getClientHandle
328 * @return a COPSHandle
331 public COPSHandle getClientHandle() {
332 return _clientHandle;
335 public COPSClientSI getClientSI() {
340 * Returns true if it has error object
345 public boolean hasError() {
346 return (_error != null);
350 * Should check hasError() before calling
352 * @return a COPSError
355 public COPSError getError() {
360 * Returns a map of decision for which is an arry of context and vector of
361 * associated decision object.
363 * @return a Hashtable
366 public Hashtable getDecisions() {
371 * Returns true if it has integrity object
376 public boolean hasIntegrity() {
377 return (_integrity != null);
381 * Should check hasIntegrity() before calling
383 * @return a COPSIntegrity
386 public COPSIntegrity getIntegrity() {
391 * Method setMsgLength
393 * @throws COPSException
396 protected void setMsgLength() throws COPSException {
398 if (_clientHandle != null)
399 len += _clientHandle.getDataLength();
401 len += _error.getDataLength();
403 // Display any local decisions
404 for (Enumeration e = _decisions.keys(); e.hasMoreElements();) {
406 COPSContext context = (COPSContext) e.nextElement();
407 Vector v = (Vector) _decisions.get(context);
408 len += context.getDataLength();
410 for (Enumeration ee = v.elements(); ee.hasMoreElements();) {
411 COPSDecision decision = (COPSDecision) ee.nextElement();
412 len += decision.getDataLength();
415 if (clientSI != null)
416 len += clientSI.getDataLength();
417 if (_integrity != null) {
418 len += _integrity.getDataLength();
421 _hdr.setMsgLength((int) len);
425 * Write an object textual description in the output stream
430 * @throws IOException
433 public void dump(OutputStream os) throws IOException {
436 if (_clientHandle != null)
437 _clientHandle.dump(os);
441 // Display any local decisions
442 for (Enumeration e = _decisions.keys(); e.hasMoreElements();) {
444 COPSContext context = (COPSContext) e.nextElement();
445 Vector v = (Vector) _decisions.get(context);
448 for (Enumeration ee = v.elements(); ee.hasMoreElements();) {
449 COPSDecision decision = (COPSDecision) ee.nextElement();
453 if (clientSI != null)
455 if (_integrity != null) {