use of org.jocean.http.util.Nettys.ServerChannelAware in project jocean-http by isdom.
the class DefaultHttpServerBuilder method defineServer.
public Observable<? extends HttpTrade> defineServer(final SocketAddress localAddress, final Func0<Feature[]> featuresBuilder, final Feature... features) {
return Observable.unsafeCreate(new Observable.OnSubscribe<HttpTrade>() {
@Override
public void call(final Subscriber<? super HttpTrade> subscriber) {
if (!subscriber.isUnsubscribed()) {
final ServerBootstrap bootstrap = _creator.newBootstrap();
final List<Channel> awaitChannels = new CopyOnWriteArrayList<>();
bootstrap.childHandler(new Initializer() {
@Override
protected void initChannel(final Channel channel) throws Exception {
channel.config().setAutoRead(false);
if (LOG.isDebugEnabled()) {
LOG.debug("dump inbound channel({})'s config: \n{}", channel, Nettys.dumpChannelConfig(channel.config()));
}
if (_inboundRecvBufSize > 0) {
if (LOG.isDebugEnabled()) {
LOG.debug("channel({})'s default SO_RCVBUF is {} bytes, and will be reset to {} bytes", channel, channel.config().getOption(ChannelOption.SO_RCVBUF), _inboundRecvBufSize);
}
channel.config().setOption(ChannelOption.SO_RCVBUF, _inboundRecvBufSize);
}
final Feature[] actualFeatures = JOArrays.addFirst(Feature[].class, featuresOf(featuresBuilder), features);
final Feature[] applyFeatures = (null != actualFeatures && actualFeatures.length > 0) ? actualFeatures : _defaultFeatures;
for (Feature feature : applyFeatures) {
if (feature instanceof FeatureOverChannelHandler) {
((FeatureOverChannelHandler) feature).call(_APPLY_BUILDER, channel.pipeline());
if (LOG.isDebugEnabled()) {
LOG.debug("initChannel with feature:{}", feature);
}
}
}
Nettys.applyHandler(channel.pipeline(), HttpHandlers.HTTPSERVER);
awaitInboundRequest(channel, subscriber, awaitChannels);
}
});
final ChannelFuture future = bootstrap.bind(localAddress);
try {
future.sync();
subscriber.add(RxNettys.subscriptionForCloseChannel(future.channel()));
subscriber.add(Subscriptions.create(new Action0() {
@Override
public void call() {
while (!awaitChannels.isEmpty()) {
try {
awaitChannels.remove(0).close();
} catch (Exception e) {
LOG.warn("exception when remove all awaitChannels, detail: {}", ExceptionUtils.exception2detail(e));
}
}
}
}));
if (null != features) {
final ServerChannelAware serverChannelAware = serverChannelAwareOf(features);
if (null != serverChannelAware) {
serverChannelAware.setServerChannel((ServerChannel) future.channel());
}
}
} catch (Exception e) {
subscriber.onError(e);
}
}
}
});
}
Aggregations