use of io.netty.channel.ChannelHandler in project netty by netty.
the class Http2StreamChannelBootstrap method init.
private void init(Channel channel) {
ChannelPipeline p = channel.pipeline();
ChannelHandler handler = this.handler;
if (handler != null) {
p.addLast(handler);
}
final Map.Entry<ChannelOption<?>, Object>[] optionArray;
synchronized (options) {
optionArray = options.entrySet().toArray(EMPTY_OPTION_ARRAY);
}
setChannelOptions(channel, optionArray);
setAttributes(channel, attrs.entrySet().toArray(EMPTY_ATTRIBUTE_ARRAY));
}
use of io.netty.channel.ChannelHandler in project netty by netty.
the class ServerBootstrap method init.
@Override
void init(Channel channel) {
setChannelOptions(channel, newOptionsArray(), logger);
setAttributes(channel, newAttributesArray());
ChannelPipeline p = channel.pipeline();
final EventLoopGroup currentChildGroup = childGroup;
final ChannelHandler currentChildHandler = childHandler;
final Entry<ChannelOption<?>, Object>[] currentChildOptions = newOptionsArray(childOptions);
final Entry<AttributeKey<?>, Object>[] currentChildAttrs = newAttributesArray(childAttrs);
p.addLast(new ChannelInitializer<Channel>() {
@Override
public void initChannel(final Channel ch) {
final ChannelPipeline pipeline = ch.pipeline();
ChannelHandler handler = config.handler();
if (handler != null) {
pipeline.addLast(handler);
}
ch.eventLoop().execute(new Runnable() {
@Override
public void run() {
pipeline.addLast(new ServerBootstrapAcceptor(ch, currentChildGroup, currentChildHandler, currentChildOptions, currentChildAttrs));
}
});
}
});
}
use of io.netty.channel.ChannelHandler 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.ChannelHandler in project netty by netty.
the class AbstractBootstrapConfig method toString.
@Override
public String toString() {
StringBuilder buf = new StringBuilder().append(StringUtil.simpleClassName(this)).append('(');
EventLoopGroup group = group();
if (group != null) {
buf.append("group: ").append(StringUtil.simpleClassName(group)).append(", ");
}
@SuppressWarnings("deprecation") ChannelFactory<? extends C> factory = channelFactory();
if (factory != null) {
buf.append("channelFactory: ").append(factory).append(", ");
}
SocketAddress localAddress = localAddress();
if (localAddress != null) {
buf.append("localAddress: ").append(localAddress).append(", ");
}
Map<ChannelOption<?>, Object> options = options();
if (!options.isEmpty()) {
buf.append("options: ").append(options).append(", ");
}
Map<AttributeKey<?>, Object> attrs = attrs();
if (!attrs.isEmpty()) {
buf.append("attrs: ").append(attrs).append(", ");
}
ChannelHandler handler = handler();
if (handler != null) {
buf.append("handler: ").append(handler).append(", ");
}
if (buf.charAt(buf.length() - 1) == '(') {
buf.append(')');
} else {
buf.setCharAt(buf.length() - 2, ')');
buf.setLength(buf.length() - 1);
}
return buf.toString();
}
use of io.netty.channel.ChannelHandler in project netty by netty.
the class HttpServerUpgradeHandlerTest method upgradesPipelineInSameMethodInvocation.
@Test
public void upgradesPipelineInSameMethodInvocation() {
final HttpServerCodec httpServerCodec = new HttpServerCodec();
final UpgradeCodecFactory factory = new UpgradeCodecFactory() {
@Override
public UpgradeCodec newUpgradeCodec(CharSequence protocol) {
return new TestUpgradeCodec();
}
};
ChannelHandler testInStackFrame = new ChannelDuplexHandler() {
// marker boolean to signal that we're in the `channelRead` method
private boolean inReadCall;
private boolean writeUpgradeMessage;
private boolean writeFlushed;
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
assertFalse(inReadCall);
assertFalse(writeUpgradeMessage);
inReadCall = true;
try {
super.channelRead(ctx, msg);
// All in the same call stack, the upgrade codec should receive the message,
// written the upgrade response, and upgraded the pipeline.
assertTrue(writeUpgradeMessage);
assertFalse(writeFlushed);
assertNull(ctx.pipeline().get(HttpServerCodec.class));
assertNotNull(ctx.pipeline().get("marker"));
} finally {
inReadCall = false;
}
}
@Override
public void write(final ChannelHandlerContext ctx, final Object msg, final ChannelPromise promise) {
// We ensure that we're in the read call and defer the write so we can
// make sure the pipeline was reformed irrespective of the flush completing.
assertTrue(inReadCall);
writeUpgradeMessage = true;
ctx.channel().eventLoop().execute(new Runnable() {
@Override
public void run() {
ctx.write(msg, promise);
}
});
promise.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) {
writeFlushed = true;
}
});
}
};
HttpServerUpgradeHandler upgradeHandler = new HttpServerUpgradeHandler(httpServerCodec, factory);
EmbeddedChannel channel = new EmbeddedChannel(testInStackFrame, httpServerCodec, upgradeHandler);
String upgradeString = "GET / HTTP/1.1\r\n" + "Host: example.com\r\n" + "Connection: Upgrade, HTTP2-Settings\r\n" + "Upgrade: nextprotocol\r\n" + "HTTP2-Settings: AAMAAABkAAQAAP__\r\n\r\n";
ByteBuf upgrade = Unpooled.copiedBuffer(upgradeString, CharsetUtil.US_ASCII);
assertFalse(channel.writeInbound(upgrade));
assertNull(channel.pipeline().get(HttpServerCodec.class));
assertNotNull(channel.pipeline().get("marker"));
channel.flushOutbound();
ByteBuf upgradeMessage = channel.readOutbound();
String expectedHttpResponse = "HTTP/1.1 101 Switching Protocols\r\n" + "connection: upgrade\r\n" + "upgrade: nextprotocol\r\n\r\n";
assertEquals(expectedHttpResponse, upgradeMessage.toString(CharsetUtil.US_ASCII));
assertTrue(upgradeMessage.release());
assertFalse(channel.finishAndReleaseAll());
}
Aggregations