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.ClientType;
14 import org.umu.cops.stack.COPSHeader.OPCode;
16 import java.io.IOException;
17 import java.net.ServerSocket;
18 import java.net.Socket;
21 * This is a provisioning COPS PEP. Responsible for making connection to the PDP
24 public class PCMMPepAgent extends COPSPepAgent implements Runnable {
26 public final static Logger logger = LoggerFactory.getLogger(PCMMPepAgent.class);
28 /** Well-known port for COPS */
29 public static final int WELL_KNOWN_CMTS_PORT = 3918;
34 private ServerSocket serverSocket;
39 private int serverPort;
42 * COPS error returned by PDP
44 private COPSError error;
54 public PCMMPepAgent(final String pepID, final ClientType clientType) {
55 super(pepID, clientType);
56 serverPort = WELL_KNOWN_CMTS_PORT;
60 * Creates a PEP agent with a PEP-ID equal to "noname"
65 public PCMMPepAgent(final ClientType clientType) {
67 serverPort = WELL_KNOWN_CMTS_PORT;
71 * Runs the PEP process XXX - not sure of the exception throwing
76 logger.info("Create Server Socket on Port " + serverPort);
78 serverSocket = new ServerSocket(serverPort);
79 // Loop through for Incoming messages
81 // server infinite loop
84 // Wait for an incoming connection from a PEP
85 Socket socket = serverSocket.accept();
87 logger.info("New connection accepted " + socket.getInetAddress() + ":" + socket.getPort());
89 processConnection(socket);
91 * XXX - processConnection handles the open request from PEP And
92 * a thread is created for conn = new
93 * COPSPepConnection(_clientType, socket); the main processing
98 } catch (Exception e) {
99 logger.error("Error while processing the socket connection", e);
104 * Establish connection to PDP's IP address
106 * <Client-Open> ::= <Common Header> <PEPID> [<ClientSI>] [<LastPDPAddr>]
109 * Not support [<ClientSI>], [<LastPDPAddr>], [<Integrity>]
111 * <Client-Accept> ::= <Common Header> <KA Timer> [<ACCT Timer>]
114 * Not send [<Integrity>]
116 * <Client-Close> ::= <Common Header> <Error> [<PDPRedirAddr>] [<Integrity>]
118 * Not send [<PDPRedirAddr>], [<Integrity>]
120 * @throws IOException
121 * @throws COPSException
122 * @throws COPSPepException
125 private COPSPepConnection processConnection(Socket socket) throws IOException, COPSException, COPSPepException {
127 final COPSPepId pepId = new COPSPepId(new COPSData(getPepID()));
128 final COPSClientOpenMsg msg = new COPSClientOpenMsg(getClientType(), pepId, null, null, null);
130 // Create Socket and send OPN
132 * InetAddress addr = InetAddress.getByName(psHost); Socket socket = new
133 * Socket(addr,psPort);
135 logger.info("Send COPSClientOpenMsg to PDP");
136 msg.writeData(socket);
138 // Receive the response
139 logger.info("Receive the resposne from PDP");
140 COPSMsg recvmsg = COPSTransceiver.receiveMsg(socket);
142 if (recvmsg.getHeader().getOpCode().equals(OPCode.CAT)) {
143 logger.info("isAClientAccept from PDP");
144 COPSClientAcceptMsg cMsg = (COPSClientAcceptMsg) recvmsg;
147 if (cMsg.getIntegrity() != null) {
148 throw new COPSPepException("Unsupported object (Integrity)");
152 COPSKATimer kt = cMsg.getKATimer();
154 throw new COPSPepException(
155 "Mandatory COPS object missing (KA Timer)");
156 short _kaTimeVal = kt.getTimerVal();
159 COPSAcctTimer at = cMsg.getAcctTimer();
160 short _acctTimer = 0;
162 _acctTimer = at.getTimerVal();
164 // Create the connection manager
165 COPSPepConnection conn = new COPSPepConnection(getClientType(),
167 conn.setKaTimer(_kaTimeVal);
168 conn.setAcctTimer(_acctTimer);
169 logger.info("Thread(conn).start");
170 new Thread(conn).start();
173 } else if (recvmsg.getHeader().getOpCode().equals(OPCode.CC)) {
174 logger.info("isAClientClose from PDP");
175 COPSClientCloseMsg cMsg = (COPSClientCloseMsg) recvmsg;
176 error = cMsg.getError();
179 } else { // messages of other types are not expected
180 throw new COPSPepException(
181 "Message not expected. Closing connection for "
182 + socket.toString());
187 * Gets the COPS error returned by the PDP
189 * @return <tt>COPSError</tt> returned by PDP
191 public COPSError getConnectionError() {
195 public void setConnectionError(COPSError _error) {
200 * @return the serverSocket
202 public ServerSocket getServerSocket() {
207 * @param serverSocket
208 * the serverSocket to set
210 public void setServerSocket(ServerSocket serverSocket) {
211 this.serverSocket = serverSocket;
215 * @return the serverPort
217 public int getServerPort() {
223 * the serverPort to set
225 public void setServerPort(int serverPort) {
226 this.serverPort = serverPort;