2 * Copyright (c) 2004 University of Murcia. All rights reserved.
\r
3 * --------------------------------------------------------------
\r
4 * For more information, please see <http://www.umu.euro6ix.org/>.
\r
7 package org.umu.cops.prpep;
\r
9 import java.io.IOException;
\r
10 import java.net.InetAddress;
\r
11 import java.net.Socket;
\r
12 import java.net.UnknownHostException;
\r
13 import java.util.Hashtable;
\r
15 import org.umu.cops.stack.COPSAcctTimer;
\r
16 import org.umu.cops.stack.COPSClientAcceptMsg;
\r
17 import org.umu.cops.stack.COPSClientCloseMsg;
\r
18 import org.umu.cops.stack.COPSClientOpenMsg;
\r
19 import org.umu.cops.stack.COPSData;
\r
20 import org.umu.cops.stack.COPSError;
\r
21 import org.umu.cops.stack.COPSException;
\r
22 import org.umu.cops.stack.COPSHeader;
\r
23 import org.umu.cops.stack.COPSKATimer;
\r
24 import org.umu.cops.stack.COPSMsg;
\r
25 import org.umu.cops.stack.COPSPepId;
\r
26 import org.umu.cops.stack.COPSTransceiver;
\r
29 * This is a provisioning COPS PEP. Responsible for making
\r
30 * connection to the PDP and maintaining it
\r
32 public class COPSPepAgent {
\r
37 private String _pepID;
\r
42 private short _clientType;
\r
47 private String _psHost;
\r
52 private int _psPort;
\r
55 PEP-PDP connection manager
\r
57 private COPSPepConnection _conn;
\r
60 COPS error returned by PDP
\r
62 private COPSError _error;
\r
65 * Creates a PEP agent
\r
66 * @param pepID PEP-ID
\r
67 * @param clientType Client-type
\r
69 public COPSPepAgent(String pepID, short clientType) {
\r
71 _clientType = clientType;
\r
75 * Creates a PEP agent with a PEP-ID equal to "noname"
\r
76 * @param clientType Client-type
\r
78 public COPSPepAgent(short clientType) {
\r
82 _pepID = InetAddress.getLocalHost().getHostName();
\r
83 } catch (Exception e) {
\r
87 _clientType = clientType;
\r
91 * Gets the identifier of the PEP
\r
94 public String getPepID() {
\r
99 * Gets the COPS client-type
\r
100 * @return PEP's client-type
\r
102 public short getClientType() {
\r
103 return _clientType;
\r
107 * Gets PDP host name
\r
108 * @return PDP host name
\r
110 public String getPDPName() {
\r
115 * Gets the port of the PDP
\r
118 public int getPDPPort() {
\r
123 * Connects to a PDP
\r
124 * @param psHost PDP host name
\r
125 * @param psPort PDP port
\r
126 * @return <tt>true</tt> if PDP accepts the connection; <tt>false</tt> otherwise
\r
127 * @throws java.net.UnknownHostException
\r
128 * @throws java.io.IOException
\r
129 * @throws COPSException
\r
130 * @throws COPSPepException
\r
132 public boolean connect(String psHost, int psPort)
\r
133 throws UnknownHostException, IOException, COPSException, COPSPepException {
\r
135 // COPSDebug.out(getClass().getName(), "Thread ( " + _pepID + ") - Connecting to PDP");
\r
139 // Check whether it already exists
\r
141 _conn = processConnection(psHost,psPort);
\r
143 // Check if it's closed
\r
144 if (_conn.isClosed()) {
\r
145 _conn = processConnection(psHost,psPort);
\r
148 _conn = processConnection(psHost,psPort);
\r
152 return (_conn != null);
\r
156 * Gets the connection manager
\r
157 * @return PEP-PDP connection manager object
\r
159 public COPSPepConnection getConnection () {
\r
164 * Gets the COPS error returned by the PDP
\r
165 * @return <tt>COPSError</tt> returned by PDP
\r
167 public COPSError getConnectionError() {
\r
172 * Disconnects from the PDP
\r
173 * @param error Reason
\r
174 * @throws COPSException
\r
175 * @throws IOException
\r
177 public void disconnect(COPSError error)
\r
178 throws COPSException, IOException {
\r
180 COPSHeader cHdr = new COPSHeader(COPSHeader.COPS_OP_CC, _clientType);
\r
181 COPSClientCloseMsg closeMsg = new COPSClientCloseMsg();
\r
182 closeMsg.add(cHdr);
\r
184 closeMsg.add(error);
\r
186 closeMsg.writeData(_conn.getSocket());
\r
192 * Adds a request state to the connection manager.
\r
193 * @return The newly created connection manager
\r
194 * @throws COPSPepException
\r
195 * @throws COPSException
\r
197 public COPSPepReqStateMan addRequestState (String handle, COPSPepDataProcess process)
\r
198 throws COPSPepException, COPSException {
\r
199 if (_conn != null) {
\r
200 return _conn.addRequestState(handle, process);
\r
207 * Queries the connection manager to delete a request state
\r
208 * @param man Request state manager
\r
209 * @throws COPSPepException
\r
210 * @throws COPSException
\r
212 public void deleteRequestState (COPSPepReqStateMan man)
\r
213 throws COPSPepException, COPSException {
\r
215 _conn.deleteRequestState(man);
\r
219 * Gets all the request state managers
\r
220 * @return A <tt>Hashtable</tt> holding all active request state managers
\r
222 public Hashtable getReqStateMans() {
\r
224 return _conn.getReqStateMans();
\r
229 * Establish connection to PDP's IP address
\r
231 * <Client-Open> ::= <Common Header>
\r
237 * Not support [<ClientSI>], [<LastPDPAddr>], [<Integrity>]
\r
239 * <Client-Accept> ::= <Common Header>
\r
244 * Not send [<Integrity>]
\r
246 * <Client-Close> ::= <Common Header>
\r
251 * Not send [<PDPRedirAddr>], [<Integrity>]
\r
253 * @throws UnknownHostException
\r
254 * @throws IOException
\r
255 * @throws COPSException
\r
256 * @throws COPSPepException
\r
259 private COPSPepConnection processConnection(String psHost, int psPort)
\r
260 throws UnknownHostException, IOException, COPSException, COPSPepException {
\r
262 COPSHeader hdr = new COPSHeader(COPSHeader.COPS_OP_OPN, _clientType);
\r
264 COPSPepId pepId = new COPSPepId();
\r
265 COPSData d = new COPSData(_pepID);
\r
268 COPSClientOpenMsg msg = new COPSClientOpenMsg();
\r
272 // Create Socket and send OPN
\r
273 InetAddress addr = InetAddress.getByName(psHost);
\r
274 Socket socket = new Socket(addr,psPort);
\r
275 msg.writeData(socket);
\r
277 // Receive the response
\r
278 COPSMsg recvmsg = COPSTransceiver.receiveMsg(socket);
\r
280 if (recvmsg.getHeader().isAClientAccept()) {
\r
281 COPSClientAcceptMsg cMsg = (COPSClientAcceptMsg) recvmsg;
\r
284 if (cMsg.getIntegrity() != null) {
\r
285 throw new COPSPepException("Unsupported object (Integrity)");
\r
288 // Mandatory KATimer
\r
289 COPSKATimer kt = cMsg.getKATimer();
\r
291 throw new COPSPepException ("Mandatory COPS object missing (KA Timer)");
\r
292 short _kaTimeVal = kt.getTimerVal();
\r
295 COPSAcctTimer at = cMsg.getAcctTimer();
\r
296 short _acctTimer = 0;
\r
298 _acctTimer = at.getTimerVal();
\r
300 // Create the connection manager
\r
301 COPSPepConnection conn = new COPSPepConnection(_clientType, socket);
\r
302 conn.setKaTimer(_kaTimeVal);
\r
303 conn.setAcctTimer(_acctTimer);
\r
304 new Thread(conn).start();
\r
307 } else if (recvmsg.getHeader().isAClientClose()) {
\r
308 COPSClientCloseMsg cMsg = (COPSClientCloseMsg) recvmsg;
\r
309 _error = cMsg.getError();
\r
312 } else { // messages of other types are not expected
\r
313 throw new COPSPepException("Message not expected. Closing connection for " + socket.toString());
\r