Merge changes If0630105,I9d2d5e61,I1cea2a32,Icc05b6a7,Ic57eb4f8, ...
[packetcable.git] / packetcable-driver / src / main / java / org / umu / cops / prpep / COPSPepAgent.java
index ca6f2de8630d70610aa1d6436e0c94947de99990..e7ab809d9260249aec2701409b8f13f2eb7d9117 100644 (file)
@@ -6,13 +6,15 @@
 
 package org.umu.cops.prpep;
 
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.umu.cops.stack.*;
+import org.umu.cops.stack.COPSHeader.OPCode;
 
 import java.io.IOException;
 import java.net.InetAddress;
 import java.net.Socket;
 import java.net.UnknownHostException;
-import java.util.Hashtable;
 
 /**
  * This is a provisioning COPS PEP. Responsible for making
@@ -20,6 +22,8 @@ import java.util.Hashtable;
  */
 public class COPSPepAgent {
 
+    private final static Logger logger = LoggerFactory.getLogger(COPSPepAgent.class);
+
     /**
         PEP's Identifier
      */
@@ -55,7 +59,7 @@ public class COPSPepAgent {
      * @param    pepID              PEP-ID
      * @param    clientType         Client-type
      */
-    public COPSPepAgent(String pepID, short clientType) {
+    public COPSPepAgent(final String pepID, final short clientType) {
         _pepID = pepID;
         _clientType = clientType;
     }
@@ -64,7 +68,7 @@ public class COPSPepAgent {
      * Creates a PEP agent with a PEP-ID equal to "noname"
      * @param    clientType         Client-type
      */
-    public COPSPepAgent(short clientType) {
+    public COPSPepAgent(final short clientType) {
 
         // PEPId
         try {
@@ -117,9 +121,8 @@ public class COPSPepAgent {
      * @throws   COPSException
      * @throws   COPSPepException
      */
-    public boolean connect(String psHost, int psPort) throws IOException, COPSException, COPSPepException {
-
-        // COPSDebug.out(getClass().getName(), "Thread ( " + _pepID + ") - Connecting to PDP");
+    public boolean connect(String psHost, int psPort) throws IOException, COPSException {
+        logger.info("Thread ( " + _pepID + ") - Connecting to PDP");
         _psHost = psHost;
         _psPort = psPort;
 
@@ -161,18 +164,10 @@ public class COPSPepAgent {
      * @throws COPSException
      * @throws IOException
      */
-    public void disconnect(COPSError error)
-    throws COPSException, IOException {
-
-        COPSHeader cHdr = new COPSHeader(COPSHeader.COPS_OP_CC, _clientType);
-        COPSClientCloseMsg closeMsg = new COPSClientCloseMsg();
-        closeMsg.add(cHdr);
-        if (error != null)
-            closeMsg.add(error);
-
+    public void disconnect(final COPSError error) throws COPSException, IOException {
+        final COPSClientCloseMsg closeMsg = new COPSClientCloseMsg(_clientType, error, null, null);
         closeMsg.writeData(_conn.getSocket());
         _conn.close();
-        _conn = null;
     }
 
     /**
@@ -181,8 +176,8 @@ public class COPSPepAgent {
      * @throws COPSPepException
      * @throws COPSException
      */
-    public COPSPepReqStateMan addRequestState (String handle, COPSPepDataProcess process)
-    throws COPSPepException, COPSException {
+    public COPSPepReqStateMan addRequestState(final COPSHandle handle, final COPSPepDataProcess process)
+            throws COPSException {
         if (_conn != null) {
             return _conn.addRequestState(handle, process);
         }
@@ -196,22 +191,11 @@ public class COPSPepAgent {
      * @throws COPSPepException
      * @throws COPSException
      */
-    public void deleteRequestState (COPSPepReqStateMan man)
-    throws COPSPepException, COPSException {
+    public void deleteRequestState(final COPSPepReqStateMan man) throws COPSException {
         if (_conn != null)
             _conn.deleteRequestState(man);
     }
 
-    /**
-     * Gets all the request state managers
-     * @return  A <tt>Hashtable</tt> holding all active request state managers
-     */
-    public Hashtable getReqStateMans() {
-        if (_conn != null)
-            return _conn.getReqStateMans();
-        return null;
-    }
-
     /**
      * Establish connection to PDP's IP address
      *
@@ -243,26 +227,22 @@ public class COPSPepAgent {
      * @throws   COPSPepException
      *
      */
-    private COPSPepConnection processConnection(String psHost, int psPort) throws IOException, COPSException,
-            COPSPepException {
+    private COPSPepConnection processConnection(final String psHost, final int psPort)
+            throws IOException, COPSException {
         // Build OPN
-        COPSHeader hdr = new COPSHeader(COPSHeader.COPS_OP_OPN, _clientType);
-
-        COPSPepId pepId = new COPSPepId(new COPSData(_pepID));
-        COPSClientOpenMsg msg = new COPSClientOpenMsg();
-        msg.add(hdr);
-        msg.add(pepId);
+        final COPSClientOpenMsg msg = new COPSClientOpenMsg(_clientType, new COPSPepId(new COPSData(_pepID)),
+                null, null, null);
 
         // Create Socket and send OPN
-        InetAddress addr = InetAddress.getByName(psHost);
-        Socket socket = new Socket(addr,psPort);
+        final InetAddress addr = InetAddress.getByName(psHost);
+        final Socket socket = new Socket(addr,psPort);
         msg.writeData(socket);
 
         // Receive the response
-        COPSMsg recvmsg = COPSTransceiver.receiveMsg(socket);
+        final COPSMsg recvmsg = COPSTransceiver.receiveMsg(socket);
 
-        if (recvmsg.getHeader().isAClientAccept()) {
-            COPSClientAcceptMsg cMsg = (COPSClientAcceptMsg) recvmsg;
+        if (recvmsg.getHeader().getOpCode().equals(OPCode.CAT)) {
+            final COPSClientAcceptMsg cMsg = (COPSClientAcceptMsg) recvmsg;
 
             // Support
             if (cMsg.getIntegrity() != null) {
@@ -270,26 +250,26 @@ public class COPSPepAgent {
             }
 
             // Mandatory KATimer
-            COPSKATimer kt = cMsg.getKATimer();
+            final COPSKATimer kt = cMsg.getKATimer();
             if (kt == null)
                 throw new COPSPepException ("Mandatory COPS object missing (KA Timer)");
             short _kaTimeVal = kt.getTimerVal();
 
             // ACTimer
-            COPSAcctTimer at = cMsg.getAcctTimer();
+            final COPSAcctTimer at = cMsg.getAcctTimer();
             short _acctTimer = 0;
             if (at != null)
                 _acctTimer = at.getTimerVal();
 
             // Create the connection manager
-            COPSPepConnection conn = new COPSPepConnection(_clientType, socket);
+            final COPSPepConnection conn = new COPSPepConnection(_clientType, socket);
             conn.setKaTimer(_kaTimeVal);
             conn.setAcctTimer(_acctTimer);
             new Thread(conn).start();
 
             return conn;
-        } else if (recvmsg.getHeader().isAClientClose()) {
-            COPSClientCloseMsg cMsg = (COPSClientCloseMsg) recvmsg;
+        } else if (recvmsg.getHeader().getOpCode().equals(OPCode.CC)) {
+            final COPSClientCloseMsg cMsg = (COPSClientCloseMsg) recvmsg;
             _error = cMsg.getError();
             socket.close();
             return null;