Search in sources :

Example 1 with IdleStateHandler

use of org.jboss.netty.handler.timeout.IdleStateHandler in project hadoop by apache.

the class Portmap method start.

void start(final int idleTimeMilliSeconds, final SocketAddress tcpAddress, final SocketAddress udpAddress) {
    tcpServer = new ServerBootstrap(new NioServerSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool()));
    tcpServer.setPipelineFactory(new ChannelPipelineFactory() {

        private final HashedWheelTimer timer = new HashedWheelTimer();

        private final IdleStateHandler idleStateHandler = new IdleStateHandler(timer, 0, 0, idleTimeMilliSeconds, TimeUnit.MILLISECONDS);

        @Override
        public ChannelPipeline getPipeline() throws Exception {
            return Channels.pipeline(RpcUtil.constructRpcFrameDecoder(), RpcUtil.STAGE_RPC_MESSAGE_PARSER, idleStateHandler, handler, RpcUtil.STAGE_RPC_TCP_RESPONSE);
        }
    });
    tcpServer.setOption("reuseAddress", true);
    tcpServer.setOption("child.reuseAddress", true);
    udpServer = new ConnectionlessBootstrap(new NioDatagramChannelFactory(Executors.newCachedThreadPool()));
    udpServer.setPipeline(Channels.pipeline(RpcUtil.STAGE_RPC_MESSAGE_PARSER, handler, RpcUtil.STAGE_RPC_UDP_RESPONSE));
    udpServer.setOption("reuseAddress", true);
    tcpChannel = tcpServer.bind(tcpAddress);
    udpChannel = udpServer.bind(udpAddress);
    allChannels.add(tcpChannel);
    allChannels.add(udpChannel);
    LOG.info("Portmap server started at tcp://" + tcpChannel.getLocalAddress() + ", udp://" + udpChannel.getLocalAddress());
}
Also used : NioDatagramChannelFactory(org.jboss.netty.channel.socket.nio.NioDatagramChannelFactory) NioServerSocketChannelFactory(org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory) IdleStateHandler(org.jboss.netty.handler.timeout.IdleStateHandler) HashedWheelTimer(org.jboss.netty.util.HashedWheelTimer) ChannelPipelineFactory(org.jboss.netty.channel.ChannelPipelineFactory) ServerBootstrap(org.jboss.netty.bootstrap.ServerBootstrap) ChannelPipeline(org.jboss.netty.channel.ChannelPipeline) ConnectionlessBootstrap(org.jboss.netty.bootstrap.ConnectionlessBootstrap)

Example 2 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 = AdminNettyUtils.errorPacket(300, MessageFormatter.format("auth failed for seed is null", clientAuth.getUsername()).getMessage());
                AdminNettyUtils.write(ctx.getChannel(), errorBytes);
            }
            if (!canalAdmin.auth(clientAuth.getUsername(), clientAuth.getPassword().toStringUtf8(), seed)) {
                byte[] errorBytes = AdminNettyUtils.errorPacket(300, MessageFormatter.format("auth failed for user:{}", clientAuth.getUsername()).getMessage());
                AdminNettyUtils.write(ctx.getChannel(), errorBytes);
            }
            byte[] ackBytes = AdminNettyUtils.ackPacket();
            AdminNettyUtils.write(ctx.getChannel(), ackBytes, 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 : IdleStateEvent(org.jboss.netty.handler.timeout.IdleStateEvent) Packet(com.alibaba.otter.canal.protocol.AdminPacket.Packet) IdleStateHandler(org.jboss.netty.handler.timeout.IdleStateHandler) ChannelHandlerContext(org.jboss.netty.channel.ChannelHandlerContext) ClientAuth(com.alibaba.otter.canal.protocol.AdminPacket.ClientAuth) IdleStateAwareChannelHandler(org.jboss.netty.handler.timeout.IdleStateAwareChannelHandler) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 3 with IdleStateHandler

use of org.jboss.netty.handler.timeout.IdleStateHandler in project traccar by tananaev.

the class BasePipelineFactory method getPipeline.

@Override
public ChannelPipeline getPipeline() {
    ChannelPipeline pipeline = Channels.pipeline();
    if (timeout > 0 && !server.isConnectionless()) {
        pipeline.addLast("idleHandler", new IdleStateHandler(GlobalTimer.getTimer(), timeout, 0, 0));
    }
    pipeline.addLast("openHandler", new OpenChannelHandler(server));
    if (Context.isLoggerEnabled()) {
        pipeline.addLast("logger", new StandardLoggingHandler());
    }
    addSpecificHandlers(pipeline);
    if (geolocationHandler != null) {
        pipeline.addLast("location", geolocationHandler);
    }
    if (hemisphereHandler != null) {
        pipeline.addLast("hemisphere", hemisphereHandler);
    }
    if (distanceHandler != null) {
        pipeline.addLast("distance", distanceHandler);
    }
    if (remoteAddressHandler != null) {
        pipeline.addLast("remoteAddress", remoteAddressHandler);
    }
    addDynamicHandlers(pipeline);
    if (filterHandler != null) {
        pipeline.addLast("filter", filterHandler);
    }
    if (geocoderHandler != null) {
        pipeline.addLast("geocoder", geocoderHandler);
    }
    if (motionHandler != null) {
        pipeline.addLast("motion", motionHandler);
    }
    if (copyAttributesHandler != null) {
        pipeline.addLast("copyAttributes", copyAttributesHandler);
    }
    if (computedAttributesHandler != null) {
        pipeline.addLast("computedAttributes", computedAttributesHandler);
    }
    if (Context.getDataManager() != null) {
        pipeline.addLast("dataHandler", new DefaultDataHandler());
    }
    if (Context.getConfig().getBoolean("forward.enable")) {
        pipeline.addLast("webHandler", new WebDataHandler(Context.getConfig().getString("forward.url"), Context.getConfig().getBoolean("forward.json")));
    }
    if (commandResultEventHandler != null) {
        pipeline.addLast("CommandResultEventHandler", commandResultEventHandler);
    }
    if (overspeedEventHandler != null) {
        pipeline.addLast("OverspeedEventHandler", overspeedEventHandler);
    }
    if (fuelDropEventHandler != null) {
        pipeline.addLast("FuelDropEventHandler", fuelDropEventHandler);
    }
    if (motionEventHandler != null) {
        pipeline.addLast("MotionEventHandler", motionEventHandler);
    }
    if (geofenceEventHandler != null) {
        pipeline.addLast("GeofenceEventHandler", geofenceEventHandler);
    }
    if (alertEventHandler != null) {
        pipeline.addLast("AlertEventHandler", alertEventHandler);
    }
    if (ignitionEventHandler != null) {
        pipeline.addLast("IgnitionEventHandler", ignitionEventHandler);
    }
    if (maintenanceEventHandler != null) {
        pipeline.addLast("MaintenanceEventHandler", maintenanceEventHandler);
    }
    if (driverEventHandler != null) {
        pipeline.addLast("DriverEventHandler", driverEventHandler);
    }
    pipeline.addLast("mainHandler", new MainEventHandler());
    return pipeline;
}
Also used : IdleStateHandler(org.jboss.netty.handler.timeout.IdleStateHandler) ChannelPipeline(org.jboss.netty.channel.ChannelPipeline)

Example 4 with IdleStateHandler

use of org.jboss.netty.handler.timeout.IdleStateHandler in project cdap by caskdata.

the class ClientChannelPipelineFactory method getPipeline.

@Override
public ChannelPipeline getPipeline() throws Exception {
    ChannelPipeline pipeline = Channels.pipeline();
    pipeline.addLast("tracker", connectionTracker);
    pipeline.addLast("request-encoder", new HttpRequestEncoder());
    // outbound handler gets dynamically added here (after 'request-encoder')
    pipeline.addLast("response-decoder", new HttpResponseDecoder());
    // disable the read-specific and write-specific timeouts; we only utilize IdleState#ALL_IDLE
    pipeline.addLast("idle-event-generator", new IdleStateHandler(timer, 0, 0, connectionTimeout));
    pipeline.addLast("idle-event-processor", new IdleEventProcessor());
    return pipeline;
}
Also used : IdleEventProcessor(co.cask.cdap.gateway.router.handlers.IdleEventProcessor) IdleStateHandler(org.jboss.netty.handler.timeout.IdleStateHandler) HttpRequestEncoder(org.jboss.netty.handler.codec.http.HttpRequestEncoder) HttpResponseDecoder(org.jboss.netty.handler.codec.http.HttpResponseDecoder) ChannelPipeline(org.jboss.netty.channel.ChannelPipeline)

Example 5 with IdleStateHandler

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

the class BasePipelineFactory method getPipeline.

@Override
public ChannelPipeline getPipeline() {
    ChannelPipeline pipeline = Channels.pipeline();
    if (timeout > 0 && !server.isConnectionless()) {
        pipeline.addLast("idleHandler", new IdleStateHandler(GlobalTimer.getTimer(), timeout, 0, 0));
    }
    pipeline.addLast("openHandler", new OpenChannelHandler(server));
    if (Context.isLoggerEnabled()) {
        pipeline.addLast("logger", new StandardLoggingHandler());
    }
    addSpecificHandlers(pipeline);
    if (geolocationHandler != null) {
        pipeline.addLast("location", geolocationHandler);
    }
    if (hemisphereHandler != null) {
        pipeline.addLast("hemisphere", hemisphereHandler);
    }
    if (distanceHandler != null) {
        pipeline.addLast("distance", distanceHandler);
    }
    if (remoteAddressHandler != null) {
        pipeline.addLast("remoteAddress", remoteAddressHandler);
    }
    addDynamicHandlers(pipeline);
    if (filterHandler != null) {
        pipeline.addLast("filter", filterHandler);
    }
    if (geocoderHandler != null) {
        pipeline.addLast("geocoder", geocoderHandler);
    }
    if (motionHandler != null) {
        pipeline.addLast("motion", motionHandler);
    }
    if (copyAttributesHandler != null) {
        pipeline.addLast("copyAttributes", copyAttributesHandler);
    }
    if (computedAttributesHandler != null) {
        pipeline.addLast("computedAttributes", computedAttributesHandler);
    }
    if (Context.getDataManager() != null) {
        pipeline.addLast("dataHandler", new DefaultDataHandler());
    }
    if (Context.getConfig().getBoolean("forward.enable")) {
        pipeline.addLast("webHandler", new WebDataHandler(Context.getConfig().getString("forward.url"), Context.getConfig().getBoolean("forward.json")));
    }
    if (commandResultEventHandler != null) {
        pipeline.addLast("CommandResultEventHandler", commandResultEventHandler);
    }
    if (overspeedEventHandler != null) {
        pipeline.addLast("OverspeedEventHandler", overspeedEventHandler);
    }
    if (fuelDropEventHandler != null) {
        pipeline.addLast("FuelDropEventHandler", fuelDropEventHandler);
    }
    if (motionEventHandler != null) {
        pipeline.addLast("MotionEventHandler", motionEventHandler);
    }
    if (geofenceEventHandler != null) {
        pipeline.addLast("GeofenceEventHandler", geofenceEventHandler);
    }
    if (alertEventHandler != null) {
        pipeline.addLast("AlertEventHandler", alertEventHandler);
    }
    if (ignitionEventHandler != null) {
        pipeline.addLast("IgnitionEventHandler", ignitionEventHandler);
    }
    if (maintenanceEventHandler != null) {
        pipeline.addLast("MaintenanceEventHandler", maintenanceEventHandler);
    }
    if (driverEventHandler != null) {
        pipeline.addLast("DriverEventHandler", driverEventHandler);
    }
    pipeline.addLast("mainHandler", new MainEventHandler());
    return pipeline;
}
Also used : 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