4 package org.pcmm.messages.impl;
6 import org.umu.cops.stack.*;
7 import org.umu.cops.stack.COPSDecision.DecisionFlag;
8 import org.umu.cops.stack.COPSObjHeader.CNum;
10 import java.io.IOException;
11 import java.io.OutputStream;
12 import java.net.Socket;
13 import java.util.Enumeration;
14 import java.util.Hashtable;
15 import java.util.Vector;
18 * COPS Decision Message
23 public class COPSDecisionMsgEX extends COPSMsg {
25 /* COPSHeader coming from base class */
26 private COPSHandle _clientHandle;
27 private COPSError _error;
28 private Hashtable _decisions;
29 private COPSIntegrity _integrity;
30 private COPSContext _decContext;
31 private COPSClientSI clientSI;
34 public COPSDecisionMsgEX() {
37 _decisions = new Hashtable(20);
44 * Checks the sanity of COPS message and throw an COPSBadDataException when
47 public void checkSanity() throws COPSException {
48 if ((_hdr == null) || (_clientHandle == null)
49 || ((_error == null) && (_decisions.size() == 0))) {
50 throw new COPSException("Bad message format");
55 protected COPSDecisionMsgEX(byte[] data) throws COPSException {
56 _decisions = new Hashtable(20);
66 * Parses the data and fills COPSDecisionMsg with its constituents
71 * @throws COPSException
74 protected void parse(byte[] data) throws COPSException {
75 super.parseHeader(data);
77 while (_dataStart < _dataLength) {
78 byte[] buf = new byte[data.length - _dataStart];
79 System.arraycopy(data, _dataStart, buf, 0, data.length - _dataStart);
81 final COPSObjHeaderData objHdrData = COPSObjectParser.parseObjHeader(buf);
82 switch (objHdrData.header.getCNum()) {
84 _clientHandle = COPSHandle.parse(objHdrData, buf);
85 _dataStart += _clientHandle.getDataLength();
89 _decContext = COPSContext.parse(objHdrData, buf);
90 _dataStart += _decContext.getDataLength();
93 _error = COPSError.parse(objHdrData, buf);
94 _dataStart += _error.getDataLength();
97 COPSDecision decs = COPSDecision.parse(objHdrData, buf);
98 _dataStart += decs.getDataLength();
99 addDecision(decs, _decContext);
102 _integrity = COPSIntegrity.parse(objHdrData, buf);
103 _dataStart += _integrity.getDataLength();
106 clientSI = COPSClientSI.parse(objHdrData, buf);
107 _dataStart += clientSI.getDataLength();
110 throw new COPSException(
111 "Bad Message format, unknown object type");
118 * Parses the data and fills that follows the header hdr and fills
126 * @throws COPSException
129 protected void parse(COPSHeader hdr, byte[] data) throws COPSException {
141 * @throws COPSException
144 public void add(COPSHeader hdr) throws COPSException {
146 throw new COPSException("Null Header");
147 if (hdr.getOpCode() != COPSHeader.COPS_OP_DEC)
148 throw new COPSException("Error Header (no COPS_OP_DEC)");
154 * Add client handle to the message
159 * @throws COPSException
162 public void add(COPSHandle handle) throws COPSException {
164 throw new COPSException("Null Handle");
165 _clientHandle = handle;
170 * Add an Error object
175 * @throws COPSException
178 public void add(COPSError error) throws COPSException {
179 if (_decisions.size() != 0)
180 throw new COPSException("No null decisions");
182 throw new COPSException("No null error");
183 // Message integrity object should be the very last one
184 // If it is already added
185 if (_integrity != null)
186 throw new COPSException("No null integrity");
192 * Add one or more local decision object for a given decision context the
193 * context is optional, if null all decision object are tided to message
201 * @throws COPSException
204 public void addDecision(COPSDecision decision, COPSContext context)
205 throws COPSException {
206 // Either error or decision can be added
207 // If error is aleady there assert
209 throw new COPSException("No null error");
211 if (decision.getHeader().getCNum().equals(CNum.LPDP_DEC))
212 throw new COPSException("Is local decision");
214 Vector v = (Vector) _decisions.get(context);
218 if (!decision.getFlag().equals(DecisionFlag.NA)) {// Commented out as advised by Felix
219 // if (v.size() != 0)
221 // Only one set of decision flags is allowed
223 // throw new COPSException
224 // ("Bad Message format, only one set of decision flags is allowed.");
228 // The flags decision must precede any other
229 // decision message, since the decision is not
230 // flags throw exception
231 throw new COPSException(
232 "Bad Message format, flags decision must precede any other decision object.");
236 _decisions.put(context, v);
242 * Add integrity object
247 * @throws COPSException
250 public void add(COPSIntegrity integrity) throws COPSException {
251 if (integrity == null)
252 throw new COPSException("Null Integrity");
253 _integrity = integrity;
258 * Add a client specific informations
263 * @throws COPSException
266 public void add(COPSClientSI clientSI) throws COPSException {
267 if (clientSI == null)
268 throw new COPSException("Null ClientSI");
269 this.clientSI = clientSI;
274 * Writes data to given socket
279 * @throws IOException
282 public void writeData(Socket id) throws IOException {
286 if (_clientHandle != null)
287 _clientHandle.writeData(id);
289 _error.writeData(id);
292 // Display any local decisions
293 for (Enumeration e = _decisions.keys(); e.hasMoreElements();) {
295 COPSContext context = (COPSContext) e.nextElement();
296 Vector v = (Vector) _decisions.get(context);
297 context.writeData(id);
299 for (Enumeration ee = v.elements(); ee.hasMoreElements();) {
300 COPSDecision decision = (COPSDecision) ee.nextElement();
301 decision.writeData(id);
304 if (clientSI != null)
305 clientSI.writeData(id);
306 if (_integrity != null)
307 _integrity.writeData(id);
313 * @return a COPSHeader
316 public COPSHeader getHeader() {
321 * Method getClientHandle
323 * @return a COPSHandle
326 public COPSHandle getClientHandle() {
327 return _clientHandle;
330 public COPSClientSI getClientSI() {
335 * Returns true if it has error object
340 public boolean hasError() {
341 return (_error != null);
345 * Should check hasError() before calling
347 * @return a COPSError
350 public COPSError getError() {
355 * Returns a map of decision for which is an arry of context and vector of
356 * associated decision object.
358 * @return a Hashtable
361 public Hashtable getDecisions() {
366 * Returns true if it has integrity object
371 public boolean hasIntegrity() {
372 return (_integrity != null);
376 * Should check hasIntegrity() before calling
378 * @return a COPSIntegrity
381 public COPSIntegrity getIntegrity() {
386 * Method setMsgLength
388 * @throws COPSException
391 protected void setMsgLength() throws COPSException {
393 if (_clientHandle != null)
394 len += _clientHandle.getDataLength();
396 len += _error.getDataLength();
398 // Display any local decisions
399 for (Enumeration e = _decisions.keys(); e.hasMoreElements();) {
401 COPSContext context = (COPSContext) e.nextElement();
402 Vector v = (Vector) _decisions.get(context);
403 len += context.getDataLength();
405 for (Enumeration ee = v.elements(); ee.hasMoreElements();) {
406 COPSDecision decision = (COPSDecision) ee.nextElement();
407 len += decision.getDataLength();
410 if (clientSI != null)
411 len += clientSI.getDataLength();
412 if (_integrity != null) {
413 len += _integrity.getDataLength();
416 _hdr.setMsgLength((int) len);
420 * Write an object textual description in the output stream
425 * @throws IOException
428 public void dump(OutputStream os) throws IOException {
431 if (_clientHandle != null)
432 _clientHandle.dump(os);
436 // Display any local decisions
437 for (Enumeration e = _decisions.keys(); e.hasMoreElements();) {
439 COPSContext context = (COPSContext) e.nextElement();
440 Vector v = (Vector) _decisions.get(context);
443 for (Enumeration ee = v.elements(); ee.hasMoreElements();) {
444 COPSDecision decision = (COPSDecision) ee.nextElement();
448 if (clientSI != null)
450 if (_integrity != null) {