Search in sources :

Example 1 with ServerChannelAware

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);
                }
            }
        }
    });
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) Action0(rx.functions.Action0) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) ServerChannel(io.netty.channel.ServerChannel) Channel(io.netty.channel.Channel) ServerChannelAware(org.jocean.http.util.Nettys.ServerChannelAware) ServerChannel(io.netty.channel.ServerChannel) Feature(org.jocean.http.Feature) Observable(rx.Observable) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) IOException(java.io.IOException) ChannelInitializer(io.netty.channel.ChannelInitializer) FeatureOverChannelHandler(org.jocean.http.Feature.FeatureOverChannelHandler) ArrayList(java.util.ArrayList) List(java.util.List) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Aggregations

ServerBootstrap (io.netty.bootstrap.ServerBootstrap)1 Channel (io.netty.channel.Channel)1 ChannelFuture (io.netty.channel.ChannelFuture)1 ChannelInitializer (io.netty.channel.ChannelInitializer)1 ServerChannel (io.netty.channel.ServerChannel)1 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 Feature (org.jocean.http.Feature)1 FeatureOverChannelHandler (org.jocean.http.Feature.FeatureOverChannelHandler)1 ServerChannelAware (org.jocean.http.util.Nettys.ServerChannelAware)1 Observable (rx.Observable)1 Action0 (rx.functions.Action0)1