+++ /dev/null
-/*\r
- * Copyright (c) 2004 University of Murcia. All rights reserved.\r
- * --------------------------------------------------------------\r
- * For more information, please see <http://www.umu.euro6ix.org/>.\r
- */\r
-\r
-package org.umu.cops.prpep;\r
-\r
-import java.io.IOException;\r
-import java.net.InetAddress;\r
-import java.net.Socket;\r
-import java.net.UnknownHostException;\r
-import java.util.Hashtable;\r
-\r
-import org.umu.cops.stack.COPSAcctTimer;\r
-import org.umu.cops.stack.COPSClientAcceptMsg;\r
-import org.umu.cops.stack.COPSClientCloseMsg;\r
-import org.umu.cops.stack.COPSClientOpenMsg;\r
-import org.umu.cops.stack.COPSData;\r
-import org.umu.cops.stack.COPSError;\r
-import org.umu.cops.stack.COPSException;\r
-import org.umu.cops.stack.COPSHeader;\r
-import org.umu.cops.stack.COPSKATimer;\r
-import org.umu.cops.stack.COPSMsg;\r
-import org.umu.cops.stack.COPSPepId;\r
-import org.umu.cops.stack.COPSTransceiver;\r
-\r
-/**\r
- * This is a provisioning COPS PEP. Responsible for making\r
- * connection to the PDP and maintaining it\r
- */\r
-public class COPSPepAgent {\r
-\r
- /**\r
- PEP's Identifier\r
- */\r
- private String _pepID;\r
-\r
- /**\r
- PEP's client-type\r
- */\r
- private short _clientType;\r
-\r
- /**\r
- PDP host name\r
- */\r
- private String _psHost;\r
-\r
- /**\r
- PDP port\r
- */\r
- private int _psPort;\r
-\r
- /**\r
- PEP-PDP connection manager\r
- */\r
- private COPSPepConnection _conn;\r
-\r
- /**\r
- COPS error returned by PDP\r
- */\r
- private COPSError _error;\r
-\r
- /**\r
- * Creates a PEP agent\r
- * @param pepID PEP-ID\r
- * @param clientType Client-type\r
- */\r
- public COPSPepAgent(String pepID, short clientType) {\r
- _pepID = pepID;\r
- _clientType = clientType;\r
- }\r
-\r
- /**\r
- * Creates a PEP agent with a PEP-ID equal to "noname"\r
- * @param clientType Client-type\r
- */\r
- public COPSPepAgent(short clientType) {\r
-\r
- // PEPId\r
- try {\r
- _pepID = InetAddress.getLocalHost().getHostName();\r
- } catch (Exception e) {\r
- _pepID = "noname";\r
- }\r
-\r
- _clientType = clientType;\r
- }\r
-\r
- /**\r
- * Gets the identifier of the PEP\r
- * @return PEP-ID\r
- */\r
- public String getPepID() {\r
- return _pepID;\r
- }\r
-\r
- /**\r
- * Gets the COPS client-type\r
- * @return PEP's client-type\r
- */\r
- public short getClientType() {\r
- return _clientType;\r
- }\r
-\r
- /**\r
- * Gets PDP host name\r
- * @return PDP host name\r
- */\r
- public String getPDPName() {\r
- return _psHost;\r
- }\r
-\r
- /**\r
- * Gets the port of the PDP\r
- * @return PDP port\r
- */\r
- public int getPDPPort() {\r
- return _psPort;\r
- }\r
-\r
- /**\r
- * Connects to a PDP\r
- * @param psHost PDP host name\r
- * @param psPort PDP port\r
- * @return <tt>true</tt> if PDP accepts the connection; <tt>false</tt> otherwise\r
- * @throws java.net.UnknownHostException\r
- * @throws java.io.IOException\r
- * @throws COPSException\r
- * @throws COPSPepException\r
- */\r
- public boolean connect(String psHost, int psPort)\r
- throws UnknownHostException, IOException, COPSException, COPSPepException {\r
-\r
- // COPSDebug.out(getClass().getName(), "Thread ( " + _pepID + ") - Connecting to PDP");\r
- _psHost = psHost;\r
- _psPort = psPort;\r
-\r
- // Check whether it already exists\r
- if (_conn == null)\r
- _conn = processConnection(psHost,psPort);\r
- else {\r
- // Check if it's closed\r
- if (_conn.isClosed()) {\r
- _conn = processConnection(psHost,psPort);\r
- } else {\r
- disconnect(null);\r
- _conn = processConnection(psHost,psPort);\r
- }\r
- }\r
-\r
- return (_conn != null);\r
- }\r
-\r
- /**\r
- * Gets the connection manager\r
- * @return PEP-PDP connection manager object\r
- */\r
- public COPSPepConnection getConnection () {\r
- return (_conn);\r
- }\r
-\r
- /**\r
- * Gets the COPS error returned by the PDP\r
- * @return <tt>COPSError</tt> returned by PDP\r
- */\r
- public COPSError getConnectionError() {\r
- return _error;\r
- }\r
-\r
- /**\r
- * Disconnects from the PDP\r
- * @param error Reason\r
- * @throws COPSException\r
- * @throws IOException\r
- */\r
- public void disconnect(COPSError error)\r
- throws COPSException, IOException {\r
-\r
- COPSHeader cHdr = new COPSHeader(COPSHeader.COPS_OP_CC, _clientType);\r
- COPSClientCloseMsg closeMsg = new COPSClientCloseMsg();\r
- closeMsg.add(cHdr);\r
- if (error != null)\r
- closeMsg.add(error);\r
-\r
- closeMsg.writeData(_conn.getSocket());\r
- _conn.close();\r
- _conn = null;\r
- }\r
-\r
- /**\r
- * Adds a request state to the connection manager.\r
- * @return The newly created connection manager\r
- * @throws COPSPepException\r
- * @throws COPSException\r
- */\r
- public COPSPepReqStateMan addRequestState (String handle, COPSPepDataProcess process)\r
- throws COPSPepException, COPSException {\r
- if (_conn != null) {\r
- return _conn.addRequestState(handle, process);\r
- }\r
- return null;\r
- }\r
-\r
-\r
- /**\r
- * Queries the connection manager to delete a request state\r
- * @param man Request state manager\r
- * @throws COPSPepException\r
- * @throws COPSException\r
- */\r
- public void deleteRequestState (COPSPepReqStateMan man)\r
- throws COPSPepException, COPSException {\r
- if (_conn != null)\r
- _conn.deleteRequestState(man);\r
- }\r
-\r
- /**\r
- * Gets all the request state managers\r
- * @return A <tt>Hashtable</tt> holding all active request state managers\r
- */\r
- public Hashtable getReqStateMans() {\r
- if (_conn != null)\r
- return _conn.getReqStateMans();\r
- return null;\r
- }\r
-\r
- /**\r
- * Establish connection to PDP's IP address\r
- *\r
- * <Client-Open> ::= <Common Header>\r
- * <PEPID>\r
- * [<ClientSI>]\r
- * [<LastPDPAddr>]\r
- * [<Integrity>]\r
- *\r
- * Not support [<ClientSI>], [<LastPDPAddr>], [<Integrity>]\r
- *\r
- * <Client-Accept> ::= <Common Header>\r
- * <KA Timer>\r
- * [<ACCT Timer>]\r
- * [<Integrity>]\r
- *\r
- * Not send [<Integrity>]\r
- *\r
- * <Client-Close> ::= <Common Header>\r
- * <Error>\r
- * [<PDPRedirAddr>]\r
- * [<Integrity>]\r
- *\r
- * Not send [<PDPRedirAddr>], [<Integrity>]\r
- *\r
- * @throws UnknownHostException\r
- * @throws IOException\r
- * @throws COPSException\r
- * @throws COPSPepException\r
- *\r
- */\r
- private COPSPepConnection processConnection(String psHost, int psPort)\r
- throws UnknownHostException, IOException, COPSException, COPSPepException {\r
- // Build OPN\r
- COPSHeader hdr = new COPSHeader(COPSHeader.COPS_OP_OPN, _clientType);\r
-\r
- COPSPepId pepId = new COPSPepId();\r
- COPSData d = new COPSData(_pepID);\r
- pepId.setData(d);\r
-\r
- COPSClientOpenMsg msg = new COPSClientOpenMsg();\r
- msg.add(hdr);\r
- msg.add(pepId);\r
-\r
- // Create Socket and send OPN\r
- InetAddress addr = InetAddress.getByName(psHost);\r
- Socket socket = new Socket(addr,psPort);\r
- msg.writeData(socket);\r
-\r
- // Receive the response\r
- COPSMsg recvmsg = COPSTransceiver.receiveMsg(socket);\r
-\r
- if (recvmsg.getHeader().isAClientAccept()) {\r
- COPSClientAcceptMsg cMsg = (COPSClientAcceptMsg) recvmsg;\r
-\r
- // Support\r
- if (cMsg.getIntegrity() != null) {\r
- throw new COPSPepException("Unsupported object (Integrity)");\r
- }\r
-\r
- // Mandatory KATimer\r
- COPSKATimer kt = cMsg.getKATimer();\r
- if (kt == null)\r
- throw new COPSPepException ("Mandatory COPS object missing (KA Timer)");\r
- short _kaTimeVal = kt.getTimerVal();\r
-\r
- // ACTimer\r
- COPSAcctTimer at = cMsg.getAcctTimer();\r
- short _acctTimer = 0;\r
- if (at != null)\r
- _acctTimer = at.getTimerVal();\r
-\r
- // Create the connection manager\r
- COPSPepConnection conn = new COPSPepConnection(_clientType, socket);\r
- conn.setKaTimer(_kaTimeVal);\r
- conn.setAcctTimer(_acctTimer);\r
- new Thread(conn).start();\r
-\r
- return conn;\r
- } else if (recvmsg.getHeader().isAClientClose()) {\r
- COPSClientCloseMsg cMsg = (COPSClientCloseMsg) recvmsg;\r
- _error = cMsg.getError();\r
- socket.close();\r
- return null;\r
- } else { // messages of other types are not expected\r
- throw new COPSPepException("Message not expected. Closing connection for " + socket.toString());\r
- }\r
- }\r
-}\r
-\r
-\r
-\r