Search in sources :

Example 1 with ChannelHandler

use of io.netty.channel.ChannelHandler in project mongo-java-driver by mongodb.

the class NettyStream method adjustTimeout.

private void adjustTimeout(final boolean disable) {
    ChannelHandler timeoutHandler = channel.pipeline().get(READ_HANDLER_NAME);
    if (timeoutHandler != null) {
        final ReadTimeoutHandler readTimeoutHandler = (ReadTimeoutHandler) timeoutHandler;
        final ChannelHandlerContext handlerContext = channel.pipeline().context(timeoutHandler);
        EventExecutor executor = handlerContext.executor();
        if (disable) {
            if (executor.inEventLoop()) {
                readTimeoutHandler.removeTimeout(handlerContext);
            } else {
                executor.submit(new Runnable() {

                    @Override
                    public void run() {
                        readTimeoutHandler.removeTimeout(handlerContext);
                    }
                });
            }
        } else {
            if (executor.inEventLoop()) {
                readTimeoutHandler.scheduleTimeout(handlerContext);
            } else {
                executor.submit(new Runnable() {

                    @Override
                    public void run() {
                        readTimeoutHandler.scheduleTimeout(handlerContext);
                    }
                });
            }
        }
    }
}
Also used : EventExecutor(io.netty.util.concurrent.EventExecutor) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelHandler(io.netty.channel.ChannelHandler)

Example 2 with ChannelHandler

use of io.netty.channel.ChannelHandler in project camel by apache.

the class LumberjackServer method start.

/**
     * Starts the server.
     *
     * @throws InterruptedException when interrupting while connecting the socket
     */
public void start() throws InterruptedException {
    LOG.info("Starting the LUMBERJACK server (host={}, port={}).", host, port);
    // Create the group that will listen for incoming connections
    bossGroup = new NioEventLoopGroup(1);
    // Create the group that will process the connections
    workerGroup = new NioEventLoopGroup(WORKER_THREADS);
    // Create the executor service that will process the payloads without blocking netty threads
    executorService = new DefaultEventExecutorGroup(WORKER_THREADS, threadFactory);
    // Create the channel initializer
    ChannelHandler initializer = new LumberjackChannelInitializer(sslContext, executorService, messageProcessor);
    // Bootstrap the netty server
    ServerBootstrap serverBootstrap = //
    new ServerBootstrap().group(bossGroup, //
    workerGroup).channel(//
    NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG, //
    100).childHandler(//
    initializer);
    // Connect the socket
    channel = serverBootstrap.bind(host, port).sync().channel();
    LOG.info("LUMBERJACK server is started (host={}, port={}).", host, port);
}
Also used : DefaultEventExecutorGroup(io.netty.util.concurrent.DefaultEventExecutorGroup) ChannelHandler(io.netty.channel.ChannelHandler) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ServerBootstrap(io.netty.bootstrap.ServerBootstrap)

Example 3 with ChannelHandler

use of io.netty.channel.ChannelHandler in project netty by netty.

the class Http2FrameCodecTest method connectionHandlerShouldBeAddedBeforeFramingHandler.

@Test
public void connectionHandlerShouldBeAddedBeforeFramingHandler() {
    Iterator<Entry<String, ChannelHandler>> iter = channel.pipeline().iterator();
    while (iter.hasNext()) {
        ChannelHandler handler = iter.next().getValue();
        if (handler instanceof Http2ConnectionHandler) {
            break;
        }
    }
    assertTrue(iter.hasNext());
    assertThat(iter.next().getValue(), instanceOf(Http2FrameCodec.class));
}
Also used : Entry(java.util.Map.Entry) ChannelHandler(io.netty.channel.ChannelHandler) Test(org.junit.Test)

Example 4 with ChannelHandler

use of io.netty.channel.ChannelHandler in project pulsar by yahoo.

the class ServerCnx method handleConnect.

@Override
protected void handleConnect(CommandConnect connect) {
    checkArgument(state == State.Start);
    if (service.isAuthenticationEnabled()) {
        try {
            String authMethod = "none";
            if (connect.hasAuthMethodName()) {
                authMethod = connect.getAuthMethodName();
            } else if (connect.hasAuthMethod()) {
                // Legacy client is passing enum
                authMethod = connect.getAuthMethod().name().substring(10).toLowerCase();
            }
            String authData = connect.getAuthData().toStringUtf8();
            ChannelHandler sslHandler = ctx.channel().pipeline().get(PulsarChannelInitializer.TLS_HANDLER);
            SSLSession sslSession = null;
            if (sslHandler != null) {
                sslSession = ((SslHandler) sslHandler).engine().getSession();
            }
            authRole = getBrokerService().getAuthenticationService().authenticate(new AuthenticationDataCommand(authData, remoteAddress, sslSession), authMethod);
            log.info("[{}] Client successfully authenticated with {} role {}", remoteAddress, authMethod, authRole);
        } catch (AuthenticationException e) {
            String msg = "Unable to authenticate";
            log.warn("[{}] {}: {}", remoteAddress, msg, e.getMessage());
            ctx.writeAndFlush(Commands.newError(-1, ServerError.AuthenticationError, msg));
            close();
            return;
        }
    }
    if (log.isDebugEnabled()) {
        log.debug("Received CONNECT from {}", remoteAddress);
    }
    ctx.writeAndFlush(Commands.newConnected(connect));
    state = State.Connected;
    remoteEndpointProtocolVersion = connect.getProtocolVersion();
}
Also used : AuthenticationDataCommand(com.yahoo.pulsar.broker.authentication.AuthenticationDataCommand) AuthenticationException(javax.naming.AuthenticationException) SSLSession(javax.net.ssl.SSLSession) ChannelHandler(io.netty.channel.ChannelHandler) SslHandler(io.netty.handler.ssl.SslHandler)

Example 5 with ChannelHandler

use of io.netty.channel.ChannelHandler in project riposte by Nike-Inc.

the class HttpChannelInitializerTest method initChannel_does_not_add_debugLoggingHandler_if_debugChannelLifecycleLoggingEnabled_is_false.

@Test
public void initChannel_does_not_add_debugLoggingHandler_if_debugChannelLifecycleLoggingEnabled_is_false() throws SSLException {
    // given
    HttpChannelInitializer hci = basicHttpChannelInitializer(new JdkSslClientContext(), 42, 100, false, mock(RequestValidator.class), createRequestAndResponseFilterMock());
    // when
    hci.initChannel(socketChannelMock);
    // then
    ArgumentCaptor<ChannelHandler> channelHandlerArgumentCaptor = ArgumentCaptor.forClass(ChannelHandler.class);
    verify(channelPipelineMock, atLeastOnce()).addLast(anyString(), channelHandlerArgumentCaptor.capture());
    List<ChannelHandler> handlers = channelHandlerArgumentCaptor.getAllValues();
    assertThat(findChannelHandler(handlers, LoggingHandler.class), nullValue());
}
Also used : LoggingHandler(io.netty.handler.logging.LoggingHandler) RequestValidator(com.nike.riposte.server.error.validation.RequestValidator) JdkSslClientContext(io.netty.handler.ssl.JdkSslClientContext) ChannelHandler(io.netty.channel.ChannelHandler) Test(org.junit.Test)

Aggregations

ChannelHandler (io.netty.channel.ChannelHandler)186 Test (org.junit.Test)88 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)44 Channel (io.netty.channel.Channel)26 ChannelPipeline (io.netty.channel.ChannelPipeline)25 SslHandler (io.netty.handler.ssl.SslHandler)25 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)22 FilterChainMatchingHandler (io.grpc.xds.FilterChainMatchingProtocolNegotiators.FilterChainMatchingHandler)20 ChannelFuture (io.netty.channel.ChannelFuture)20 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)20 FilterChainSelector (io.grpc.xds.FilterChainMatchingProtocolNegotiators.FilterChainMatchingHandler.FilterChainSelector)19 ChannelHandlerAdapter (io.netty.channel.ChannelHandlerAdapter)18 DownstreamTlsContext (io.grpc.xds.EnvoyServerProtoData.DownstreamTlsContext)17 FilterChain (io.grpc.xds.EnvoyServerProtoData.FilterChain)17 InetSocketAddress (java.net.InetSocketAddress)16 Test (org.junit.jupiter.api.Test)16 LineBasedFrameDecoder (io.netty.handler.codec.LineBasedFrameDecoder)12 AtomicReference (java.util.concurrent.atomic.AtomicReference)12 Bootstrap (io.netty.bootstrap.Bootstrap)11 ArrayList (java.util.ArrayList)11