2 * Copyright (c) 2014, 2015 Cable Television Laboratories, Inc. and others. All rights reserved.
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
11 import org.slf4j.Logger;
12 import org.slf4j.LoggerFactory;
13 import org.umu.cops.stack.COPSClientCloseMsg;
14 import org.umu.cops.stack.COPSError;
16 import javax.annotation.concurrent.ThreadSafe;
17 import java.io.IOException;
18 import java.net.Socket;
21 * Abstract class for all COPS connection implementations.
24 public abstract class COPSConnection implements Runnable {
26 private final static Logger logger = LoggerFactory.getLogger(COPSConnection.class);
29 Socket connected to PEP
31 protected final Socket _sock;
34 * Accounting timer value (secs)
35 * TODO FIXME - Why is this member never being used?
37 protected transient short _acctTimer;
40 * Keep-alive timer value (secs)
42 protected transient short _kaTimer;
45 * COPS error returned by PEP
47 protected transient COPSError _error;
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
55 protected COPSConnection(final Socket sock, final short kaTimer, final short acctTimer) {
57 this._kaTimer = kaTimer;
58 this._acctTimer = acctTimer;
62 * Sets the keep-alive timer value
63 * @param kaTimer Keep-alive timer value (secs)
65 public void setKaTimer(short kaTimer) {
70 * Sets the accounting timer value
71 * @param acctTimer Accounting timer value (secs)
73 public void setAcctTimer(short acctTimer) {
74 _acctTimer = acctTimer;
78 * Checks whether the socket to the PEP is closed or not
79 * @return <tt>true</tt> if closed, <tt>false</tt> otherwise
81 public boolean isClosed() {
82 return _sock.isClosed();
86 * Closes the socket to the PEP
89 if (!_sock.isClosed())
92 } catch (IOException e) {
93 logger.error("Error closing socket", e);
98 * Gets the socket to the PEP
99 * @return Socket connected to the PEP
101 public Socket getSocket() {
107 * @return a COPSError
109 protected COPSError getError() {
114 * Handle Client Close Message, close the passed connection
115 * @param conn a Socket
116 * @param cMsg a COPSClientCloseMsg
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() + "]");
124 if (cMsg.getIntegrity() != null) {
125 logger.warn("Unsupported objects (Integrity) to connection " + conn.getInetAddress());
128 } catch (Exception unae) {
129 logger.error("Unexpected exception closing connection", unae);