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.*;
14 import java.io.IOException;
15 import java.net.ServerSocket;
16 import java.net.Socket;
19 * This is a provisioning COPS PEP. Responsible for making connection to the PDP
22 public class PCMMPepAgent extends COPSPepAgent implements Runnable {
24 public final static Logger logger = LoggerFactory.getLogger(PCMMPepAgent.class);
26 /** Well-known port for COPS */
27 public static final int WELL_KNOWN_CMTS_PORT = 3918;
32 private ServerSocket serverSocket;
37 private int serverPort;
40 * COPS error returned by PDP
42 private COPSError error;
52 public PCMMPepAgent(String pepID, short clientType) {
53 super(pepID, clientType);
54 serverPort = WELL_KNOWN_CMTS_PORT;
58 * Creates a PEP agent with a PEP-ID equal to "noname"
63 public PCMMPepAgent(short clientType) {
65 serverPort = WELL_KNOWN_CMTS_PORT;
69 * Runs the PEP process XXX - not sure of the exception throwing
74 logger.info("Create Server Socket on Port " + serverPort);
76 serverSocket = new ServerSocket(serverPort);
77 // Loop through for Incoming messages
79 // server infinite loop
82 // Wait for an incoming connection from a PEP
83 Socket socket = serverSocket.accept();
85 logger.info("New connection accepted " + socket.getInetAddress() + ":" + socket.getPort());
87 processConnection(socket);
89 * XXX - processConnection handles the open request from PEP And
90 * a thread is created for conn = new
91 * COPSPepConnection(_clientType, socket); the main processing
96 } catch (Exception e) {
97 logger.error("Error while processing the socket connection", e);
102 * Establish connection to PDP's IP address
104 * <Client-Open> ::= <Common Header> <PEPID> [<ClientSI>] [<LastPDPAddr>]
107 * Not support [<ClientSI>], [<LastPDPAddr>], [<Integrity>]
109 * <Client-Accept> ::= <Common Header> <KA Timer> [<ACCT Timer>]
112 * Not send [<Integrity>]
114 * <Client-Close> ::= <Common Header> <Error> [<PDPRedirAddr>] [<Integrity>]
116 * Not send [<PDPRedirAddr>], [<Integrity>]
118 * @throws IOException
119 * @throws COPSException
120 * @throws COPSPepException
123 private COPSPepConnection processConnection(Socket socket) throws IOException, COPSException, COPSPepException {
125 COPSHeader hdr = new COPSHeader(COPSHeader.COPS_OP_OPN, getClientType());
127 COPSPepId pepId = new COPSPepId();
128 COPSData d = new COPSData(getPepID());
131 COPSClientOpenMsg msg = new COPSClientOpenMsg();
135 // Create Socket and send OPN
137 * InetAddress addr = InetAddress.getByName(psHost); Socket socket = new
138 * Socket(addr,psPort);
140 logger.info("Send COPSClientOpenMsg to PDP");
141 msg.writeData(socket);
143 // Receive the response
144 logger.info("Receive the resposne from PDP");
145 COPSMsg recvmsg = COPSTransceiver.receiveMsg(socket);
147 if (recvmsg.getHeader().isAClientAccept()) {
148 logger.info("isAClientAccept from PDP");
149 COPSClientAcceptMsg cMsg = (COPSClientAcceptMsg) recvmsg;
152 if (cMsg.getIntegrity() != null) {
153 throw new COPSPepException("Unsupported object (Integrity)");
157 COPSKATimer kt = cMsg.getKATimer();
159 throw new COPSPepException(
160 "Mandatory COPS object missing (KA Timer)");
161 short _kaTimeVal = kt.getTimerVal();
164 COPSAcctTimer at = cMsg.getAcctTimer();
165 short _acctTimer = 0;
167 _acctTimer = at.getTimerVal();
169 // Create the connection manager
170 COPSPepConnection conn = new COPSPepConnection(getClientType(),
172 conn.setKaTimer(_kaTimeVal);
173 conn.setAcctTimer(_acctTimer);
174 logger.info("Thread(conn).start");
175 new Thread(conn).start();
178 } else if (recvmsg.getHeader().isAClientClose()) {
179 logger.info("isAClientClose from PDP");
180 COPSClientCloseMsg cMsg = (COPSClientCloseMsg) recvmsg;
181 error = cMsg.getError();
184 } else { // messages of other types are not expected
185 throw new COPSPepException(
186 "Message not expected. Closing connection for "
187 + socket.toString());
192 * Gets the COPS error returned by the PDP
194 * @return <tt>COPSError</tt> returned by PDP
196 public COPSError getConnectionError() {
200 public void setConnectionError(COPSError _error) {
205 * @return the serverSocket
207 public ServerSocket getServerSocket() {
212 * @param serverSocket
213 * the serverSocket to set
215 public void setServerSocket(ServerSocket serverSocket) {
216 this.serverSocket = serverSocket;
220 * @return the serverPort
222 public int getServerPort() {
228 * the serverPort to set
230 public void setServerPort(int serverPort) {
231 this.serverPort = serverPort;