Search in sources :

Example 6 with IdleStateHandler

use of org.jboss.netty.handler.timeout.IdleStateHandler in project canal by alibaba.

the class ClientAuthenticationHandler method messageReceived.

public void messageReceived(final ChannelHandlerContext ctx, MessageEvent e) throws Exception {
    ChannelBuffer buffer = (ChannelBuffer) e.getMessage();
    final Packet packet = Packet.parseFrom(buffer.readBytes(buffer.readableBytes()).array());
    switch(packet.getVersion()) {
        case SUPPORTED_VERSION:
        default:
            final ClientAuth clientAuth = ClientAuth.parseFrom(packet.getBody());
            if (seed == null) {
                byte[] errorBytes = NettyUtils.errorPacket(400, MessageFormatter.format("auth failed for seed is null", clientAuth.getUsername()).getMessage());
                NettyUtils.write(ctx.getChannel(), errorBytes, null);
            }
            if (!embeddedServer.auth(clientAuth.getUsername(), clientAuth.getPassword().toStringUtf8(), seed)) {
                byte[] errorBytes = NettyUtils.errorPacket(400, MessageFormatter.format("auth failed for user:{}", clientAuth.getUsername()).getMessage());
                NettyUtils.write(ctx.getChannel(), errorBytes, null);
            }
            // 如果存在订阅信息
            if (StringUtils.isNotEmpty(clientAuth.getDestination()) && StringUtils.isNotEmpty(clientAuth.getClientId())) {
                ClientIdentity clientIdentity = new ClientIdentity(clientAuth.getDestination(), Short.valueOf(clientAuth.getClientId()), clientAuth.getFilter());
                try {
                    MDC.put("destination", clientIdentity.getDestination());
                    embeddedServer.subscribe(clientIdentity);
                    // 尝试启动,如果已经启动,忽略
                    if (!embeddedServer.isStart(clientIdentity.getDestination())) {
                        ServerRunningMonitor runningMonitor = ServerRunningMonitors.getRunningMonitor(clientIdentity.getDestination());
                        if (!runningMonitor.isStart()) {
                            runningMonitor.start();
                        }
                    }
                } finally {
                    MDC.remove("destination");
                }
            }
            // 鉴权一次性,暂不统计
            NettyUtils.ack(ctx.getChannel(), future -> {
                logger.info("remove unused channel handlers after authentication is done successfully.");
                ctx.getPipeline().remove(HandshakeInitializationHandler.class.getName());
                ctx.getPipeline().remove(ClientAuthenticationHandler.class.getName());
                int readTimeout = defaultSubscriptorDisconnectIdleTimeout;
                int writeTimeout = defaultSubscriptorDisconnectIdleTimeout;
                if (clientAuth.getNetReadTimeout() > 0) {
                    readTimeout = clientAuth.getNetReadTimeout();
                }
                if (clientAuth.getNetWriteTimeout() > 0) {
                    writeTimeout = clientAuth.getNetWriteTimeout();
                }
                // fix bug: soTimeout parameter's unit from connector is
                // millseconds.
                IdleStateHandler idleStateHandler = new IdleStateHandler(NettyUtils.hashedWheelTimer, readTimeout, writeTimeout, 0, TimeUnit.MILLISECONDS);
                ctx.getPipeline().addBefore(SessionHandler.class.getName(), IdleStateHandler.class.getName(), idleStateHandler);
                IdleStateAwareChannelHandler idleStateAwareChannelHandler = new IdleStateAwareChannelHandler() {

                    public void channelIdle(ChannelHandlerContext ctx1, IdleStateEvent e1) throws Exception {
                        logger.warn("channel:{} idle timeout exceeds, close channel to save server resources...", ctx1.getChannel());
                        ctx1.getChannel().close();
                    }
                };
                ctx.getPipeline().addBefore(SessionHandler.class.getName(), IdleStateAwareChannelHandler.class.getName(), idleStateAwareChannelHandler);
            });
            break;
    }
}
Also used : Packet(com.alibaba.otter.canal.protocol.CanalPacket.Packet) ChannelHandlerContext(org.jboss.netty.channel.ChannelHandlerContext) ClientAuth(com.alibaba.otter.canal.protocol.CanalPacket.ClientAuth) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer) IdleStateEvent(org.jboss.netty.handler.timeout.IdleStateEvent) ClientIdentity(com.alibaba.otter.canal.protocol.ClientIdentity) IdleStateHandler(org.jboss.netty.handler.timeout.IdleStateHandler) ServerRunningMonitor(com.alibaba.otter.canal.common.zookeeper.running.ServerRunningMonitor) IdleStateAwareChannelHandler(org.jboss.netty.handler.timeout.IdleStateAwareChannelHandler)

Example 7 with IdleStateHandler

use of org.jboss.netty.handler.timeout.IdleStateHandler in project NabAlive by jcheype.

the class NabaliveServer method start.

@PostConstruct
public void start() {
    logger.info("Starting server.");
    // Configure the server.
    bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool()));
    // Set up the pipeline factory.
    bootstrap.setPipelineFactory(new ChannelPipelineFactory() {

        public ChannelPipeline getPipeline() throws Exception {
            ChannelPipeline pipeline = pipeline();
            pipeline.addLast("timeout", new IdleStateHandler(timer, 0, 0, 20));
            pipeline.addLast("nabaliveServerHandler", nabaliveServerHandler);
            return pipeline;
        }
    });
    bootstrap.setOption("reuseAddress", true);
    // Bind and start to accept incoming connections.
    bind = bootstrap.bind(new InetSocketAddress(XMPP_PORT));
}
Also used : NioServerSocketChannelFactory(org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory) InetSocketAddress(java.net.InetSocketAddress) IdleStateHandler(org.jboss.netty.handler.timeout.IdleStateHandler) ChannelPipelineFactory(org.jboss.netty.channel.ChannelPipelineFactory) ServerBootstrap(org.jboss.netty.bootstrap.ServerBootstrap) ChannelPipeline(org.jboss.netty.channel.ChannelPipeline) PostConstruct(javax.annotation.PostConstruct)

Example 8 with IdleStateHandler

use of org.jboss.netty.handler.timeout.IdleStateHandler in project NabAlive by jcheype.

the class HttpApiServerPipelineFactory method getPipeline.

@Override
public ChannelPipeline getPipeline() throws Exception {
    ChannelPipeline pipeline = pipeline();
    // Uncomment the following line if you want HTTPS
    // SSLEngine engine = //SecureChatSslContextFactory.getServerContext().createSSLEngine();
    // engine.setUseClientMode(false);
    // pipeline.addLast("ssl", new SslHandler(engine));
    pipeline.addLast("timeout", new IdleStateHandler(timer, 0, 0, 20));
    pipeline.addLast("decoder", new HttpRequestDecoder());
    pipeline.addLast("encoder", new HttpResponseEncoder());
    // pipeline.addLast("comressor", new HttpContentCompressor(9));
    pipeline.addLast("aggregator", new HttpChunkAggregator(65536));
    pipeline.addLast("chunkedWriter", new ChunkedWriteHandler());
    // pipeline.addLast("executor", eh);
    pipeline.addLast("handler", handler);
    return pipeline;
}
Also used : ChunkedWriteHandler(org.jboss.netty.handler.stream.ChunkedWriteHandler) IdleStateHandler(org.jboss.netty.handler.timeout.IdleStateHandler) ChannelPipeline(org.jboss.netty.channel.ChannelPipeline)

Aggregations

IdleStateHandler (org.jboss.netty.handler.timeout.IdleStateHandler)8 ChannelPipeline (org.jboss.netty.channel.ChannelPipeline)6 ServerBootstrap (org.jboss.netty.bootstrap.ServerBootstrap)2 ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)2 ChannelHandlerContext (org.jboss.netty.channel.ChannelHandlerContext)2 ChannelPipelineFactory (org.jboss.netty.channel.ChannelPipelineFactory)2 NioServerSocketChannelFactory (org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory)2 IdleStateAwareChannelHandler (org.jboss.netty.handler.timeout.IdleStateAwareChannelHandler)2 IdleStateEvent (org.jboss.netty.handler.timeout.IdleStateEvent)2 IdleEventProcessor (co.cask.cdap.gateway.router.handlers.IdleEventProcessor)1 ServerRunningMonitor (com.alibaba.otter.canal.common.zookeeper.running.ServerRunningMonitor)1 ClientAuth (com.alibaba.otter.canal.protocol.AdminPacket.ClientAuth)1 Packet (com.alibaba.otter.canal.protocol.AdminPacket.Packet)1 ClientAuth (com.alibaba.otter.canal.protocol.CanalPacket.ClientAuth)1 Packet (com.alibaba.otter.canal.protocol.CanalPacket.Packet)1 ClientIdentity (com.alibaba.otter.canal.protocol.ClientIdentity)1 InetSocketAddress (java.net.InetSocketAddress)1 PostConstruct (javax.annotation.PostConstruct)1 ConnectionlessBootstrap (org.jboss.netty.bootstrap.ConnectionlessBootstrap)1 NioDatagramChannelFactory (org.jboss.netty.channel.socket.nio.NioDatagramChannelFactory)1