Replaced use of logger abstraction COPSDebug.java to having this class log directly...
[packetcable.git] / packetcable-driver / src / main / java / org / umu / cops / prpep / COPSPepConnection.java
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();
         }
     }