7 import org.slf4j.Logger;
8 import org.slf4j.LoggerFactory;
9 import org.umu.cops.prpep.COPSPepAgent;
10 import org.umu.cops.prpep.COPSPepConnection;
11 import org.umu.cops.prpep.COPSPepException;
12 import org.umu.cops.stack.*;
13 import org.umu.cops.stack.COPSHeader.OPCode;
15 import java.io.IOException;
16 import java.net.ServerSocket;
17 import java.net.Socket;
20 * This is a provisioning COPS PEP. Responsible for making connection to the PDP
23 public class PCMMPepAgent extends COPSPepAgent implements Runnable {
25 public final static Logger logger = LoggerFactory.getLogger(PCMMPepAgent.class);
27 /** Well-known port for COPS */
28 public static final int WELL_KNOWN_CMTS_PORT = 3918;
33 private ServerSocket serverSocket;
38 private int serverPort;
41 * COPS error returned by PDP
43 private COPSError error;
53 public PCMMPepAgent(final String pepID, final short clientType) {
54 super(pepID, clientType);
55 serverPort = WELL_KNOWN_CMTS_PORT;
59 * Creates a PEP agent with a PEP-ID equal to "noname"
64 public PCMMPepAgent(final short clientType) {
66 serverPort = WELL_KNOWN_CMTS_PORT;
70 * Runs the PEP process XXX - not sure of the exception throwing
75 logger.info("Create Server Socket on Port " + serverPort);
77 serverSocket = new ServerSocket(serverPort);
78 // Loop through for Incoming messages
80 // server infinite loop
83 // Wait for an incoming connection from a PEP
84 Socket socket = serverSocket.accept();
86 logger.info("New connection accepted " + socket.getInetAddress() + ":" + socket.getPort());
88 processConnection(socket);
90 * XXX - processConnection handles the open request from PEP And
91 * a thread is created for conn = new
92 * COPSPepConnection(_clientType, socket); the main processing
97 } catch (Exception e) {
98 logger.error("Error while processing the socket connection", e);
103 * Establish connection to PDP's IP address
105 * <Client-Open> ::= <Common Header> <PEPID> [<ClientSI>] [<LastPDPAddr>]
108 * Not support [<ClientSI>], [<LastPDPAddr>], [<Integrity>]
110 * <Client-Accept> ::= <Common Header> <KA Timer> [<ACCT Timer>]
113 * Not send [<Integrity>]
115 * <Client-Close> ::= <Common Header> <Error> [<PDPRedirAddr>] [<Integrity>]
117 * Not send [<PDPRedirAddr>], [<Integrity>]
119 * @throws IOException
120 * @throws COPSException
121 * @throws COPSPepException
124 private COPSPepConnection processConnection(Socket socket) throws IOException, COPSException, COPSPepException {
126 final COPSPepId pepId = new COPSPepId(new COPSData(getPepID()));
127 final COPSClientOpenMsg msg = new COPSClientOpenMsg(getClientType(), pepId, null, null, null);
129 // Create Socket and send OPN
131 * InetAddress addr = InetAddress.getByName(psHost); Socket socket = new
132 * Socket(addr,psPort);
134 logger.info("Send COPSClientOpenMsg to PDP");
135 msg.writeData(socket);
137 // Receive the response
138 logger.info("Receive the resposne from PDP");
139 COPSMsg recvmsg = COPSTransceiver.receiveMsg(socket);
141 if (recvmsg.getHeader().getOpCode().equals(OPCode.CAT)) {
142 logger.info("isAClientAccept from PDP");
143 COPSClientAcceptMsg cMsg = (COPSClientAcceptMsg) recvmsg;
146 if (cMsg.getIntegrity() != null) {
147 throw new COPSPepException("Unsupported object (Integrity)");
151 COPSKATimer kt = cMsg.getKATimer();
153 throw new COPSPepException(
154 "Mandatory COPS object missing (KA Timer)");
155 short _kaTimeVal = kt.getTimerVal();
158 COPSAcctTimer at = cMsg.getAcctTimer();
159 short _acctTimer = 0;
161 _acctTimer = at.getTimerVal();
163 // Create the connection manager
164 COPSPepConnection conn = new COPSPepConnection(getClientType(),
166 conn.setKaTimer(_kaTimeVal);
167 conn.setAcctTimer(_acctTimer);
168 logger.info("Thread(conn).start");
169 new Thread(conn).start();
172 } else if (recvmsg.getHeader().getOpCode().equals(OPCode.CC)) {
173 logger.info("isAClientClose from PDP");
174 COPSClientCloseMsg cMsg = (COPSClientCloseMsg) recvmsg;
175 error = cMsg.getError();
178 } else { // messages of other types are not expected
179 throw new COPSPepException(
180 "Message not expected. Closing connection for "
181 + socket.toString());
186 * Gets the COPS error returned by the PDP
188 * @return <tt>COPSError</tt> returned by PDP
190 public COPSError getConnectionError() {
194 public void setConnectionError(COPSError _error) {
199 * @return the serverSocket
201 public ServerSocket getServerSocket() {
206 * @param serverSocket
207 * the serverSocket to set
209 public void setServerSocket(ServerSocket serverSocket) {
210 this.serverSocket = serverSocket;
214 * @return the serverPort
216 public int getServerPort() {
222 * the serverPort to set
224 public void setServerPort(int serverPort) {
225 this.serverPort = serverPort;