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 / COPSKATimer.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
10 import org.umu.cops.stack.COPSObjHeader.CNum;
11 import org.umu.cops.stack.COPSObjHeader.CType;
12
13 /**
14  * COPS Keep Alive Timer (RFC 2748)
15  *
16  * Times are encoded as 2 octet integer values and are in units of
17  * seconds.  The timer value is treated as a delta.
18  *
19  * C-Num = 10,
20  *
21  * C-Type = 1, Keep-alive timer value
22  * Timer object used to specify the maximum time interval over which a
23  * COPS message MUST be sent or received. The range of finite timeouts
24  * is 1 to 65535 seconds represented as an unsigned two-octet integer.
25  * The value of zero implies infinity.
26  */
27 public class COPSKATimer extends COPSTimer {
28
29     /**
30      * Constructor generally used for sending messages
31      * @throws java.lang.IllegalArgumentException
32      */
33     public COPSKATimer(final short timeVal) {
34         this((short)0, timeVal);
35     }
36
37     /**
38      * Constructor to override the reserved member value from 0
39      * @throws java.lang.IllegalArgumentException
40      */
41     public COPSKATimer(final short reserved, final short timeVal) {
42         this(new COPSObjHeader(CNum.KA, CType.DEF), reserved, timeVal);
43     }
44
45     /**
46      * Constructor generally used when parsing the bytes of an inbound COPS message but can also be used when the
47      * COPSObjHeader information is known
48      * @param hdr - the object header
49      * @param reserved - ???
50      * @param timeVal - the timer value
51      * @throws java.lang.IllegalArgumentException
52      */
53     protected COPSKATimer(final COPSObjHeader hdr, final short reserved, final short timeVal) {
54         super(hdr, reserved, timeVal);
55         if (!hdr.getCNum().equals(CNum.KA))
56             throw new IllegalArgumentException("Invalid CNum value. Must be " + CNum.KA);
57         if (!hdr.getCType().equals(CType.DEF))
58             throw new IllegalArgumentException("Invalid CType value. Must be " + CType.DEF);
59     }
60
61     /**
62      * Creates this object from a byte array
63      * @param objHdrData - the header
64      * @param dataPtr - the data to parse
65      * @return - a new Timer
66      * @throws java.lang.IllegalArgumentException
67      */
68     public static COPSKATimer parse(final COPSObjHeaderData objHdrData, byte[] dataPtr) {
69         short reserved = 0;
70         reserved |= ((short) dataPtr[4]) << 8;
71         reserved |= ((short) dataPtr[5]) & 0xFF;
72
73         short timerValue = 0;
74         timerValue |= ((short) dataPtr[6]) << 8;
75         timerValue |= ((short) dataPtr[7]) & 0xFF;
76
77         return new COPSKATimer(objHdrData.header, reserved, timerValue);
78     }
79
80 }
81