Example usage for io.netty.channel.embedded EmbeddedChannel EmbeddedChannel

List of usage examples for io.netty.channel.embedded EmbeddedChannel EmbeddedChannel

Introduction

In this page you can find the example usage for io.netty.channel.embedded EmbeddedChannel EmbeddedChannel.

Prototype

public EmbeddedChannel(ChannelId channelId, boolean hasDisconnect, final ChannelConfig config,
        final ChannelHandler... handlers) 

Source Link

Document

Create a new instance with the channel ID set to the given ID and the pipeline initialized with the specified handlers.

Usage

From source file:org.opendaylight.controller.netconf.impl.MessageParserTest.java

License:Open Source License

@Test
public void testChunkedFramingMechanismOnPipeline() throws Exception {
    EmbeddedChannel testChunkChannel = new EmbeddedChannel(
            FramingMechanismHandlerFactory.createHandler(FramingMechanism.CHUNK),
            new NetconfMessageToXMLEncoder(),

            new NetconfChunkAggregator(), new NetconfXMLToMessageDecoder());

    testChunkChannel.writeOutbound(this.msg);
    Queue<Object> messages = testChunkChannel.outboundMessages();
    assertFalse(messages.isEmpty());// w w  w.j av a 2  s  .c  om

    final NetconfMessageToXMLEncoder enc = new NetconfMessageToXMLEncoder();
    final ByteBuf out = Unpooled.buffer();
    enc.encode(null, msg, out);
    int msgLength = out.readableBytes();

    int chunkCount = msgLength / ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE;
    if ((msgLength % ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE) != 0) {
        chunkCount++;
    }
    for (int i = 1; i <= chunkCount; i++) {
        ByteBuf recievedOutbound = (ByteBuf) messages.poll();
        int exptHeaderLength = ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE;
        if (i == chunkCount) {
            exptHeaderLength = msgLength - (ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE * (i - 1));
            byte[] eom = new byte[NetconfMessageConstants.END_OF_CHUNK.length];
            recievedOutbound.getBytes(
                    recievedOutbound.readableBytes() - NetconfMessageConstants.END_OF_CHUNK.length, eom);
            assertArrayEquals(NetconfMessageConstants.END_OF_CHUNK, eom);
        }

        byte[] header = new byte[String.valueOf(exptHeaderLength).length()
                + NetconfMessageConstants.MIN_HEADER_LENGTH - 1];
        recievedOutbound.getBytes(0, header);
        NetconfMessageHeader messageHeader = NetconfMessageHeader.fromBytes(header);
        assertEquals(exptHeaderLength, messageHeader.getLength());

        testChunkChannel.writeInbound(recievedOutbound);
    }
    assertTrue(messages.isEmpty());

    NetconfMessage receivedMessage = (NetconfMessage) testChunkChannel.readInbound();
    assertNotNull(receivedMessage);
    assertXMLEqual(this.msg.getDocument(), receivedMessage.getDocument());
}

From source file:org.opendaylight.controller.netconf.impl.MessageParserTest.java

License:Open Source License

@Test
public void testEOMFramingMechanismOnPipeline() throws Exception {
    EmbeddedChannel testChunkChannel = new EmbeddedChannel(
            FramingMechanismHandlerFactory.createHandler(FramingMechanism.EOM),
            new NetconfMessageToXMLEncoder(), new NetconfEOMAggregator(), new NetconfXMLToMessageDecoder());

    testChunkChannel.writeOutbound(this.msg);
    ByteBuf recievedOutbound = (ByteBuf) testChunkChannel.readOutbound();

    byte[] eom = new byte[NetconfMessageConstants.END_OF_MESSAGE.length];
    recievedOutbound.getBytes(recievedOutbound.readableBytes() - NetconfMessageConstants.END_OF_MESSAGE.length,
            eom);/*from  w ww.  ja v  a  2  s  .co m*/
    assertArrayEquals(NetconfMessageConstants.END_OF_MESSAGE, eom);

    testChunkChannel.writeInbound(recievedOutbound);
    NetconfMessage receivedMessage = (NetconfMessage) testChunkChannel.readInbound();
    assertNotNull(receivedMessage);
    assertXMLEqual(this.msg.getDocument(), receivedMessage.getDocument());
}

From source file:org.opendaylight.netconf.impl.MessageParserTest.java

License:Open Source License

@Test
public void testChunkedFramingMechanismOnPipeline() throws Exception {
    EmbeddedChannel testChunkChannel = new EmbeddedChannel(
            FramingMechanismHandlerFactory.createHandler(FramingMechanism.CHUNK),
            new NetconfMessageToXMLEncoder(),

            new NetconfChunkAggregator(), new NetconfXMLToMessageDecoder());

    testChunkChannel.writeOutbound(this.msg);
    Queue<Object> messages = testChunkChannel.outboundMessages();
    assertFalse(messages.isEmpty());//from  w w w.  ja  v  a  2 s.co  m

    final NetconfMessageToXMLEncoder enc = new NetconfMessageToXMLEncoder();
    final ByteBuf out = Unpooled.buffer();
    enc.encode(null, msg, out);
    int msgLength = out.readableBytes();

    int chunkCount = msgLength / ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE;
    if ((msgLength % ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE) != 0) {
        chunkCount++;
    }
    for (int i = 1; i <= chunkCount; i++) {
        ByteBuf recievedOutbound = (ByteBuf) messages.poll();
        int exptHeaderLength = ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE;
        if (i == chunkCount) {
            exptHeaderLength = msgLength - (ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE * (i - 1));
            byte[] eom = new byte[NetconfMessageConstants.END_OF_CHUNK.length];
            recievedOutbound.getBytes(
                    recievedOutbound.readableBytes() - NetconfMessageConstants.END_OF_CHUNK.length, eom);
            assertArrayEquals(NetconfMessageConstants.END_OF_CHUNK, eom);
        }

        byte[] header = new byte[String.valueOf(exptHeaderLength).length()
                + NetconfMessageConstants.MIN_HEADER_LENGTH - 1];
        recievedOutbound.getBytes(0, header);
        assertEquals(exptHeaderLength, getHeaderLength(header));

        testChunkChannel.writeInbound(recievedOutbound);
    }
    assertTrue(messages.isEmpty());

    NetconfMessage receivedMessage = (NetconfMessage) testChunkChannel.readInbound();
    assertNotNull(receivedMessage);
    assertXMLEqual(this.msg.getDocument(), receivedMessage.getDocument());
}

From source file:reactor.ipc.netty.NettyOutboundTest.java

License:Open Source License

@Test
public void sendFileWithTlsUsesChunkedFile()
        throws URISyntaxException, NoSuchAlgorithmException, SSLException, CertificateException {
    SelfSignedCertificate ssc = new SelfSignedCertificate();
    SslContext sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
    final SslHandler sslHandler = sslCtx.newHandler(ByteBufAllocator.DEFAULT);

    List<Class<?>> messageWritten = new ArrayList<>(2);
    List<Object> clearMessages = new ArrayList<>(2);

    EmbeddedChannel channel = new EmbeddedChannel(
            //outbound: pipeline reads inverted
            //bytes are encrypted
            sslHandler,/*from   w w  w  .j a v  a 2  s  .c  om*/
            //capture the chunks unencrypted, transform as Strings:
            new MessageToMessageEncoder<ByteBuf>() {
                @Override
                protected void encode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out)
                        throws Exception {
                    clearMessages.add(msg.toString(CharsetUtil.UTF_8));
                    out.add(msg.retain()); //the encoder will release the buffer, make sure it is retained for SslHandler
                }
            },
            //transform the ChunkedFile into ByteBuf chunks:
            new ChunkedWriteHandler(),
            //helps to ensure a ChunkedFile was written outs
            new MessageToMessageEncoder<Object>() {
                @Override
                protected void encode(ChannelHandlerContext ctx, Object msg, List<Object> out)
                        throws Exception {
                    messageWritten.add(msg.getClass());
                    //passing the ChunkedFile through this method releases it, which is undesired
                    ReferenceCountUtil.retain(msg);
                    out.add(msg);
                }
            });

    NettyContext mockContext = () -> channel;
    NettyOutbound outbound = new NettyOutbound() {
        @Override
        public NettyContext context() {
            return mockContext;
        }

        @Override
        public FileChunkedStrategy getFileChunkedStrategy() {
            return FILE_CHUNKED_STRATEGY_1024_NOPIPELINE;
        }
    };
    channel.writeOneOutbound(1);

    try {
        outbound.sendFile(Paths.get(getClass().getResource("/largeFile.txt").toURI())).then()
                .block(Duration.ofSeconds(1)); //TODO investigate why this hangs
    } catch (IllegalStateException e) {
        if (!"Timeout on blocking read for 1000 MILLISECONDS".equals(e.getMessage()))
            throw e;
        System.err.println(e);
    }

    assertThat(messageWritten).containsExactly(Integer.class, ChunkedFile.class);

    assertThat(clearMessages).hasSize(2).element(0).asString().startsWith(
            "This is an UTF-8 file that is larger than 1024 bytes.\nIt contains accents like .\nGARBAGE")
            .endsWith("1024 mark here ->");
    assertThat(clearMessages).element(1).asString().startsWith("<- 1024 mark here").endsWith("End of File");
}