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, 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:alluxio.worker.netty.CodecTest.java

License:Apache License

@Before
public void before() throws Exception {
    ResourceLeakDetector.setLevel(ResourceLeakDetector.Level.ADVANCED);
    mChannel = new EmbeddedChannel(RPCMessage.createFrameDecoder(), new RPCMessageDecoder(),
            new RPCMessageEncoder());
}

From source file:com.cloudera.livy.client.local.rpc.Rpc.java

License:Apache License

@VisibleForTesting
static Rpc createEmbedded(RpcDispatcher dispatcher) {
    EmbeddedChannel c = new EmbeddedChannel(new LoggingHandler(Rpc.class),
            new KryoMessageCodec(0, MessageHeader.class, NullMessage.class), dispatcher);
    Rpc rpc = new Rpc(new LocalConf(null), c, ImmediateEventExecutor.INSTANCE);
    rpc.dispatcher = dispatcher;//from   ww w .j av  a  2  s.c  o  m
    return rpc;
}

From source file:com.cloudera.livy.rsc.rpc.Rpc.java

License:Apache License

static Rpc createEmbedded(RpcDispatcher dispatcher) {
    EmbeddedChannel c = new EmbeddedChannel(new LoggingHandler(Rpc.class),
            new KryoMessageCodec(0, MessageHeader.class, NullMessage.class), dispatcher);
    Rpc rpc = new Rpc(new RSCConf(null), c, ImmediateEventExecutor.INSTANCE);
    rpc.dispatcher = dispatcher;//from w  w w  .  ja  v  a 2 s  .  c o  m
    return rpc;
}

From source file:com.difference.historybook.proxy.littleproxy.LittleProxy.java

License:Apache License

private HttpFiltersSource getFiltersSource() {
    return new HttpFiltersSourceAdapter() {
        @Override//from   w  w  w  .  ja va2  s .c o  m
        public HttpFilters filterRequest(HttpRequest originalRequest) {

            return new HttpFiltersAdapter(originalRequest) {
                private final ProxyFilter filter = filterFactory != null ? filterFactory.getInstance() : null;
                private EmbeddedChannel bufferChannel = null;

                @Override
                public HttpResponse clientToProxyRequest(HttpObject httpObject) {
                    if (filter != null && httpObject instanceof DefaultHttpRequest) {
                        filter.processRequest(new LittleProxyRequest((DefaultHttpRequest) httpObject));
                    }
                    return null;
                }

                @Override
                public HttpObject proxyToClientResponse(HttpObject httpObject) {
                    if (httpObject instanceof DefaultHttpResponse) {
                        DefaultHttpResponse response = (DefaultHttpResponse) httpObject;
                        Map<String, String> headers = new HashMap<>();
                        response.headers().forEach(e -> {
                            headers.put(e.getKey(), e.getValue());
                        });

                        if (selector != null && selector.test(new ProxyTransactionInfo(originalRequest.getUri(),
                                response.getStatus().code(), headers))) {
                            bufferChannel = new EmbeddedChannel(new HttpResponseDecoder(),
                                    new HttpContentDecompressor(), new HttpObjectAggregator(maxBufferSize));

                            DefaultHttpResponse copiedResponse = new DefaultHttpResponse(
                                    response.getProtocolVersion(), response.getStatus());
                            copiedResponse.headers().add(response.headers());

                            bufferChannel.writeInbound(copiedResponse);
                        }
                    } else if (httpObject instanceof DefaultHttpContent && bufferChannel != null) {
                        DefaultHttpContent httpContent = (DefaultHttpContent) httpObject;
                        bufferChannel.writeInbound(httpContent.copy());

                        if (ProxyUtils.isLastChunk(httpObject))
                            processChunkedResponse();
                    } else if (httpObject instanceof LastHttpContent && bufferChannel != null) {
                        LastHttpContent httpContent = (LastHttpContent) httpObject;
                        bufferChannel.writeInbound(httpContent.copy());
                        processChunkedResponse();
                    } else if (filter != null && httpObject instanceof FullHttpResponse) {
                        filter.processResponse(new LittleProxyResponse((FullHttpResponse) httpObject));
                    }
                    return httpObject;
                };

                private void processChunkedResponse() {
                    bufferChannel.flush();
                    bufferChannel.finish();
                    FullHttpResponse fullResponse = (FullHttpResponse) bufferChannel.readInbound();
                    filter.processResponse(new LittleProxyResponse(fullResponse));
                    bufferChannel = null;
                }
            };
        };
    };
}

From source file:com.linkedin.r2.transport.http.client.TestRAPClientCodec.java

License:Apache License

@Test
public void testDecodeException() {
    final EmbeddedChannel ch = new EmbeddedChannel(new HttpClientCodec(), new HttpObjectAggregator(65536),
            new RAPClientCodec());

    // When we received an invalid message, a decode exception should be thrown out of the
    // end of netty pipeline.
    String junk = "Not a HTTP message\r\n";
    try {/* www .j av a 2 s  . co  m*/
        ch.writeInbound(Unpooled.copiedBuffer(junk, CHARSET));
        Assert.fail("Should have thrown decode exception");
    } catch (Exception ex) {
        // expected.
    }
    ch.finish();
}

From source file:com.spotify.netty.handler.codec.zmtp.ProtocolViolationTests.java

License:Apache License

@Before
public void setup() {
    ZMTPSession session = new ZMTPSession(Addressed, identity.getBytes());
    serverChannel = new EmbeddedChannel(new ZMTPFramingDecoder(session), new ZMTPFramingEncoder(session),
            mockHandler);//from  w  w w  . j  a  va2 s.  c om
}

From source file:org.apache.hive.spark.client.rpc.Rpc.java

License:Apache License

@VisibleForTesting
static Rpc createEmbedded(RpcDispatcher dispatcher) {
    EmbeddedChannel c = new EmbeddedChannel(new LoggingHandler(Rpc.class),
            new KryoMessageCodec(0, MessageHeader.class, NullMessage.class), dispatcher);
    Rpc rpc = new Rpc(new RpcConfiguration(Collections.<String, String>emptyMap()), c,
            ImmediateEventExecutor.INSTANCE);
    rpc.dispatcher = dispatcher;/* w  ww  .  j a  v a2 s  .  c  o  m*/
    return rpc;
}

From source file:org.codice.alliance.video.stream.mpegts.netty.H262Test.java

License:Open Source License

@Test
public void testIDRFrameCount() throws Exception {
    EmbeddedChannel channel = new EmbeddedChannel(new MTSPacketToPESPacketDecoder(),
            new PESPacketToApplicationDataDecoder(), new DecodedStreamDataHandler(packetBuffer));

    InputStream inputStream = getInputStream("/Closed_Caption_EIA_MPEG2.ts");
    byte[] buffer = new byte[TS_SIZE];
    int c;//  w ww  . j av a2 s.  c o  m
    while ((c = inputStream.read(buffer)) != -1) {
        if (c == TS_SIZE) {
            ResettableMTSSource src = MTSSources.from(ByteSource.wrap(buffer));
            MTSPacket packet = null;
            try {
                packet = src.nextPacket();
            } catch (IOException e) {
                LOGGER.debug("unable to parse mpegst packet", e);
            }

            if (packet != null) {
                channel.writeInbound(packet);
            }
        }
    }
    verify(packetBuffer, times(37)).frameComplete(PacketBuffer.FrameType.IDR);
}

From source file:org.codice.alliance.video.stream.mpegts.netty.TestH262.java

License:Open Source License

@Test
public void testIDRFrameCount() throws Exception {
    EmbeddedChannel channel = new EmbeddedChannel(new MTSPacketToPESPacketDecoder(),
            new PESPacketToApplicationDataDecoder(), new DecodedStreamDataHandler(packetBuffer));

    InputStream inputStream = getInputStream("/Closed_Caption_EIA_MPEG2.ts");
    byte[] buffer = new byte[TS_SIZE];
    int c;/*from  w w  w. j a  v a 2s  . c o m*/
    while ((c = inputStream.read(buffer)) != -1) {
        if (c == TS_SIZE) {
            ResettableMTSSource src = MTSSources.from(ByteSource.wrap(buffer));
            MTSPacket packet = null;
            try {
                packet = src.nextPacket();
            } catch (IOException e) {
                LOGGER.warn("unable to parse mpegst packet", e);
            }

            if (packet != null) {
                channel.writeInbound(packet);
            }
        }
    }
    verify(packetBuffer, times(37)).frameComplete(PacketBuffer.FrameType.IDR);
}

From source file:org.elasticsearch.http.netty4.pipelining.Netty4HttpPipeliningHandlerTests.java

License:Apache License

public void testThatPipeliningWorksWithChunkedRequests() throws InterruptedException {
    final int numberOfRequests = randomIntBetween(2, 128);
    final EmbeddedChannel embeddedChannel = new EmbeddedChannel(new AggregateUrisAndHeadersHandler(),
            new HttpPipeliningHandler(numberOfRequests), new WorkEmulatorHandler());

    for (int i = 0; i < numberOfRequests; i++) {
        final DefaultHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET,
                "/" + i);
        embeddedChannel.writeInbound(request);
        embeddedChannel.writeInbound(LastHttpContent.EMPTY_LAST_CONTENT);
    }// w w  w .ja  va  2 s .c om

    final List<CountDownLatch> latches = new ArrayList<>();
    for (int i = numberOfRequests - 1; i >= 0; i--) {
        latches.add(finishRequest(Integer.toString(i)));
    }

    for (final CountDownLatch latch : latches) {
        latch.await();
    }

    embeddedChannel.flush();

    for (int i = 0; i < numberOfRequests; i++) {
        assertReadHttpMessageHasContent(embeddedChannel, Integer.toString(i));
    }

    assertTrue(embeddedChannel.isOpen());
}