/** * 链接服务端开始服务 * * @throws ClientException */ @Override public void start() throws IOException { synchronized (_monitor) { if (isNew()) { logger.debug("{} Session is start", _key.toString()); try { _status = Status.Active; _channel = _conn.connect(this); } catch (IOException e) { logger.error("{} Session start is fail,error is {}", _key.toString(), e.getMessage()); _status = Status.Close; throw e; } } } }
@Override public String toString() { StringBuilder builder = new StringBuilder(); builder.append("OperatorChannel [_operatorKey="); builder.append(_key.toString()); builder.append("]"); return builder.toString(); }
private void logout() { try { LogoutRequest r = new LogoutRequest(); send(r); } catch (IOException e) { logger.error("{} send logout", _key.toString()); } }
@Override public void heartbeatTouch() { _lastHeartbeanTime = System.currentTimeMillis(); try { if (isClientRquestTimeout()) { logger.debug("{} client request timeout close session", _key.toString()); close(); } } catch (IOException e) { logger.error("Client request timeout close error {}", e.toString()); } }
@Override public void close() throws IOException { synchronized (_monitor) { if (isClose()) { return; } if (isNew()) { _status = Status.Close; return; } logger.debug("{} start session close", _key.toString()); logout(); _status = Status.Close; _heartbeatKeep.shutdown(); if (_channel != null && _channel.isOpen()) { _channel.close(); } logger.debug("{} finish session close", _key.toString()); } }
@Override public void send(Requestable<? extends Response> request) throws IOException { synchronized (_monitor) { if (isNew()) { start(); } } if (isClose()) { logger.debug("{} Send message,but closed", _key.toString()); throw new IOException("Session already closed,not send message."); } if (isAccess(request.getMessageType())) { _messageProcess.sendProcess(this, request, _generator); if (isRequestMessage(request.getMessageType())) { _lastClientRequestTime = System.currentTimeMillis(); heartbeatTouch(); } } else { logger.debug("{} not access cti server.", _key.toString()); throw new SessionAccessException(_key); } }