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 Context Object
\r
16 * @version COPSContext.java, v 1.00 2003
\r
19 public class COPSContext extends COPSObjBase {
\r
21 public final static byte IN_ADMIN = 0x01;
\r
22 public final static byte RES_ALLOC = 0x02;
\r
23 public final static byte OUT = 0x04;
\r
24 public final static byte CONFIG = 0x08;
\r
26 private COPSObjHeader _objHdr;
\r
27 private short _rType;
\r
28 private short _mType;
\r
31 public COPSContext(short rType, short mType ) {
\r
32 _objHdr = new COPSObjHeader();
\r
33 _objHdr.setCNum(COPSObjHeader.COPS_CONTEXT);
\r
34 _objHdr.setCType((byte) 1);
\r
37 _objHdr.setDataLength((short) 4);
\r
41 Parse the data and create a Context object
\r
43 protected COPSContext(byte[] dataPtr) {
\r
44 _objHdr = new COPSObjHeader();
\r
45 _objHdr.parse(dataPtr);
\r
46 // _objHdr.checkDataLength();
\r
48 _rType |= ((short) dataPtr[4]) << 8;
\r
49 _rType |= ((short) dataPtr[5]) & 0xFF;
\r
51 _mType |= ((short) dataPtr[6]) << 8;
\r
52 _mType |= ((short) dataPtr[7]) & 0xFF;
\r
54 _objHdr.setDataLength( (short) 4);
\r
58 * Write object in network byte order to a given network socket
\r
60 * @param id a Socket
\r
62 * @throws IOException
\r
65 public void writeData(Socket id) throws IOException {
\r
66 _objHdr.writeData(id);
\r
68 byte[] buf = new byte[4];
\r
70 buf[0] = (byte) (_rType >> 8);
\r
71 buf[1] = (byte) _rType;
\r
73 buf[2] = (byte) (_mType >> 8);
\r
74 buf[3] = (byte) _mType;
\r
76 COPSUtil.writeData(id, buf, 4);
\r
80 * Returns size in number of octects, including header
\r
85 public short getDataLength() {
\r
86 //Add the size of the header also
\r
87 return (_objHdr.getDataLength());
\r
91 * Returns the detail description of the request type
\r
96 public String getDescription() {
\r
97 String retStr = new String();
\r
98 if ((_rType & 0x01) != 0) {
\r
99 retStr += (retStr.length() != 0) ? "," : "";
\r
100 retStr += "Incoming Message/Admission Control";
\r
102 if ((_rType & 0x02) != 0) {
\r
103 retStr += (retStr.length() != 0) ? "," : "";
\r
104 retStr += "Resource allocation";
\r
106 if ((_rType & 0x04) != 0) {
\r
107 retStr += (retStr.length() != 0) ? "," : "";
\r
108 retStr += "Outgoing message";
\r
110 if ((_rType & 0x08) != 0) {
\r
111 retStr += (retStr.length() != 0) ? "," : "";
\r
112 retStr += "Configuration";
\r
118 * Method isIncomingMessage
\r
120 * @return a boolean
\r
123 public boolean isIncomingMessage() {
\r
124 return (_rType & IN_ADMIN) != 0;
\r
128 * Method isAdminControl
\r
130 * @return a boolean
\r
133 public boolean isAdminControl() {
\r
134 return (_rType & IN_ADMIN) != 0;
\r
138 * Method isResourceAllocationReq
\r
140 * @return a boolean
\r
143 public boolean isResourceAllocationReq() {
\r
144 return (_rType & RES_ALLOC) != 0;
\r
148 * Method isOutgoingMessage
\r
150 * @return a boolean
\r
153 public boolean isOutgoingMessage() {
\r
154 return (_rType & OUT) != 0;
\r
158 * Method isConfigRequest
\r
160 * @return a boolean
\r
163 public boolean isConfigRequest() {
\r
164 return (_rType & CONFIG) != 0;
\r
168 * Method getMessageType
\r
173 public short getMessageType() {
\r
178 * Method getRequestType
\r
183 public short getRequestType() {
\r
190 * @return a boolean
\r
193 public boolean isContext() {
\r
198 * Write an object textual description in the output stream
\r
200 * @param os an OutputStream
\r
202 * @throws IOException
\r
205 public void dump(OutputStream os) throws IOException {
\r
207 os.write(new String("context: " + getDescription() + "," + _mType + "\n").getBytes());
\r