7 import java.io.IOException;
8 import java.net.ServerSocket;
9 import java.net.Socket;
10 import java.net.UnknownHostException;
12 import org.umu.cops.common.COPSDebug;
13 import org.umu.cops.prpep.COPSPepAgent;
14 import org.umu.cops.prpep.COPSPepConnection;
15 import org.umu.cops.prpep.COPSPepException;
16 import org.umu.cops.stack.COPSAcctTimer;
17 import org.umu.cops.stack.COPSClientAcceptMsg;
18 import org.umu.cops.stack.COPSClientCloseMsg;
19 import org.umu.cops.stack.COPSClientOpenMsg;
20 import org.umu.cops.stack.COPSData;
21 import org.umu.cops.stack.COPSError;
22 import org.umu.cops.stack.COPSException;
23 import org.umu.cops.stack.COPSHeader;
24 import org.umu.cops.stack.COPSKATimer;
25 import org.umu.cops.stack.COPSMsg;
26 import org.umu.cops.stack.COPSPepId;
27 import org.umu.cops.stack.COPSTransceiver;
30 * This is a provisioning COPS PEP. Responsible for making connection to the PDP
33 public class PCMMPepAgent extends COPSPepAgent implements Runnable {
35 /** Well-known port for COPS */
36 public static final int WELL_KNOWN_CMTS_PORT = 3918;
41 private ServerSocket serverSocket;
46 private int serverPort;
49 * COPS error returned by PDP
51 private COPSError error;
61 public PCMMPepAgent(String pepID, short clientType) {
62 super(pepID, clientType);
63 serverPort = WELL_KNOWN_CMTS_PORT;
67 * Creates a PEP agent with a PEP-ID equal to "noname"
72 public PCMMPepAgent(short clientType) {
74 serverPort = WELL_KNOWN_CMTS_PORT;
78 * Runs the PEP process XXX - not sure of the exception throwing
83 COPSDebug.err(getClass().getName(), "Create Server Socket on Port "
86 serverSocket = new ServerSocket(serverPort);
87 // Loop through for Incoming messages
89 // server infinite loop
92 // Wait for an incoming connection from a PEP
93 Socket socket = serverSocket.accept();
95 COPSDebug.err(getClass().getName(), "New connection accepted "
96 + socket.getInetAddress() + ":" + socket.getPort());
98 processConnection(socket);
100 * XXX - processConnection handles the open request from PEP And
101 * a thread is created for conn = new
102 * COPSPepConnection(_clientType, socket); the main processing
107 } catch (IOException e) {
108 COPSDebug.err(getClass().getName(), COPSDebug.ERROR_SOCKET, e);
109 } catch (COPSException e) {
110 COPSDebug.err(getClass().getName(), COPSDebug.ERROR_SOCKET, e);
111 } catch (COPSPepException e) {
112 COPSDebug.err(getClass().getName(), COPSDebug.ERROR_SOCKET, e);
117 * Establish connection to PDP's IP address
119 * <Client-Open> ::= <Common Header> <PEPID> [<ClientSI>] [<LastPDPAddr>]
122 * Not support [<ClientSI>], [<LastPDPAddr>], [<Integrity>]
124 * <Client-Accept> ::= <Common Header> <KA Timer> [<ACCT Timer>]
127 * Not send [<Integrity>]
129 * <Client-Close> ::= <Common Header> <Error> [<PDPRedirAddr>] [<Integrity>]
131 * Not send [<PDPRedirAddr>], [<Integrity>]
133 * @throws UnknownHostException
134 * @throws IOException
135 * @throws COPSException
136 * @throws COPSPepException
139 private COPSPepConnection processConnection(Socket socket)
140 throws UnknownHostException, IOException, COPSException,
143 COPSHeader hdr = new COPSHeader(COPSHeader.COPS_OP_OPN, getClientType());
145 COPSPepId pepId = new COPSPepId();
146 COPSData d = new COPSData(getPepID());
149 COPSClientOpenMsg msg = new COPSClientOpenMsg();
153 // Create Socket and send OPN
155 * InetAddress addr = InetAddress.getByName(psHost); Socket socket = new
156 * Socket(addr,psPort);
158 COPSDebug.err(getClass().getName(), "Send COPSClientOpenMsg to PDP");
159 msg.writeData(socket);
161 // Receive the response
162 COPSDebug.err(getClass().getName(), "Receive the resposne from PDP");
163 COPSMsg recvmsg = COPSTransceiver.receiveMsg(socket);
165 if (recvmsg.getHeader().isAClientAccept()) {
166 COPSDebug.err(getClass().getName(), "isAClientAccept from PDP");
167 COPSClientAcceptMsg cMsg = (COPSClientAcceptMsg) recvmsg;
170 if (cMsg.getIntegrity() != null) {
171 throw new COPSPepException("Unsupported object (Integrity)");
175 COPSKATimer kt = cMsg.getKATimer();
177 throw new COPSPepException(
178 "Mandatory COPS object missing (KA Timer)");
179 short _kaTimeVal = kt.getTimerVal();
182 COPSAcctTimer at = cMsg.getAcctTimer();
183 short _acctTimer = 0;
185 _acctTimer = at.getTimerVal();
187 // Create the connection manager
188 COPSPepConnection conn = new COPSPepConnection(getClientType(),
190 conn.setKaTimer(_kaTimeVal);
191 conn.setAcctTimer(_acctTimer);
192 COPSDebug.err(getClass().getName(), "Thread(conn).start");
193 new Thread(conn).start();
196 } else if (recvmsg.getHeader().isAClientClose()) {
197 COPSDebug.err(getClass().getName(), "isAClientClose from PDP");
198 COPSClientCloseMsg cMsg = (COPSClientCloseMsg) recvmsg;
199 error = cMsg.getError();
202 } else { // messages of other types are not expected
203 throw new COPSPepException(
204 "Message not expected. Closing connection for "
205 + socket.toString());
210 * Gets the COPS error returned by the PDP
212 * @return <tt>COPSError</tt> returned by PDP
214 public COPSError getConnectionError() {
218 public void setConnectionError(COPSError _error) {
223 * @return the serverSocket
225 public ServerSocket getServerSocket() {
230 * @param serverSocket
231 * the serverSocket to set
233 public void setServerSocket(ServerSocket serverSocket) {
234 this.serverSocket = serverSocket;
238 * @return the serverPort
240 public int getServerPort() {
246 * the serverPort to set
248 public void setServerPort(int serverPort) {
249 this.serverPort = serverPort;