2 #################################################################################
4 ## Copyright (c) 2014, 2015 Cable Television Laboratories, Inc and others. ##
5 ## All rights reserved. ##
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 ##
11 #################################################################################
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18 import org.umu.cops.stack.COPSClientCloseMsg;
19 import org.umu.cops.stack.COPSError;
21 import javax.annotation.concurrent.ThreadSafe;
22 import java.io.IOException;
23 import java.net.Socket;
26 * Abstract class for all COPS connection implementations.
29 public abstract class COPSConnection implements Runnable {
31 private final static Logger logger = LoggerFactory.getLogger(COPSConnection.class);
34 Socket connected to PEP
36 protected final Socket _sock;
39 * Accounting timer value (secs)
40 * TODO FIXME - Why is this member never being used?
42 protected transient short _acctTimer;
45 * Keep-alive timer value (secs)
47 protected transient short _kaTimer;
50 * COPS error returned by PEP
52 protected transient COPSError _error;
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
60 protected COPSConnection(final Socket sock, final short kaTimer, final short acctTimer) {
62 this._kaTimer = kaTimer;
63 this._acctTimer = acctTimer;
67 * Sets the keep-alive timer value
68 * @param kaTimer Keep-alive timer value (secs)
70 public void setKaTimer(short kaTimer) {
75 * Sets the accounting timer value
76 * @param acctTimer Accounting timer value (secs)
78 public void setAcctTimer(short acctTimer) {
79 _acctTimer = acctTimer;
83 * Checks whether the socket to the PEP is closed or not
84 * @return <tt>true</tt> if closed, <tt>false</tt> otherwise
86 public boolean isClosed() {
87 return _sock.isClosed();
91 * Closes the socket to the PEP
94 if (!_sock.isClosed())
97 } catch (IOException e) {
98 logger.error("Error closing socket", e);
103 * Gets the socket to the PEP
104 * @return Socket connected to the PEP
106 public Socket getSocket() {
112 * @return a COPSError
114 protected COPSError getError() {
119 * Handle Client Close Message, close the passed connection
120 * @param conn a Socket
121 * @param cMsg a COPSClientCloseMsg
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() + "]");
129 if (cMsg.getIntegrity() != null) {
130 logger.warn("Unsupported objects (Integrity) to connection " + conn.getInetAddress());
133 } catch (Exception unae) {
134 logger.error("Unexpected exception closing connection", unae);