Merge changes I0d4117b1,Ib233ea80,I307b4ddd,I03338c43,Id8641e04, ...
authorThomas Kee <xsited@yahoo.com>
Sat, 18 Apr 2015 00:06:07 +0000 (00:06 +0000)
committerGerrit Code Review <gerrit@opendaylight.org>
Sat, 18 Apr 2015 00:06:07 +0000 (00:06 +0000)
* changes:
  Removed DOS CR characters as per Alixis de Talhouet's code review to patch #18478.
  Removed COPSDebug.java and all remaining references.
  Replaced use of logger abstraction COPSDebug.java to having this class log directly to it's own logger as well as fixing several compiler warnings.
  Replaced use of logger abstraction COPSDebug.java to having this class log directly to it's own logger as well as fixing several compiler warnings.
  Replaced use of logger abstraction COPSDebug.java to having this class log directly to it's own logger as well as fixing several compiler warnings.
  Replaced use of logger abstraction COPSDebug.java to having this class log directly to it's own logger as well as fixing several compiler warnings.
  Replaced use of logger abstraction COPSDebug.java to having this class log directly to it's own logger as well as fixing several compiler warnings.

packetcable-driver/src/main/java/org/pcmm/PCMMPdpAgent.java
packetcable-driver/src/main/java/org/pcmm/PCMMPdpConnection.java
packetcable-driver/src/main/java/org/umu/cops/common/COPSDebug.java [deleted file]
packetcable-driver/src/main/java/org/umu/cops/ospdp/COPSPdpOSAgent.java
packetcable-driver/src/main/java/org/umu/cops/ospep/COPSPepOSConnection.java
packetcable-driver/src/main/java/org/umu/cops/prpdp/COPSPdpAgent.java
packetcable-driver/src/main/java/org/umu/cops/prpdp/COPSPdpConnection.java
packetcable-driver/src/main/java/org/umu/cops/prpep/COPSPepConnection.java

index bbb74661732a72c7ba38dcd7e4d0c42a233eafe4..7c1f47cf9892cf081cd54e6f760c24f7b6375586 100644 (file)
@@ -7,7 +7,6 @@ package org.pcmm;
 import org.pcmm.objects.MMVersionInfo;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import org.umu.cops.common.COPSDebug;
 import org.umu.cops.prpdp.COPSPdpAgent;
 import org.umu.cops.prpdp.COPSPdpException;
 import org.umu.cops.stack.*;
@@ -16,9 +15,6 @@ import java.io.IOException;
 import java.net.InetAddress;
 import java.net.Socket;
 
-// import org.umu.cops.prpdp.COPSPdpDataProcess;
-
-
 /**
  * Core PDP agent for provisioning
  */
@@ -111,7 +107,7 @@ public class PCMMPdpAgent extends COPSPdpAgent {
         try {
             socket = new Socket(addr, psPort);
         } catch (IOException e) {
-            logger.error(COPSDebug.ERROR_SOCKET, e);
+            logger.error("Error creating socket connection", e);
             return (false);
         }
         logger.info("PDP Socket Opened");
@@ -287,6 +283,7 @@ public class PCMMPdpAgent extends COPSPdpAgent {
         try {
             man.initRequestState(conn);
         } catch (COPSPdpException unae) {
+            logger.error("Error initializing the state manager's request state");
         }
         // XXX - End handleRequestMsg
 
index e691721acf26347e88df2934eaa7191279db24a3..a6c2c01b4a6a754c9aef238af42c92157102db0f 100644 (file)
-/*\r
- @header@\r
- */\r
-\r
-package org.pcmm;\r
-\r
-import org.slf4j.Logger;\r
-import org.slf4j.LoggerFactory;\r
-import org.umu.cops.prpdp.COPSPdpException;\r
-import org.umu.cops.stack.*;\r
-\r
-import java.io.IOException;\r
-import java.net.Socket;\r
-import java.util.Date;\r
-import java.util.Enumeration;\r
-import java.util.Hashtable;\r
-\r
-/**\r
- * Class for managing an provisioning connection at the PDP side.\r
- */\r
-public class PCMMPdpConnection implements Runnable {\r
-\r
-\r
-    public final static Logger logger = LoggerFactory.getLogger(PCMMPdpConnection.class);\r
-\r
-    /**\r
-    Socket connected to PEP\r
-     */\r
-    private Socket _sock;\r
-\r
-    /**\r
-    PEP identifier\r
-    */\r
-    private COPSPepId _pepId;\r
-\r
-    /**\r
-    Time of the latest keep-alive sent\r
-     */\r
-    private Date _lastKa;\r
-\r
-    /**\r
-   Opcode of the latest message sent\r
-    */\r
-    private byte _lastmessage;\r
-\r
-    /**\r
-     *  Time of the latest keep-alive received\r
-     */\r
-    protected Date _lastRecKa;\r
-\r
-    /**\r
-   Maps a Client Handle to a Handler\r
-     */\r
-    protected Hashtable _managerMap;\r
-    // map < String(COPSHandle), COPSPdpHandler> HandlerMap;\r
-\r
-    /**\r
-     *  PDP policy data processor class\r
-     */\r
-    protected PCMMPdpDataProcess _process;\r
-\r
-    /**\r
-   Accounting timer value (secs)\r
-     */\r
-    protected short _acctTimer;\r
-\r
-    /**\r
-   Keep-alive timer value (secs)\r
-     */\r
-    protected short _kaTimer;\r
-\r
-    /**\r
-   COPS error returned by PEP\r
-     */\r
-    protected COPSError _error;\r
-\r
-    /**\r
-     * Creates a new PDP connection\r
-     *\r
-     * @param pepId PEP-ID of the connected PEP\r
-     * @param sock Socket connected to PEP\r
-     * @param process Object for processing policy data\r
-     */\r
-    public PCMMPdpConnection(COPSPepId pepId, Socket sock, PCMMPdpDataProcess process) {\r
-        _sock = sock;\r
-        _pepId = pepId;\r
-\r
-        _lastKa = new Date();\r
-        _lastmessage = COPSHeader.COPS_OP_OPN;\r
-        _managerMap = new Hashtable(20);\r
-\r
-        _kaTimer = 120;\r
-        _process = process;\r
-    }\r
-\r
-    /**\r
-     * Gets the time of that latest keep-alive sent\r
-     * @return Time of that latest keep-alive sent\r
-     */\r
-    public Date getLastKAlive() {\r
-        return _lastKa;\r
-    }\r
-\r
-    /**\r
-     * Sets the keep-alive timer value\r
-     * @param kaTimer Keep-alive timer value (secs)\r
-     */\r
-    public void setKaTimer(short kaTimer) {\r
-        _kaTimer = kaTimer;\r
-    }\r
-\r
-    /**\r
-     * Gets the keep-alive timer value\r
-     * @return Keep-alive timer value (secs)\r
-     */\r
-    public short getKaTimer() {\r
-        return _kaTimer;\r
-    }\r
-\r
-    /**\r
-     * Sets the accounting timer value\r
-     * @param acctTimer Accounting timer value (secs)\r
-     */\r
-    public void setAccTimer(short acctTimer) {\r
-        _acctTimer = acctTimer;\r
-    }\r
-\r
-    /**\r
-     * Gets the accounting timer value\r
-     * @return Accounting timer value (secs)\r
-     */\r
-    public short getAcctTimer() {\r
-        return _acctTimer;\r
-    }\r
-\r
-    /**\r
-     * Gets the latest COPS message\r
-     * @return   Code of the latest message sent\r
-     */\r
-    public byte getLastMessage() {\r
-        return _lastmessage;\r
-    }\r
-\r
-    /**\r
-     * Gets active handles\r
-     * @return   An <tt>Enumeration</tt> holding all active handles\r
-     */\r
-    public Enumeration getHandles() {\r
-        return _managerMap.keys();\r
-    }\r
-\r
-    /**\r
-     * Gets the handle map\r
-     * @return   A <tt>Hashtable</tt> holding the handle map\r
-     */\r
-    public Hashtable getReqStateMans() {\r
-        return _managerMap;\r
-    }\r
-\r
-    /**\r
-     * Gets the PEP-ID\r
-     * @return   The ID of the PEP, as a <tt>String</tt>\r
-     */\r
-    public String getPepId() {\r
-        return _pepId.getData().str();\r
-    }\r
-\r
-    /**\r
-     * Checks whether the socket to the PEP is closed or not\r
-     * @return   <tt>true</tt> if closed, <tt>false</tt> otherwise\r
-     */\r
-    public boolean isClosed() {\r
-        return _sock.isClosed();\r
-    }\r
-\r
-    /**\r
-     * Closes the socket to the PEP\r
-     * @throws IOException\r
-     */\r
-    protected void close()\r
-    throws IOException {\r
-        _sock.close();\r
-    }\r
-\r
-    /**\r
-     * Gets the socket to the PEP\r
-     * @return   Socket connected to the PEP\r
-     */\r
-    public Socket getSocket() {\r
-        return _sock;\r
-    }\r
-\r
-    /**\r
-     * Main loop\r
-     */\r
-    public void run () {\r
-        Date _lastSendKa = new Date();\r
-        _lastRecKa = new Date();\r
-        try {\r
-            while (!_sock.isClosed()) {\r
-                if (_sock.getInputStream().available() != 0) {\r
-                    _lastmessage = processMessage(_sock);\r
-                    _lastRecKa = new Date();\r
-                }\r
-\r
-                // Keep Alive\r
-                if (_kaTimer > 0) {\r
-                    // Timeout at PDP\r
-                    int _startTime = (int) (_lastRecKa.getTime());\r
-                    int cTime = (int) (new Date().getTime());\r
-\r
-                    if ((int)(cTime - _startTime) > _kaTimer*1000) {\r
-                        _sock.close();\r
-                        // Notify all Request State Managers\r
-                        notifyNoKAAllReqStateMan();\r
-                    }\r
-\r
-                    // Send to PEP\r
-                    _startTime = (int) (_lastSendKa.getTime());\r
-                    cTime = (int) (new Date().getTime());\r
-\r
-                    if ((cTime - _startTime) > ((_kaTimer * 3/4) * 1000)) {\r
-                        COPSHeader hdr = new COPSHeader(COPSHeader.COPS_OP_KA);\r
-                        COPSKAMsg msg = new COPSKAMsg();\r
-\r
-                        msg.add(hdr);\r
-\r
-                        COPSTransceiver.sendMsg(msg, _sock);\r
-                        _lastSendKa = new Date();\r
-                    }\r
-                }\r
-\r
-                try {\r
-                    Thread.sleep(500);\r
-                } catch (Exception e) {\r
-                    logger.error("Unexpected exception while sleeping", e);\r
-                }\r
-\r
-            }\r
-        } catch (Exception e) {\r
-            logger.error("Error reading messages from socket", e);\r
-        }\r
-\r
-        // connection closed by server\r
-        // COPSDebug.out(getClass().getName(),"Connection closed by client");\r
-        try {\r
-            _sock.close();\r
-        } catch (IOException e) {\r
-            logger.error("Error closing socket", e);\r
-        }\r
-\r
-        // Notify all Request State Managers\r
-        try {\r
-            notifyCloseAllReqStateMan();\r
-        } catch (COPSPdpException e) {\r
-            logger.error("Error closing state managers", e);\r
-        }\r
-    }\r
-\r
-    /**\r
-     * Gets a COPS message from the socket and processes it\r
-     * @param    conn Socket connected to the PEP\r
-     * @return Type of COPS message\r
-     */\r
-    private byte processMessage(Socket conn)\r
-    throws COPSPdpException, COPSException, IOException {\r
-        COPSMsg msg = COPSTransceiver.receiveMsg(conn);\r
-\r
-        if (msg.getHeader().isAClientClose()) {\r
-            handleClientCloseMsg(conn, msg);\r
-            return COPSHeader.COPS_OP_CC;\r
-        } else if (msg.getHeader().isAKeepAlive()) {\r
-            handleKeepAliveMsg(conn, msg);\r
-            return COPSHeader.COPS_OP_KA;\r
-        } else if (msg.getHeader().isARequest()) {\r
-            handleRequestMsg(conn, msg);\r
-            return COPSHeader.COPS_OP_REQ;\r
-        } else if (msg.getHeader().isAReport()) {\r
-            handleReportMsg(conn, msg);\r
-            return COPSHeader.COPS_OP_RPT;\r
-        } else if (msg.getHeader().isADeleteReq()) {\r
-            handleDeleteRequestMsg(conn, msg);\r
-            return COPSHeader.COPS_OP_DRQ;\r
-        } else if (msg.getHeader().isASyncComplete()) {\r
-            handleSyncComplete(conn, msg);\r
-            return COPSHeader.COPS_OP_SSC;\r
-        } else {\r
-            throw new COPSPdpException("Message not expected (" + msg.getHeader().getOpCode() + ").");\r
-        }\r
-    }\r
-\r
-    /**\r
-     * Handle Client Close Message, close the passed connection\r
-     *\r
-     * @param    conn                a  Socket\r
-     * @param    msg                 a  COPSMsg\r
-     *\r
-     *\r
-     * <Client-Close> ::= <Common Header>\r
-     *  <Error>\r
-     *  [<Integrity>]\r
-     *\r
-     * Not support [<Integrity>]\r
-     *\r
-     */\r
-    private void handleClientCloseMsg(Socket conn, COPSMsg msg) {\r
-        COPSClientCloseMsg cMsg = (COPSClientCloseMsg) msg;\r
-        _error = cMsg.getError();\r
-\r
-        // COPSDebug.out(getClass().getName(),"Got close request, closing connection " +\r
-        //  conn.getInetAddress() + ":" + conn.getPort() + ":[Error " + _error.getDescription() + "]");\r
-\r
-        try {\r
-            // Support\r
-            if (cMsg.getIntegrity() != null) {\r
-                logger.error("Unsupported objects (Integrity) to connection " + conn.getInetAddress());\r
-            }\r
-\r
-            conn.close();\r
-        } catch (Exception unae) {\r
-            logger.error("Unexpected exception closing connection", unae);\r
-        }\r
-    }\r
-\r
-    /**\r
-     * Gets the occurred COPS Error\r
-     * @return   <tt>COPSError</tt> object\r
-     */\r
-    protected COPSError getError()  {\r
-        return _error;\r
-    }\r
-\r
-    /**\r
-     * Handle Keep Alive Message\r
-     *\r
-     * <Keep-Alive> ::= <Common Header>\r
-     *                  [<Integrity>]\r
-     *\r
-     * Not support [<Integrity>]\r
-     *\r
-     * @param    conn                a  Socket\r
-     * @param    msg                 a  COPSMsg\r
-     *\r
-     */\r
-    private void handleKeepAliveMsg(Socket conn, COPSMsg msg) {\r
-        COPSKAMsg cMsg = (COPSKAMsg) msg;\r
-\r
-        COPSKAMsg kaMsg = (COPSKAMsg) msg;\r
-        try {\r
-            // Support\r
-            if (cMsg.getIntegrity() != null) {\r
-                logger.error("Unsupported objects (Integrity) to connection " + conn.getInetAddress());\r
-            }\r
-\r
-            kaMsg.writeData(conn);\r
-        } catch (Exception unae) {\r
-            logger.error("Unexpected exception while writing keep-alive message", unae);\r
-        }\r
-    }\r
-\r
-    /**\r
-     * Handle Delete Request Message\r
-     *\r
-     * <Delete Request> ::= <Common Header>\r
-     *                      <Client Handle>\r
-     *                      <Reason>\r
-     *                      [<Integrity>]\r
-     *\r
-     * Not support [<Integrity>]\r
-     *\r
-     * @param    conn                a  Socket\r
-     * @param    msg                 a  COPSMsg\r
-     *\r
-     */\r
-    private void handleDeleteRequestMsg(Socket conn, COPSMsg msg)\r
-    throws COPSPdpException {\r
-        COPSDeleteMsg cMsg = (COPSDeleteMsg) msg;\r
-        // COPSDebug.out(getClass().getName(),"Removing ClientHandle for " +\r
-        //  conn.getInetAddress() + ":" + conn.getPort() + ":[Reason " + cMsg.getReason().getDescription() + "]");\r
-\r
-        // Support\r
-        if (cMsg.getIntegrity() != null) {\r
-            logger.error("Unsupported objects (Integrity) to connection " + conn.getInetAddress());\r
-        }\r
-\r
-        // Delete clientHandler\r
-        if (_managerMap.remove(cMsg.getClientHandle().getId().str()) == null) {\r
-            // COPSDebug.out(getClass().getName(),"Missing for ClientHandle " +\r
-            //  cMsg.getClientHandle().getId().getData());\r
-        }\r
-\r
-        PCMMPdpReqStateMan man = (PCMMPdpReqStateMan) _managerMap.get(cMsg.getClientHandle().getId().str());\r
-        if (man == null) {\r
-            logger.warn("State manager not found");\r
-        } else {\r
-            man.processDeleteRequestState(cMsg);\r
-        }\r
-\r
-    }\r
-\r
-    /**\r
-     * Handle Request Message\r
-     *\r
-     * <Request> ::= <Common Header>\r
-     *  <Client Handle>\r
-     *  <Context>\r
-     *  *(<Named ClientSI>)\r
-     *  [<Integrity>]\r
-     * <Named ClientSI> ::= <*(<PRID> <EPD>)>\r
-     *\r
-     * Not support [<Integrity>]\r
-     *\r
-     * @param    conn                a  Socket\r
-     * @param    msg                 a  COPSMsg\r
-     *\r
-     */\r
-    private void handleRequestMsg(Socket conn, COPSMsg msg)\r
-    throws COPSPdpException {\r
-\r
-        COPSReqMsg reqMsg = (COPSReqMsg) msg;\r
-        COPSContext cntxt = reqMsg.getContext();\r
-        COPSHeader header = reqMsg.getHeader();\r
-        //short reqType = cntxt.getRequestType();\r
-        short cType   = header.getClientType();\r
-\r
-        // Support\r
-        if (reqMsg.getIntegrity() != null) {\r
-            logger.error("Unsupported objects (Integrity) to connection " + conn.getInetAddress());\r
-        }\r
-\r
-        PCMMPdpReqStateMan man;\r
-        man = (PCMMPdpReqStateMan) _managerMap.get(reqMsg.getClientHandle().getId().str());\r
-        if (man == null) {\r
-\r
-            man = new PCMMPdpReqStateMan(cType, reqMsg.getClientHandle().getId().str());\r
-            _managerMap.put(reqMsg.getClientHandle().getId().str(),man);\r
-            man.setDataProcess(_process);\r
-            man.initRequestState(_sock);\r
-\r
-            // COPSDebug.out(getClass().getName(),"createHandler called, clientType=" +\r
-            //    header.getClientType() + " msgType=" +\r
-            //    cntxt.getMessageType() + ", connId=" + conn.toString());\r
-        }\r
-\r
-        man.processRequest(reqMsg);\r
-    }\r
-\r
-    /**\r
-     * Handle Report Message\r
-     *\r
-     * <Report State> ::= <Common Header>\r
-     *  <Client Handle>\r
-     *  <Report Type>\r
-     *  *(<Named ClientSI>)\r
-     *  [<Integrity>]\r
-     *\r
-     * Not support [<Integrity>]\r
-     *\r
-     * @param    conn                a  Socket\r
-     * @param    msg                 a  COPSMsg\r
-     *\r
-     */\r
-    private void handleReportMsg(Socket conn, COPSMsg msg)\r
-    throws COPSPdpException {\r
-        COPSReportMsg repMsg = (COPSReportMsg) msg;\r
-        // COPSHandle handle = repMsg.getClientHandle();\r
-        // COPSHeader header = repMsg.getHeader();\r
-\r
-        // Support\r
-        if (repMsg.getIntegrity() != null) {\r
-            logger.error("Unsupported objects (Integrity) to connection " + conn.getInetAddress());\r
-        }\r
-\r
-        PCMMPdpReqStateMan man = (PCMMPdpReqStateMan) _managerMap.get(repMsg.getClientHandle().getId().str());\r
-        if (man == null) {\r
-            logger.warn("State manager not found");\r
-        } else {\r
-            man.processReport(repMsg);\r
-        }\r
-    }\r
-\r
-    /**\r
-     * Method handleSyncComplete\r
-     *\r
-     * @param    conn                a  Socket\r
-     * @param    msg                 a  COPSMsg\r
-     *\r
-     */\r
-    private void handleSyncComplete(Socket conn, COPSMsg msg)\r
-    throws COPSPdpException {\r
-        COPSSyncStateMsg cMsg = (COPSSyncStateMsg) msg;\r
-        // COPSHandle handle = cMsg.getClientHandle();\r
-        // COPSHeader header = cMsg.getHeader();\r
-\r
-        // Support\r
-        if (cMsg.getIntegrity() != null) {\r
-            logger.error("Unsupported objects (Integrity) to connection " + conn.getInetAddress());\r
-        }\r
-\r
-        PCMMPdpReqStateMan man = (PCMMPdpReqStateMan) _managerMap.get(cMsg.getClientHandle().getId().str());\r
-        if (man == null) {\r
-            logger.warn("State manager not found");\r
-        } else {\r
-            man.processSyncComplete(cMsg);\r
-        }\r
-    }\r
-\r
-    /**\r
-     * Requests a COPS sync from the PEP\r
-     * @throws COPSException\r
-     * @throws COPSPdpException\r
-     */\r
-    protected void syncAllRequestState()\r
-    throws COPSException, COPSPdpException {\r
-        if (_managerMap.size() > 0) {\r
-            for (Enumeration e = _managerMap.keys() ; e.hasMoreElements() ;) {\r
-                String handle = (String) e.nextElement();\r
-                PCMMPdpReqStateMan man = (PCMMPdpReqStateMan) _managerMap.get(handle);\r
-\r
-                man.syncRequestState();\r
-            }\r
-        }\r
-    }\r
-\r
-    private void notifyCloseAllReqStateMan()\r
-    throws COPSPdpException {\r
-        if (_managerMap.size() > 0) {\r
-            for (Enumeration e = _managerMap.keys() ; e.hasMoreElements() ;) {\r
-                String handle = (String) e.nextElement();\r
-                PCMMPdpReqStateMan man = (PCMMPdpReqStateMan) _managerMap.get(handle);\r
-\r
-                man.processClosedConnection(_error);\r
-            }\r
-        }\r
-    }\r
-\r
-    private void notifyNoKAAllReqStateMan()\r
-    throws COPSPdpException {\r
-        if (_managerMap.size() > 0) {\r
-            for (Enumeration e = _managerMap.keys() ; e.hasMoreElements() ;) {\r
-                String handle = (String) e.nextElement();\r
-                PCMMPdpReqStateMan man = (PCMMPdpReqStateMan) _managerMap.get(handle);\r
-\r
-                man.processNoKAConnection();\r
-            }\r
-        }\r
-    }\r
-\r
-}\r
+/*
+ @header@
+ */
+
+package org.pcmm;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.umu.cops.prpdp.COPSPdpException;
+import org.umu.cops.stack.*;
+
+import java.io.IOException;
+import java.net.Socket;
+import java.util.Date;
+import java.util.Enumeration;
+import java.util.Hashtable;
+
+/**
+ * Class for managing an provisioning connection at the PDP side.
+ */
+public class PCMMPdpConnection implements Runnable {
+
+
+    public final static Logger logger = LoggerFactory.getLogger(PCMMPdpConnection.class);
+
+    /**
+    Socket connected to PEP
+     */
+    private Socket _sock;
+
+    /**
+    PEP identifier
+    */
+    private COPSPepId _pepId;
+
+    /**
+    Time of the latest keep-alive sent
+     */
+    private Date _lastKa;
+
+    /**
+   Opcode of the latest message sent
+    */
+    private byte _lastmessage;
+
+    /**
+     *  Time of the latest keep-alive received
+     */
+    protected Date _lastRecKa;
+
+    /**
+   Maps a Client Handle to a Handler
+     */
+    protected Hashtable _managerMap;
+    // map < String(COPSHandle), COPSPdpHandler> HandlerMap;
+
+    /**
+     *  PDP policy data processor class
+     */
+    protected PCMMPdpDataProcess _process;
+
+    /**
+   Accounting timer value (secs)
+     */
+    protected short _acctTimer;
+
+    /**
+   Keep-alive timer value (secs)
+     */
+    protected short _kaTimer;
+
+    /**
+   COPS error returned by PEP
+     */
+    protected COPSError _error;
+
+    /**
+     * Creates a new PDP connection
+     *
+     * @param pepId PEP-ID of the connected PEP
+     * @param sock Socket connected to PEP
+     * @param process Object for processing policy data
+     */
+    public PCMMPdpConnection(COPSPepId pepId, Socket sock, PCMMPdpDataProcess process) {
+        _sock = sock;
+        _pepId = pepId;
+
+        _lastKa = new Date();
+        _lastmessage = COPSHeader.COPS_OP_OPN;
+        _managerMap = new Hashtable(20);
+
+        _kaTimer = 120;
+        _process = process;
+    }
+
+    /**
+     * Gets the time of that latest keep-alive sent
+     * @return Time of that latest keep-alive sent
+     */
+    public Date getLastKAlive() {
+        return _lastKa;
+    }
+
+    /**
+     * Sets the keep-alive timer value
+     * @param kaTimer Keep-alive timer value (secs)
+     */
+    public void setKaTimer(short kaTimer) {
+        _kaTimer = kaTimer;
+    }
+
+    /**
+     * Gets the keep-alive timer value
+     * @return Keep-alive timer value (secs)
+     */
+    public short getKaTimer() {
+        return _kaTimer;
+    }
+
+    /**
+     * Sets the accounting timer value
+     * @param acctTimer Accounting timer value (secs)
+     */
+    public void setAccTimer(short acctTimer) {
+        _acctTimer = acctTimer;
+    }
+
+    /**
+     * Gets the accounting timer value
+     * @return Accounting timer value (secs)
+     */
+    public short getAcctTimer() {
+        return _acctTimer;
+    }
+
+    /**
+     * Gets the latest COPS message
+     * @return   Code of the latest message sent
+     */
+    public byte getLastMessage() {
+        return _lastmessage;
+    }
+
+    /**
+     * Gets active handles
+     * @return   An <tt>Enumeration</tt> holding all active handles
+     */
+    public Enumeration getHandles() {
+        return _managerMap.keys();
+    }
+
+    /**
+     * Gets the handle map
+     * @return   A <tt>Hashtable</tt> holding the handle map
+     */
+    public Hashtable getReqStateMans() {
+        return _managerMap;
+    }
+
+    /**
+     * Gets the PEP-ID
+     * @return   The ID of the PEP, as a <tt>String</tt>
+     */
+    public String getPepId() {
+        return _pepId.getData().str();
+    }
+
+    /**
+     * Checks whether the socket to the PEP is closed or not
+     * @return   <tt>true</tt> if closed, <tt>false</tt> otherwise
+     */
+    public boolean isClosed() {
+        return _sock.isClosed();
+    }
+
+    /**
+     * Closes the socket to the PEP
+     * @throws IOException
+     */
+    protected void close()
+    throws IOException {
+        _sock.close();
+    }
+
+    /**
+     * Gets the socket to the PEP
+     * @return   Socket connected to the PEP
+     */
+    public Socket getSocket() {
+        return _sock;
+    }
+
+    /**
+     * Main loop
+     */
+    public void run () {
+        Date _lastSendKa = new Date();
+        _lastRecKa = new Date();
+        try {
+            while (!_sock.isClosed()) {
+                if (_sock.getInputStream().available() != 0) {
+                    _lastmessage = processMessage(_sock);
+                    _lastRecKa = new Date();
+                }
+
+                // Keep Alive
+                if (_kaTimer > 0) {
+                    // Timeout at PDP
+                    int _startTime = (int) (_lastRecKa.getTime());
+                    int cTime = (int) (new Date().getTime());
+
+                    if ((int)(cTime - _startTime) > _kaTimer*1000) {
+                        _sock.close();
+                        // Notify all Request State Managers
+                        notifyNoKAAllReqStateMan();
+                    }
+
+                    // Send to PEP
+                    _startTime = (int) (_lastSendKa.getTime());
+                    cTime = (int) (new Date().getTime());
+
+                    if ((cTime - _startTime) > ((_kaTimer * 3/4) * 1000)) {
+                        COPSHeader hdr = new COPSHeader(COPSHeader.COPS_OP_KA);
+                        COPSKAMsg msg = new COPSKAMsg();
+
+                        msg.add(hdr);
+
+                        COPSTransceiver.sendMsg(msg, _sock);
+                        _lastSendKa = new Date();
+                    }
+                }
+
+                try {
+                    Thread.sleep(500);
+                } catch (Exception e) {
+                    logger.error("Unexpected exception while sleeping", e);
+                }
+
+            }
+        } catch (Exception e) {
+            logger.error("Error reading messages from socket", e);
+        }
+
+        // connection closed by server
+        // COPSDebug.out(getClass().getName(),"Connection closed by client");
+        try {
+            _sock.close();
+        } catch (IOException e) {
+            logger.error("Error closing socket", e);
+        }
+
+        // Notify all Request State Managers
+        try {
+            notifyCloseAllReqStateMan();
+        } catch (COPSPdpException e) {
+            logger.error("Error closing state managers", e);
+        }
+    }
+
+    /**
+     * Gets a COPS message from the socket and processes it
+     * @param    conn Socket connected to the PEP
+     * @return Type of COPS message
+     */
+    private byte processMessage(Socket conn)
+    throws COPSPdpException, COPSException, IOException {
+        COPSMsg msg = COPSTransceiver.receiveMsg(conn);
+
+        if (msg.getHeader().isAClientClose()) {
+            handleClientCloseMsg(conn, msg);
+            return COPSHeader.COPS_OP_CC;
+        } else if (msg.getHeader().isAKeepAlive()) {
+            handleKeepAliveMsg(conn, msg);
+            return COPSHeader.COPS_OP_KA;
+        } else if (msg.getHeader().isARequest()) {
+            handleRequestMsg(conn, msg);
+            return COPSHeader.COPS_OP_REQ;
+        } else if (msg.getHeader().isAReport()) {
+            handleReportMsg(conn, msg);
+            return COPSHeader.COPS_OP_RPT;
+        } else if (msg.getHeader().isADeleteReq()) {
+            handleDeleteRequestMsg(conn, msg);
+            return COPSHeader.COPS_OP_DRQ;
+        } else if (msg.getHeader().isASyncComplete()) {
+            handleSyncComplete(conn, msg);
+            return COPSHeader.COPS_OP_SSC;
+        } else {
+            throw new COPSPdpException("Message not expected (" + msg.getHeader().getOpCode() + ").");
+        }
+    }
+
+    /**
+     * Handle Client Close Message, close the passed connection
+     *
+     * @param    conn                a  Socket
+     * @param    msg                 a  COPSMsg
+     *
+     *
+     * <Client-Close> ::= <Common Header>
+     *  <Error>
+     *  [<Integrity>]
+     *
+     * Not support [<Integrity>]
+     *
+     */
+    private void handleClientCloseMsg(Socket conn, COPSMsg msg) {
+        COPSClientCloseMsg cMsg = (COPSClientCloseMsg) msg;
+        _error = cMsg.getError();
+
+        // COPSDebug.out(getClass().getName(),"Got close request, closing connection " +
+        //  conn.getInetAddress() + ":" + conn.getPort() + ":[Error " + _error.getDescription() + "]");
+
+        try {
+            // Support
+            if (cMsg.getIntegrity() != null) {
+                logger.error("Unsupported objects (Integrity) to connection " + conn.getInetAddress());
+            }
+
+            conn.close();
+        } catch (Exception unae) {
+            logger.error("Unexpected exception closing connection", unae);
+        }
+    }
+
+    /**
+     * Gets the occurred COPS Error
+     * @return   <tt>COPSError</tt> object
+     */
+    protected COPSError getError()  {
+        return _error;
+    }
+
+    /**
+     * Handle Keep Alive Message
+     *
+     * <Keep-Alive> ::= <Common Header>
+     *                  [<Integrity>]
+     *
+     * Not support [<Integrity>]
+     *
+     * @param    conn                a  Socket
+     * @param    msg                 a  COPSMsg
+     *
+     */
+    private void handleKeepAliveMsg(Socket conn, COPSMsg msg) {
+        COPSKAMsg cMsg = (COPSKAMsg) msg;
+
+        COPSKAMsg kaMsg = (COPSKAMsg) msg;
+        try {
+            // Support
+            if (cMsg.getIntegrity() != null) {
+                logger.error("Unsupported objects (Integrity) to connection " + conn.getInetAddress());
+            }
+
+            kaMsg.writeData(conn);
+        } catch (Exception unae) {
+            logger.error("Unexpected exception while writing keep-alive message", unae);
+        }
+    }
+
+    /**
+     * Handle Delete Request Message
+     *
+     * <Delete Request> ::= <Common Header>
+     *                      <Client Handle>
+     *                      <Reason>
+     *                      [<Integrity>]
+     *
+     * Not support [<Integrity>]
+     *
+     * @param    conn                a  Socket
+     * @param    msg                 a  COPSMsg
+     *
+     */
+    private void handleDeleteRequestMsg(Socket conn, COPSMsg msg)
+    throws COPSPdpException {
+        COPSDeleteMsg cMsg = (COPSDeleteMsg) msg;
+        // COPSDebug.out(getClass().getName(),"Removing ClientHandle for " +
+        //  conn.getInetAddress() + ":" + conn.getPort() + ":[Reason " + cMsg.getReason().getDescription() + "]");
+
+        // Support
+        if (cMsg.getIntegrity() != null) {
+            logger.error("Unsupported objects (Integrity) to connection " + conn.getInetAddress());
+        }
+
+        // Delete clientHandler
+        if (_managerMap.remove(cMsg.getClientHandle().getId().str()) == null) {
+            // COPSDebug.out(getClass().getName(),"Missing for ClientHandle " +
+            //  cMsg.getClientHandle().getId().getData());
+        }
+
+        PCMMPdpReqStateMan man = (PCMMPdpReqStateMan) _managerMap.get(cMsg.getClientHandle().getId().str());
+        if (man == null) {
+            logger.warn("State manager not found");
+        } else {
+            man.processDeleteRequestState(cMsg);
+        }
+
+    }
+
+    /**
+     * Handle Request Message
+     *
+     * <Request> ::= <Common Header>
+     *  <Client Handle>
+     *  <Context>
+     *  *(<Named ClientSI>)
+     *  [<Integrity>]
+     * <Named ClientSI> ::= <*(<PRID> <EPD>)>
+     *
+     * Not support [<Integrity>]
+     *
+     * @param    conn                a  Socket
+     * @param    msg                 a  COPSMsg
+     *
+     */
+    private void handleRequestMsg(Socket conn, COPSMsg msg)
+    throws COPSPdpException {
+
+        COPSReqMsg reqMsg = (COPSReqMsg) msg;
+        COPSContext cntxt = reqMsg.getContext();
+        COPSHeader header = reqMsg.getHeader();
+        //short reqType = cntxt.getRequestType();
+        short cType   = header.getClientType();
+
+        // Support
+        if (reqMsg.getIntegrity() != null) {
+            logger.error("Unsupported objects (Integrity) to connection " + conn.getInetAddress());
+        }
+
+        PCMMPdpReqStateMan man;
+        man = (PCMMPdpReqStateMan) _managerMap.get(reqMsg.getClientHandle().getId().str());
+        if (man == null) {
+
+            man = new PCMMPdpReqStateMan(cType, reqMsg.getClientHandle().getId().str());
+            _managerMap.put(reqMsg.getClientHandle().getId().str(),man);
+            man.setDataProcess(_process);
+            man.initRequestState(_sock);
+
+            // COPSDebug.out(getClass().getName(),"createHandler called, clientType=" +
+            //    header.getClientType() + " msgType=" +
+            //    cntxt.getMessageType() + ", connId=" + conn.toString());
+        }
+
+        man.processRequest(reqMsg);
+    }
+
+    /**
+     * Handle Report Message
+     *
+     * <Report State> ::= <Common Header>
+     *  <Client Handle>
+     *  <Report Type>
+     *  *(<Named ClientSI>)
+     *  [<Integrity>]
+     *
+     * Not support [<Integrity>]
+     *
+     * @param    conn                a  Socket
+     * @param    msg                 a  COPSMsg
+     *
+     */
+    private void handleReportMsg(Socket conn, COPSMsg msg)
+    throws COPSPdpException {
+        COPSReportMsg repMsg = (COPSReportMsg) msg;
+        // COPSHandle handle = repMsg.getClientHandle();
+        // COPSHeader header = repMsg.getHeader();
+
+        // Support
+        if (repMsg.getIntegrity() != null) {
+            logger.error("Unsupported objects (Integrity) to connection " + conn.getInetAddress());
+        }
+
+        PCMMPdpReqStateMan man = (PCMMPdpReqStateMan) _managerMap.get(repMsg.getClientHandle().getId().str());
+        if (man == null) {
+            logger.warn("State manager not found");
+        } else {
+            man.processReport(repMsg);
+        }
+    }
+
+    /**
+     * Method handleSyncComplete
+     *
+     * @param    conn                a  Socket
+     * @param    msg                 a  COPSMsg
+     *
+     */
+    private void handleSyncComplete(Socket conn, COPSMsg msg)
+    throws COPSPdpException {
+        COPSSyncStateMsg cMsg = (COPSSyncStateMsg) msg;
+        // COPSHandle handle = cMsg.getClientHandle();
+        // COPSHeader header = cMsg.getHeader();
+
+        // Support
+        if (cMsg.getIntegrity() != null) {
+            logger.error("Unsupported objects (Integrity) to connection " + conn.getInetAddress());
+        }
+
+        PCMMPdpReqStateMan man = (PCMMPdpReqStateMan) _managerMap.get(cMsg.getClientHandle().getId().str());
+        if (man == null) {
+            logger.warn("State manager not found");
+        } else {
+            man.processSyncComplete(cMsg);
+        }
+    }
+
+    /**
+     * Requests a COPS sync from the PEP
+     * @throws COPSException
+     * @throws COPSPdpException
+     */
+    protected void syncAllRequestState()
+    throws COPSException, COPSPdpException {
+        if (_managerMap.size() > 0) {
+            for (Enumeration e = _managerMap.keys() ; e.hasMoreElements() ;) {
+                String handle = (String) e.nextElement();
+                PCMMPdpReqStateMan man = (PCMMPdpReqStateMan) _managerMap.get(handle);
+
+                man.syncRequestState();
+            }
+        }
+    }
+
+    private void notifyCloseAllReqStateMan()
+    throws COPSPdpException {
+        if (_managerMap.size() > 0) {
+            for (Enumeration e = _managerMap.keys() ; e.hasMoreElements() ;) {
+                String handle = (String) e.nextElement();
+                PCMMPdpReqStateMan man = (PCMMPdpReqStateMan) _managerMap.get(handle);
+
+                man.processClosedConnection(_error);
+            }
+        }
+    }
+
+    private void notifyNoKAAllReqStateMan()
+    throws COPSPdpException {
+        if (_managerMap.size() > 0) {
+            for (Enumeration e = _managerMap.keys() ; e.hasMoreElements() ;) {
+                String handle = (String) e.nextElement();
+                PCMMPdpReqStateMan man = (PCMMPdpReqStateMan) _managerMap.get(handle);
+
+                man.processNoKAConnection();
+            }
+        }
+    }
+
+}
diff --git a/packetcable-driver/src/main/java/org/umu/cops/common/COPSDebug.java b/packetcable-driver/src/main/java/org/umu/cops/common/COPSDebug.java
deleted file mode 100644 (file)
index 1ddf18d..0000000
+++ /dev/null
@@ -1,74 +0,0 @@
-/*\r
- * Copyright (c) 2004 University of Murcia.  All rights reserved.\r
- * --------------------------------------------------------------\r
- * For more information, please see <http://www.umu.euro6ix.org/>.\r
- */\r
-package org.umu.cops.common;\r
-\r
-import org.slf4j.Logger;\r
-import org.slf4j.LoggerFactory;\r
-\r
-/**\r
- * Class to print debug and error messages.\r
- *\r
- * @version COPSDebug.java, v 3.00 2004\r
- *\r
- */\r
-public class COPSDebug {\r
-\r
-    static public final String ERROR_NOEXPECTEDMSG = "Message not expected";\r
-    static public final String ERROR_EXCEPTION = "Exception not expected";\r
-    static public final String ERROR_SOCKET = "Error socket";\r
-    static public final String ERROR_NOSUPPORTED = "Object not supported";\r
-\r
-    private static final Logger logger = LoggerFactory.getLogger(COPSDebug.class);\r
-\r
-    /** Prints an error message.\r
-        *\r
-        * @param          cname           Name of class that generated the error\r
-     * @param          str                Error message\r
-     *\r
-       */\r
-    @Deprecated\r
-    public static void err (String cname, String str) {\r
-        logger.info(cname + ":" + str);\r
-    }\r
-\r
-    /** Prints an error message.\r
-        *\r
-        * @param          cname           Name of class that generated the error\r
-     * @param          str                Error message\r
-     * @param           e                  Exception\r
-     *\r
-       */\r
-    @Deprecated\r
-    public static void err (String cname, String str, Exception e) {\r
-        logger.info(cname + ":" + str, e);\r
-    }\r
-\r
-    /** Prints an error message.\r
-        *\r
-        * @param          cname           Name of class that generated the error\r
-     * @param          str                Error message\r
-     * @param          extra           Information\r
-     * @param           e                  Exception\r
-     *\r
-       */\r
-    @Deprecated\r
-    public static void err (String cname, String str, String extra, Exception e) {\r
-        logger.info(cname + ":" + str + " -Info: " + extra, e);\r
-    }\r
-\r
-    /** Prints an error message.\r
-        *\r
-        * @param          cname           Name of class that generated the error\r
-     * @param          str                Error message\r
-     * @param          extra           Information\r
-     *\r
-       */\r
-    @Deprecated\r
-    public static void err (String cname, String str, String extra) {\r
-        logger.info(cname + ":" + str + " -Info: " + extra);\r
-    }\r
-\r
-}\r
index 354bf76e742a8d386557fd6aab8217214cfd7ed0..d701d8fd9a720cc59ab84ab8ccfe7add976a17e4 100644 (file)
@@ -1,28 +1,22 @@
 package org.umu.cops.ospdp;\r
 \r
+import org.slf4j.Logger;\r
+import org.slf4j.LoggerFactory;\r
+import org.umu.cops.stack.*;\r
+\r
 import java.io.IOException;\r
 import java.net.ServerSocket;\r
 import java.net.Socket;\r
-import java.util.Enumeration;\r
-import java.util.Hashtable;\r
-\r
-import org.umu.cops.common.COPSDebug;\r
-import org.umu.cops.stack.COPSAcctTimer;\r
-import org.umu.cops.stack.COPSClientAcceptMsg;\r
-import org.umu.cops.stack.COPSClientCloseMsg;\r
-import org.umu.cops.stack.COPSClientOpenMsg;\r
-import org.umu.cops.stack.COPSError;\r
-import org.umu.cops.stack.COPSException;\r
-import org.umu.cops.stack.COPSHeader;\r
-import org.umu.cops.stack.COPSKATimer;\r
-import org.umu.cops.stack.COPSMsg;\r
-import org.umu.cops.stack.COPSPepId;\r
-import org.umu.cops.stack.COPSTransceiver;\r
+import java.util.Map;\r
+import java.util.concurrent.ConcurrentHashMap;\r
 \r
 /**\r
  * Core PDP agent for outsourcing.\r
  */\r
 public class COPSPdpOSAgent extends Thread {\r
+\r
+    public final static Logger logger = LoggerFactory.getLogger(COPSPdpOSAgent.class);\r
+\r
     /** Well-known port for COPS */\r
     public static final int WELL_KNOWN_PDP_PORT = 3288;\r
     /** Default keep-alive timer value (secs) */\r
@@ -30,11 +24,6 @@ public class COPSPdpOSAgent extends Thread {
     /** Default accounting timer value (secs) */\r
     public static final short ACCT_TIMER_VALUE = 0;\r
 \r
-    /**\r
-        PDP host IP\r
-     */\r
-    private ServerSocket _serverSocket;\r
-\r
     /**\r
         PDP host port\r
      */\r
@@ -58,7 +47,7 @@ public class COPSPdpOSAgent extends Thread {
     /**\r
         Maps a PEP-ID to a connection\r
      */\r
-    private Hashtable _connectionMap;\r
+    private final Map<String, COPSPdpOSConnection> _connectionMap;\r
     // map < String(PEPID), COPSPdpOSConnection > ConnectionMap;\r
 \r
     /**\r
@@ -78,7 +67,7 @@ public class COPSPdpOSAgent extends Thread {
         _acctTimer = ACCT_TIMER_VALUE;\r
 \r
         _clientType = clientType;\r
-        _connectionMap = new Hashtable(40);\r
+        _connectionMap = new ConcurrentHashMap<>();\r
         _process = process;\r
     }\r
 \r
@@ -96,7 +85,7 @@ public class COPSPdpOSAgent extends Thread {
         _acctTimer = ACCT_TIMER_VALUE;\r
 \r
         _clientType = clientType;\r
-        _connectionMap = new Hashtable(40);\r
+        _connectionMap = new ConcurrentHashMap<>();\r
         _process = process;\r
     }\r
 \r
@@ -132,22 +121,6 @@ public class COPSPdpOSAgent extends Thread {
         return _acctTimer;\r
     }\r
 \r
-    /**\r
-     * Gets the PEPs connected to this PDP\r
-     * @return   An <tt>Enumeration</tt> of all connected PEPs\r
-     */\r
-    public Enumeration getConnectedPEPIds() {\r
-        return _connectionMap.keys();\r
-    }\r
-\r
-    /**\r
-     * Gets the connection map\r
-     * @return   A <tt>Hashtable</tt> holding the connection map\r
-     */\r
-    public Hashtable getConnectionMap() {\r
-        return _connectionMap;\r
-    }\r
-\r
     /**\r
      * Gets the client-type\r
      * @return   The client-type\r
@@ -164,7 +137,7 @@ public class COPSPdpOSAgent extends Thread {
      * @throws IOException\r
      */\r
     public void disconnect (String pepID, COPSError error) throws COPSException, IOException {\r
-        COPSPdpOSConnection pdpConn = (COPSPdpOSConnection) _connectionMap.get(pepID);\r
+        COPSPdpOSConnection pdpConn = _connectionMap.get(pepID);\r
 \r
         COPSHeader cHdr = new COPSHeader(COPSHeader.COPS_OP_CC, _clientType);\r
         COPSClientCloseMsg closeMsg = new COPSClientCloseMsg();\r
@@ -174,7 +147,6 @@ public class COPSPdpOSAgent extends Thread {
 \r
         closeMsg.writeData(pdpConn.getSocket());\r
         pdpConn.close();\r
-        pdpConn = null;\r
     }\r
 \r
     /**\r
@@ -184,7 +156,7 @@ public class COPSPdpOSAgent extends Thread {
      * @throws COPSPdpException\r
      */\r
     public void sync(String pepID) throws COPSException, COPSPdpException {\r
-        COPSPdpOSConnection pdpConn = (COPSPdpOSConnection) _connectionMap.get(pepID);\r
+        COPSPdpOSConnection pdpConn = _connectionMap.get(pepID);\r
         pdpConn.syncAllRequestState();\r
     }\r
 \r
@@ -201,14 +173,14 @@ public class COPSPdpOSAgent extends Thread {
      */\r
     public void run() {\r
         try {\r
-            _serverSocket = new ServerSocket (_serverPort);\r
+            final ServerSocket serverSocket = new ServerSocket (_serverPort);\r
 \r
             //Loop through for Incoming messages\r
 \r
             // server infinite loop\r
             while (true) {\r
                 // Wait for an incoming connection from a PEP\r
-                Socket socket = _serverSocket.accept();\r
+                Socket socket = serverSocket.accept();\r
 \r
                 // COPSDebug.out(getClass().getName(),"New connection accepted " +\r
                 //           socket.getInetAddress() +\r
@@ -223,19 +195,22 @@ public class COPSPdpOSAgent extends Thread {
                         // COPSDebug.err(getClass().getName(), COPSDebug.ERROR_NOEXPECTEDMSG);\r
                         try {\r
                             socket.close();\r
-                        } catch (Exception ex) {};\r
+                        } catch (Exception ex) {\r
+                            logger.error("Error closing socket", ex);\r
+                        }\r
                     }\r
                 } catch (Exception e) { // COPSException, IOException\r
                     // COPSDebug.err(getClass().getName(), COPSDebug.ERROR_EXCEPTION,\r
                     //    "(" + socket.getInetAddress() + ":" + socket.getPort() + ")", e);\r
                     try {\r
                         socket.close();\r
-                    } catch (Exception ex) {};\r
+                    } catch (Exception ex) {\r
+                        logger.error("Error closing socket", ex);\r
+                    }\r
                 }\r
             }\r
         } catch (IOException e) {\r
-            COPSDebug.err(getClass().getName(), COPSDebug.ERROR_SOCKET, e);\r
-            return;\r
+            logger.error("Error processing socket messages", e);\r
         }\r
     }\r
 \r
@@ -260,7 +235,9 @@ public class COPSPdpOSAgent extends Thread {
             closeMsg.add(err);\r
             try {\r
                 closeMsg.writeData(conn);\r
-            } catch (IOException unae) {}\r
+            } catch (IOException unae) {\r
+                logger.error("Error writing data", unae);\r
+            }\r
 \r
             throw new COPSException("Unsupported client type");\r
         }\r
@@ -275,7 +252,9 @@ public class COPSPdpOSAgent extends Thread {
             closeMsg.add(err);\r
             try {\r
                 closeMsg.writeData(conn);\r
-            } catch (IOException unae) {}\r
+            } catch (IOException unae) {\r
+                logger.error("Error writing data", unae);\r
+            }\r
 \r
             throw new COPSException("Mandatory COPS object missing (PEPId)");\r
         }\r
@@ -293,7 +272,9 @@ public class COPSPdpOSAgent extends Thread {
             closeMsg.add(err);\r
             try {\r
                 closeMsg.writeData(conn);\r
-            } catch (IOException unae) {}\r
+            } catch (IOException unae) {\r
+                logger.error("Error writing data", unae);\r
+            }\r
 \r
             throw new COPSException("Unsupported objects (ClientSI, PdpAddress, Integrity)");\r
         }\r
index 31a0b64febbe49c4be2bb86c209821b6e1ae76b3..96bfa8e43f3171ef2c8d4673145a22bcc2f5a206 100644 (file)
@@ -1,47 +1,40 @@
 package org.umu.cops.ospep;\r
 \r
+import org.slf4j.Logger;\r
+import org.slf4j.LoggerFactory;\r
+import org.umu.cops.stack.*;\r
+\r
 import java.io.IOException;\r
 import java.net.Socket;\r
-import java.util.Date;\r
-import java.util.Enumeration;\r
-import java.util.Hashtable;\r
-import java.util.Vector;\r
-\r
-import org.umu.cops.common.COPSDebug;\r
-import org.umu.cops.stack.COPSClientCloseMsg;\r
-import org.umu.cops.stack.COPSDecisionMsg;\r
-import org.umu.cops.stack.COPSError;\r
-import org.umu.cops.stack.COPSException;\r
-import org.umu.cops.stack.COPSHandle;\r
-import org.umu.cops.stack.COPSHeader;\r
-import org.umu.cops.stack.COPSKAMsg;\r
-import org.umu.cops.stack.COPSMsg;\r
-import org.umu.cops.stack.COPSSyncStateMsg;\r
-import org.umu.cops.stack.COPSTransceiver;\r
+import java.util.*;\r
+import java.util.concurrent.ConcurrentHashMap;\r
 \r
 /**\r
  * COPSPepConnection represents a PEP-PDP Connection Manager.\r
  * Responsible for processing messages received from PDP.\r
  */\r
 public class COPSPepOSConnection implements Runnable {\r
+\r
+    public final static Logger logger = LoggerFactory.getLogger(COPSPepOSConnection.class);\r
+\r
     /** Socket connected to PDP */\r
     protected Socket _sock;\r
 \r
     /** Time to wait responses (milliseconds), default is 10 seconds */\r
-    protected int _responseTime;\r
+    protected final int _responseTime;\r
 \r
     /** COPS Client-type */\r
-    protected short _clientType;\r
+    protected final short _clientType;\r
 \r
     /**\r
         Accounting timer value (secs)\r
      */\r
-    protected short _acctTimer;\r
+    protected transient short _acctTimer;\r
 \r
     /**\r
         Keep-alive timer value (secs)\r
      */\r
-    protected short _kaTimer;\r
+    protected transient short _kaTimer;\r
 \r
     /**\r
      *  Time of the latest keep-alive received\r
@@ -56,7 +49,7 @@ public class COPSPepOSConnection implements Runnable {
     /**\r
         Maps a COPS Client Handle to a Request State Manager\r
      */\r
-    protected Hashtable _managerMap;\r
+    protected final Map<String, COPSPepOSReqStateMan> _managerMap;\r
     // map < String(COPSHandle), COPSPepOSReqStateMan>;\r
 \r
     /**\r
@@ -79,7 +72,7 @@ public class COPSPepOSConnection implements Runnable {
         _responseTime = 10000;\r
         _lastmessage = COPSHeader.COPS_OP_CAT;\r
 \r
-        _managerMap = new Hashtable(20);\r
+        _managerMap = new ConcurrentHashMap<>();\r
     }\r
 \r
     /**\r
@@ -114,20 +107,13 @@ public class COPSPepOSConnection implements Runnable {
         return _acctTimer;\r
     }\r
 \r
-    /**\r
-     * Gets active COPS handles\r
-     * @return  An <tt>Enumeration</tt> holding all active handles\r
-     */\r
-    protected Enumeration getHandles() {\r
-        return _managerMap.keys();\r
-    }\r
-\r
     /**\r
      * Gets all request state managers\r
      * @return  A <tt>Hashatable</tt> holding all request state managers\r
+     * TODO - change the return to Map\r
      */\r
     protected Hashtable getReqStateMans() {\r
-        return _managerMap;\r
+        return new Hashtable(_managerMap);\r
     }\r
 \r
     /**\r
@@ -155,14 +141,6 @@ public class COPSPepOSConnection implements Runnable {
         return _lastmessage;\r
     }\r
 \r
-    /**\r
-     * Sets response time\r
-     * @param respTime  Response time value (msecs)\r
-     */\r
-    public void setResponseTime(int respTime) {\r
-        _responseTime = respTime;\r
-    };\r
-\r
     /**\r
      * Sets keep-alive timer\r
      * @param kaTimer   Keep-alive timer value (secs)\r
@@ -200,7 +178,7 @@ public class COPSPepOSConnection implements Runnable {
                     int _startTime = (int) (_lastRecKa.getTime());\r
                     int cTime = (int) (new Date().getTime());\r
 \r
-                    if ((int)(cTime - _startTime) > _kaTimer*1000) {\r
+                    if ((cTime - _startTime) > _kaTimer*1000) {\r
                         _sock.close();\r
                         // Notify all Request State Managers\r
                         notifyNoKAAllReqStateMan();\r
@@ -210,7 +188,7 @@ public class COPSPepOSConnection implements Runnable {
                     _startTime = (int) (_lastSendKa.getTime());\r
                     cTime = (int) (new Date().getTime());\r
 \r
-                    if ((int)(cTime - _startTime) > ((_kaTimer*3/4) * 1000)) {\r
+                    if ((cTime - _startTime) > ((_kaTimer*3/4) * 1000)) {\r
                         COPSHeader hdr = new COPSHeader(COPSHeader.COPS_OP_KA);\r
                         COPSKAMsg msg = new COPSKAMsg();\r
 \r
@@ -226,7 +204,7 @@ public class COPSPepOSConnection implements Runnable {
                     int _startTime = (int) (_lastSendAcc.getTime());\r
                     int cTime = (int) (new Date().getTime());\r
 \r
-                    if ((int)(cTime - _startTime) > ((_acctTimer*3/4)*1000)) {\r
+                    if ((cTime - _startTime) > ((_acctTimer*3/4)*1000)) {\r
                         // Notify all Request State Managers\r
                         notifyAcctAllReqStateMan();\r
                         _lastSendAcc = new Date();\r
@@ -235,23 +213,28 @@ public class COPSPepOSConnection implements Runnable {
 \r
                 try {\r
                     Thread.sleep(500);\r
-                } catch (Exception e) {};\r
+                } catch (Exception e) {\r
+                    logger.error("Exception thrown while sleeping", e);\r
+                }\r
             }\r
         } catch (Exception e) {\r
-            e.printStackTrace();\r
-            COPSDebug.err(getClass().getName(), COPSDebug.ERROR_SOCKET, e);\r
+            logger.error("Error while processing socket messages", e);\r
         }\r
 \r
         // connection closed by server\r
         // COPSDebug.out(getClass().getName(),"Connection closed by server");\r
         try {\r
             _sock.close();\r
-        } catch (IOException e) {};\r
+        } catch (IOException e) {\r
+            logger.error("Unexpected exception closing the socket", e);\r
+        }\r
 \r
         // Notify all Request State Managers\r
         try {\r
             notifyCloseAllReqStateMan();\r
-        } catch (COPSPepException e) {};\r
+        } catch (COPSPepException e) {\r
+            logger.error("Error closing state managers", e);\r
+        }\r
     }\r
 \r
     /**\r
@@ -306,10 +289,12 @@ public class COPSPepOSConnection implements Runnable {
         try {\r
             // Support\r
             if (cMsg.getIntegrity() != null)\r
-                COPSDebug.err(getClass().getName(), COPSDebug.ERROR_NOSUPPORTED, "Unsupported objects (Integrity) to connection " + conn.getInetAddress());\r
+                logger.warn("Unsupported objects (Integrity) to connection " + conn.getInetAddress());\r
 \r
             conn.close();\r
-        } catch (Exception unae) { };\r
+        } catch (Exception unae) {\r
+            logger.error("Unexpected exception closing connection", unae);\r
+        }\r
     }\r
 \r
     /**\r
@@ -337,14 +322,11 @@ public class COPSPepOSConnection implements Runnable {
 \r
         // COPSDebug.out(getClass().getName(),"Get KAlive Msg");\r
 \r
-        try {\r
-            // Support\r
-            if (cMsg.getIntegrity() != null)\r
-                COPSDebug.err(getClass().getName(), COPSDebug.ERROR_NOSUPPORTED, "Unsupported objects (Integrity) to connection " + conn.getInetAddress());\r
-\r
-            // must we do anything else?\r
+        // Support\r
+        if (cMsg.getIntegrity() != null)\r
+            logger.warn("Unsupported objects (Integrity) to connection " + conn.getInetAddress());\r
 \r
-        } catch (Exception unae) { };\r
+        // must we do anything else?\r
     }\r
 \r
     /**\r
@@ -391,13 +373,12 @@ public class COPSPepOSConnection implements Runnable {
 \r
         // Support\r
         if (cMsg.getIntegrity() != null)\r
-            COPSDebug.err(getClass().getName(), COPSDebug.ERROR_NOSUPPORTED,\r
-                          "Unsupported objects (Integrity) to connection " + conn.getInetAddress());\r
+            logger.warn("Unsupported objects (Integrity) to connection " + conn.getInetAddress());\r
 \r
-        COPSPepOSReqStateMan manager = (COPSPepOSReqStateMan) _managerMap.get(cMsg.getClientHandle().getId().str());\r
+        COPSPepOSReqStateMan manager = _managerMap.get(cMsg.getClientHandle().getId().str());\r
 \r
         if (manager == null)\r
-            COPSDebug.err(getClass().getName(), COPSDebug.ERROR_NOEXPECTEDMSG);\r
+            logger.warn("Unable to find state manager with ID - " + cMsg.getClientHandle().getId().str());\r
         else\r
             manager.processSyncStateRequest(cMsg);\r
     }\r
@@ -434,35 +415,20 @@ public class COPSPepOSConnection implements Runnable {
     }\r
 \r
     private void notifyCloseAllReqStateMan() throws COPSPepException {\r
-        if (_managerMap.size() > 0) {\r
-            for (Enumeration e = _managerMap.keys() ; e.hasMoreElements() ;) {\r
-                String handle = (String) e.nextElement();\r
-                COPSPepOSReqStateMan man = (COPSPepOSReqStateMan) _managerMap.get(handle);\r
-\r
+        for (final COPSPepOSReqStateMan man : _managerMap.values()) {\r
                 man.processClosedConnection(_error);\r
-            }\r
         }\r
     }\r
 \r
     private void notifyNoKAAllReqStateMan() throws COPSPepException {\r
-        if (_managerMap.size() > 0) {\r
-            for (Enumeration e = _managerMap.keys() ; e.hasMoreElements() ;) {\r
-                String handle = (String) e.nextElement();\r
-                COPSPepOSReqStateMan man = (COPSPepOSReqStateMan) _managerMap.get(handle);\r
-\r
-                man.processNoKAConnection();\r
-            }\r
+        for (final COPSPepOSReqStateMan man : _managerMap.values()) {\r
+            man.processNoKAConnection();\r
         }\r
     }\r
 \r
     private void notifyAcctAllReqStateMan() throws COPSPepException {\r
-        if (_managerMap.size() > 0) {\r
-            for (Enumeration e = _managerMap.keys() ; e.hasMoreElements() ;) {\r
-                String handle = (String) e.nextElement();\r
-                COPSPepOSReqStateMan man = (COPSPepOSReqStateMan) _managerMap.get(handle);\r
-\r
-                man.processAcctReport();\r
-            }\r
+        for (final COPSPepOSReqStateMan man : _managerMap.values()) {\r
+            man.processAcctReport();\r
         }\r
     }\r
 \r
index b6043c60ab19c58f150751fca575ece68c36b142..be174403844701cc5d3c7803d58fd27ca90dfd18 100644 (file)
@@ -6,29 +6,24 @@
 \r
 package org.umu.cops.prpdp;\r
 \r
+import org.slf4j.Logger;\r
+import org.slf4j.LoggerFactory;\r
+import org.umu.cops.stack.*;\r
+\r
 import java.io.IOException;\r
 import java.net.ServerSocket;\r
 import java.net.Socket;\r
-import java.util.Enumeration;\r
 import java.util.Hashtable;\r
-\r
-import org.umu.cops.common.COPSDebug;\r
-import org.umu.cops.stack.COPSAcctTimer;\r
-import org.umu.cops.stack.COPSClientAcceptMsg;\r
-import org.umu.cops.stack.COPSClientCloseMsg;\r
-import org.umu.cops.stack.COPSClientOpenMsg;\r
-import org.umu.cops.stack.COPSError;\r
-import org.umu.cops.stack.COPSException;\r
-import org.umu.cops.stack.COPSHeader;\r
-import org.umu.cops.stack.COPSKATimer;\r
-import org.umu.cops.stack.COPSMsg;\r
-import org.umu.cops.stack.COPSPepId;\r
-import org.umu.cops.stack.COPSTransceiver;\r
+import java.util.Map;\r
+import java.util.concurrent.ConcurrentHashMap;\r
 \r
 /**\r
  * Core PDP agent for provisioning\r
  */\r
 public class COPSPdpAgent extends Thread {\r
+\r
+    public final static Logger logger = LoggerFactory.getLogger(COPSPdpAgent.class);\r
+\r
     /** Well-known port for COPS */\r
     public static final int WELL_KNOWN_PDP_PORT = 3288;\r
     /** Default keep-alive timer value (secs) */\r
@@ -36,11 +31,6 @@ public class COPSPdpAgent extends Thread {
     /** Default accounting timer value (secs) */\r
     public static final short ACCT_TIMER_VALUE = 0;\r
 \r
-    /**\r
-        PDP host IP\r
-     */\r
-    private ServerSocket _serverSocket;\r
-\r
     /**\r
         PDP host port\r
      */\r
@@ -64,7 +54,7 @@ public class COPSPdpAgent extends Thread {
     /**\r
         Maps a PEP-ID to a connection\r
      */\r
-    private Hashtable _connectionMap;\r
+    private final Map<String, COPSPdpConnection> _connectionMap;\r
     // map < String(PEPID), COPSPdpConnection > ConnectionMap;\r
 \r
     /**\r
@@ -84,7 +74,7 @@ public class COPSPdpAgent extends Thread {
         _acctTimer = ACCT_TIMER_VALUE;\r
 \r
         _clientType = clientType;\r
-        _connectionMap = new Hashtable(40);\r
+        _connectionMap = new ConcurrentHashMap<>();\r
         _process = process;\r
     }\r
 \r
@@ -102,7 +92,7 @@ public class COPSPdpAgent extends Thread {
         _acctTimer = ACCT_TIMER_VALUE;\r
 \r
         _clientType = clientType;\r
-        _connectionMap = new Hashtable(40);\r
+        _connectionMap = new ConcurrentHashMap<>();\r
         _process = process;\r
     }\r
 \r
@@ -138,20 +128,12 @@ public class COPSPdpAgent extends Thread {
         return _acctTimer;\r
     }\r
 \r
-    /**\r
-     * Gets the PEPs connected to this PDP\r
-     * @return   An <tt>Enumeration</tt> of all connected PEPs\r
-     */\r
-    public Enumeration getConnectedPEPIds() {\r
-        return _connectionMap.keys();\r
-    }\r
-\r
     /**\r
      * Gets the connection map\r
      * @return   A <tt>Hashtable</tt> holding the connection map\r
      */\r
     public Hashtable getConnectionMap() {\r
-        return _connectionMap;\r
+        return new Hashtable(_connectionMap);\r
     }\r
 \r
     /**\r
@@ -172,7 +154,7 @@ public class COPSPdpAgent extends Thread {
     public void disconnect (String pepID, COPSError error)\r
     throws COPSException, IOException {\r
 \r
-        COPSPdpConnection pdpConn = (COPSPdpConnection) _connectionMap.get(pepID);\r
+        COPSPdpConnection pdpConn = _connectionMap.get(pepID);\r
 \r
         COPSHeader cHdr = new COPSHeader(COPSHeader.COPS_OP_CC, _clientType);\r
         COPSClientCloseMsg closeMsg = new COPSClientCloseMsg();\r
@@ -182,7 +164,6 @@ public class COPSPdpAgent extends Thread {
 \r
         closeMsg.writeData(pdpConn.getSocket());\r
         pdpConn.close();\r
-        pdpConn = null;\r
     }\r
 \r
     /**\r
@@ -194,7 +175,7 @@ public class COPSPdpAgent extends Thread {
     public void sync (String pepID)\r
     throws COPSException, COPSPdpException {\r
 \r
-        COPSPdpConnection pdpConn = (COPSPdpConnection) _connectionMap.get(pepID);\r
+        COPSPdpConnection pdpConn = _connectionMap.get(pepID);\r
         pdpConn.syncAllRequestState();\r
     }\r
 \r
@@ -212,7 +193,7 @@ public class COPSPdpAgent extends Thread {
      */\r
     public void run() {\r
         try {\r
-            _serverSocket = new ServerSocket (_serverPort);\r
+            final ServerSocket serverSocket = new ServerSocket (_serverPort);\r
 \r
             //Loop through for Incoming messages\r
 \r
@@ -220,7 +201,7 @@ public class COPSPdpAgent extends Thread {
             while (true) {\r
 \r
                 // Wait for an incoming connection from a PEP\r
-                Socket socket = _serverSocket.accept();\r
+                Socket socket = serverSocket.accept();\r
 \r
                 // COPSDebug.out(getClass().getName(),"New connection accepted " +\r
                 //           socket.getInetAddress() +\r
@@ -235,19 +216,22 @@ public class COPSPdpAgent extends Thread {
                         // COPSDebug.err(getClass().getName(), COPSDebug.ERROR_NOEXPECTEDMSG);\r
                         try {\r
                             socket.close();\r
-                        } catch (Exception ex) {};\r
+                        } catch (Exception ex) {\r
+                            logger.error("Error closing socket", ex);\r
+                        }\r
                     }\r
                 } catch (Exception e) { // COPSException, IOException\r
                     // COPSDebug.err(getClass().getName(), COPSDebug.ERROR_EXCEPTION,\r
                     //    "(" + socket.getInetAddress() + ":" + socket.getPort() + ")", e);\r
                     try {\r
                         socket.close();\r
-                    } catch (Exception ex) {};\r
+                    } catch (Exception ex) {\r
+                        logger.error("Error closing socket", ex);\r
+                    }\r
                 }\r
             }\r
         } catch (IOException e) {\r
-            COPSDebug.err(getClass().getName(), COPSDebug.ERROR_SOCKET, e);\r
-            return;\r
+            logger.error("Error caught while processing socket messages", e);\r
         }\r
     }\r
 \r
@@ -273,7 +257,9 @@ public class COPSPdpAgent extends Thread {
             closeMsg.add(err);\r
             try {\r
                 closeMsg.writeData(conn);\r
-            } catch (IOException unae) {}\r
+            } catch (IOException unae) {\r
+                logger.error("Error writing COPS data", unae);\r
+            }\r
 \r
             throw new COPSException("Unsupported client type");\r
         }\r
@@ -288,7 +274,9 @@ public class COPSPdpAgent extends Thread {
             closeMsg.add(err);\r
             try {\r
                 closeMsg.writeData(conn);\r
-            } catch (IOException unae) {}\r
+            } catch (IOException unae) {\r
+                logger.error("Error writing close message", unae);\r
+            }\r
 \r
             throw new COPSException("Mandatory COPS object missing (PEPId)");\r
         }\r
@@ -306,7 +294,9 @@ public class COPSPdpAgent extends Thread {
             closeMsg.add(err);\r
             try {\r
                 closeMsg.writeData(conn);\r
-            } catch (IOException unae) {}\r
+            } catch (IOException unae) {\r
+                logger.error("Error writing close message", unae);\r
+            }\r
 \r
             throw new COPSException("Unsupported objects (ClientSI, PdpAddress, Integrity)");\r
         }\r
index d1c011cb4746ffe5c3e8a415c0ad9dfafb871b69..c79abc6a9a4e85103c25324c0549370812fea77a 100644 (file)
@@ -6,32 +6,23 @@
 \r
 package org.umu.cops.prpdp;\r
 \r
+import org.slf4j.Logger;\r
+import org.slf4j.LoggerFactory;\r
+import org.umu.cops.stack.*;\r
+\r
 import java.io.IOException;\r
 import java.net.Socket;\r
 import java.util.Date;\r
-import java.util.Enumeration;\r
-import java.util.Hashtable;\r
-\r
-import org.umu.cops.common.COPSDebug;\r
-import org.umu.cops.stack.COPSClientCloseMsg;\r
-import org.umu.cops.stack.COPSContext;\r
-import org.umu.cops.stack.COPSDeleteMsg;\r
-import org.umu.cops.stack.COPSError;\r
-import org.umu.cops.stack.COPSException;\r
-import org.umu.cops.stack.COPSHeader;\r
-import org.umu.cops.stack.COPSKAMsg;\r
-import org.umu.cops.stack.COPSMsg;\r
-import org.umu.cops.stack.COPSPepId;\r
-import org.umu.cops.stack.COPSReportMsg;\r
-import org.umu.cops.stack.COPSReqMsg;\r
-import org.umu.cops.stack.COPSSyncStateMsg;\r
-import org.umu.cops.stack.COPSTransceiver;\r
+import java.util.Map;\r
+import java.util.concurrent.ConcurrentHashMap;\r
 \r
 /**\r
  * Class for managing an provisioning connection at the PDP side.\r
  */\r
 public class COPSPdpConnection implements Runnable {\r
 \r
+    public final static Logger logger = LoggerFactory.getLogger(COPSPdpConnection.class);\r
+\r
     /**\r
         Socket connected to PEP\r
      */\r
@@ -60,7 +51,7 @@ public class COPSPdpConnection implements Runnable {
     /**\r
         Maps a Client Handle to a Handler\r
      */\r
-    protected Hashtable _managerMap;\r
+    protected final Map<String, COPSPdpReqStateMan> _managerMap;\r
     // map < String(COPSHandle), COPSPdpHandler> HandlerMap;\r
 \r
     /**\r
@@ -96,7 +87,7 @@ public class COPSPdpConnection implements Runnable {
 \r
         _lastKa = new Date();\r
         _lastmessage = COPSHeader.COPS_OP_OPN;\r
-        _managerMap = new Hashtable(20);\r
+        _managerMap = new ConcurrentHashMap<>();\r
 \r
         _kaTimer = 0;\r
         _process = process;\r
@@ -150,22 +141,6 @@ public class COPSPdpConnection implements Runnable {
         return _lastmessage;\r
     }\r
 \r
-    /**\r
-     * Gets active handles\r
-     * @return   An <tt>Enumeration</tt> holding all active handles\r
-     */\r
-    public Enumeration getHandles() {\r
-        return _managerMap.keys();\r
-    }\r
-\r
-    /**\r
-     * Gets the handle map\r
-     * @return   A <tt>Hashtable</tt> holding the handle map\r
-     */\r
-    public Hashtable getReqStateMans() {\r
-        return _managerMap;\r
-    }\r
-\r
     /**\r
      * Gets the PEP-ID\r
      * @return   The ID of the PEP, as a <tt>String</tt>\r
@@ -218,7 +193,7 @@ public class COPSPdpConnection implements Runnable {
                     int _startTime = (int) (_lastRecKa.getTime());\r
                     int cTime = (int) (new Date().getTime());\r
 \r
-                    if ((int)(cTime - _startTime) > _kaTimer*1000) {\r
+                    if ((cTime - _startTime) > _kaTimer * 1000) {\r
                         _sock.close();\r
                         // Notify all Request State Managers\r
                         notifyNoKAAllReqStateMan();\r
@@ -228,7 +203,7 @@ public class COPSPdpConnection implements Runnable {
                     _startTime = (int) (_lastSendKa.getTime());\r
                     cTime = (int) (new Date().getTime());\r
 \r
-                    if ((int)(cTime - _startTime) > ((_kaTimer*3/4)*1000)) {\r
+                    if ((cTime - _startTime) > ((_kaTimer * 3/4) * 1000)) {\r
                         COPSHeader hdr = new COPSHeader(COPSHeader.COPS_OP_KA);\r
                         COPSKAMsg msg = new COPSKAMsg();\r
 \r
@@ -241,23 +216,29 @@ public class COPSPdpConnection implements Runnable {
 \r
                 try {\r
                     Thread.sleep(500);\r
-                } catch (Exception e) {};\r
+                } catch (Exception e) {\r
+                    logger.error("Exception thrown while sleeping", e);\r
+                }\r
 \r
             }\r
         } catch (Exception e) {\r
-            COPSDebug.err(getClass().getName(), COPSDebug.ERROR_SOCKET, e);\r
+            logger.error("Error while processing socket messages", e);\r
         }\r
 \r
         // connection closed by server\r
         // COPSDebug.out(getClass().getName(),"Connection closed by client");\r
         try {\r
             _sock.close();\r
-        } catch (IOException e) {};\r
+        } catch (IOException e) {\r
+            logger.error("Error closing socket", e);\r
+        }\r
 \r
         // Notify all Request State Managers\r
         try {\r
             notifyCloseAllReqStateMan();\r
-        } catch (COPSPdpException e) {};\r
+        } catch (COPSPdpException e) {\r
+            logger.error("Error closing state managers");\r
+        }\r
     }\r
 \r
     /**\r
@@ -316,12 +297,13 @@ public class COPSPdpConnection implements Runnable {
         try {\r
             // Support\r
             if (cMsg.getIntegrity() != null) {\r
-                COPSDebug.err(getClass().getName(), COPSDebug.ERROR_NOSUPPORTED,\r
-                              "Unsupported objects (Integrity) to connection " + conn.getInetAddress());\r
+                logger.warn("Unsupported objects (Integrity) to connection " + conn.getInetAddress());\r
             }\r
 \r
             conn.close();\r
-        } catch (Exception unae) { };\r
+        } catch (Exception unae) {\r
+            logger.error("Unexpected exception closing connection", unae);\r
+        }\r
     }\r
 \r
     /**\r
@@ -351,12 +333,12 @@ public class COPSPdpConnection implements Runnable {
         try {\r
             // Support\r
             if (cMsg.getIntegrity() != null) {\r
-                COPSDebug.err(getClass().getName(), COPSDebug.ERROR_NOSUPPORTED,\r
-                              "Unsupported objects (Integrity) to connection " + conn.getInetAddress());\r
+                logger.warn("Unsupported objects (Integrity) to connection " + conn.getInetAddress());\r
             }\r
-\r
             kaMsg.writeData(conn);\r
-        } catch (Exception unae) { };\r
+        } catch (Exception unae) {\r
+            logger.error("Unexpected exception while writing COPS data", unae);\r
+        }\r
     }\r
 \r
     /**\r
@@ -381,8 +363,7 @@ public class COPSPdpConnection implements Runnable {
 \r
         // Support\r
         if (cMsg.getIntegrity() != null) {\r
-            COPSDebug.err(getClass().getName(), COPSDebug.ERROR_NOSUPPORTED,\r
-                          "Unsupported objects (Integrity) to connection " + conn.getInetAddress());\r
+            logger.warn("Unsupported objects (Integrity) to connection " + conn.getInetAddress());\r
         }\r
 \r
         // Delete clientHandler\r
@@ -391,9 +372,9 @@ public class COPSPdpConnection implements Runnable {
             //  cMsg.getClientHandle().getId().getData());\r
         }\r
 \r
-        COPSPdpReqStateMan man = (COPSPdpReqStateMan) _managerMap.get(cMsg.getClientHandle().getId().str());\r
+        COPSPdpReqStateMan man = _managerMap.get(cMsg.getClientHandle().getId().str());\r
         if (man == null) {\r
-            COPSDebug.err(getClass().getName(), COPSDebug.ERROR_NOEXPECTEDMSG);\r
+            logger.warn("No state manager found with ID - " + cMsg.getClientHandle().getId().str());\r
         } else {\r
             man.processDeleteRequestState(cMsg);\r
         }\r
@@ -427,8 +408,7 @@ public class COPSPdpConnection implements Runnable {
 \r
         // Support\r
         if (reqMsg.getIntegrity() != null) {\r
-            COPSDebug.err(getClass().getName(), COPSDebug.ERROR_NOSUPPORTED,\r
-                          "Unsupported objects (Integrity) to connection " + conn.getInetAddress());\r
+            logger.warn("Unsupported objects (Integrity) to connection " + conn.getInetAddress());\r
         }\r
 \r
         COPSPdpReqStateMan man;\r
@@ -471,13 +451,12 @@ public class COPSPdpConnection implements Runnable {
 \r
         // Support\r
         if (repMsg.getIntegrity() != null) {\r
-            COPSDebug.err(getClass().getName(), COPSDebug.ERROR_NOSUPPORTED,\r
-                          "Unsupported objects (Integrity) to connection " + conn.getInetAddress());\r
+            logger.warn("Unsupported objects (Integrity) to connection " + conn.getInetAddress());\r
         }\r
 \r
         COPSPdpReqStateMan man = (COPSPdpReqStateMan) _managerMap.get(repMsg.getClientHandle().getId().str());\r
         if (man == null) {\r
-            COPSDebug.err(getClass().getName(), COPSDebug.ERROR_NOEXPECTEDMSG);\r
+            logger.warn("No state manager found with ID - " + repMsg.getClientHandle().getId().str());\r
         } else {\r
             man.processReport(repMsg);\r
         }\r
@@ -498,13 +477,12 @@ public class COPSPdpConnection implements Runnable {
 \r
         // Support\r
         if (cMsg.getIntegrity() != null) {\r
-            COPSDebug.err(getClass().getName(), COPSDebug.ERROR_NOSUPPORTED,\r
-                          "Unsupported objects (Integrity) to connection " + conn.getInetAddress());\r
+            logger.warn("Unsupported objects (Integrity) to connection " + conn.getInetAddress());\r
         }\r
 \r
         COPSPdpReqStateMan man = (COPSPdpReqStateMan) _managerMap.get(cMsg.getClientHandle().getId().str());\r
         if (man == null) {\r
-            COPSDebug.err(getClass().getName(), COPSDebug.ERROR_NOEXPECTEDMSG);\r
+            logger.warn("No state manager found with ID - " + cMsg.getClientHandle().getId().str());\r
         } else {\r
             man.processSyncComplete(cMsg);\r
         }\r
@@ -515,39 +493,21 @@ public class COPSPdpConnection implements Runnable {
      * @throws COPSException\r
      * @throws COPSPdpException\r
      */\r
-    protected void syncAllRequestState()\r
-    throws COPSException, COPSPdpException {\r
-        if (_managerMap.size() > 0) {\r
-            for (Enumeration e = _managerMap.keys() ; e.hasMoreElements() ;) {\r
-                String handle = (String) e.nextElement();\r
-                COPSPdpReqStateMan man = (COPSPdpReqStateMan) _managerMap.get(handle);\r
-\r
-                man.syncRequestState();\r
-            }\r
+    protected void syncAllRequestState() throws COPSException, COPSPdpException {\r
+        for (final COPSPdpReqStateMan man : _managerMap.values()) {\r
+            man.syncRequestState();\r
         }\r
     }\r
 \r
-    private void notifyCloseAllReqStateMan()\r
-    throws COPSPdpException {\r
-        if (_managerMap.size() > 0) {\r
-            for (Enumeration e = _managerMap.keys() ; e.hasMoreElements() ;) {\r
-                String handle = (String) e.nextElement();\r
-                COPSPdpReqStateMan man = (COPSPdpReqStateMan) _managerMap.get(handle);\r
-\r
-                man.processClosedConnection(_error);\r
-            }\r
+    private void notifyCloseAllReqStateMan() throws COPSPdpException {\r
+        for (final COPSPdpReqStateMan man : _managerMap.values()) {\r
+            man.processClosedConnection(_error);\r
         }\r
     }\r
 \r
-    private void notifyNoKAAllReqStateMan()\r
-    throws COPSPdpException {\r
-        if (_managerMap.size() > 0) {\r
-            for (Enumeration e = _managerMap.keys() ; e.hasMoreElements() ;) {\r
-                String handle = (String) e.nextElement();\r
-                COPSPdpReqStateMan man = (COPSPdpReqStateMan) _managerMap.get(handle);\r
-\r
-                man.processNoKAConnection();\r
-            }\r
+    private void notifyNoKAAllReqStateMan() throws COPSPdpException {\r
+        for (final COPSPdpReqStateMan man : _managerMap.values()) {\r
+            man.processNoKAConnection();\r
         }\r
     }\r
 \r
index 0e888ce22250e004e005b614dcac0a794099b1b1..89943ddc250cfcb038980d13da035452a8bfc8cb 100644 (file)
@@ -6,26 +6,14 @@
 
 package org.umu.cops.prpep;
 
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.umu.cops.stack.*;
+
 import java.io.IOException;
 import java.net.Socket;
-import java.util.Date;
-import java.util.Enumeration;
-import java.util.Hashtable;
-import java.util.Vector;
-
-import org.umu.cops.common.COPSDebug;
-import org.umu.cops.stack.COPSClientCloseMsg;
-import org.umu.cops.stack.COPSContext;
-import org.umu.cops.stack.COPSDecision;
-import org.umu.cops.stack.COPSDecisionMsg;
-import org.umu.cops.stack.COPSError;
-import org.umu.cops.stack.COPSException;
-import org.umu.cops.stack.COPSHandle;
-import org.umu.cops.stack.COPSHeader;
-import org.umu.cops.stack.COPSKAMsg;
-import org.umu.cops.stack.COPSMsg;
-import org.umu.cops.stack.COPSSyncStateMsg;
-import org.umu.cops.stack.COPSTransceiver;
+import java.util.*;
+import java.util.concurrent.ConcurrentHashMap;
 
 /**
  * COPSPepConnection represents a PEP-PDP Connection Manager.
@@ -33,6 +21,8 @@ import org.umu.cops.stack.COPSTransceiver;
  */
 public class COPSPepConnection implements Runnable {
 
+    public final static Logger logger = LoggerFactory.getLogger(COPSPepConnection.class);
+
     /** Socket connected to PDP */
     protected Socket _sock;
 
@@ -65,7 +55,7 @@ public class COPSPepConnection implements Runnable {
     /**
         Maps a COPS Client Handle to a Request State Manager
      */
-    protected Hashtable _managerMap;
+    protected final Map<String, COPSPepReqStateMan> _managerMap;
     // map < String(COPSHandle), COPSPepReqStateMan>;
 
     /**
@@ -89,7 +79,7 @@ public class COPSPepConnection implements Runnable {
         _responseTime = 10000;
         _lastmessage = COPSHeader.COPS_OP_CAT;
 
-        _managerMap = new Hashtable(20);
+        _managerMap = new ConcurrentHashMap<>();
     }
 
     /**
@@ -124,20 +114,12 @@ public class COPSPepConnection implements Runnable {
         return _acctTimer;
     }
 
-    /**
-     * Gets active COPS handles
-     * @return  An <tt>Enumeration</tt> holding all active handles
-     */
-    protected Enumeration getHandles() {
-        return _managerMap.keys();
-    }
-
     /**
      * Gets all request state managers
      * @return  A <tt>Hashatable</tt> holding all request state managers
      */
     protected Hashtable getReqStateMans() {
-        return _managerMap;
+        return new Hashtable(_managerMap);
     }
 
     /**
@@ -172,7 +154,7 @@ public class COPSPepConnection implements Runnable {
      */
     public void setResponseTime(int respTime) {
         _responseTime = respTime;
-    };
+    }
 
     /**
      * Sets keep-alive timer
@@ -210,7 +192,7 @@ public class COPSPepConnection implements Runnable {
                     int _startTime = (int) (_lastRecKa.getTime());
                     int cTime = (int) (new Date().getTime());
 
-                    if ((int)(cTime - _startTime) > _kaTimer*1000) {
+                    if ((cTime - _startTime) > _kaTimer*1000) {
                         _sock.close();
                         // Notify all Request State Managers
                         notifyNoKAAllReqStateMan();
@@ -220,7 +202,7 @@ public class COPSPepConnection implements Runnable {
                     _startTime = (int) (_lastSendKa.getTime());
                     cTime = (int) (new Date().getTime());
 
-                    if ((int)(cTime - _startTime) > ((_kaTimer*3/4) * 1000)) {
+                    if ((cTime - _startTime) > ((_kaTimer*3/4) * 1000)) {
                         COPSHeader hdr = new COPSHeader(COPSHeader.COPS_OP_KA);
                         COPSKAMsg msg = new COPSKAMsg();
 
@@ -236,7 +218,7 @@ public class COPSPepConnection implements Runnable {
                     int _startTime = (int) (_lastSendAcc.getTime());
                     int cTime = (int) (new Date().getTime());
 
-                    if ((int)(cTime - _startTime) > ((_acctTimer*3/4)*1000)) {
+                    if ((cTime - _startTime) > ((_acctTimer*3/4)*1000)) {
                         // Notify all Request State Managers
                         notifyAcctAllReqStateMan();
                         _lastSendAcc = new Date();
@@ -245,22 +227,28 @@ public class COPSPepConnection implements Runnable {
 
                 try {
                     Thread.sleep(500);
-                } catch (Exception e) {};
+                } catch (Exception e) {
+                    logger.error("Exception thrown while sleeping", e);
+                }
             }
         } catch (Exception e) {
-            COPSDebug.err(getClass().getName(), COPSDebug.ERROR_SOCKET, e);
+            logger.error("Error while processing socket messages", e);
         }
 
         // connection closed by server
         // COPSDebug.out(getClass().getName(),"Connection closed by server");
         try {
             _sock.close();
-        } catch (IOException e) {};
+        } catch (IOException e) {
+            logger.error("Error closing socket", e);
+        }
 
         // Notify all Request State Managers
         try {
             notifyCloseAllReqStateMan();
-        } catch (COPSPepException e) {};
+        } catch (COPSPepException e) {
+            logger.error("Error closing state managers");
+        }
     }
 
     /**
@@ -316,12 +304,13 @@ public class COPSPepConnection implements Runnable {
         try {
             // Support
             if (cMsg.getIntegrity() != null) {
-                COPSDebug.err(getClass().getName(), COPSDebug.ERROR_NOSUPPORTED,
-                              "Unsupported objects (Integrity) to connection " + conn.getInetAddress());
+                logger.warn("Unsupported objects (Integrity) to connection " + conn.getInetAddress());
             }
 
             conn.close();
-        } catch (Exception unae) { };
+        } catch (Exception unae) {
+            logger.error("Unexpected exception closing connection", unae);
+        }
     }
 
     /**
@@ -354,13 +343,14 @@ public class COPSPepConnection implements Runnable {
         try {
             // Support
             if (cMsg.getIntegrity() != null) {
-                COPSDebug.err(getClass().getName(), COPSDebug.ERROR_NOSUPPORTED,
-                              "Unsupported objects (Integrity) to connection " + conn.getInetAddress());
+                logger.warn("Unsupported objects (Integrity) to connection " + conn.getInetAddress());
             }
 
             // should we do anything else?? ....
 
-        } catch (Exception unae) { };
+        } catch (Exception unae) {
+            logger.error("Unexpected exception while writing COPS data", unae);
+        }
     }
 
     /**
@@ -403,9 +393,9 @@ public class COPSPepConnection implements Runnable {
                 COPSDecision decision = (COPSDecision) ee.nextElement();
 
                 // Get the associated manager
-                COPSPepReqStateMan manager = (COPSPepReqStateMan) _managerMap.get(handle.getId().str());
+                COPSPepReqStateMan manager = _managerMap.get(handle.getId().str());
                 if (manager == null)
-                    COPSDebug.err(getClass().getName(), COPSDebug.ERROR_NOEXPECTEDMSG);
+                    logger.warn("Unable to find state manager with key - " + handle.getId().str());
 
                 // Check message type
                 if (decision.getFlags() == COPSDecision.F_REQSTATE) {
@@ -433,9 +423,9 @@ public class COPSPepConnection implements Runnable {
     private void handleOpenNewRequestStateMsg(Socket conn, COPSHandle handle)
     throws COPSPepException {
 
-        COPSPepReqStateMan manager = (COPSPepReqStateMan) _managerMap.get(handle.getId().str());
+        COPSPepReqStateMan manager = _managerMap.get(handle.getId().str());
         if (manager == null)
-            COPSDebug.err(getClass().getName(), COPSDebug.ERROR_NOEXPECTEDMSG);
+            logger.warn("Unable to find state manager with key - " + handle.getId().str());
 
         manager.processOpenNewRequestState();
     }
@@ -459,13 +449,12 @@ public class COPSPepConnection implements Runnable {
 
         // Support
         if (cMsg.getIntegrity() != null) {
-            COPSDebug.err(getClass().getName(), COPSDebug.ERROR_NOSUPPORTED,
-                          "Unsupported objects (Integrity) to connection " + conn.getInetAddress());
+            logger.warn("Unsupported objects (Integrity) to connection " + conn.getInetAddress());
         }
 
         COPSPepReqStateMan manager = (COPSPepReqStateMan) _managerMap.get(cMsg.getClientHandle().getId().str());
         if (manager == null) {
-            COPSDebug.err(getClass().getName(), COPSDebug.ERROR_NOEXPECTEDMSG);
+            logger.warn("Unable to find state manager with key - " + cMsg.getClientHandle().getId().str());
         } else {
             manager.processSyncStateRequest(cMsg);
         }
@@ -509,39 +498,21 @@ public class COPSPepConnection implements Runnable {
         manager.finalizeRequestState();
     }
 
-    private void notifyCloseAllReqStateMan()
-    throws COPSPepException {
-        if (_managerMap.size() > 0) {
-            for (Enumeration e = _managerMap.keys() ; e.hasMoreElements() ;) {
-                String handle = (String) e.nextElement();
-                COPSPepReqStateMan man = (COPSPepReqStateMan) _managerMap.get(handle);
-
-                man.processClosedConnection(_error);
-            }
+    private void notifyCloseAllReqStateMan() throws COPSPepException {
+        for (final COPSPepReqStateMan man: _managerMap.values()) {
+            man.processClosedConnection(_error);
         }
     }
 
-    private void notifyNoKAAllReqStateMan()
-    throws COPSPepException {
-        if (_managerMap.size() > 0) {
-            for (Enumeration e = _managerMap.keys() ; e.hasMoreElements() ;) {
-                String handle = (String) e.nextElement();
-                COPSPepReqStateMan man = (COPSPepReqStateMan) _managerMap.get(handle);
-
-                man.processNoKAConnection();
-            }
+    private void notifyNoKAAllReqStateMan() throws COPSPepException {
+        for (final COPSPepReqStateMan man: _managerMap.values()) {
+            man.processNoKAConnection();
         }
     }
 
-    private void notifyAcctAllReqStateMan()
-    throws COPSPepException {
-        if (_managerMap.size() > 0) {
-            for (Enumeration e = _managerMap.keys() ; e.hasMoreElements() ;) {
-                String handle = (String) e.nextElement();
-                COPSPepReqStateMan man = (COPSPepReqStateMan) _managerMap.get(handle);
-
-                man.processAcctReport();
-            }
+    private void notifyAcctAllReqStateMan() throws COPSPepException {
+        for (final COPSPepReqStateMan man: _managerMap.values()) {
+            man.processAcctReport();
         }
     }