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