Merge "Remove "response" from yang."
[packetcable.git] / protocol_plugins.packetcable / src / main / java / org / umu / cops / stack / COPSClientCloseMsg.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 Close Message\r
15  *\r
16  * @version COPSClientCloseMsg.java, v 1.00 2003\r
17  *\r
18  */\r
19 public class COPSClientCloseMsg extends COPSMsg {\r
20 \r
21     /* COPSHeader coming from base class */\r
22     private COPSError _error;\r
23     private COPSIntegrity _integrity;\r
24 \r
25 \r
26     public COPSClientCloseMsg() {\r
27         _error = null;\r
28         _integrity = null;\r
29     }\r
30 \r
31     protected COPSClientCloseMsg(byte[] data) throws COPSException {\r
32         parse (data);\r
33     }\r
34 \r
35     /** Checks the sanity of COPS message and throw an\r
36       * COPSBadDataException when data is bad.\r
37       */\r
38     public void checkSanity() throws COPSException {\r
39         if ((_hdr == null) || (_error == null))\r
40             throw new COPSException("Bad message format");\r
41     }\r
42 \r
43     /**\r
44      * Add message header\r
45      *\r
46      * @param    hdr                 a  COPSHeader\r
47      *\r
48      * @throws   COPSException\r
49      *\r
50      */\r
51     public void add (COPSHeader hdr) throws COPSException {\r
52         if (hdr == null)\r
53             throw new COPSException ("Null Header");\r
54         if (hdr.getOpCode() != COPSHeader.COPS_OP_CC)\r
55             throw new COPSException ("Error Header (no COPS_OP_CC)");\r
56         _hdr = hdr;\r
57         setMsgLength();\r
58     }\r
59 \r
60     /**\r
61      * Add Error object\r
62      *\r
63      * @param    error               a  COPSError\r
64      *\r
65      * @throws   COPSException\r
66      *\r
67      */\r
68     public void add (COPSError error) throws COPSException {\r
69         //Message integrity object should be the very last one\r
70         //If it is already added\r
71         if (_error != null)\r
72             throw new COPSException ("No null Error");\r
73         _error = error;\r
74         setMsgLength();\r
75     }\r
76 \r
77     /**\r
78      * Add Integrity objects\r
79      *\r
80      * @param    integrity           a  COPSIntegrity\r
81      *\r
82      * @throws   COPSException\r
83      *\r
84      */\r
85     public void add (COPSIntegrity integrity) throws COPSException {\r
86         if (integrity == null)\r
87             throw new COPSException ("Null Integrity");\r
88         if (!integrity.isMessageIntegrity())\r
89             throw new COPSException ("Error Integrity");\r
90         _integrity = integrity;\r
91         setMsgLength();\r
92     }\r
93 \r
94     /**\r
95      * Method getError\r
96      *\r
97      * @return   a COPSError\r
98      *\r
99      */\r
100     public COPSError getError() {\r
101         return (_error);\r
102     }\r
103 \r
104     /**\r
105      * Returns true If it has integrity object\r
106      *\r
107      * @return   a boolean\r
108      *\r
109      */\r
110     public boolean hasIntegrity() {\r
111         return (_integrity != null);\r
112     };\r
113 \r
114     /**\r
115      * Should check hasIntegrity() before calling\r
116      *\r
117      * @return   a COPSIntegrity\r
118      *\r
119      */\r
120     public COPSIntegrity getIntegrity() {\r
121         return (_integrity);\r
122     }\r
123 \r
124     /**\r
125      * Write object data to given network socket\r
126      *\r
127      * @param    id                  a  Socket\r
128      *\r
129      * @throws   IOException\r
130      *\r
131      */\r
132     public void writeData(Socket id) throws IOException {\r
133         // checkSanity();\r
134         if (_hdr != null) _hdr.writeData(id);\r
135         if (_error != null) _error.writeData(id);\r
136         if (_integrity != null) _integrity.writeData(id);\r
137     }\r
138 \r
139     /**\r
140      * Method parse\r
141      *\r
142      * @param    data                a  byte[]\r
143      *\r
144      * @throws   COPSException\r
145      *\r
146      */\r
147     protected void parse(byte[] data) throws COPSException {\r
148         parseHeader(data);\r
149 \r
150         while (_dataStart < _dataLength) {\r
151             byte[] buf = new byte[data.length - _dataStart];\r
152             System.arraycopy(data,_dataStart,buf,0,data.length - _dataStart);\r
153 \r
154             COPSObjHeader objHdr = new COPSObjHeader (buf);\r
155             switch (objHdr.getCNum()) {\r
156             case COPSObjHeader.COPS_ERROR: {\r
157                 _error = new COPSError(buf);\r
158                 _dataStart += _error.getDataLength();\r
159             }\r
160             break;\r
161             case COPSObjHeader.COPS_MSG_INTEGRITY: {\r
162                 _integrity = new COPSIntegrity(buf);\r
163                 _dataStart += _integrity.getDataLength();\r
164             }\r
165             break;\r
166             default: {\r
167                 throw new COPSException("Bad Message format");\r
168             }\r
169             }\r
170         }\r
171         checkSanity();\r
172     }\r
173 \r
174     /**\r
175      * Method parse\r
176      *\r
177      * @param    hdr                 a  COPSHeader\r
178      * @param    data                a  byte[]\r
179      *\r
180      * @throws   COPSException\r
181      *\r
182      */\r
183     protected void parse(COPSHeader hdr, byte[] data) throws COPSException {\r
184         if (hdr.getOpCode() != COPSHeader.COPS_OP_CC)\r
185             throw new COPSException("Error Header");\r
186         _hdr = hdr;\r
187         parse(data);\r
188         setMsgLength();\r
189     }\r
190 \r
191     /**\r
192      * Set the message length, base on the set of objects it contains\r
193      *\r
194      * @throws   COPSException\r
195      *\r
196      */\r
197     protected void setMsgLength() throws COPSException {\r
198         int len = 0;\r
199         if (_error != null) len += _error.getDataLength();\r
200         if (_integrity != null) len += _integrity.getDataLength();\r
201         _hdr.setMsgLength(len);\r
202     }\r
203 \r
204     /**\r
205      * Write an object textual description in the output stream\r
206      *\r
207      * @param    os                  an OutputStream\r
208      *\r
209      * @throws   IOException\r
210      *\r
211      */\r
212     public void dump(OutputStream os) throws IOException {\r
213         _hdr.dump(os);\r
214 \r
215         if (_error != null)\r
216             _error.dump(os);\r
217 \r
218         if (_integrity != null) {\r
219             _integrity.dump(os);\r
220         }\r
221     }\r
222 \r
223 }\r
224 \r