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.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
11 import org.umu.cops.stack.*;
12 import org.umu.cops.stack.COPSHeader.OPCode;
14 import java.io.IOException;
15 import java.net.InetAddress;
16 import java.net.Socket;
17 import java.net.UnknownHostException;
20 * This is a provisioning COPS PEP. Responsible for making
21 * connection to the PDP and maintaining it
23 public class COPSPepAgent {
25 private final static Logger logger = LoggerFactory.getLogger(COPSPepAgent.class);
30 private String _pepID;
35 private short _clientType;
40 private String _psHost;
48 PEP-PDP connection manager
50 private COPSPepConnection _conn;
53 COPS error returned by PDP
55 private COPSError _error;
60 * @param clientType Client-type
62 public COPSPepAgent(final String pepID, final short clientType) {
64 _clientType = clientType;
68 * Creates a PEP agent with a PEP-ID equal to "noname"
69 * @param clientType Client-type
71 public COPSPepAgent(final short clientType) {
75 _pepID = InetAddress.getLocalHost().getHostName();
76 } catch (Exception e) {
80 _clientType = clientType;
84 * Gets the identifier of the PEP
87 public String getPepID() {
92 * Gets the COPS client-type
93 * @return PEP's client-type
95 public short getClientType() {
101 * @return PDP host name
103 public String getPDPName() {
108 * Gets the port of the PDP
111 public int getPDPPort() {
117 * @param psHost PDP host name
118 * @param psPort PDP port
119 * @return <tt>true</tt> if PDP accepts the connection; <tt>false</tt> otherwise
120 * @throws java.io.IOException
121 * @throws COPSException
122 * @throws COPSPepException
124 public boolean connect(String psHost, int psPort) throws IOException, COPSException {
125 logger.info("Thread ( " + _pepID + ") - Connecting to PDP");
129 // Check whether it already exists
131 _conn = processConnection(psHost,psPort);
133 // Check if it's closed
134 if (_conn.isClosed()) {
135 _conn = processConnection(psHost,psPort);
138 _conn = processConnection(psHost,psPort);
142 return (_conn != null);
146 * Gets the connection manager
147 * @return PEP-PDP connection manager object
149 public COPSPepConnection getConnection () {
154 * Gets the COPS error returned by the PDP
155 * @return <tt>COPSError</tt> returned by PDP
157 public COPSError getConnectionError() {
162 * Disconnects from the PDP
163 * @param error Reason
164 * @throws COPSException
165 * @throws IOException
167 public void disconnect(final COPSError error) throws COPSException, IOException {
168 final COPSClientCloseMsg closeMsg = new COPSClientCloseMsg(_clientType, error, null, null);
169 closeMsg.writeData(_conn.getSocket());
174 * Adds a request state to the connection manager.
175 * @return The newly created connection manager
176 * @throws COPSPepException
177 * @throws COPSException
179 public COPSPepReqStateMan addRequestState(final COPSHandle handle, final COPSPepDataProcess process)
180 throws COPSException {
182 return _conn.addRequestState(handle, process);
189 * Queries the connection manager to delete a request state
190 * @param man Request state manager
191 * @throws COPSPepException
192 * @throws COPSException
194 public void deleteRequestState(final COPSPepReqStateMan man) throws COPSException {
196 _conn.deleteRequestState(man);
200 * Establish connection to PDP's IP address
202 * <Client-Open> ::= <Common Header>
208 * Not support [<ClientSI>], [<LastPDPAddr>], [<Integrity>]
210 * <Client-Accept> ::= <Common Header>
215 * Not send [<Integrity>]
217 * <Client-Close> ::= <Common Header>
222 * Not send [<PDPRedirAddr>], [<Integrity>]
224 * @throws UnknownHostException
225 * @throws IOException
226 * @throws COPSException
227 * @throws COPSPepException
230 private COPSPepConnection processConnection(final String psHost, final int psPort)
231 throws IOException, COPSException {
233 final COPSClientOpenMsg msg = new COPSClientOpenMsg(_clientType, new COPSPepId(new COPSData(_pepID)),
236 // Create Socket and send OPN
237 final InetAddress addr = InetAddress.getByName(psHost);
238 final Socket socket = new Socket(addr,psPort);
239 msg.writeData(socket);
241 // Receive the response
242 final COPSMsg recvmsg = COPSTransceiver.receiveMsg(socket);
244 if (recvmsg.getHeader().getOpCode().equals(OPCode.CAT)) {
245 final COPSClientAcceptMsg cMsg = (COPSClientAcceptMsg) recvmsg;
248 if (cMsg.getIntegrity() != null) {
249 throw new COPSPepException("Unsupported object (Integrity)");
253 final COPSKATimer kt = cMsg.getKATimer();
255 throw new COPSPepException ("Mandatory COPS object missing (KA Timer)");
256 short _kaTimeVal = kt.getTimerVal();
259 final COPSAcctTimer at = cMsg.getAcctTimer();
260 short _acctTimer = 0;
262 _acctTimer = at.getTimerVal();
264 // Create the connection manager
265 final COPSPepConnection conn = new COPSPepConnection(_clientType, socket);
266 conn.setKaTimer(_kaTimeVal);
267 conn.setAcctTimer(_acctTimer);
268 new Thread(conn).start();
271 } else if (recvmsg.getHeader().getOpCode().equals(OPCode.CC)) {
272 final COPSClientCloseMsg cMsg = (COPSClientCloseMsg) recvmsg;
273 _error = cMsg.getError();
276 } else { // messages of other types are not expected
277 throw new COPSPepException("Message not expected. Closing connection for " + socket.toString());