-/*\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();
+ }
+ }
+ }
+
+}