The second patch of an estimated 4 to complete the COPS message refactoring as descri...
[packetcable.git] / packetcable-driver / src / main / java / org / umu / cops / stack / COPSKAMsg.java
1 /*
2  * Copyright (c) 2003 University of Murcia.  All rights reserved.
3  * --------------------------------------------------------------
4  * For more information, please see <http://www.umu.euro6ix.org/>.
5  */
6
7 package org.umu.cops.stack;
8
9 import java.io.IOException;
10 import java.io.OutputStream;
11 import java.net.Socket;
12
13 /**
14  * COPS Keep Alive Message
15  *
16  * @version COPSKAMsg.java, v 1.00 2003
17  *
18  */
19 public class COPSKAMsg extends COPSMsg {
20
21     /* COPSHeader coming from base class */
22     private COPSIntegrity  _integrity;
23
24     public COPSKAMsg() {
25         _integrity = null;
26     }
27
28     protected COPSKAMsg(byte[] data) throws COPSException {
29         _integrity = null;
30         parse(data);
31     }
32
33     /** Checks the sanity of COPS message and throw an
34       * COPSBadDataException when data is bad.
35       */
36     public void checkSanity() throws COPSException {
37         //The client type in the header MUST always be set to 0
38         //as KA is used for connection verification.RFC 2748
39         if ((_hdr == null) && (_hdr.getClientType() != 0))
40             throw new COPSException("Bad message format");
41     }
42
43     /**
44      * Add message header
45      *
46      * @param    hdr                 a  COPSHeader
47      *
48      * @throws   COPSException
49      *
50      */
51     public void add (COPSHeader hdr) throws COPSException {
52         if (hdr == null)
53             throw new COPSException ("Null Header");
54         if (hdr.getOpCode() != COPSHeader.COPS_OP_KA)
55             throw new COPSException ("Error Header (no COPS_OP_KA)");
56         _hdr = hdr;
57         setMsgLength();
58     }
59
60     /**
61      * Add Integrity objects
62      *
63      * @param    integrity           a  COPSIntegrity
64      *
65      * @throws   COPSException
66      *
67      */
68     public void add (COPSIntegrity integrity) throws COPSException {
69         if (integrity == null)
70             throw new COPSException ("Null Integrity");
71         _integrity = integrity;
72         setMsgLength();
73     }
74
75     /**
76      * Returns true if it has Integrity object
77      *
78      * @return   a boolean
79      *
80      */
81     public boolean hasIntegrity() {
82         return (_integrity != null);
83     };
84
85     /**
86      * Should check hasIntegrity() before calling
87      *
88      * @return   a COPSIntegrity
89      *
90      */
91     public COPSIntegrity getIntegrity() {
92         return (_integrity);
93     }
94
95     /**
96      * Writes data to given network socket
97      *
98      * @param    id                  a  Socket
99      *
100      * @throws   IOException
101      *
102      */
103     public void writeData(Socket id) throws IOException {
104         // checkSanity();
105         if (_hdr != null) _hdr.writeData(id);
106         if (_integrity != null) _integrity.writeData(id);
107     }
108
109     /**
110      * Method parse
111      *
112      * @param    data                a  byte[]
113      *
114      * @throws   COPSException
115      *
116      */
117     protected void parse(byte[] data) throws COPSException {
118         super.parseHeader(data);
119
120         while (_dataStart < _dataLength) {
121             byte[] buf = new byte[data.length - _dataStart];
122             System.arraycopy(data,_dataStart,buf,0,data.length - _dataStart);
123
124             final COPSObjHeaderData objHdrData = COPSObjectParser.parseObjHeader(buf);
125             switch (objHdrData.header.getCNum()) {
126             case MSG_INTEGRITY:
127                 _integrity = COPSIntegrity.parse(objHdrData, buf);
128                 _dataStart += _integrity.getDataLength();
129                 break;
130             default: {
131                 throw new COPSException("Bad Message format, unknown object type");
132             }
133             }
134         }
135         checkSanity();
136     }
137
138     /**
139      * Method parse
140      *
141      * @param    hdr                 a  COPSHeader
142      * @param    data                a  byte[]
143      *
144      * @throws   COPSException
145      *
146      */
147     protected void parse(COPSHeader hdr, byte[] data)     throws COPSException {
148         if (hdr.getOpCode() != COPSHeader.COPS_OP_KA)
149             throw new COPSException("Error Header");
150         _hdr = hdr;
151         parse(data);
152         setMsgLength();
153     }
154
155     /**
156      * Set the message length, base on the set of objects it contains
157      *
158      * @throws   COPSException
159      *
160      */
161     private void setMsgLength() throws COPSException {
162         short len = 0;
163         if (_integrity != null) len += _integrity.getDataLength();
164         _hdr.setMsgLength(len);
165     }
166
167     /**
168      * Write an object textual description in the output stream
169      *
170      * @param    os                  an OutputStream
171      *
172      * @throws   IOException
173      *
174      */
175     public void dump(OutputStream os) throws IOException {
176         _hdr.dump(os);
177
178         if (_integrity != null) {
179             _integrity.dump(os);
180         }
181     }
182 }
183
184
185
186
187
188