use of io.netty.channel.nio.NioEventLoopGroup in project moco by dreamhead.
the class MocoClient method run.
public void run(final String host, final int port, final ChannelHandler pipelineFactory) {
EventLoopGroup group = new NioEventLoopGroup();
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(group).channel(NioSocketChannel.class).remoteAddress(host, port).option(ChannelOption.TCP_NODELAY, true).handler(pipelineFactory);
try {
Channel channel = bootstrap.connect().sync().channel();
ChannelFuture future = channel.closeFuture().sync();
future.addListener(ChannelFutureListener.CLOSE);
} catch (InterruptedException e) {
throw new MocoException(e);
} finally {
group.shutdownGracefully();
}
}
use of io.netty.channel.nio.NioEventLoopGroup in project netty by netty.
the class HttpClientCodecTest method testServerCloseSocketInputProvidesData.
@Test
public void testServerCloseSocketInputProvidesData() throws InterruptedException {
ServerBootstrap sb = new ServerBootstrap();
Bootstrap cb = new Bootstrap();
final CountDownLatch serverChannelLatch = new CountDownLatch(1);
final CountDownLatch responseRecievedLatch = new CountDownLatch(1);
try {
sb.group(new NioEventLoopGroup(2));
sb.channel(NioServerSocketChannel.class);
sb.childHandler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) throws Exception {
// Don't use the HttpServerCodec, because we don't want to have content-length or anything added.
ch.pipeline().addLast(new HttpRequestDecoder(4096, 8192, 8192, true));
ch.pipeline().addLast(new HttpObjectAggregator(4096));
ch.pipeline().addLast(new SimpleChannelInboundHandler<FullHttpRequest>() {
@Override
protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest msg) {
// This is just a simple demo...don't block in IO
assertTrue(ctx.channel() instanceof SocketChannel);
final SocketChannel sChannel = (SocketChannel) ctx.channel();
/**
* The point of this test is to not add any content-length or content-encoding headers
* and the client should still handle this.
* See <a href="https://tools.ietf.org/html/rfc7230#section-3.3.3">RFC 7230, 3.3.3</a>.
*/
sChannel.writeAndFlush(Unpooled.wrappedBuffer(("HTTP/1.0 200 OK\r\n" + "Date: Fri, 31 Dec 1999 23:59:59 GMT\r\n" + "Content-Type: text/html\r\n\r\n").getBytes(CharsetUtil.ISO_8859_1))).addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
assertTrue(future.isSuccess());
sChannel.writeAndFlush(Unpooled.wrappedBuffer("<html><body>hello half closed!</body></html>\r\n".getBytes(CharsetUtil.ISO_8859_1))).addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
assertTrue(future.isSuccess());
sChannel.shutdownOutput();
}
});
}
});
}
});
serverChannelLatch.countDown();
}
});
cb.group(new NioEventLoopGroup(1));
cb.channel(NioSocketChannel.class);
cb.option(ChannelOption.ALLOW_HALF_CLOSURE, true);
cb.handler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) throws Exception {
ch.pipeline().addLast(new HttpClientCodec(4096, 8192, 8192, true, true));
ch.pipeline().addLast(new HttpObjectAggregator(4096));
ch.pipeline().addLast(new SimpleChannelInboundHandler<FullHttpResponse>() {
@Override
protected void channelRead0(ChannelHandlerContext ctx, FullHttpResponse msg) {
responseRecievedLatch.countDown();
}
});
}
});
Channel serverChannel = sb.bind(new InetSocketAddress(0)).sync().channel();
int port = ((InetSocketAddress) serverChannel.localAddress()).getPort();
ChannelFuture ccf = cb.connect(new InetSocketAddress(NetUtil.LOCALHOST, port));
assertTrue(ccf.awaitUninterruptibly().isSuccess());
Channel clientChannel = ccf.channel();
assertTrue(serverChannelLatch.await(5, SECONDS));
clientChannel.writeAndFlush(new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/"));
assertTrue(responseRecievedLatch.await(5, SECONDS));
} finally {
sb.config().group().shutdownGracefully();
sb.config().childGroup().shutdownGracefully();
cb.config().group().shutdownGracefully();
}
}
use of io.netty.channel.nio.NioEventLoopGroup in project netty by netty.
the class UdtNetty method main.
public static void main(final String[] args) throws Exception {
log.info("init");
TrafficControl.delay(0);
final AtomicBoolean isOn = new AtomicBoolean(true);
final InetSocketAddress addr1 = UnitHelp.localSocketAddress();
final InetSocketAddress addr2 = UnitHelp.localSocketAddress();
final ChannelHandler handler1 = new EchoMessageHandler(rate, size);
final ChannelHandler handler2 = new EchoMessageHandler(null, size);
final NioEventLoopGroup group1 = new NioEventLoopGroup(1, Executors.defaultThreadFactory(), NioUdtProvider.MESSAGE_PROVIDER);
final NioEventLoopGroup group2 = new NioEventLoopGroup(1, Executors.defaultThreadFactory(), NioUdtProvider.MESSAGE_PROVIDER);
final Bootstrap peerBoot1 = new Bootstrap();
peerBoot1.group(group1).channelFactory(NioUdtProvider.MESSAGE_RENDEZVOUS).localAddress(addr1).remoteAddress(addr2).handler(handler1);
final Bootstrap peerBoot2 = new Bootstrap();
peerBoot2.group(group2).channelFactory(NioUdtProvider.MESSAGE_RENDEZVOUS).localAddress(addr2).remoteAddress(addr1).handler(handler2);
final ChannelFuture peerFuture1 = peerBoot1.connect();
final ChannelFuture peerFuture2 = peerBoot2.connect();
CustomReporter.enable(3, TimeUnit.SECONDS);
Thread.sleep(time);
isOn.set(false);
Thread.sleep(1000);
peerFuture1.channel().close().sync();
peerFuture2.channel().close().sync();
Thread.sleep(1000);
group1.shutdownGracefully();
group2.shutdownGracefully();
Metrics.defaultRegistry().shutdown();
TrafficControl.delay(0);
log.info("done");
}
use of io.netty.channel.nio.NioEventLoopGroup in project netty by netty.
the class NioUdtMessageRendezvousChannelTest method basicEcho.
/**
* verify basic echo message rendezvous
*
* FIXME: Re-enable after making it pass on Windows without unncessary tight loop.
* https://github.com/netty/netty/issues/2853
*/
@Test(timeout = 10 * 1000)
@Ignore
public void basicEcho() throws Exception {
final int messageSize = 64 * 1024;
final int transferLimit = messageSize * 16;
final Meter rate1 = Metrics.newMeter(NioUdtMessageRendezvousChannelTest.class, "send rate", "bytes", TimeUnit.SECONDS);
final Meter rate2 = Metrics.newMeter(NioUdtMessageRendezvousChannelTest.class, "send rate", "bytes", TimeUnit.SECONDS);
final InetSocketAddress addr1 = UnitHelp.localSocketAddress();
final InetSocketAddress addr2 = UnitHelp.localSocketAddress();
final EchoMessageHandler handler1 = new EchoMessageHandler(rate1, messageSize);
final EchoMessageHandler handler2 = new EchoMessageHandler(rate2, messageSize);
final NioEventLoopGroup group1 = new NioEventLoopGroup(1, Executors.defaultThreadFactory(), NioUdtProvider.MESSAGE_PROVIDER);
final NioEventLoopGroup group2 = new NioEventLoopGroup(1, Executors.defaultThreadFactory(), NioUdtProvider.MESSAGE_PROVIDER);
final Bootstrap boot1 = new Bootstrap();
boot1.group(group1).channelFactory(NioUdtProvider.MESSAGE_RENDEZVOUS).localAddress(addr1).remoteAddress(addr2).handler(handler1);
final Bootstrap boot2 = new Bootstrap();
boot2.group(group2).channelFactory(NioUdtProvider.MESSAGE_RENDEZVOUS).localAddress(addr2).remoteAddress(addr1).handler(handler2);
final ChannelFuture connectFuture1 = boot1.connect();
final ChannelFuture connectFuture2 = boot2.connect();
while (handler1.meter().count() < transferLimit && handler2.meter().count() < transferLimit) {
log.info("progress : {} {}", handler1.meter().count(), handler2.meter().count());
Thread.sleep(1000);
}
connectFuture1.channel().close().sync();
connectFuture2.channel().close().sync();
log.info("handler1 : {}", handler1.meter().count());
log.info("handler2 : {}", handler2.meter().count());
assertTrue(handler1.meter().count() >= transferLimit);
assertTrue(handler2.meter().count() >= transferLimit);
assertEquals(handler1.meter().count(), handler2.meter().count());
group1.shutdownGracefully();
group2.shutdownGracefully();
group1.terminationFuture().sync();
group2.terminationFuture().sync();
}
use of io.netty.channel.nio.NioEventLoopGroup in project pulsar by yahoo.
the class DiscoveryServiceTest method testClientServerConnection.
/**
* It verifies: client connects to Discovery-service and receives discovery response successfully.
*
* @throws Exception
*/
@Test
public void testClientServerConnection() throws Exception {
addBrokerToZk(2);
// 1. client connects to DiscoveryService, 2. Client receive service-lookup response
final int messageTransfer = 2;
final CountDownLatch latch = new CountDownLatch(messageTransfer);
NioEventLoopGroup workerGroup = connectToService(service.getServiceUrl(), latch, false);
try {
assertTrue(latch.await(1, TimeUnit.SECONDS));
} catch (InterruptedException e) {
fail("should have received lookup response message from server", e);
}
workerGroup.shutdownGracefully();
}
Aggregations