use of io.netty.channel.ChannelInitializer in project netty by netty.
the class OcspTest method newServerHandler.
private static ChannelHandler newServerHandler(final SslContext context, final byte[] response, final ChannelHandler handler) {
return new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
SslHandler sslHandler = context.newHandler(ch.alloc());
if (response != null) {
ReferenceCountedOpenSslEngine engine = (ReferenceCountedOpenSslEngine) sslHandler.engine();
engine.setOcspResponse(response);
}
pipeline.addLast(sslHandler);
if (handler != null) {
pipeline.addLast(handler);
}
}
};
}
use of io.netty.channel.ChannelInitializer in project netty by netty.
the class LocalEcho method main.
public static void main(String[] args) throws Exception {
// Address to bind on / connect to.
final LocalAddress addr = new LocalAddress(PORT);
EventLoopGroup serverGroup = new DefaultEventLoopGroup();
// NIO event loops are also OK
EventLoopGroup clientGroup = new NioEventLoopGroup();
try {
// Note that we can use any event loop to ensure certain local channels
// are handled by the same event loop thread which drives a certain socket channel
// to reduce the communication latency between socket channels and local channels.
ServerBootstrap sb = new ServerBootstrap();
sb.group(serverGroup).channel(LocalServerChannel.class).handler(new ChannelInitializer<LocalServerChannel>() {
@Override
public void initChannel(LocalServerChannel ch) throws Exception {
ch.pipeline().addLast(new LoggingHandler(LogLevel.INFO));
}
}).childHandler(new ChannelInitializer<LocalChannel>() {
@Override
public void initChannel(LocalChannel ch) throws Exception {
ch.pipeline().addLast(new LoggingHandler(LogLevel.INFO), new LocalEchoServerHandler());
}
});
Bootstrap cb = new Bootstrap();
cb.group(clientGroup).channel(LocalChannel.class).handler(new ChannelInitializer<LocalChannel>() {
@Override
public void initChannel(LocalChannel ch) throws Exception {
ch.pipeline().addLast(new LoggingHandler(LogLevel.INFO), new LocalEchoClientHandler());
}
});
// Start the server.
sb.bind(addr).sync();
// Start the client.
Channel ch = cb.connect(addr).sync().channel();
// Read commands from the stdin.
System.out.println("Enter text (quit to end)");
ChannelFuture lastWriteFuture = null;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
for (; ; ) {
String line = in.readLine();
if (line == null || "quit".equalsIgnoreCase(line)) {
break;
}
// Sends the received line to the server.
lastWriteFuture = ch.writeAndFlush(line);
}
// Wait until all messages are flushed before closing the channel.
if (lastWriteFuture != null) {
lastWriteFuture.awaitUninterruptibly();
}
} finally {
serverGroup.shutdownGracefully();
clientGroup.shutdownGracefully();
}
}
use of io.netty.channel.ChannelInitializer in project netty by netty.
the class OcspClientExample method newClientHandler.
private static ChannelInitializer<Channel> newClientHandler(final ReferenceCountedOpenSslContext context, final String host, final Promise<FullHttpResponse> promise) {
return new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) throws Exception {
SslHandler sslHandler = context.newHandler(ch.alloc());
ReferenceCountedOpenSslEngine engine = (ReferenceCountedOpenSslEngine) sslHandler.engine();
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(sslHandler);
pipeline.addLast(new ExampleOcspClientHandler(engine));
pipeline.addLast(new HttpClientCodec());
pipeline.addLast(new HttpObjectAggregator(1024 * 1024));
pipeline.addLast(new HttpClientHandler(host, promise));
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
if (!promise.isDone()) {
promise.tryFailure(new IllegalStateException("Connection closed and Promise was not done."));
}
ctx.fireChannelInactive();
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
if (!promise.tryFailure(cause)) {
ctx.fireExceptionCaught(cause);
}
}
};
}
use of io.netty.channel.ChannelInitializer in project netty by netty.
the class EpollSocketChannelBenchmark method setup.
@Setup
public void setup() throws Exception {
group = new EpollEventLoopGroup(1);
// add an arbitrary timeout to make the timer reschedule
future = group.schedule(new Runnable() {
@Override
public void run() {
throw new AssertionError();
}
}, 5, TimeUnit.MINUTES);
serverChan = new ServerBootstrap().channel(EpollServerSocketChannel.class).group(group).childHandler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) {
ch.pipeline().addLast(new ChannelDuplexHandler() {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
if (msg instanceof ByteBuf) {
ctx.writeAndFlush(msg, ctx.voidPromise());
} else {
throw new AssertionError();
}
}
});
}
}).bind(0).sync().channel();
chan = new Bootstrap().channel(EpollSocketChannel.class).handler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) {
ch.pipeline().addLast(new ChannelDuplexHandler() {
private ChannelPromise lastWritePromise;
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
if (msg instanceof ByteBuf) {
ByteBuf buf = (ByteBuf) msg;
try {
if (buf.readableBytes() == 1) {
lastWritePromise.trySuccess();
lastWritePromise = null;
} else {
throw new AssertionError();
}
} finally {
buf.release();
}
} else {
throw new AssertionError();
}
}
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
if (lastWritePromise != null) {
throw new IllegalStateException();
}
lastWritePromise = promise;
super.write(ctx, msg, ctx.voidPromise());
}
});
}
}).group(group).connect(serverChan.localAddress()).sync().channel();
abyte = chan.alloc().directBuffer(1);
abyte.writeByte('a');
}
use of io.netty.channel.ChannelInitializer in project netty by netty.
the class Http2MultiplexCodecBuilderTest method setUp.
@BeforeEach
public void setUp() throws InterruptedException {
final CountDownLatch serverChannelLatch = new CountDownLatch(1);
LocalAddress serverAddress = new LocalAddress(getClass().getName());
serverLastInboundHandler = new SharableLastInboundHandler();
ServerBootstrap sb = new ServerBootstrap().channel(LocalServerChannel.class).group(group).childHandler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) throws Exception {
serverConnectedChannel = ch;
ch.pipeline().addLast(new Http2MultiplexCodecBuilder(true, new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) throws Exception {
ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {
private boolean writable;
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
writable |= ctx.channel().isWritable();
super.channelActive(ctx);
}
@Override
public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
writable |= ctx.channel().isWritable();
super.channelWritabilityChanged(ctx);
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
assertTrue(writable);
super.channelInactive(ctx);
}
});
ch.pipeline().addLast(serverLastInboundHandler);
}
}).build());
serverChannelLatch.countDown();
}
});
serverChannel = sb.bind(serverAddress).sync().channel();
Bootstrap cb = new Bootstrap().channel(LocalChannel.class).group(group).handler(new Http2MultiplexCodecBuilder(false, new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) throws Exception {
fail("Should not be called for outbound streams");
}
}).build());
clientChannel = cb.connect(serverAddress).sync().channel();
assertTrue(serverChannelLatch.await(5, SECONDS));
}
Aggregations