@Override public void handleUpstream(ChannelHandlerContext ctx, ChannelEvent e) throws Exception { if (e instanceof IdleStateEvent) { channelIdle(ctx, (IdleStateEvent) e); } else { super.handleUpstream(ctx, e); } }
@Override public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception { LOG.warn("Channel {} has thrown exception {}",e.getChannel(),e); e.getChannel().close(); super.exceptionCaught(ctx, e); } }
@Override public void writeRequested(ChannelHandlerContext ctx, MessageEvent e) throws Exception { if (!(e.getMessage() instanceof OdetteFtpExchangeBuffer)) { super.writeRequested(ctx, e); return; } OdetteFtpSession session = ChannelContext.SESSION.get(ctx.getChannel()); OdetteFtpExchangeBuffer message = (OdetteFtpExchangeBuffer) e.getMessage(); // set last command sent in session attribute session.setLastCommandSent(message.getIdentifier()); super.writeRequested(ctx, e); }
@Override public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { // Metrics for connect is handled by ServiceRouter. transportMetrics.connectionsOpen.dec(); super.channelDisconnected(ctx, e); }
@Override public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception { if (e.getMessage() instanceof HttpRequest && isKeepAliveSupported) { ctx.setAttachment(e.getMessage()); } super.messageReceived(ctx, e); }
@Override public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { OdetteFtpSession session = ChannelContext.SESSION.get(ctx.getChannel()); /* Get handler implementation for the correct protocol version. */ OdetteFtpVersion version = session.getVersion(); ProtocolHandler handler = getProtocolHandlerByVersion(version); /* * Delegate the session opened processing to the correct handler * implementation version. */ handler.sessionConnected(session); super.channelConnected(ctx, e); }
@Override public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { OdetteFtpSession session = new OdetteFtpSession(entityType); ChannelContext.SESSION.set(e.getChannel(), session); ChannelCallbackHandler channelWriter = new ChannelCallbackHandler(e.getChannel()); session.setChannelCallback(channelWriter); /* create new instance of the Oftplet implementation */ Oftplet oftplet = getOftpletFactory().createProvider(); setSessionOftplet(session, oftplet); /* let the Oftplet implementation adjust the parameters */ oftplet.init(session); // configure channel idle based on configured session timeout int timeoutSeconds = session.getTimeout(); ctx.getPipeline().addFirst("Timeout-HANDLER", new IdleStateHandler(timer, timeoutSeconds, timeoutSeconds, 0)); // Add all accepted channels to the group so that they are closed // properly on shutdown. If the added channel is closed before shutdown, // it will be removed from the group automatically. if (channelGroup != null) { channelGroup.add(e.getChannel()); } super.channelOpen(ctx, e); }
super.writeRequested(ctx, e);
@Override public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { OdetteFtpSession session = ChannelContext.SESSION.get(ctx.getChannel()); Oftplet oftplet = getSessionOftplet(session); oftplet.destroy(); super.channelDisconnected(ctx, e); }
@Override public void channelIdle(ChannelHandlerContext ctx, IdleStateEvent e) throws Exception { if (e.getState().equals(IdleState.WRITER_IDLE)) { e.getChannel().write(ChannelBuffers.wrappedBuffer("* OK Hang in there..\r\n".getBytes("US-ASCII"))); } super.channelIdle(ctx, e); }
@Override public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception { if (e.getCause() instanceof SessionHandler.NotFoundException) { respond(e.getChannel(), HttpResponseStatus.NOT_FOUND, "Session not found."); } else if (e.getCause() instanceof SessionHandler.LockException) { if (e.getChannel().isWritable()) { e.getChannel().write(Frame.closeFrame(2010, "Another connection still open")).addListener(ChannelFutureListener.CLOSE); } } else { super.exceptionCaught(ctx, e); } }
@Override public void channelIdle(ChannelHandlerContext ctx, IdleStateEvent e) throws Exception { // check if the client did nothing for too long if (e.getState().equals(IdleState.ALL_IDLE)) { ImapSession session = (ImapSession) attributes.get(ctx.getChannel()); InetSocketAddress address = (InetSocketAddress) ctx.getChannel().getRemoteAddress(); LOGGER.info("Logout client {} ({}) because it idled for too long...", address.getHostName(), address.getAddress().getHostAddress()); // logout the client session.logout(); // close the channel ctx.getChannel().close(); } super.channelIdle(ctx, e); }
/** * This method is called whenever the communication data flow is idle and * the session timeout limit is over. */ @Override public void channelIdle(ChannelHandlerContext ctx, IdleStateEvent e) throws Exception { OdetteFtpSession session = ChannelContext.SESSION.get(ctx.getChannel()); // session is terminated by timeout OdetteFtpVersion version = session.getVersion(); ProtocolHandler handler = getProtocolHandlerByVersion(version); // TODO perhaps we may specify the timeout period in release message handler.abort(session, EndSessionReason.TIME_OUT, null); super.channelIdle(ctx, e); }