Refactor shared attributes and methods to the PEP agent super COPSPepAgent. 42/19742/2
authorSteven Pisarski <s.pisarski@cablelabs.com>
Wed, 6 May 2015 19:34:48 +0000 (13:34 -0600)
committerThomas Kee <xsited@yahoo.com>
Fri, 8 May 2015 19:52:03 +0000 (12:52 -0700)
Change-Id: Ibadb787afc54549bc46fc2efcab2f5f0eb9c223e
Signed-off-by: Steven Pisarski <s.pisarski@cablelabs.com>
packetcable-driver/src/main/java/org/pcmm/PCMMPepAgent.java
packetcable-driver/src/main/java/org/umu/cops/ospep/COPSPepOSAgent.java
packetcable-driver/src/main/java/org/umu/cops/ospep/COPSPepOSConnection.java
packetcable-driver/src/main/java/org/umu/cops/prpep/COPSPepAgent.java
packetcable-driver/src/main/java/org/umu/cops/prpep/COPSPepConnection.java

index cbe268b022ac0d9e07b4c1bd808db4e35f184d7e..536cd578055bfd9af06341dd2d5c940afb42d054 100644 (file)
 
 package org.pcmm;
 
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 import org.umu.cops.prpep.COPSPepAgent;
-import org.umu.cops.prpep.COPSPepConnection;
-import org.umu.cops.prpep.COPSPepException;
-import org.umu.cops.stack.*;
-import org.umu.cops.stack.COPSHeader.OPCode;
-
-import java.io.IOException;
-import java.net.ServerSocket;
-import java.net.Socket;
+import org.umu.cops.stack.COPSPepId;
 
 /**
  * This is a provisioning COPS PEP. Responsible for making connection to the PDP
- * and maintaining it
+ * and maintaining it.
+ *
+ * TODO - implement me
  */
-public class PCMMPepAgent extends COPSPepAgent implements Runnable {
-
-    public final static Logger logger = LoggerFactory.getLogger(PCMMPepAgent.class);
-
-    /** Well-known port for COPS */
-    public static final int WELL_KNOWN_CMTS_PORT = 3918;
-
-    /**
-     * PDP host IP
-     */
-    private ServerSocket serverSocket;
-
-    /**
-     * PDP host port
-     */
-    private int serverPort;
-
-    /**
-     * COPS error returned by PDP
-     */
-    private COPSError error;
+public class PCMMPepAgent extends COPSPepAgent {
 
     /**
      * Creates a PEP agent
-     *
-     * @param pepID
-     *            PEP-ID
-     * @param clientType
-     *            Client-type
-     */
-    public PCMMPepAgent(final String pepID, final short clientType) {
-        super(pepID, clientType);
-        serverPort = WELL_KNOWN_CMTS_PORT;
-    }
-
-    /**
-     * Creates a PEP agent with a PEP-ID equal to "noname"
-     *
-     * @param clientType
-     *            Client-type
-     */
-    public PCMMPepAgent(final short clientType) {
-        super(clientType);
-        serverPort = WELL_KNOWN_CMTS_PORT;
-    }
-
-    /**
-     * Runs the PEP process XXX - not sure of the exception throwing
-     */
-    public void run() {
-        try {
-
-            logger.info("Create Server Socket on Port " + serverPort);
-
-            serverSocket = new ServerSocket(serverPort);
-            // Loop through for Incoming messages
-
-            // server infinite loop
-            while (true) {
-
-                // Wait for an incoming connection from a PEP
-                Socket socket = serverSocket.accept();
-
-                logger.info("New connection accepted " + socket.getInetAddress() + ":" + socket.getPort());
-
-                processConnection(socket);
-                /**
-                 * XXX - processConnection handles the open request from PEP And
-                 * a thread is created for conn = new
-                 * COPSPepConnection(_clientType, socket); the main processing
-                 * loop for PEP
-                 */
-
-            }
-        } catch (Exception e) {
-            logger.error("Error while processing the socket connection", e);
-        }
-    }
-
-    /**
-     * Establish connection to PDP's IP address
-     *
-     * <Client-Open> ::= <Common Header> <PEPID> [<ClientSI>] [<LastPDPAddr>]
-     * [<Integrity>]
-     *
-     * Not support [<ClientSI>], [<LastPDPAddr>], [<Integrity>]
-     *
-     * <Client-Accept> ::= <Common Header> <KA Timer> [<ACCT Timer>]
-     * [<Integrity>]
-     *
-     * Not send [<Integrity>]
-     *
-     * <Client-Close> ::= <Common Header> <Error> [<PDPRedirAddr>] [<Integrity>]
-     *
-     * Not send [<PDPRedirAddr>], [<Integrity>]
-     *
-     * @throws IOException
-     * @throws COPSException
-     * @throws COPSPepException
-     *
-     */
-    private COPSPepConnection processConnection(Socket socket) throws IOException, COPSException, COPSPepException {
-        // Build OPN
-        final COPSPepId pepId = new COPSPepId(new COPSData(getPepID()));
-        final COPSClientOpenMsg msg = new COPSClientOpenMsg(getClientType(), pepId, null, null, null);
-
-        // Create Socket and send OPN
-        /*
-         * InetAddress addr = InetAddress.getByName(psHost); Socket socket = new
-         * Socket(addr,psPort);
-         */
-        logger.info("Send COPSClientOpenMsg to PDP");
-        msg.writeData(socket);
-
-        // Receive the response
-        logger.info("Receive the resposne from PDP");
-        COPSMsg recvmsg = COPSTransceiver.receiveMsg(socket);
-
-        if (recvmsg.getHeader().getOpCode().equals(OPCode.CAT)) {
-            logger.info("isAClientAccept from PDP");
-            COPSClientAcceptMsg cMsg = (COPSClientAcceptMsg) recvmsg;
-
-            // Support
-            if (cMsg.getIntegrity() != null) {
-                throw new COPSPepException("Unsupported object (Integrity)");
-            }
-
-            // Mandatory KATimer
-            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();
-            short _acctTimer = 0;
-            if (at != null)
-                _acctTimer = at.getTimerVal();
-
-            // Create the connection manager
-            COPSPepConnection conn = new COPSPepConnection(getClientType(),
-                    socket);
-            conn.setKaTimer(_kaTimeVal);
-            conn.setAcctTimer(_acctTimer);
-            logger.info("Thread(conn).start");
-            new Thread(conn).start();
-
-            return conn;
-        } else if (recvmsg.getHeader().getOpCode().equals(OPCode.CC)) {
-            logger.info("isAClientClose from PDP");
-            COPSClientCloseMsg cMsg = (COPSClientCloseMsg) recvmsg;
-            error = cMsg.getError();
-            socket.close();
-            return null;
-        } else { // messages of other types are not expected
-            throw new COPSPepException(
-                "Message not expected. Closing connection for "
-                + socket.toString());
-        }
-    }
-
-    /**
-     * Gets the COPS error returned by the PDP
-     *
-     * @return <tt>COPSError</tt> returned by PDP
-     */
-    public COPSError getConnectionError() {
-        return error;
-    }
-
-    public void setConnectionError(COPSError _error) {
-        this.error = _error;
-    }
-
-    /**
-     * @return the serverSocket
-     */
-    public ServerSocket getServerSocket() {
-        return serverSocket;
-    }
-
-    /**
-     * @param serverSocket
-     *            the serverSocket to set
-     */
-    public void setServerSocket(ServerSocket serverSocket) {
-        this.serverSocket = serverSocket;
-    }
-
-    /**
-     * @return the serverPort
-     */
-    public int getServerPort() {
-        return serverPort;
-    }
-
-    /**
-     * @param serverPort
-     *            the serverPort to set
+     * @param    clientType         Client-type
+     * @param    pepID              PEP-ID
+     * @param    port               the server socket port to open on this host
      */
-    public void setServerPort(int serverPort) {
-        this.serverPort = serverPort;
+    public PCMMPepAgent(final short clientType, final COPSPepId pepID, final int port) {
+        super(clientType, pepID, port);
     }
 
 }
index 1c16497f14a2e9d99f8a924ebd986145f7729268..1e7a466bd7a13f82d32f4027b2bc7fd07eaea31e 100644 (file)
@@ -2,181 +2,40 @@ package org.umu.cops.ospep;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import org.umu.cops.stack.*;
-import org.umu.cops.stack.COPSHeader.OPCode;
+import org.umu.cops.prpep.COPSPepAgent;
+import org.umu.cops.prpep.COPSPepConnection;
+import org.umu.cops.prpep.COPSPepReqStateMan;
+import org.umu.cops.stack.COPSClientSI;
+import org.umu.cops.stack.COPSException;
+import org.umu.cops.stack.COPSHandle;
+import org.umu.cops.stack.COPSPepId;
 
-import java.io.IOException;
-import java.net.InetAddress;
 import java.net.Socket;
-import java.util.Hashtable;
 import java.util.List;
 
 /**
  * This is a outsourcing COPS PEP. Responsible for making
  * connection to the PDP and maintaining it
  */
-public class COPSPepOSAgent {
+public class COPSPepOSAgent extends COPSPepAgent {
 
     public final static Logger logger = LoggerFactory.getLogger(COPSPepOSAgent.class);
 
-    /**
-        PEP's identifier
-     */
-    private String _pepID;
-
-    /**
-        PEP's client-type
-     */
-    private short _clientType;
-
-    /**
-        PDP host name
-     */
-    private String _psHost;
-
-    /**
-        PDP port
-     */
-    private int _psPort;
-
-    /**
-        PEP-PDP connection manager
-     */
-    private COPSPepOSConnection _conn;
-
-    /**
-        COPS error returned by the PDP
-     */
-    private COPSError _error;
-
     /**
      * Policy data processor class
      */
-    private COPSPepOSDataProcess _process;
+    private transient COPSPepOSDataProcess _process;
 
     /**
      * Creates a PEP agent
-     * @param    pepID              PEP-ID
-     * @param    clientType         Client-type
-     */
-    public COPSPepOSAgent(final String pepID, final short clientType) {
-        _pepID = pepID;
-        _clientType = clientType;
-    }
-
-    /**
-     * Creates a PEP agent with a PEP-ID equal to "noname"
      * @param    clientType         Client-type
+     * @param    pepID              PEP-ID
+     * @param    port               The port to begin listening
      */
-    public COPSPepOSAgent(final short clientType) {
-        // PEPId
-        try {
-            _pepID = InetAddress.getLocalHost().getHostName();
-        } catch (Exception e) {
-            _pepID = "noname";
-        }
-
-        _clientType = clientType;
-    }
-
-    /**
-     * Gets the identifier of the PEP
-     * @return  PEP-ID
-     */
-    public String getPepID() {
-        return _pepID;
-    }
-
-    /**
-     * Sets the policy data processor
-     * @param aDataProcess  Data processor class
-     */
-    public void setDataProcess(COPSPepOSDataProcess aDataProcess) {
-        this._process = aDataProcess;
-    }
-
-    /**
-     * Gets the COPS client-type
-     * @return  PEP's client-type
-     */
-    public int getClientType() {
-        return _clientType;
-    }
-
-    /**
-     * Gets PDP host name
-     * @return  PDP host name
-     */
-    public String getPDPName() {
-        return _psHost;
-    }
-
-    /**
-     * Gets the port of the PDP
-     * @return  PDP port
-     */
-    public int getPDPPort() {
-        return _psPort;
-    }
-
-    /**
-     * Connects to a PDP
-     * @param    psHost              PDP host name
-     * @param    psPort              PDP port
-     * @return   <tt>true</tt> if PDP accepts the connection; <tt>false</tt> otherwise
-     * @throws   java.net.UnknownHostException
-     * @throws   java.io.IOException
-     * @throws   COPSException
-     * @throws   COPSPepException
-     */
-    public boolean connect(String psHost, int psPort) throws IOException, COPSException, COPSPepException {
-        // COPSDebug.out(getClass().getName(), "Thread ( " + _pepID + ") - Connecting to PDP");
-        _psHost = psHost;
-        _psPort = psPort;
-
-        // Check whether it already exists
-        if (_conn == null)
-            _conn = processConnection(psHost,psPort);
-        else {
-            // Check whether it's closed
-            if (_conn.isClosed())
-                _conn = processConnection(psHost,psPort);
-            else {
-                disconnect(null);
-                _conn = processConnection(psHost,psPort);
-            }
-        }
-
-        return (_conn != null);
-    }
-
-    /**
-     * Gets the connection manager
-     * @return  PEP-PDP connection manager object
-     */
-    public COPSPepOSConnection getConnection() {
-        return (_conn);
-    }
-
-    /**
-     * Gets the COPS error returned by the PDP
-     * @return   <tt>COPSError</tt> returned by PDP
-     */
-    public COPSError getConnectionError() {
-        return _error;
-    }
-
-    /**
-     * Disconnects from the PDP
-     * @param error Reason
-     * @throws COPSException
-     * @throws IOException
-     */
-    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;
+    public COPSPepOSAgent(final short clientType, final COPSPepId pepID, final int port,
+                          final COPSPepOSDataProcess process) {
+        super(clientType, pepID, port);
+        this._process = process;
     }
 
     /**
@@ -186,113 +45,17 @@ public class COPSPepOSAgent {
      * @throws COPSPepException
      * @throws COPSException
      */
-    public COPSPepOSReqStateMan addRequestState(final COPSHandle handle, List<COPSClientSI> clientSIs) throws COPSPepException, COPSException {
-        if (_conn != null)
-            return _conn.addRequestState(handle.getId().str(), _process, clientSIs);
-
-        return null;
-    }
-
-    /**
-     * Queries the connection manager to delete a request state
-     * @param man   Request state manager
-     * @throws COPSPepException
-     * @throws COPSException
-     */
-    public void deleteRequestState (COPSPepOSReqStateMan man) throws COPSPepException, COPSException {
+    public COPSPepReqStateMan addRequestState(final COPSHandle handle, final List<COPSClientSI> clientSIs)
+            throws COPSException {
         if (_conn != null)
-            _conn.deleteRequestState(man);
-    }
+            return ((COPSPepOSConnection)_conn).addRequestState(handle, _process, clientSIs);
 
-    /**
-     * 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
-     *
-     * <Client-Open> ::= <Common Header>
-     *                  <PEPID>
-     *                  [<ClientSI>]
-     *                  [<LastPDPAddr>]
-     *                  [<Integrity>]
-     *
-     * Not support [<ClientSI>], [<LastPDPAddr>], [<Integrity>]
-     *
-     * <Client-Accept> ::= <Common Header>
-     *                      <KA Timer>
-     *                      [<ACCT Timer>]
-     *                      [<Integrity>]
-     *
-     * Not send [<Integrity>]
-     *
-     * <Client-Close> ::= <Common Header>
-     *                      <Error>
-     *                      [<PDPRedirAddr>]
-     *                      [<Integrity>]
-     *
-     * Not send [<PDPRedirAddr>], [<Integrity>]
-     *
-     * @throws   IOException
-     * @throws   COPSException
-     * @throws   COPSPepException
-     *
-     */
-    private COPSPepOSConnection processConnection(final String psHost, final int psPort)
-            throws IOException, COPSException, COPSPepException {
-        // Build OPN
-        final COPSClientOpenMsg msg = new COPSClientOpenMsg(_clientType, new COPSPepId(new COPSData(_pepID)),
-                null, null, null);
-
-        // Create socket and send OPN
-        final InetAddress addr = InetAddress.getByName(psHost);
-        final Socket socket = new Socket(addr,psPort);
-        msg.writeData(socket);
-
-        // Get response
-        final COPSMsg recvmsg = COPSTransceiver.receiveMsg(socket);
-
-        if (recvmsg.getHeader().getOpCode().equals(OPCode.CAT)) {
-            final COPSClientAcceptMsg cMsg = (COPSClientAcceptMsg) recvmsg;
-
-            // Support
-            if (cMsg.getIntegrity() != null) {
-                throw new COPSPepException("Unsupported object (Integrity)");
-            }
-
-            // Mandatory KATimer
-            final COPSKATimer kt = cMsg.getKATimer();
-            if (kt == null)
-                throw new COPSPepException ("Mandatory COPS object missing (KA Timer)");
-            short _kaTimeVal = kt.getTimerVal();
-
-            // ACTimer
-            final COPSAcctTimer at = cMsg.getAcctTimer();
-            short _acctTimer;
-            if (at != null) _acctTimer = at.getTimerVal();
-            else _acctTimer = 0;
-
-            // Create connection manager
-            final COPSPepOSConnection conn = new COPSPepOSConnection(_clientType, socket);
-            conn.setKaTimer(_kaTimeVal);
-            conn.setAcctTimer(_acctTimer);
-            new Thread(conn).start();
-
-            return conn;
-        } else if (recvmsg.getHeader().getOpCode().equals(OPCode.CC)) {
-            final COPSClientCloseMsg cMsg = (COPSClientCloseMsg) recvmsg;
-            _error = cMsg.getError();
-            socket.close();
-            return null;
-        } else { // other message types are unexpected
-            throw new COPSPepException("Message not expected. Closing connection for " + socket.toString());
-        }
+    @Override
+    protected COPSPepConnection createPepConnection(final Socket socket) {
+        return new COPSPepOSConnection(_clientType, socket);
     }
 
     /**
@@ -300,7 +63,7 @@ public class COPSPepOSAgent {
      * @param handle The COPS handle for this request
      * @param clientSIs The client specific data for this request
      */
-    public void dispatchEvent(COPSHandle handle, final List<COPSClientSI> clientSIs) {
+    public void dispatchEvent(final COPSHandle handle, final List<COPSClientSI> clientSIs) {
         try {
             addRequestState(handle, clientSIs);
         } catch (Exception e) {
index a375d3e1ff5015b16e9a87525d0973ecc3734e04..8a2eb863101ef559d21454e8811e1ec8cc21843d 100644 (file)
@@ -1,10 +1,12 @@
 package org.umu.cops.ospep;
 
 import org.umu.cops.prpep.COPSPepConnection;
-import org.umu.cops.stack.*;
+import org.umu.cops.stack.COPSClientSI;
+import org.umu.cops.stack.COPSDecisionMsg;
+import org.umu.cops.stack.COPSException;
+import org.umu.cops.stack.COPSHandle;
 
 import java.net.Socket;
-import java.util.Hashtable;
 import java.util.List;
 
 /**
@@ -22,15 +24,6 @@ public class COPSPepOSConnection extends COPSPepConnection {
         super(clientType, sock);
     }
 
-    /**
-     * Gets all request state managers
-     * @return  A <tt>Hashatable</tt> holding all request state managers
-     * TODO - change the return to Map
-     */
-    protected Hashtable getReqStateMans() {
-        return new Hashtable(_managerMap);
-    }
-
     @Override
     protected void handleDecisionMsg(final COPSDecisionMsg dMsg) throws COPSException {
         final COPSPepOSReqStateMan manager = (COPSPepOSReqStateMan)_managerMap.get(dMsg.getClientHandle());
@@ -39,18 +32,17 @@ public class COPSPepOSConnection extends COPSPepConnection {
 
     /**
      * Adds a new request state
-     * @param clientHandle  Client's handle
+     * @param handle        Client's handle
      * @param process       Policy data processing object
      * @param clientSIs     Client data from the outsourcing event
      * @return              The newly created request state manager
      * @throws COPSException
      */
-    protected COPSPepOSReqStateMan addRequestState(final String clientHandle, final COPSPepOSDataProcess process,
+    protected COPSPepOSReqStateMan addRequestState(final COPSHandle handle, final COPSPepOSDataProcess process,
                                                    final List<COPSClientSI> clientSIs) throws COPSException {
-        final COPSHandle handle = new COPSHandle(new COPSData(clientHandle));
         final COPSPepOSReqStateMan manager = new COPSPepOSReqStateMan(_clientType, handle, process, clientSIs);
         if (_managerMap.get(handle) != null)
-            throw new COPSPepException("Duplicate Handle, rejecting " + clientHandle);
+            throw new COPSPepException("Duplicate Handle, rejecting " + handle.getId().str());
         _managerMap.put(handle, manager);
         manager.initRequestState(_sock);
         return manager;
index e7ab809d9260249aec2701409b8f13f2eb7d9117..028c9e4b05268b3045aaf5c5763792edf6efec4d 100644 (file)
@@ -9,12 +9,12 @@ package org.umu.cops.prpep;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.umu.cops.stack.*;
+import org.umu.cops.stack.COPSError.ErrorTypes;
 import org.umu.cops.stack.COPSHeader.OPCode;
 
 import java.io.IOException;
 import java.net.InetAddress;
 import java.net.Socket;
-import java.net.UnknownHostException;
 
 /**
  * This is a provisioning COPS PEP. Responsible for making
@@ -22,140 +22,72 @@ import java.net.UnknownHostException;
  */
 public class COPSPepAgent {
 
-    private final static Logger logger = LoggerFactory.getLogger(COPSPepAgent.class);
+    /** Well-known port for COPS */
+    public static final int WELL_KNOWN_CMTS_PORT = 3918;
 
-    /**
-        PEP's Identifier
-     */
-    private String _pepID;
+    private final static Logger logger = LoggerFactory.getLogger(COPSPepAgent.class);
 
     /**
-        PEP's client-type
+     PEP's client-type
      */
-    private short _clientType;
+    protected final short _clientType;
 
     /**
-        PDP host name
+     * PEP's Identifier
      */
-    private String _psHost;
+    protected final COPSPepId _pepID;
 
     /**
-        PDP port
+     * PDP port
      */
-    private int _psPort;
+    private final int _psPort;
 
+    // The next two attributes are instantiated after the connect() method has successfully completed.
     /**
-        PEP-PDP connection manager
+     * PEP-PDP connection manager
      */
-    private COPSPepConnection _conn;
+    protected transient COPSPepConnection _conn;
 
     /**
-        COPS error returned by PDP
+     * The thread object to manage the connection thread.
      */
-    private COPSError _error;
+    private transient Thread thread;
 
     /**
      * Creates a PEP agent
-     * @param    pepID              PEP-ID
      * @param    clientType         Client-type
+     * @param    pepID              PEP-ID
+     * @param    port               the server socket port to open on this host
      */
-    public COPSPepAgent(final String pepID, final short clientType) {
-        _pepID = pepID;
+    public COPSPepAgent(final short clientType, final COPSPepId pepID, final int port) {
         _clientType = clientType;
+        _pepID = pepID;
+        this._psPort = port;
     }
 
     /**
-     * Creates a PEP agent with a PEP-ID equal to "noname"
-     * @param    clientType         Client-type
-     */
-    public COPSPepAgent(final short clientType) {
-
-        // PEPId
-        try {
-            _pepID = InetAddress.getLocalHost().getHostName();
-        } catch (Exception e) {
-            _pepID = "noname";
-        }
-
-        _clientType = clientType;
-    }
-
-    /**
-     * Gets the identifier of the PEP
-     * @return  PEP-ID
-     */
-    public String getPepID() {
-        return _pepID;
-    }
-
-    /**
-     * Gets the COPS client-type
-     * @return  PEP's client-type
-     */
-    public short getClientType() {
-        return _clientType;
-    }
-
-    /**
-     * Gets PDP host name
-     * @return  PDP host name
-     */
-    public String getPDPName() {
-        return _psHost;
-    }
-
-    /**
-     * Gets the port of the PDP
-     * @return  PDP port
-     */
-    public int getPDPPort() {
-        return _psPort;
-    }
-
-    /**
-     * Connects to a PDP
-     * @param    psHost              PDP host name
-     * @param    psPort              PDP port
-     * @return   <tt>true</tt> if PDP accepts the connection; <tt>false</tt> otherwise
+     * Connects to a PDP and is responsible for setting up the connection
      * @throws   java.io.IOException
      * @throws   COPSException
      * @throws   COPSPepException
      */
-    public boolean connect(String psHost, int psPort) throws IOException, COPSException {
+    public void connect() throws IOException, COPSException {
         logger.info("Thread ( " + _pepID + ") - Connecting to PDP");
-        _psHost = psHost;
-        _psPort = psPort;
 
         // Check whether it already exists
         if (_conn == null)
-            _conn = processConnection(psHost,psPort);
+            _conn = processConnection();
         else {
             // Check if it's closed
             if (_conn.isClosed()) {
-                _conn = processConnection(psHost,psPort);
+                _conn = processConnection();
             } else {
-                disconnect(null);
-                _conn = processConnection(psHost,psPort);
+                disconnect(new COPSError(ErrorTypes.SHUTTING_DOWN, ErrorTypes.NA));
+                _conn = processConnection();
             }
         }
 
-        return (_conn != null);
-    }
-
-    /**
-     * Gets the connection manager
-     * @return  PEP-PDP connection manager object
-     */
-    public COPSPepConnection getConnection () {
-        return (_conn);
-    }
-
-    /**
-     * Gets the COPS error returned by the PDP
-     * @return   <tt>COPSError</tt> returned by PDP
-     */
-    public COPSError getConnectionError()   {
-        return _error;
+        if (_conn == null) throw new COPSException("Unable to process PEP connection");
     }
 
     /**
@@ -166,8 +98,11 @@ public class COPSPepAgent {
      */
     public void disconnect(final COPSError error) throws COPSException, IOException {
         final COPSClientCloseMsg closeMsg = new COPSClientCloseMsg(_clientType, error, null, null);
+        thread.interrupt();
+        thread = null;
         closeMsg.writeData(_conn.getSocket());
         _conn.close();
+        _conn = null;
     }
 
     /**
@@ -198,44 +133,23 @@ public class COPSPepAgent {
 
     /**
      * Establish connection to PDP's IP address
-     *
-     * <Client-Open> ::= <Common Header>
-     *                  <PEPID>
-     *                  [<ClientSI>]
-     *                  [<LastPDPAddr>]
-     *                  [<Integrity>]
-     *
-     * Not support [<ClientSI>], [<LastPDPAddr>], [<Integrity>]
-     *
-     * <Client-Accept> ::= <Common Header>
-     *                      <KA Timer>
-     *                      [<ACCT Timer>]
-     *                      [<Integrity>]
-     *
-     * Not send [<Integrity>]
-     *
-     * <Client-Close> ::= <Common Header>
-     *                      <Error>
-     *                      [<PDPRedirAddr>]
-     *                      [<Integrity>]
-     *
-     * Not send [<PDPRedirAddr>], [<Integrity>]
-     *
-     * @throws   UnknownHostException
-     * @throws   IOException
      * @throws   COPSException
      * @throws   COPSPepException
-     *
      */
-    private COPSPepConnection processConnection(final String psHost, final int psPort)
-            throws IOException, COPSException {
-        // Build OPN
-        final COPSClientOpenMsg msg = new COPSClientOpenMsg(_clientType, new COPSPepId(new COPSData(_pepID)),
-                null, null, null);
+    private COPSPepConnection processConnection() throws IOException, COPSException {
+        // Create Socket and send OPN
+        final InetAddress addr = InetAddress.getLocalHost();
+        return processConnection(new Socket(addr, _psPort));
+    }
 
+    /**
+     * Establish connection to PDP's IP address
+     * @throws   COPSException
+     * @throws   COPSPepException
+     */
+    private COPSPepConnection processConnection(final Socket socket) throws IOException, COPSException {
         // Create Socket and send OPN
-        final InetAddress addr = InetAddress.getByName(psHost);
-        final Socket socket = new Socket(addr,psPort);
+        final COPSClientOpenMsg msg = new COPSClientOpenMsg(_clientType, _pepID, null, null, null);
         msg.writeData(socket);
 
         // Receive the response
@@ -262,21 +176,33 @@ public class COPSPepAgent {
                 _acctTimer = at.getTimerVal();
 
             // Create the connection manager
-            final COPSPepConnection conn = new COPSPepConnection(_clientType, socket);
+            final COPSPepConnection conn = createPepConnection(socket);
             conn.setKaTimer(_kaTimeVal);
             conn.setAcctTimer(_acctTimer);
-            new Thread(conn).start();
+            thread = new Thread(conn);
+            thread.start();
 
             return conn;
         } else if (recvmsg.getHeader().getOpCode().equals(OPCode.CC)) {
             final COPSClientCloseMsg cMsg = (COPSClientCloseMsg) recvmsg;
-            _error = cMsg.getError();
+            logger.error("Received client-close message with error description [" + cMsg.getError().getDescription()
+                    + "]. Closing socket.");
             socket.close();
             return null;
         } else { // messages of other types are not expected
             throw new COPSPepException("Message not expected. Closing connection for " + socket.toString());
         }
     }
+
+    /**
+     * Creates a COPSPepConnection object
+     * @param socket - the socket on which to create the connection
+     * @return - the connection object
+     */
+    protected COPSPepConnection createPepConnection(final Socket socket) {
+        return new COPSPepConnection(_clientType, socket);
+    }
+
 }
 
 
index 453ca39544aae79dada0b0b83062792d5490399f..341857529aa3c4aeb035a5ea76a730834bebe66d 100644 (file)
@@ -244,7 +244,7 @@ public class COPSPepConnection extends COPSConnection {
      * @throws   COPSException
      * @throws   COPSPepException
      */
-    protected COPSPepReqStateMan addRequestState(final COPSHandle clientHandle, final COPSPepDataProcess process)
+    public COPSPepReqStateMan addRequestState(final COPSHandle clientHandle, final COPSPepDataProcess process)
             throws COPSException {
         final COPSPepReqStateMan manager = new COPSPepReqStateMan(_clientType, clientHandle, process);
         if (_managerMap.get(clientHandle) != null)