Search in sources :

Example 26 with Channel

use of io.netty.channel.Channel in project neo4j by neo4j.

the class FragmentedMessageDeliveryTest method testPermutation.

private void testPermutation(byte[] unfragmented, ByteBuf[] fragments) throws Exception {
    // Given
    BoltStateMachine machine = mock(BoltStateMachine.class);
    Channel ch = mock(Channel.class);
    when(ch.alloc()).thenReturn(UnpooledByteBufAllocator.DEFAULT);
    ChannelHandlerContext ctx = mock(ChannelHandlerContext.class);
    when(ctx.channel()).thenReturn(ch);
    BoltProtocolV1 protocol = new BoltProtocolV1(new SynchronousBoltWorker(machine), ch, NullLogService.getInstance());
    // When data arrives split up according to the current permutation
    for (ByteBuf fragment : fragments) {
        fragment.readerIndex(0).retain();
        protocol.handle(ctx, fragment);
    }
    // Then the session should've received the specified messages, and the protocol should be in a nice clean state
    try {
        verify(machine).run(eq("Mjölnir"), anyMapOf(String.class, Object.class), any(BoltResponseHandler.class));
    } catch (AssertionError e) {
        throw new AssertionError("Failed to handle fragmented delivery.\n" + "Messages: " + Arrays.toString(messages) + "\n" + "Chunk size: " + chunkSize + "\n" + "Serialized data delivered in fragments: " + describeFragments(fragments) + "\n" + "Unfragmented data: " + HexPrinter.hex(unfragmented) + "\n", e);
    } finally {
        // To avoid buffer leak errors
        protocol.close();
    }
}
Also used : SynchronousBoltWorker(org.neo4j.bolt.v1.runtime.SynchronousBoltWorker) BoltStateMachine(org.neo4j.bolt.v1.runtime.BoltStateMachine) RecordingByteChannel(org.neo4j.bolt.v1.messaging.RecordingByteChannel) Channel(io.netty.channel.Channel) BoltResponseHandler(org.neo4j.bolt.v1.runtime.BoltResponseHandler) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) BoltProtocolV1(org.neo4j.bolt.v1.transport.BoltProtocolV1) ByteBuf(io.netty.buffer.ByteBuf)

Example 27 with Channel

use of io.netty.channel.Channel in project neo4j by neo4j.

the class SocketTransportHandlerTest method channelHandlerContextMock.

private static ChannelHandlerContext channelHandlerContextMock() {
    Channel channel = mock(Channel.class);
    ChannelHandlerContext context = mock(ChannelHandlerContext.class);
    when(context.channel()).thenReturn(channel);
    when(channel.alloc()).thenReturn(UnpooledByteBufAllocator.DEFAULT);
    when(context.alloc()).thenReturn(UnpooledByteBufAllocator.DEFAULT);
    return context;
}
Also used : Channel(io.netty.channel.Channel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext)

Example 28 with Channel

use of io.netty.channel.Channel in project jersey by jersey.

the class App method main.

public static void main(String[] args) {
    try {
        System.out.println("\"Hello World\" Jersey Example App on Netty container.");
        ResourceConfig resourceConfig = new ResourceConfig(HelloWorldResource.class);
        final Channel server = NettyHttpContainerProvider.createHttp2Server(BASE_URI, resourceConfig, null);
        Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {

            @Override
            public void run() {
                server.close();
            }
        }));
        System.out.println(String.format("Application started. (HTTP/2 enabled!)\nTry out %s%s\nStop the application using " + "CTRL+C.", BASE_URI, ROOT_PATH));
        Thread.currentThread().join();
    } catch (InterruptedException ex) {
        Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
    }
}
Also used : Channel(io.netty.channel.Channel) ResourceConfig(org.glassfish.jersey.server.ResourceConfig)

Example 29 with Channel

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

the class SocketSslGreetingTest method testSslGreeting.

public void testSslGreeting(ServerBootstrap sb, Bootstrap cb) throws Throwable {
    final ServerHandler sh = new ServerHandler();
    final ClientHandler ch = new ClientHandler();
    sb.childHandler(new ChannelInitializer<Channel>() {

        @Override
        public void initChannel(Channel sch) throws Exception {
            ChannelPipeline p = sch.pipeline();
            p.addLast(serverCtx.newHandler(sch.alloc()));
            p.addLast(new LoggingHandler(LOG_LEVEL));
            p.addLast(sh);
        }
    });
    cb.handler(new ChannelInitializer<Channel>() {

        @Override
        public void initChannel(Channel sch) throws Exception {
            ChannelPipeline p = sch.pipeline();
            p.addLast(clientCtx.newHandler(sch.alloc()));
            p.addLast(new LoggingHandler(LOG_LEVEL));
            p.addLast(ch);
        }
    });
    Channel sc = sb.bind().sync().channel();
    Channel cc = cb.connect().sync().channel();
    ch.latch.await();
    sh.channel.close().awaitUninterruptibly();
    cc.close().awaitUninterruptibly();
    sc.close().awaitUninterruptibly();
    if (sh.exception.get() != null && !(sh.exception.get() instanceof IOException)) {
        throw sh.exception.get();
    }
    if (ch.exception.get() != null && !(ch.exception.get() instanceof IOException)) {
        throw ch.exception.get();
    }
    if (sh.exception.get() != null) {
        throw sh.exception.get();
    }
    if (ch.exception.get() != null) {
        throw ch.exception.get();
    }
}
Also used : LoggingHandler(io.netty.handler.logging.LoggingHandler) Channel(io.netty.channel.Channel) IOException(java.io.IOException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) SSLPeerUnverifiedException(javax.net.ssl.SSLPeerUnverifiedException) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 30 with Channel

use of io.netty.channel.Channel in project neo4j by neo4j.

the class NonBlockingChannel method ensureConnected.

private void ensureConnected() throws IOException {
    if (nettyChannel != null && !nettyChannel.isOpen()) {
        nettyChannel = null;
    }
    while (nettyChannel == null && stillRunning) {
        ChannelFuture channelFuture = bootstrap.connect(destination);
        Channel channel = channelFuture.awaitUninterruptibly().channel();
        if (channelFuture.isSuccess()) {
            channel.flush();
            nettyChannel = channel;
            log.info("Connected: " + nettyChannel);
        } else {
            channel.close();
            parkNanos(MILLISECONDS.toNanos(CONNECT_BACKOFF_IN_MS));
        }
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) Channel(io.netty.channel.Channel)

Aggregations

Channel (io.netty.channel.Channel)886 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)226 ChannelFuture (io.netty.channel.ChannelFuture)204 Test (org.junit.Test)203 Bootstrap (io.netty.bootstrap.Bootstrap)199 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)191 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)177 InetSocketAddress (java.net.InetSocketAddress)165 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)151 EventLoopGroup (io.netty.channel.EventLoopGroup)142 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)138 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)132 IOException (java.io.IOException)126 ByteBuf (io.netty.buffer.ByteBuf)112 SocketChannel (io.netty.channel.socket.SocketChannel)106 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)99 ChannelPipeline (io.netty.channel.ChannelPipeline)98 CountDownLatch (java.util.concurrent.CountDownLatch)96 LocalChannel (io.netty.channel.local.LocalChannel)93 LocalServerChannel (io.netty.channel.local.LocalServerChannel)89