Basic fixes to make build work :)
[packetcable.git] / protocol_plugins.packetcable / src / main / java / org / umu / cops / stack / COPSClientAcceptMsg.java
1 /*\r
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
5  */\r
6 \r
7 package org.umu.cops.stack;\r
8 \r
9 import java.io.IOException;\r
10 import java.io.OutputStream;\r
11 import java.net.Socket;\r
12 \r
13 /**\r
14  *     COPS Client Accept Message\r
15  *\r
16  * @version COPSClientAcceptMsg.java, v 1.00 2003\r
17  *\r
18  */\r
19 public class COPSClientAcceptMsg extends COPSMsg {\r
20 \r
21     /* COPSHeader coming from base class */\r
22     private COPSKATimer _kaTimer;\r
23     private COPSAcctTimer _acctTimer;\r
24     private COPSIntegrity _integrity;\r
25 \r
26     ///Constructor\r
27     public COPSClientAcceptMsg() {\r
28         _kaTimer = null;\r
29         _acctTimer = null;\r
30         _integrity = null;\r
31     }\r
32 \r
33     ///Create object from data\r
34     protected COPSClientAcceptMsg(byte[] data) throws COPSException {\r
35         parse(data);\r
36     }\r
37 \r
38     /** Checks the sanity of COPS message and throw an\r
39       * COPSBadDataException when data is bad.\r
40       */\r
41     public void checkSanity() throws COPSException {\r
42         if ((_hdr == null) || (_kaTimer == null))\r
43             throw new COPSException("Bad message format");\r
44     }\r
45 \r
46     /**\r
47      * Add message header\r
48      *\r
49      * @param    hdr                 a  COPSHeader\r
50      *\r
51      * @throws   COPSException\r
52      *\r
53      */\r
54     public void add (COPSHeader hdr) throws COPSException {\r
55         if (hdr == null)\r
56             throw new COPSException ("Null Header");\r
57         if (hdr.getOpCode() != COPSHeader.COPS_OP_CAT)\r
58             throw new COPSException ("Error Header (no COPS_OP_CAT)");\r
59         _hdr = hdr;\r
60         setMsgLength();\r
61     }\r
62 \r
63     /**\r
64      * Add Timer object to the message\r
65      *\r
66      * @param    timer               a  COPSTimer\r
67      *\r
68      * @throws   COPSException\r
69      *\r
70      */\r
71     public void add (COPSTimer timer) throws COPSException {\r
72         if (timer.isKATimer()) {\r
73             _kaTimer = (COPSKATimer) timer;\r
74         } else {\r
75             _acctTimer = (COPSAcctTimer) timer;\r
76         }\r
77         setMsgLength();\r
78     }\r
79 \r
80     /**\r
81      * Add Integrity objects\r
82      *\r
83      * @param    integrity           a  COPSIntegrity\r
84      *\r
85      * @throws   COPSException\r
86      *\r
87      */\r
88     public void add (COPSIntegrity integrity) throws COPSException {\r
89         if (integrity == null)\r
90             throw new COPSException ("Null Integrity");\r
91         if (!integrity.isMessageIntegrity())\r
92             throw new COPSException ("Error Integrity");\r
93         _integrity = integrity;\r
94         setMsgLength();\r
95     }\r
96 \r
97     /**\r
98      * Method getKATimer\r
99      *\r
100      * @return   a COPSKATimer\r
101      *\r
102      */\r
103     public COPSKATimer getKATimer() {\r
104         return _kaTimer;\r
105     };\r
106 \r
107     /**\r
108      * Returns true if has a account timer object\r
109      *\r
110      * @return   a boolean\r
111      *\r
112      */\r
113     public boolean hasAcctTimer() {\r
114         return (_acctTimer != null);\r
115     };\r
116 \r
117     /**\r
118      * Should check hasAcctTimer() before calling\r
119      *\r
120      * @return   a COPSAcctTimer\r
121      *\r
122      */\r
123     public COPSAcctTimer getAcctTimer() {\r
124         return (_acctTimer);\r
125     }\r
126 \r
127     /**\r
128      * Returns true if has a Integrity object\r
129      *\r
130      * @return   a boolean\r
131      *\r
132      */\r
133     public boolean hasIntegrity() {\r
134         return (_integrity != null);\r
135     };\r
136 \r
137     /**\r
138      * Should check hasIntegrity() before calling\r
139      *\r
140      * @return   a COPSIntegrity\r
141      *\r
142      */\r
143     public COPSIntegrity getIntegrity() {\r
144         return (_integrity);\r
145     }\r
146 \r
147     /**\r
148      * Writes data to a given socket id\r
149      *\r
150      * @param    id                  a  Socket\r
151      *\r
152      * @throws   IOException\r
153      *\r
154      */\r
155     public void writeData(Socket id) throws IOException {\r
156         // checkSanity();\r
157         if (_hdr != null) _hdr.writeData(id);\r
158         if (_kaTimer != null) _kaTimer.writeData(id);\r
159         if (_acctTimer != null) _acctTimer.writeData(id);\r
160         if (_integrity != null) _integrity.writeData(id);\r
161     }\r
162 \r
163     /**\r
164      * Method parse\r
165      *\r
166      * @param    data                a  byte[]\r
167      *\r
168      * @throws   COPSException\r
169      *\r
170      */\r
171     protected void parse(byte[] data) throws COPSException {\r
172         parseHeader(data);\r
173 \r
174         while (_dataStart < _dataLength) {\r
175             byte[] buf = new byte[data.length - _dataStart];\r
176             System.arraycopy(data,_dataStart,buf,0,data.length - _dataStart);\r
177 \r
178             COPSObjHeader objHdr = new COPSObjHeader (buf);\r
179             switch (objHdr.getCNum()) {\r
180             case COPSObjHeader.COPS_KA: {\r
181                 _kaTimer = new COPSKATimer(buf);\r
182                 _dataStart += _kaTimer.getDataLength();\r
183             }\r
184             break;\r
185             case COPSObjHeader.COPS_ACCT_TIMER: {\r
186                 _acctTimer = new COPSAcctTimer(buf);\r
187                 _dataStart += _acctTimer.getDataLength();\r
188             }\r
189             break;\r
190             case COPSObjHeader.COPS_MSG_INTEGRITY: {\r
191                 _integrity = new COPSIntegrity(buf);\r
192                 _dataStart += _integrity.getDataLength();\r
193             }\r
194             break;\r
195             default: {\r
196                 throw new COPSException("Bad Message format");\r
197             }\r
198             }\r
199         }\r
200         checkSanity();\r
201     }\r
202 \r
203     /**\r
204      * Method parse\r
205      *\r
206      * @param    hdr                 a  COPSHeader\r
207      * @param    data                a  byte[]\r
208      *\r
209      * @throws   COPSException\r
210      *\r
211      */\r
212     protected void parse(COPSHeader hdr, byte[] data) throws COPSException {\r
213         if (hdr.getOpCode() != COPSHeader.COPS_OP_CAT)\r
214             throw new COPSException("Error Header");\r
215         _hdr = hdr;\r
216         parse(data);\r
217         setMsgLength();\r
218     }\r
219 \r
220     /**\r
221      * Set the message length, base on the set of objects it contains\r
222      *\r
223      * @throws   COPSException\r
224      *\r
225      */\r
226     protected void setMsgLength() throws COPSException {\r
227         short len = 0;\r
228         if (_kaTimer != null) len += _kaTimer.getDataLength();\r
229         if (_acctTimer != null) len += _acctTimer.getDataLength();\r
230         if (_integrity != null) len += _integrity.getDataLength();\r
231         _hdr.setMsgLength(len);\r
232     }\r
233 \r
234     /**\r
235      * Write an object textual description in the output stream\r
236      *\r
237      * @param    os                  an OutputStream\r
238      *\r
239      * @throws   IOException\r
240      *\r
241      */\r
242     public void dump(OutputStream os) throws IOException {\r
243         _hdr.dump(os);\r
244 \r
245         if (_kaTimer != null)\r
246             _kaTimer.dump(os);\r
247 \r
248         if (_acctTimer != null)\r
249             _acctTimer.dump(os);\r
250 \r
251         if (_integrity != null) {\r
252             _integrity.dump(os);\r
253         }\r
254     }\r
255 }\r
256 \r