- public Socket getSocket() {
- return _sock;
- }
-
- /**
- * Main loop
- */
- public void run () {
- Date _lastSendKa = new Date();
- _lastRecKa = new Date();
- try {
- while (!_sock.isClosed()) {
- if (_sock.getInputStream().available() != 0) {
- processMessage(_sock);
- _lastRecKa = new Date();
- }
-
- // Keep Alive
- if (_kaTimer > 0) {
- // Timeout at PDP
- int _startTime = (int) (_lastRecKa.getTime());
- int cTime = (int) (new Date().getTime());
-
- if ((cTime - _startTime) > _kaTimer*1000) {
- _sock.close();
- // Notify all Request State Managers
- notifyNoKAAllReqStateMan();
- }
-
- // Send to PEP
- _startTime = (int) (_lastSendKa.getTime());
- cTime = (int) (new Date().getTime());
-
- if ((cTime - _startTime) > ((_kaTimer*3/4)*1000)) {
- // TODO - determine what is the client type to be used here?
- final COPSKAMsg msg = new COPSKAMsg(ClientType.NA, null);
- COPSTransceiver.sendMsg(msg, _sock);
- _lastSendKa = new Date();
- }
- }
-
- try {
- Thread.sleep(500);
- } catch (Exception e) {
- logger.error("Unexpected exception while sleeping", e);
- }
-
- }
- } catch (Exception e) {
- logger.error("Error reading messages from socket", e);
- }
-
- // connection closed by server
- // COPSDebug.out(getClass().getName(),"Connection closed by client");
- try {
- _sock.close();
- } catch (IOException e) {
- logger.error("Error closing socket", e);
- }
-
- // Notify all Request State Managers
- try {
- notifyCloseAllReqStateMan();
- } catch (COPSPdpException e) {
- logger.error("Error closing state managers", e);
- }
- }
-
- /**
- * Gets a COPS message from the socket and processes it
- * @param conn Socket connected to the PEP
- */
- private void processMessage(final Socket conn) throws COPSPdpException, COPSException, IOException {
- final COPSMsg msg = COPSTransceiver.receiveMsg(conn);
-
- if (msg.getHeader().getOpCode().equals(OPCode.CC)) {
- handleClientCloseMsg(conn, msg);
- } else if (msg.getHeader().getOpCode().equals(OPCode.KA)) {
- handleKeepAliveMsg(conn, msg);
- } else if (msg.getHeader().getOpCode().equals(OPCode.REQ)) {
- handleRequestMsg(conn, msg);
- } else if (msg.getHeader().getOpCode().equals(OPCode.RPT)) {
- handleReportMsg(conn, msg);
- } else if (msg.getHeader().getOpCode().equals(OPCode.DRQ)) {
- handleDeleteRequestMsg(conn, msg);
- } else if (msg.getHeader().getOpCode().equals(OPCode.SSQ)) {
- handleSyncComplete(conn, msg);
- } else {
- throw new COPSPdpException("Message not expected (" + msg.getHeader().getOpCode() + ").");
- }
- }
-
- /**
- * Handle Client Close Message, close the passed connection
- *
- * @param conn a Socket
- * @param msg a COPSMsg
- *
- *
- * <Client-Close> ::= <Common Header>
- * <Error>
- * [<Integrity>]
- *
- * Not support [<Integrity>]
- *
- */
- private void handleClientCloseMsg(Socket conn, COPSMsg msg) {
- COPSClientCloseMsg cMsg = (COPSClientCloseMsg) msg;
- _error = cMsg.getError();
- try {
- // Support
- if (cMsg.getIntegrity() != null) {
- logger.error("Unsupported objects (Integrity) to connection " + conn.getInetAddress());
- }
-
- conn.close();
- } catch (Exception unae) {
- logger.error("Unexpected exception closing connection", unae);
- }
- }
-
- /**
- * Gets the occurred COPS Error
- * @return <tt>COPSError</tt> object
- */
- protected COPSError getError() {
- return _error;
- }
-
- /**
- * Handle Keep Alive Message
- *
- * <Keep-Alive> ::= <Common Header>
- * [<Integrity>]
- *
- * Not support [<Integrity>]
- *
- * @param conn a Socket
- * @param msg a COPSMsg
- *
- */
- private void handleKeepAliveMsg(Socket conn, COPSMsg msg) {
- COPSKAMsg cMsg = (COPSKAMsg) msg;
-
- COPSKAMsg kaMsg = (COPSKAMsg) msg;
- try {
- // Support
- if (cMsg.getIntegrity() != null) {
- logger.error("Unsupported objects (Integrity) to connection " + conn.getInetAddress());
- }
-
- kaMsg.writeData(conn);
- } catch (Exception unae) {
- logger.error("Unexpected exception while writing keep-alive message", unae);
- }
- }
-
- /**
- * Handle Delete Request Message
- *
- * <Delete Request> ::= <Common Header>
- * <Client Handle>
- * <Reason>
- * [<Integrity>]
- *
- * Not support [<Integrity>]
- *
- * @param conn a Socket
- * @param msg a COPSMsg
- *
- */
- private void handleDeleteRequestMsg(Socket conn, COPSMsg msg) throws COPSPdpException {
- COPSDeleteMsg cMsg = (COPSDeleteMsg) msg;
- // COPSDebug.out(getClass().getName(),"Removing ClientHandle for " +
- // conn.getInetAddress() + ":" + conn.getPort() + ":[Reason " + cMsg.getReason().getDescription() + "]");
-
- // Support
- if (cMsg.getIntegrity() != null) {
- logger.error("Unsupported objects (Integrity) to connection " + conn.getInetAddress());
- }
-
- // Delete clientHandler
- if (_managerMap.remove(cMsg.getClientHandle().getId().str()) == null) {
- // TODO - Determine what to do here???
- // COPSDebug.out(getClass().getName(),"Missing for ClientHandle " +
- // cMsg.getClientHandle().getId().getData());
- }
-
- final PCMMPdpReqStateMan man = _managerMap.get(cMsg.getClientHandle().getId().str());
- if (man == null) {
- logger.warn("State manager not found");
- } else {
- man.processDeleteRequestState(cMsg);
- }
-
- }
-
- /**
- * Handle Request Message
- *
- * <Request> ::= <Common Header>
- * <Client Handle>
- * <Context>
- * *(<Named ClientSI>)
- * [<Integrity>]
- * <Named ClientSI> ::= <*(<PRID> <EPD>)>
- *
- * Not support [<Integrity>]
- *
- * @param conn a Socket
- * @param msg a COPSMsg
- *
- */
- private void handleRequestMsg(Socket conn, COPSMsg msg) throws COPSPdpException {
-
- final COPSReqMsg reqMsg = (COPSReqMsg) msg;
-// COPSContext cntxt = reqMsg.getContext();
- final COPSHeader header = reqMsg.getHeader();
- //short reqType = cntxt.getRequestType();
- final ClientType cType = header.getClientType();
-
- // Support
- if (reqMsg.getIntegrity() != null) {
- logger.error("Unsupported objects (Integrity) to connection " + conn.getInetAddress());
- }
-
- PCMMPdpReqStateMan man;
- man = _managerMap.get(reqMsg.getClientHandle().getId().str());
- if (man == null) {
-
- man = new PCMMPdpReqStateMan(cType, reqMsg.getClientHandle().getId().str());
- _managerMap.put(reqMsg.getClientHandle().getId().str(), man);
- man.setDataProcess(_process);
- man.initRequestState(_sock);
-
- // COPSDebug.out(getClass().getName(),"createHandler called, clientType=" +
- // header.getClientType() + " msgType=" +
- // cntxt.getMessageType() + ", connId=" + conn.toString());
- }
-
- man.processRequest(reqMsg);
- }
-
- /**
- * Handle Report Message
- *
- * <Report State> ::= <Common Header>
- * <Client Handle>
- * <Report Type>
- * *(<Named ClientSI>)
- * [<Integrity>]
- *
- * Not support [<Integrity>]
- *
- * @param conn a Socket
- * @param msg a COPSMsg
- *
- */
- private void handleReportMsg(Socket conn, COPSMsg msg)
- throws COPSPdpException {
- COPSReportMsg repMsg = (COPSReportMsg) msg;
- // COPSHandle handle = repMsg.getClientHandle();
- // COPSHeader header = repMsg.getHeader();
-
- // Support
- if (repMsg.getIntegrity() != null) {
- logger.error("Unsupported objects (Integrity) to connection " + conn.getInetAddress());
- }
-
- final PCMMPdpReqStateMan man = _managerMap.get(repMsg.getClientHandle().getId().str());
- if (man == null) {
- logger.warn("State manager not found");
- } else {
- man.processReport(repMsg);
- }
- }
-
- /**
- * Method handleSyncComplete
- *
- * @param conn a Socket
- * @param msg a COPSMsg
- *
- */
- private void handleSyncComplete(Socket conn, COPSMsg msg)
- throws COPSPdpException {
- COPSSyncStateMsg cMsg = (COPSSyncStateMsg) msg;
- // COPSHandle handle = cMsg.getClientHandle();
- // COPSHeader header = cMsg.getHeader();
-
- // Support
- if (cMsg.getIntegrity() != null) {
- logger.error("Unsupported objects (Integrity) to connection " + conn.getInetAddress());
- }
-
- final PCMMPdpReqStateMan man = _managerMap.get(cMsg.getClientHandle().getId().str());
- if (man == null) {
- logger.warn("State manager not found");
- } else {
- man.processSyncComplete(cMsg);
- }
- }
-
- /**
- * Requests a COPS sync from the PEP
- * @throws COPSException
- * @throws COPSPdpException
- */
- protected void syncAllRequestState() throws COPSException, COPSPdpException {
- for (final PCMMPdpReqStateMan man : _managerMap.values()) {
- man.syncRequestState();
- }
- }
-
- private void notifyCloseAllReqStateMan() throws COPSPdpException {
- for (final PCMMPdpReqStateMan man : _managerMap.values()) {
- man.processClosedConnection(_error);
- }
- }
-
- private void notifyNoKAAllReqStateMan() throws COPSPdpException {
- for (final PCMMPdpReqStateMan man : _managerMap.values()) {
- man.processNoKAConnection();
- }