Fixed building of models, moved code into directory structure.
[packetcable.git] / protocol_plugins.packetcable / src / main / java / org / umu / cops / stack / COPSKAMsg.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 Keep Alive Message\r
15  *\r
16  * @version COPSKAMsg.java, v 1.00 2003\r
17  *\r
18  */\r
19 public class COPSKAMsg extends COPSMsg {\r
20 \r
21     /* COPSHeader coming from base class */\r
22     private COPSIntegrity  _integrity;\r
23 \r
24     public COPSKAMsg() {\r
25         _integrity = null;\r
26     }\r
27 \r
28     protected COPSKAMsg(byte[] data) throws COPSException {\r
29         _integrity = null;\r
30         parse(data);\r
31     }\r
32 \r
33     /** Checks the sanity of COPS message and throw an\r
34       * COPSBadDataException when data is bad.\r
35       */\r
36     public void checkSanity() throws COPSException {\r
37         //The client type in the header MUST always be set to 0\r
38         //as KA is used for connection verification.RFC 2748\r
39         if ((_hdr == null) && (_hdr.getClientType() != 0))\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_KA)\r
55             throw new COPSException ("Error Header (no COPS_OP_KA)");\r
56         _hdr = hdr;\r
57         setMsgLength();\r
58     }\r
59 \r
60     /**\r
61      * Add Integrity objects\r
62      *\r
63      * @param    integrity           a  COPSIntegrity\r
64      *\r
65      * @throws   COPSException\r
66      *\r
67      */\r
68     public void add (COPSIntegrity integrity) throws COPSException {\r
69         if (integrity == null)\r
70             throw new COPSException ("Null Integrity");\r
71         if (!integrity.isMessageIntegrity())\r
72             throw new COPSException ("Error Integrity");\r
73         _integrity = integrity;\r
74         setMsgLength();\r
75     }\r
76 \r
77     /**\r
78      * Returns true if it has Integrity object\r
79      *\r
80      * @return   a boolean\r
81      *\r
82      */\r
83     public boolean hasIntegrity() {\r
84         return (_integrity != null);\r
85     };\r
86 \r
87     /**\r
88      * Should check hasIntegrity() before calling\r
89      *\r
90      * @return   a COPSIntegrity\r
91      *\r
92      */\r
93     public COPSIntegrity getIntegrity() {\r
94         return (_integrity);\r
95     }\r
96 \r
97     /**\r
98      * Writes data to given network socket\r
99      *\r
100      * @param    id                  a  Socket\r
101      *\r
102      * @throws   IOException\r
103      *\r
104      */\r
105     public void writeData(Socket id) throws IOException {\r
106         // checkSanity();\r
107         if (_hdr != null) _hdr.writeData(id);\r
108         if (_integrity != null) _integrity.writeData(id);\r
109     }\r
110 \r
111     /**\r
112      * Method parse\r
113      *\r
114      * @param    data                a  byte[]\r
115      *\r
116      * @throws   COPSException\r
117      *\r
118      */\r
119     protected void parse(byte[] data) throws COPSException {\r
120         super.parseHeader(data);\r
121 \r
122         while (_dataStart < _dataLength) {\r
123             byte[] buf = new byte[data.length - _dataStart];\r
124             System.arraycopy(data,_dataStart,buf,0,data.length - _dataStart);\r
125 \r
126             COPSObjHeader objHdr = new COPSObjHeader (buf);\r
127             switch (objHdr.getCNum()) {\r
128             case COPSObjHeader.COPS_MSG_INTEGRITY: {\r
129                 _integrity = new COPSIntegrity(buf);\r
130                 _dataStart += _integrity.getDataLength();\r
131             }\r
132             break;\r
133             default: {\r
134                 throw new COPSException("Bad Message format, unknown object type");\r
135             }\r
136             }\r
137         }\r
138         checkSanity();\r
139     }\r
140 \r
141     /**\r
142      * Method parse\r
143      *\r
144      * @param    hdr                 a  COPSHeader\r
145      * @param    data                a  byte[]\r
146      *\r
147      * @throws   COPSException\r
148      *\r
149      */\r
150     protected void parse(COPSHeader hdr, byte[] data)     throws COPSException {\r
151         if (hdr.getOpCode() != COPSHeader.COPS_OP_KA)\r
152             throw new COPSException("Error Header");\r
153         _hdr = hdr;\r
154         parse(data);\r
155         setMsgLength();\r
156     }\r
157 \r
158     /**\r
159      * Set the message length, base on the set of objects it contains\r
160      *\r
161      * @throws   COPSException\r
162      *\r
163      */\r
164     private void setMsgLength() throws COPSException {\r
165         short len = 0;\r
166         if (_integrity != null) len += _integrity.getDataLength();\r
167         _hdr.setMsgLength(len);\r
168     }\r
169 \r
170     /**\r
171      * Write an object textual description in the output stream\r
172      *\r
173      * @param    os                  an OutputStream\r
174      *\r
175      * @throws   IOException\r
176      *\r
177      */\r
178     public void dump(OutputStream os) throws IOException {\r
179         _hdr.dump(os);\r
180 \r
181         if (_integrity != null) {\r
182             _integrity.dump(os);\r
183         }\r
184     }\r
185 }\r
186 \r
187 \r
188 \r
189 \r
190 \r
191 \r