2 * Copyright (c) 2004 University of Murcia. All rights reserved.
3 * --------------------------------------------------------------
4 * For more information, please see <http://www.umu.euro6ix.org/>.
7 package org.umu.cops.prpep;
9 import org.umu.cops.stack.*;
11 import java.io.IOException;
12 import java.net.InetAddress;
13 import java.net.Socket;
14 import java.net.UnknownHostException;
15 import java.util.Hashtable;
18 * This is a provisioning COPS PEP. Responsible for making
19 * connection to the PDP and maintaining it
21 public class COPSPepAgent {
26 private String _pepID;
31 private short _clientType;
36 private String _psHost;
44 PEP-PDP connection manager
46 private COPSPepConnection _conn;
49 COPS error returned by PDP
51 private COPSError _error;
56 * @param clientType Client-type
58 public COPSPepAgent(String pepID, short clientType) {
60 _clientType = clientType;
64 * Creates a PEP agent with a PEP-ID equal to "noname"
65 * @param clientType Client-type
67 public COPSPepAgent(short clientType) {
71 _pepID = InetAddress.getLocalHost().getHostName();
72 } catch (Exception e) {
76 _clientType = clientType;
80 * Gets the identifier of the PEP
83 public String getPepID() {
88 * Gets the COPS client-type
89 * @return PEP's client-type
91 public short getClientType() {
97 * @return PDP host name
99 public String getPDPName() {
104 * Gets the port of the PDP
107 public int getPDPPort() {
113 * @param psHost PDP host name
114 * @param psPort PDP port
115 * @return <tt>true</tt> if PDP accepts the connection; <tt>false</tt> otherwise
116 * @throws java.io.IOException
117 * @throws COPSException
118 * @throws COPSPepException
120 public boolean connect(String psHost, int psPort) throws IOException, COPSException, COPSPepException {
122 // COPSDebug.out(getClass().getName(), "Thread ( " + _pepID + ") - Connecting to PDP");
126 // Check whether it already exists
128 _conn = processConnection(psHost,psPort);
130 // Check if it's closed
131 if (_conn.isClosed()) {
132 _conn = processConnection(psHost,psPort);
135 _conn = processConnection(psHost,psPort);
139 return (_conn != null);
143 * Gets the connection manager
144 * @return PEP-PDP connection manager object
146 public COPSPepConnection getConnection () {
151 * Gets the COPS error returned by the PDP
152 * @return <tt>COPSError</tt> returned by PDP
154 public COPSError getConnectionError() {
159 * Disconnects from the PDP
160 * @param error Reason
161 * @throws COPSException
162 * @throws IOException
164 public void disconnect(COPSError error)
165 throws COPSException, IOException {
167 COPSHeader cHdr = new COPSHeader(COPSHeader.COPS_OP_CC, _clientType);
168 COPSClientCloseMsg closeMsg = new COPSClientCloseMsg();
173 closeMsg.writeData(_conn.getSocket());
179 * Adds a request state to the connection manager.
180 * @return The newly created connection manager
181 * @throws COPSPepException
182 * @throws COPSException
184 public COPSPepReqStateMan addRequestState (String handle, COPSPepDataProcess process)
185 throws COPSPepException, COPSException {
187 return _conn.addRequestState(handle, process);
194 * Queries the connection manager to delete a request state
195 * @param man Request state manager
196 * @throws COPSPepException
197 * @throws COPSException
199 public void deleteRequestState (COPSPepReqStateMan man)
200 throws COPSPepException, COPSException {
202 _conn.deleteRequestState(man);
206 * Gets all the request state managers
207 * @return A <tt>Hashtable</tt> holding all active request state managers
209 public Hashtable getReqStateMans() {
211 return _conn.getReqStateMans();
216 * Establish connection to PDP's IP address
218 * <Client-Open> ::= <Common Header>
224 * Not support [<ClientSI>], [<LastPDPAddr>], [<Integrity>]
226 * <Client-Accept> ::= <Common Header>
231 * Not send [<Integrity>]
233 * <Client-Close> ::= <Common Header>
238 * Not send [<PDPRedirAddr>], [<Integrity>]
240 * @throws UnknownHostException
241 * @throws IOException
242 * @throws COPSException
243 * @throws COPSPepException
246 private COPSPepConnection processConnection(String psHost, int psPort) throws IOException, COPSException,
249 COPSHeader hdr = new COPSHeader(COPSHeader.COPS_OP_OPN, _clientType);
251 COPSPepId pepId = new COPSPepId(new COPSData(_pepID));
252 COPSClientOpenMsg msg = new COPSClientOpenMsg();
256 // Create Socket and send OPN
257 InetAddress addr = InetAddress.getByName(psHost);
258 Socket socket = new Socket(addr,psPort);
259 msg.writeData(socket);
261 // Receive the response
262 COPSMsg recvmsg = COPSTransceiver.receiveMsg(socket);
264 if (recvmsg.getHeader().isAClientAccept()) {
265 COPSClientAcceptMsg cMsg = (COPSClientAcceptMsg) recvmsg;
268 if (cMsg.getIntegrity() != null) {
269 throw new COPSPepException("Unsupported object (Integrity)");
273 COPSKATimer kt = cMsg.getKATimer();
275 throw new COPSPepException ("Mandatory COPS object missing (KA Timer)");
276 short _kaTimeVal = kt.getTimerVal();
279 COPSAcctTimer at = cMsg.getAcctTimer();
280 short _acctTimer = 0;
282 _acctTimer = at.getTimerVal();
284 // Create the connection manager
285 COPSPepConnection conn = new COPSPepConnection(_clientType, socket);
286 conn.setKaTimer(_kaTimeVal);
287 conn.setAcctTimer(_acctTimer);
288 new Thread(conn).start();
291 } else if (recvmsg.getHeader().isAClientClose()) {
292 COPSClientCloseMsg cMsg = (COPSClientCloseMsg) recvmsg;
293 _error = cMsg.getError();
296 } else { // messages of other types are not expected
297 throw new COPSPepException("Message not expected. Closing connection for " + socket.toString());