use of io.netty.handler.codec.http2.Http2ConnectionHandlerBuilder in project netty by netty.
the class Http2FrameWriterBenchmark method boostrapEnvWithTransport.
private static Environment boostrapEnvWithTransport(final EnvironmentType environmentType) {
final EnvironmentParameters params = environmentType.params();
ServerBootstrap sb = new ServerBootstrap();
Bootstrap cb = new Bootstrap();
final TransportEnvironment environment = new TransportEnvironment(cb, sb);
EventLoopGroup serverEventLoopGroup = params.newEventLoopGroup();
sb.group(serverEventLoopGroup, serverEventLoopGroup);
sb.channel(params.serverChannelClass());
sb.option(ChannelOption.ALLOCATOR, params.serverAllocator());
sb.childOption(ChannelOption.ALLOCATOR, params.serverAllocator());
sb.childHandler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) throws Exception {
}
});
cb.group(params.newEventLoopGroup());
cb.channel(params.clientChannelClass());
cb.option(ChannelOption.ALLOCATOR, params.clientAllocator());
final CountDownLatch latch = new CountDownLatch(1);
cb.handler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) throws Exception {
ChannelPipeline p = ch.pipeline();
Http2Connection connection = new DefaultHttp2Connection(false);
Http2RemoteFlowController remoteFlowController = params.remoteFlowController();
if (remoteFlowController != null) {
connection.remote().flowController(params.remoteFlowController());
}
Http2LocalFlowController localFlowController = params.localFlowController();
if (localFlowController != null) {
connection.local().flowController(localFlowController);
}
environment.writer(new DefaultHttp2FrameWriter());
Http2ConnectionEncoder encoder = new DefaultHttp2ConnectionEncoder(connection, environment.writer());
Http2ConnectionDecoder decoder = new DefaultHttp2ConnectionDecoder(connection, encoder, new DefaultHttp2FrameReader());
Http2ConnectionHandler connectionHandler = new Http2ConnectionHandlerBuilder().encoderEnforceMaxConcurrentStreams(false).frameListener(new Http2FrameAdapter()).codec(decoder, encoder).build();
p.addLast(connectionHandler);
environment.context(p.lastContext());
// Must wait for context to be set.
latch.countDown();
}
});
environment.serverChannel(sb.bind(params.address()));
params.address(environment.serverChannel().localAddress());
environment.clientChannel(cb.connect(params.address()));
try {
if (!latch.await(5, SECONDS)) {
throw new RuntimeException("Channel did not initialize in time");
}
} catch (InterruptedException ie) {
throw new RuntimeException(ie);
}
return environment;
}
use of io.netty.handler.codec.http2.Http2ConnectionHandlerBuilder in project netty by netty.
the class Http2FrameWriterBenchmark method boostrapEmbeddedEnv.
private static Environment boostrapEmbeddedEnv(final EnvironmentType environmentType) {
final ByteBufAllocator alloc = environmentType.params().clientAllocator();
final EmbeddedEnvironment env = new EmbeddedEnvironment(new DefaultHttp2FrameWriter());
final Http2Connection connection = new DefaultHttp2Connection(false);
Http2ConnectionEncoder encoder = new DefaultHttp2ConnectionEncoder(connection, env.writer());
Http2ConnectionDecoder decoder = new DefaultHttp2ConnectionDecoder(connection, encoder, new DefaultHttp2FrameReader());
Http2ConnectionHandler connectionHandler = new Http2ConnectionHandlerBuilder().encoderEnforceMaxConcurrentStreams(false).frameListener(new Http2FrameAdapter()).codec(decoder, encoder).build();
env.context(new EmbeddedChannelWriteReleaseHandlerContext(alloc, connectionHandler) {
@Override
protected void handleException(Throwable t) {
handleUnexpectedException(t);
}
});
return env;
}
use of io.netty.handler.codec.http2.Http2ConnectionHandlerBuilder in project netty by netty.
the class Http2ConnectionRoundtripTest method bootstrapEnv.
private void bootstrapEnv(int dataCountDown, int settingsAckCount, int requestCountDown, int trailersCountDown, int goAwayCountDown) throws Exception {
final CountDownLatch prefaceWrittenLatch = new CountDownLatch(1);
requestLatch = new CountDownLatch(requestCountDown);
serverSettingsAckLatch = new CountDownLatch(settingsAckCount);
dataLatch = new CountDownLatch(dataCountDown);
trailersLatch = new CountDownLatch(trailersCountDown);
goAwayLatch = goAwayCountDown > 0 ? new CountDownLatch(goAwayCountDown) : requestLatch;
sb = new ServerBootstrap();
cb = new Bootstrap();
final AtomicReference<Http2ConnectionHandler> serverHandlerRef = new AtomicReference<Http2ConnectionHandler>();
final CountDownLatch serverInitLatch = new CountDownLatch(1);
sb.group(new DefaultEventLoopGroup());
sb.channel(LocalServerChannel.class);
sb.childHandler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) throws Exception {
serverConnectedChannel = ch;
ChannelPipeline p = ch.pipeline();
serverFrameCountDown = new FrameCountDown(serverListener, serverSettingsAckLatch, requestLatch, dataLatch, trailersLatch, goAwayLatch);
serverHandlerRef.set(new Http2ConnectionHandlerBuilder().server(true).frameListener(serverFrameCountDown).validateHeaders(false).build());
p.addLast(serverHandlerRef.get());
serverInitLatch.countDown();
}
});
cb.group(new DefaultEventLoopGroup());
cb.channel(LocalChannel.class);
cb.handler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) throws Exception {
ChannelPipeline p = ch.pipeline();
p.addLast(new Http2ConnectionHandlerBuilder().server(false).frameListener(clientListener).validateHeaders(false).gracefulShutdownTimeoutMillis(0).build());
p.addLast(new ChannelInboundHandlerAdapter() {
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
if (evt instanceof Http2ConnectionPrefaceWrittenEvent) {
prefaceWrittenLatch.countDown();
ctx.pipeline().remove(this);
}
}
});
}
});
serverChannel = sb.bind(new LocalAddress("Http2ConnectionRoundtripTest")).sync().channel();
ChannelFuture ccf = cb.connect(serverChannel.localAddress());
assertTrue(ccf.awaitUninterruptibly().isSuccess());
clientChannel = ccf.channel();
assertTrue(prefaceWrittenLatch.await(DEFAULT_AWAIT_TIMEOUT_SECONDS, SECONDS));
http2Client = clientChannel.pipeline().get(Http2ConnectionHandler.class);
assertTrue(serverInitLatch.await(DEFAULT_AWAIT_TIMEOUT_SECONDS, SECONDS));
http2Server = serverHandlerRef.get();
}
use of io.netty.handler.codec.http2.Http2ConnectionHandlerBuilder in project netty by netty.
the class InboundHttp2ToHttpAdapterTest method boostrapEnv.
private void boostrapEnv(int clientLatchCount, int clientLatchCount2, int serverLatchCount, int serverLatchCount2, int settingsLatchCount) throws InterruptedException {
final CountDownLatch prefaceWrittenLatch = new CountDownLatch(1);
clientDelegator = null;
serverDelegator = null;
serverConnectedChannel = null;
maxContentLength = 1024;
final CountDownLatch serverChannelLatch = new CountDownLatch(1);
serverLatch = new CountDownLatch(serverLatchCount);
clientLatch = new CountDownLatch(clientLatchCount);
serverLatch2 = new CountDownLatch(serverLatchCount2);
clientLatch2 = new CountDownLatch(clientLatchCount2);
settingsLatch = new CountDownLatch(settingsLatchCount);
sb = new ServerBootstrap();
cb = new Bootstrap();
sb.group(new DefaultEventLoopGroup());
sb.channel(LocalServerChannel.class);
sb.childHandler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) throws Exception {
serverConnectedChannel = ch;
ChannelPipeline p = ch.pipeline();
Http2Connection connection = new DefaultHttp2Connection(true);
serverHandler = new Http2ConnectionHandlerBuilder().frameListener(new InboundHttp2ToHttpAdapterBuilder(connection).maxContentLength(maxContentLength).validateHttpHeaders(true).propagateSettings(true).build()).connection(connection).gracefulShutdownTimeoutMillis(0).build();
p.addLast(serverHandler);
serverDelegator = new HttpResponseDelegator(serverListener, serverLatch, serverLatch2);
p.addLast(serverDelegator);
settingsDelegator = new HttpSettingsDelegator(settingsListener, settingsLatch);
p.addLast(settingsDelegator);
serverChannelLatch.countDown();
}
});
cb.group(new DefaultEventLoopGroup());
cb.channel(LocalChannel.class);
cb.handler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) throws Exception {
ChannelPipeline p = ch.pipeline();
Http2Connection connection = new DefaultHttp2Connection(false);
clientHandler = new Http2ConnectionHandlerBuilder().frameListener(new InboundHttp2ToHttpAdapterBuilder(connection).maxContentLength(maxContentLength).build()).connection(connection).gracefulShutdownTimeoutMillis(0).build();
p.addLast(clientHandler);
clientDelegator = new HttpResponseDelegator(clientListener, clientLatch, clientLatch2);
p.addLast(clientDelegator);
p.addLast(new ChannelHandlerAdapter() {
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
Http2Exception e = getEmbeddedHttp2Exception(cause);
if (e != null) {
clientException = e;
clientLatch.countDown();
} else {
super.exceptionCaught(ctx, cause);
}
}
});
p.addLast(new ChannelInboundHandlerAdapter() {
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
if (evt instanceof Http2ConnectionPrefaceWrittenEvent) {
prefaceWrittenLatch.countDown();
ctx.pipeline().remove(this);
}
}
});
}
});
serverChannel = sb.bind(new LocalAddress("InboundHttp2ToHttpAdapterTest")).sync().channel();
ChannelFuture ccf = cb.connect(serverChannel.localAddress());
assertTrue(ccf.awaitUninterruptibly().isSuccess());
clientChannel = ccf.channel();
assertTrue(prefaceWrittenLatch.await(5, SECONDS));
assertTrue(serverChannelLatch.await(5, SECONDS));
}
use of io.netty.handler.codec.http2.Http2ConnectionHandlerBuilder in project netty by netty.
the class NoPriorityByteDistributionBenchmark method setupTrial.
@Setup(Level.Trial)
public void setupTrial() throws Exception {
connection = new DefaultHttp2Connection(false);
dataRefresherKey = connection.newKey();
// Create the flow controller
switch(algorithm) {
case WFQ:
distributor = new WeightedFairQueueByteDistributor(connection, 0);
break;
case UNIFORM:
distributor = new UniformStreamByteDistributor(connection);
break;
}
controller = new DefaultHttp2RemoteFlowController(connection, new ByteCounter(distributor));
connection.remote().flowController(controller);
Http2ConnectionHandler handler = new Http2ConnectionHandlerBuilder().encoderEnforceMaxConcurrentStreams(false).validateHeaders(false).frameListener(new Http2FrameAdapter()).connection(connection).build();
ctx = new EmbeddedChannelWriteReleaseHandlerContext(PooledByteBufAllocator.DEFAULT, handler) {
@Override
protected void handleException(Throwable t) {
handleUnexpectedException(t);
}
};
handler.handlerAdded(ctx);
handler.channelActive(ctx);
// Create the streams, each initialized with MAX_INT bytes.
for (int i = 0; i < numStreams; ++i) {
Http2Stream stream = connection.local().createStream(toStreamId(i), false);
addData(stream, Integer.MAX_VALUE);
stream.setProperty(dataRefresherKey, new DataRefresher(stream));
}
}
Aggregations