Completed COPS Message refactoring. Was planning on one additional patch starting...
[packetcable.git] / packetcable-driver / src / main / java / org / umu / cops / prpdp / COPSPdpAgent.java
index 3e4d1c4b9ff61abeb9f9a9939ce858f3d621f0d1..61561c3ca962604e2b78e60f20736bdfc9172160 100644 (file)
@@ -10,11 +10,12 @@ 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.ClientType;
+import org.umu.cops.stack.COPSHeader.OPCode;
 
 import java.io.IOException;
 import java.net.ServerSocket;
 import java.net.Socket;
-import java.util.Hashtable;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 
@@ -40,7 +41,7 @@ public class COPSPdpAgent extends Thread {
     /**
         Client-type of connecting PEP
      */
-    private short _clientType;
+    private ClientType _clientType;
 
     /**
         Accounting timer (secs)
@@ -69,7 +70,7 @@ public class COPSPdpAgent extends Thread {
      * @param clientType    COPS Client-type
      * @param process       Object to perform policy data processing
      */
-    public COPSPdpAgent(short clientType, COPSPdpDataProcess process) {
+    public COPSPdpAgent(final ClientType clientType, final COPSPdpDataProcess process) {
         _serverPort = WELL_KNOWN_PDP_PORT;
         _kaTimer = KA_TIMER_VALUE;
         _acctTimer = ACCT_TIMER_VALUE;
@@ -86,7 +87,7 @@ public class COPSPdpAgent extends Thread {
      * @param clientType    COPS Client-type
      * @param process   Object to perform policy data processing
      */
-    public COPSPdpAgent(int port, short clientType, COPSPdpDataProcess process) {
+    public COPSPdpAgent(final int port, final ClientType clientType, final COPSPdpDataProcess process) {
         _serverPort = port;
 
         _kaTimer = KA_TIMER_VALUE;
@@ -129,19 +130,11 @@ public class COPSPdpAgent extends Thread {
         return _acctTimer;
     }
 
-    /**
-     * Gets the connection map
-     * @return   A <tt>Hashtable</tt> holding the connection map
-     */
-    public Hashtable getConnectionMap() {
-        return new Hashtable(_connectionMap);
-    }
-
     /**
      * Gets the client-type
      * @return   The client-type
      */
-    public short getClientType() {
+    public ClientType getClientType() {
         return _clientType;
     }
 
@@ -152,17 +145,9 @@ public class COPSPdpAgent extends Thread {
      * @throws COPSException
      * @throws IOException
      */
-    public void disconnect (String pepID, COPSError error)
-    throws COPSException, IOException {
-
-        COPSPdpConnection pdpConn = _connectionMap.get(pepID);
-
-        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 String pepID, final COPSError error) throws COPSException, IOException {
+        final COPSPdpConnection pdpConn = _connectionMap.get(pepID);
+        final COPSClientCloseMsg closeMsg = new COPSClientCloseMsg(_clientType, error, null, null);
         closeMsg.writeData(pdpConn.getSocket());
         pdpConn.close();
     }
@@ -210,8 +195,8 @@ public class COPSPdpAgent extends Thread {
 
                 // We're waiting for an OPN message
                 try {
-                    COPSMsg msg = COPSTransceiver.receiveMsg(socket);
-                    if (msg.getHeader().isAClientOpen()) {
+                    final COPSMsg msg = COPSTransceiver.receiveMsg(socket);
+                    if (msg.getHeader().getOpCode().equals(OPCode.OPN)) {
                         handleClientOpenMsg(socket, msg);
                     } else {
                         // COPSDebug.err(getClass().getName(), COPSDebug.ERROR_NOEXPECTEDMSG);
@@ -243,19 +228,15 @@ public class COPSPdpAgent extends Thread {
       * @throws COPSException
       * @throws IOException
       */
-    private void handleClientOpenMsg(Socket conn, COPSMsg msg)
-    throws COPSException, IOException {
-        COPSClientOpenMsg cMsg = (COPSClientOpenMsg) msg;
-        COPSPepId pepId = cMsg.getPepId();
+    private void handleClientOpenMsg(final Socket conn, final COPSMsg msg) throws COPSException, IOException {
+        final COPSClientOpenMsg cMsg = (COPSClientOpenMsg) msg;
+        final COPSPepId pepId = cMsg.getPepId();
 
         // Validate Client Type
-        if (msg.getHeader().getClientType() != _clientType) {
+        if (msg.getHeader().getClientType().equals(_clientType)) {
             // Unsupported client type
-            COPSHeader cHdr = new COPSHeader(COPSHeader.COPS_OP_CC, msg.getHeader().getClientType());
-            COPSError err = new COPSError(ErrorTypes.UNSUPPORTED_CLIENT_TYPE, ErrorTypes.NA);
-            COPSClientCloseMsg closeMsg = new COPSClientCloseMsg();
-            closeMsg.add(cHdr);
-            closeMsg.add(err);
+            final COPSClientCloseMsg closeMsg = new COPSClientCloseMsg(msg.getHeader().getClientType(),
+                    new COPSError(ErrorTypes.UNSUPPORTED_CLIENT_TYPE, ErrorTypes.NA), null, null);
             try {
                 closeMsg.writeData(conn);
             } catch (IOException unae) {
@@ -268,11 +249,8 @@ public class COPSPdpAgent extends Thread {
         // PEPId is mandatory
         if (pepId == null) {
             // Mandatory COPS object missing
-            COPSHeader cHdr = new COPSHeader(COPSHeader.COPS_OP_CC, msg.getHeader().getClientType());
-            COPSError err = new COPSError(ErrorTypes.MANDATORY_OBJECT_MISSING, ErrorTypes.NA);
-            COPSClientCloseMsg closeMsg = new COPSClientCloseMsg();
-            closeMsg.add(cHdr);
-            closeMsg.add(err);
+            final COPSClientCloseMsg closeMsg = new COPSClientCloseMsg(msg.getHeader().getClientType(),
+                    new COPSError(ErrorTypes.MANDATORY_OBJECT_MISSING, ErrorTypes.NA), null, null);
             try {
                 closeMsg.writeData(conn);
             } catch (IOException unae) {
@@ -288,11 +266,8 @@ public class COPSPdpAgent extends Thread {
                 (cMsg.getIntegrity() != null)) {
 
             // Unsupported objects
-            COPSHeader cHdr = new COPSHeader(COPSHeader.COPS_OP_CC, msg.getHeader().getClientType());
-            COPSError err = new COPSError(ErrorTypes.UNKNOWN_OBJECT, ErrorTypes.NA);
-            COPSClientCloseMsg closeMsg = new COPSClientCloseMsg();
-            closeMsg.add(cHdr);
-            closeMsg.add(err);
+            final COPSClientCloseMsg closeMsg = new COPSClientCloseMsg(msg.getHeader().getClientType(),
+                    new COPSError(ErrorTypes.UNKNOWN_OBJECT, ErrorTypes.NA), null, null);
             try {
                 closeMsg.writeData(conn);
             } catch (IOException unae) {
@@ -303,16 +278,14 @@ public class COPSPdpAgent extends Thread {
         }
 
         // Connection accepted
-        COPSHeader ahdr = new COPSHeader(COPSHeader.COPS_OP_CAT, msg.getHeader().getClientType());
-        COPSKATimer katimer = new COPSKATimer(_kaTimer);
-        COPSAcctTimer acctTimer = new COPSAcctTimer(_acctTimer);
-        COPSClientAcceptMsg acceptMsg = new COPSClientAcceptMsg();
-        acceptMsg.add(ahdr);
-        acceptMsg.add(katimer) ;
-        if (_acctTimer != 0) acceptMsg.add(acctTimer);
+        final COPSKATimer katimer = new COPSKATimer(_kaTimer);
+        final COPSClientAcceptMsg acceptMsg;
+        if (_acctTimer != 0) acceptMsg = new COPSClientAcceptMsg(msg.getHeader().getClientType(), katimer, null, null);
+        else acceptMsg = new COPSClientAcceptMsg(msg.getHeader().getClientType(), katimer,
+                new COPSAcctTimer(_acctTimer), null);
         acceptMsg.writeData(conn);
 
-        COPSPdpConnection pdpConn = new COPSPdpConnection(pepId,conn,_process);
+        final COPSPdpConnection pdpConn = new COPSPdpConnection(pepId,conn,_process);
         pdpConn.setKaTimer(_kaTimer);
         if (_acctTimer != 0) pdpConn.setAccTimer(_acctTimer);
         new Thread(pdpConn).start();