Add missing license headers to packetcable-driver umu/cops
[packetcable.git] / packetcable-driver / src / main / java / org / umu / cops / COPSConnection.java
1 /*
2  * Copyright (c) 2014, 2015 Cable Television Laboratories, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.umu.cops;
10
11 import org.slf4j.Logger;
12 import org.slf4j.LoggerFactory;
13 import org.umu.cops.stack.COPSClientCloseMsg;
14 import org.umu.cops.stack.COPSError;
15
16 import javax.annotation.concurrent.ThreadSafe;
17 import java.io.IOException;
18 import java.net.Socket;
19
20 /**
21  * Abstract class for all COPS connection implementations.
22  */
23 @ThreadSafe
24 public abstract class COPSConnection implements Runnable {
25
26     private final static Logger logger = LoggerFactory.getLogger(COPSConnection.class);
27
28     /**
29      Socket connected to PEP
30      */
31     protected final Socket _sock;
32
33     /**
34      * Accounting timer value (secs)
35      * TODO FIXME - Why is this member never being used?
36      */
37     protected transient short _acctTimer;
38
39     /**
40      * Keep-alive timer value (secs)
41      */
42     protected transient short _kaTimer;
43
44     /**
45      * COPS error returned by PEP
46      */
47     protected transient COPSError _error;
48
49     /**
50      * Constructor for children
51      * @param sock - Socket connection to PDP or PEP
52      * @param kaTimer - the Keep-alive timer value
53      * @param acctTimer - the accounting timer value
54      */
55     protected COPSConnection(final Socket sock, final short kaTimer, final short acctTimer) {
56         this._sock = sock;
57         this._kaTimer = kaTimer;
58         this._acctTimer = acctTimer;
59     }
60
61     /**
62      * Sets the keep-alive timer value
63      * @param kaTimer Keep-alive timer value (secs)
64      */
65     public void setKaTimer(short kaTimer) {
66         _kaTimer = kaTimer;
67     }
68
69     /**
70      * Sets the accounting timer value
71      * @param acctTimer Accounting timer value (secs)
72      */
73     public void setAcctTimer(short acctTimer) {
74         _acctTimer = acctTimer;
75     }
76
77     /**
78      * Checks whether the socket to the PEP is closed or not
79      * @return   <tt>true</tt> if closed, <tt>false</tt> otherwise
80      */
81     public boolean isClosed() {
82         return _sock.isClosed();
83     }
84
85     /**
86      * Closes the socket to the PEP
87      */
88     public void close() {
89         if (!_sock.isClosed())
90             try {
91                 _sock.close();
92             } catch (IOException e) {
93                 logger.error("Error closing socket", e);
94             }
95     }
96
97     /**
98      * Gets the socket to the PEP
99      * @return   Socket connected to the PEP
100      */
101     public Socket getSocket() {
102         return _sock;
103     }
104
105     /**
106      * Method getError
107      * @return   a COPSError
108      */
109     protected COPSError getError()  {
110         return _error;
111     }
112
113     /**
114      * Handle Client Close Message, close the passed connection
115      * @param    conn                a  Socket
116      * @param    cMsg                 a  COPSClientCloseMsg
117      */
118     protected void handleClientCloseMsg(final Socket conn, final COPSClientCloseMsg cMsg) {
119         _error = cMsg.getError();
120         logger.info("Got close request, closing connection "
121                 + conn.getInetAddress() + ":" + conn.getPort() + ":[Error " + _error.getDescription() + "]");
122         try {
123             // Support
124             if (cMsg.getIntegrity() != null) {
125                 logger.warn("Unsupported objects (Integrity) to connection " + conn.getInetAddress());
126             }
127             conn.close();
128         } catch (Exception unae) {
129             logger.error("Unexpected exception closing connection", unae);
130         }
131     }
132
133 }