2 * Copyright (c) 2004 University of Murcia. All rights reserved.
\r
3 * --------------------------------------------------------------
\r
4 * For more information, please see <http://www.umu.euro6ix.org/>.
\r
7 package org.umu.cops.prpdp;
\r
9 import java.io.IOException;
\r
10 import java.net.Socket;
\r
11 import java.util.Date;
\r
12 import java.util.Enumeration;
\r
13 import java.util.Hashtable;
\r
15 import org.umu.cops.common.COPSDebug;
\r
16 import org.umu.cops.stack.COPSClientCloseMsg;
\r
17 import org.umu.cops.stack.COPSContext;
\r
18 import org.umu.cops.stack.COPSDeleteMsg;
\r
19 import org.umu.cops.stack.COPSError;
\r
20 import org.umu.cops.stack.COPSException;
\r
21 import org.umu.cops.stack.COPSHeader;
\r
22 import org.umu.cops.stack.COPSKAMsg;
\r
23 import org.umu.cops.stack.COPSMsg;
\r
24 import org.umu.cops.stack.COPSPepId;
\r
25 import org.umu.cops.stack.COPSReportMsg;
\r
26 import org.umu.cops.stack.COPSReqMsg;
\r
27 import org.umu.cops.stack.COPSSyncStateMsg;
\r
28 import org.umu.cops.stack.COPSTransceiver;
\r
31 * Class for managing an provisioning connection at the PDP side.
\r
33 public class COPSPdpConnection implements Runnable {
\r
36 Socket connected to PEP
\r
38 private Socket _sock;
\r
43 private COPSPepId _pepId;
\r
46 Time of the latest keep-alive sent
\r
48 private Date _lastKa;
\r
51 Opcode of the latest message sent
\r
53 private byte _lastmessage;
\r
56 * Time of the latest keep-alive received
\r
58 protected Date _lastRecKa;
\r
61 Maps a Client Handle to a Handler
\r
63 protected Hashtable _managerMap;
\r
64 // map < String(COPSHandle), COPSPdpHandler> HandlerMap;
\r
67 * PDP policy data processor class
\r
69 protected COPSPdpDataProcess _process;
\r
72 Accounting timer value (secs)
\r
74 protected short _acctTimer;
\r
77 Keep-alive timer value (secs)
\r
79 protected short _kaTimer;
\r
82 COPS error returned by PEP
\r
84 protected COPSError _error;
\r
87 * Creates a new PDP connection
\r
89 * @param pepId PEP-ID of the connected PEP
\r
90 * @param sock Socket connected to PEP
\r
91 * @param process Object for processing policy data
\r
93 public COPSPdpConnection(COPSPepId pepId, Socket sock, COPSPdpDataProcess process) {
\r
97 _lastKa = new Date();
\r
98 _lastmessage = COPSHeader.COPS_OP_OPN;
\r
99 _managerMap = new Hashtable(20);
\r
102 _process = process;
\r
106 * Gets the time of that latest keep-alive sent
\r
107 * @return Time of that latest keep-alive sent
\r
109 public Date getLastKAlive() {
\r
114 * Sets the keep-alive timer value
\r
115 * @param kaTimer Keep-alive timer value (secs)
\r
117 public void setKaTimer(short kaTimer) {
\r
118 _kaTimer = kaTimer;
\r
122 * Gets the keep-alive timer value
\r
123 * @return Keep-alive timer value (secs)
\r
125 public short getKaTimer() {
\r
130 * Sets the accounting timer value
\r
131 * @param acctTimer Accounting timer value (secs)
\r
133 public void setAccTimer(short acctTimer) {
\r
134 _acctTimer = acctTimer;
\r
138 * Gets the accounting timer value
\r
139 * @return Accounting timer value (secs)
\r
141 public short getAcctTimer() {
\r
146 * Gets the latest COPS message
\r
147 * @return Code of the latest message sent
\r
149 public byte getLastMessage() {
\r
150 return _lastmessage;
\r
154 * Gets active handles
\r
155 * @return An <tt>Enumeration</tt> holding all active handles
\r
157 public Enumeration getHandles() {
\r
158 return _managerMap.keys();
\r
162 * Gets the handle map
\r
163 * @return A <tt>Hashtable</tt> holding the handle map
\r
165 public Hashtable getReqStateMans() {
\r
166 return _managerMap;
\r
171 * @return The ID of the PEP, as a <tt>String</tt>
\r
173 public String getPepId() {
\r
174 return _pepId.getData().str();
\r
178 * Checks whether the socket to the PEP is closed or not
\r
179 * @return <tt>true</tt> if closed, <tt>false</tt> otherwise
\r
181 public boolean isClosed() {
\r
182 return _sock.isClosed();
\r
186 * Closes the socket to the PEP
\r
187 * @throws IOException
\r
189 protected void close()
\r
190 throws IOException {
\r
195 * Gets the socket to the PEP
\r
196 * @return Socket connected to the PEP
\r
198 public Socket getSocket() {
\r
205 public void run () {
\r
206 Date _lastSendKa = new Date();
\r
207 _lastRecKa = new Date();
\r
209 while (!_sock.isClosed()) {
\r
210 if (_sock.getInputStream().available() != 0) {
\r
211 _lastmessage = processMessage(_sock);
\r
212 _lastRecKa = new Date();
\r
216 if (_kaTimer > 0) {
\r
218 int _startTime = (int) (_lastRecKa.getTime());
\r
219 int cTime = (int) (new Date().getTime());
\r
221 if ((int)(cTime - _startTime) > _kaTimer*1000) {
\r
223 // Notify all Request State Managers
\r
224 notifyNoKAAllReqStateMan();
\r
228 _startTime = (int) (_lastSendKa.getTime());
\r
229 cTime = (int) (new Date().getTime());
\r
231 if ((int)(cTime - _startTime) > ((_kaTimer*3/4)*1000)) {
\r
232 COPSHeader hdr = new COPSHeader(COPSHeader.COPS_OP_KA);
\r
233 COPSKAMsg msg = new COPSKAMsg();
\r
237 COPSTransceiver.sendMsg(msg, _sock);
\r
238 _lastSendKa = new Date();
\r
244 } catch (Exception e) {};
\r
247 } catch (Exception e) {
\r
248 COPSDebug.err(getClass().getName(), COPSDebug.ERROR_SOCKET, e);
\r
251 // connection closed by server
\r
252 // COPSDebug.out(getClass().getName(),"Connection closed by client");
\r
255 } catch (IOException e) {};
\r
257 // Notify all Request State Managers
\r
259 notifyCloseAllReqStateMan();
\r
260 } catch (COPSPdpException e) {};
\r
264 * Gets a COPS message from the socket and processes it
\r
265 * @param conn Socket connected to the PEP
\r
266 * @return Type of COPS message
\r
268 private byte processMessage(Socket conn)
\r
269 throws COPSPdpException, COPSException, IOException {
\r
270 COPSMsg msg = COPSTransceiver.receiveMsg(conn);
\r
272 if (msg.getHeader().isAClientClose()) {
\r
273 handleClientCloseMsg(conn, msg);
\r
274 return COPSHeader.COPS_OP_CC;
\r
275 } else if (msg.getHeader().isAKeepAlive()) {
\r
276 handleKeepAliveMsg(conn, msg);
\r
277 return COPSHeader.COPS_OP_KA;
\r
278 } else if (msg.getHeader().isARequest()) {
\r
279 handleRequestMsg(conn, msg);
\r
280 return COPSHeader.COPS_OP_REQ;
\r
281 } else if (msg.getHeader().isAReport()) {
\r
282 handleReportMsg(conn, msg);
\r
283 return COPSHeader.COPS_OP_RPT;
\r
284 } else if (msg.getHeader().isADeleteReq()) {
\r
285 handleDeleteRequestMsg(conn, msg);
\r
286 return COPSHeader.COPS_OP_DRQ;
\r
287 } else if (msg.getHeader().isASyncComplete()) {
\r
288 handleSyncComplete(conn, msg);
\r
289 return COPSHeader.COPS_OP_SSC;
\r
291 throw new COPSPdpException("Message not expected (" + msg.getHeader().getOpCode() + ").");
\r
296 * Handle Client Close Message, close the passed connection
\r
298 * @param conn a Socket
\r
299 * @param msg a COPSMsg
\r
302 * <Client-Close> ::= <Common Header>
\r
306 * Not support [<Integrity>]
\r
309 private void handleClientCloseMsg(Socket conn, COPSMsg msg) {
\r
310 COPSClientCloseMsg cMsg = (COPSClientCloseMsg) msg;
\r
311 _error = cMsg.getError();
\r
313 // COPSDebug.out(getClass().getName(),"Got close request, closing connection " +
\r
314 // conn.getInetAddress() + ":" + conn.getPort() + ":[Error " + _error.getDescription() + "]");
\r
318 if (cMsg.getIntegrity() != null) {
\r
319 COPSDebug.err(getClass().getName(), COPSDebug.ERROR_NOSUPPORTED,
\r
320 "Unsupported objects (Integrity) to connection " + conn.getInetAddress());
\r
324 } catch (Exception unae) { };
\r
328 * Gets the occurred COPS Error
\r
329 * @return <tt>COPSError</tt> object
\r
331 protected COPSError getError() {
\r
336 * Handle Keep Alive Message
\r
338 * <Keep-Alive> ::= <Common Header>
\r
341 * Not support [<Integrity>]
\r
343 * @param conn a Socket
\r
344 * @param msg a COPSMsg
\r
347 private void handleKeepAliveMsg(Socket conn, COPSMsg msg) {
\r
348 COPSKAMsg cMsg = (COPSKAMsg) msg;
\r
350 COPSKAMsg kaMsg = (COPSKAMsg) msg;
\r
353 if (cMsg.getIntegrity() != null) {
\r
354 COPSDebug.err(getClass().getName(), COPSDebug.ERROR_NOSUPPORTED,
\r
355 "Unsupported objects (Integrity) to connection " + conn.getInetAddress());
\r
358 kaMsg.writeData(conn);
\r
359 } catch (Exception unae) { };
\r
363 * Handle Delete Request Message
\r
365 * <Delete Request> ::= <Common Header>
\r
370 * Not support [<Integrity>]
\r
372 * @param conn a Socket
\r
373 * @param msg a COPSMsg
\r
376 private void handleDeleteRequestMsg(Socket conn, COPSMsg msg)
\r
377 throws COPSPdpException {
\r
378 COPSDeleteMsg cMsg = (COPSDeleteMsg) msg;
\r
379 // COPSDebug.out(getClass().getName(),"Removing ClientHandle for " +
\r
380 // conn.getInetAddress() + ":" + conn.getPort() + ":[Reason " + cMsg.getReason().getDescription() + "]");
\r
383 if (cMsg.getIntegrity() != null) {
\r
384 COPSDebug.err(getClass().getName(), COPSDebug.ERROR_NOSUPPORTED,
\r
385 "Unsupported objects (Integrity) to connection " + conn.getInetAddress());
\r
388 // Delete clientHandler
\r
389 if (_managerMap.remove(cMsg.getClientHandle().getId().str()) == null) {
\r
390 // COPSDebug.out(getClass().getName(),"Missing for ClientHandle " +
\r
391 // cMsg.getClientHandle().getId().getData());
\r
394 COPSPdpReqStateMan man = (COPSPdpReqStateMan) _managerMap.get(cMsg.getClientHandle().getId().str());
\r
396 COPSDebug.err(getClass().getName(), COPSDebug.ERROR_NOEXPECTEDMSG);
\r
398 man.processDeleteRequestState(cMsg);
\r
404 * Handle Request Message
\r
406 * <Request> ::= <Common Header>
\r
409 * *(<Named ClientSI>)
\r
411 * <Named ClientSI> ::= <*(<PRID> <EPD>)>
\r
413 * Not support [<Integrity>]
\r
415 * @param conn a Socket
\r
416 * @param msg a COPSMsg
\r
419 private void handleRequestMsg(Socket conn, COPSMsg msg)
\r
420 throws COPSPdpException {
\r
422 COPSReqMsg reqMsg = (COPSReqMsg) msg;
\r
423 COPSContext cntxt = reqMsg.getContext();
\r
424 COPSHeader header = reqMsg.getHeader();
\r
425 //short reqType = cntxt.getRequestType();
\r
426 short cType = header.getClientType();
\r
429 if (reqMsg.getIntegrity() != null) {
\r
430 COPSDebug.err(getClass().getName(), COPSDebug.ERROR_NOSUPPORTED,
\r
431 "Unsupported objects (Integrity) to connection " + conn.getInetAddress());
\r
434 COPSPdpReqStateMan man;
\r
435 man = (COPSPdpReqStateMan) _managerMap.get(reqMsg.getClientHandle().getId().str());
\r
438 man = new COPSPdpReqStateMan(cType, reqMsg.getClientHandle().getId().str());
\r
439 _managerMap.put(reqMsg.getClientHandle().getId().str(),man);
\r
440 man.setDataProcess(_process);
\r
441 man.initRequestState(_sock);
\r
443 // COPSDebug.out(getClass().getName(),"createHandler called, clientType=" +
\r
444 // header.getClientType() + " msgType=" +
\r
445 // cntxt.getMessageType() + ", connId=" + conn.toString());
\r
448 man.processRequest(reqMsg);
\r
452 * Handle Report Message
\r
454 * <Report State> ::= <Common Header>
\r
457 * *(<Named ClientSI>)
\r
460 * Not support [<Integrity>]
\r
462 * @param conn a Socket
\r
463 * @param msg a COPSMsg
\r
466 private void handleReportMsg(Socket conn, COPSMsg msg)
\r
467 throws COPSPdpException {
\r
468 COPSReportMsg repMsg = (COPSReportMsg) msg;
\r
469 // COPSHandle handle = repMsg.getClientHandle();
\r
470 // COPSHeader header = repMsg.getHeader();
\r
473 if (repMsg.getIntegrity() != null) {
\r
474 COPSDebug.err(getClass().getName(), COPSDebug.ERROR_NOSUPPORTED,
\r
475 "Unsupported objects (Integrity) to connection " + conn.getInetAddress());
\r
478 COPSPdpReqStateMan man = (COPSPdpReqStateMan) _managerMap.get(repMsg.getClientHandle().getId().str());
\r
480 COPSDebug.err(getClass().getName(), COPSDebug.ERROR_NOEXPECTEDMSG);
\r
482 man.processReport(repMsg);
\r
487 * Method handleSyncComplete
\r
489 * @param conn a Socket
\r
490 * @param msg a COPSMsg
\r
493 private void handleSyncComplete(Socket conn, COPSMsg msg)
\r
494 throws COPSPdpException {
\r
495 COPSSyncStateMsg cMsg = (COPSSyncStateMsg) msg;
\r
496 // COPSHandle handle = cMsg.getClientHandle();
\r
497 // COPSHeader header = cMsg.getHeader();
\r
500 if (cMsg.getIntegrity() != null) {
\r
501 COPSDebug.err(getClass().getName(), COPSDebug.ERROR_NOSUPPORTED,
\r
502 "Unsupported objects (Integrity) to connection " + conn.getInetAddress());
\r
505 COPSPdpReqStateMan man = (COPSPdpReqStateMan) _managerMap.get(cMsg.getClientHandle().getId().str());
\r
507 COPSDebug.err(getClass().getName(), COPSDebug.ERROR_NOEXPECTEDMSG);
\r
509 man.processSyncComplete(cMsg);
\r
514 * Requests a COPS sync from the PEP
\r
515 * @throws COPSException
\r
516 * @throws COPSPdpException
\r
518 protected void syncAllRequestState()
\r
519 throws COPSException, COPSPdpException {
\r
520 if (_managerMap.size() > 0) {
\r
521 for (Enumeration e = _managerMap.keys() ; e.hasMoreElements() ;) {
\r
522 String handle = (String) e.nextElement();
\r
523 COPSPdpReqStateMan man = (COPSPdpReqStateMan) _managerMap.get(handle);
\r
525 man.syncRequestState();
\r
530 private void notifyCloseAllReqStateMan()
\r
531 throws COPSPdpException {
\r
532 if (_managerMap.size() > 0) {
\r
533 for (Enumeration e = _managerMap.keys() ; e.hasMoreElements() ;) {
\r
534 String handle = (String) e.nextElement();
\r
535 COPSPdpReqStateMan man = (COPSPdpReqStateMan) _managerMap.get(handle);
\r
537 man.processClosedConnection(_error);
\r
542 private void notifyNoKAAllReqStateMan()
\r
543 throws COPSPdpException {
\r
544 if (_managerMap.size() > 0) {
\r
545 for (Enumeration e = _managerMap.keys() ; e.hasMoreElements() ;) {
\r
546 String handle = (String) e.nextElement();
\r
547 COPSPdpReqStateMan man = (COPSPdpReqStateMan) _managerMap.get(handle);
\r
549 man.processNoKAConnection();
\r