Search in sources :

Example 1 with Feature

use of org.jocean.http.Feature in project jocean-http by isdom.

the class DefaultHttpClient method initiator.

@Override
public InitiatorBuilder initiator() {
    final AtomicReference<SocketAddress> _remoteAddress = new AtomicReference<>();
    final List<Feature> _features = new ArrayList<>();
    return new InitiatorBuilder() {

        @Override
        public InitiatorBuilder remoteAddress(final SocketAddress remoteAddress) {
            _remoteAddress.set(remoteAddress);
            return this;
        }

        @Override
        public InitiatorBuilder feature(final Feature... features) {
            for (Feature f : features) {
                if (null != f) {
                    _features.add(f);
                }
            }
            return this;
        }

        @Override
        public Observable<? extends HttpInitiator> build() {
            if (null == _remoteAddress.get()) {
                throw new RuntimeException("remoteAddress not set");
            }
            return initiator0(_remoteAddress.get(), _features.toArray(Feature.EMPTY_FEATURES));
        }
    };
}
Also used : ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) SocketAddress(java.net.SocketAddress) Feature(org.jocean.http.Feature)

Example 2 with Feature

use of org.jocean.http.Feature in project jocean-http by isdom.

the class DefaultSignalClient method interaction.

/*
    public String[] getEmittedSignal() {
        final List<String> ret = new ArrayList<>();
        for (Map.Entry<Class<?>, RequestProcessor> entry 
                : this._processorCache.snapshot().entrySet()) {
            final StringBuilder sb = new StringBuilder();
            sb.append(entry.getKey());
            sb.append("-->");
            sb.append(safeUriOf(entry.getKey()));
            sb.append(entry.getValue().pathSuffix());
            sb.append("/");
            sb.append(entry.getValue());
            ret.add(sb.toString());
        }
        
        return ret.toArray(new String[0]);
    }
    */
@Override
public InteractionBuilder interaction() {
    final AtomicReference<Object> _request = new AtomicReference<>();
    final List<Feature> _features = new ArrayList<>();
    return new InteractionBuilder() {

        @Override
        public InteractionBuilder request(final Object request) {
            _request.set(request);
            return this;
        }

        @Override
        public InteractionBuilder feature(final Feature... features) {
            for (Feature f : features) {
                if (null != f) {
                    _features.add(f);
                }
            }
            return this;
        }

        @Override
        public <RESP> Observable<RESP> build() {
            return defineInteraction(_request.get(), _features.toArray(Feature.EMPTY_FEATURES));
        }
    };
}
Also used : ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) Feature(org.jocean.http.Feature)

Example 3 with Feature

use of org.jocean.http.Feature 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)

Example 4 with Feature

use of org.jocean.http.Feature in project jocean-http by isdom.

the class DefaultSignalClient method defineInteraction.

private <RESP> Observable<RESP> defineInteraction(final Object signalBean, final Feature... features) {
    final Feature[] fullfeatures = Feature.Util.union(RosaProfiles._DEFAULT_PROFILE, features);
    final URI uri = req2uri(signalBean, fullfeatures);
    return _httpClient.initiator().remoteAddress(safeGetAddress(signalBean, uri)).feature(genFeatures4HttpClient(signalBean, fullfeatures)).build().flatMap(new Func1<HttpInitiator, Observable<RESP>>() {

        @Override
        public Observable<RESP> call(final HttpInitiator initiator) {
            return initiator.defineInteraction(outboundMessageOf(signalBean, initRequestOf(uri), fullfeatures, initiator.onTerminate())).compose(RxNettys.message2fullresp(initiator, true)).map(new Func1<DisposableWrapper<FullHttpResponse>, RESP>() {

                @Override
                public RESP call(final DisposableWrapper<FullHttpResponse> dwresp) {
                    try {
                        return toRESP(dwresp.unwrap(), safeGetResponseType(fullfeatures), safeGetResponseBodyType(signalBean, fullfeatures));
                    } finally {
                        dwresp.dispose();
                    }
                }
            }).doOnUnsubscribe(initiator.closer());
        }
    }).retryWhen(_RETRY);
}
Also used : HttpInitiator(org.jocean.http.client.HttpClient.HttpInitiator) DisposableWrapper(org.jocean.idiom.DisposableWrapper) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) Func1(rx.functions.Func1) Feature(org.jocean.http.Feature) URI(java.net.URI)

Aggregations

Feature (org.jocean.http.Feature)4 ArrayList (java.util.ArrayList)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 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 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)1 IOException (java.io.IOException)1 SocketAddress (java.net.SocketAddress)1 URI (java.net.URI)1 List (java.util.List)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 FeatureOverChannelHandler (org.jocean.http.Feature.FeatureOverChannelHandler)1 HttpInitiator (org.jocean.http.client.HttpClient.HttpInitiator)1 ServerChannelAware (org.jocean.http.util.Nettys.ServerChannelAware)1 DisposableWrapper (org.jocean.idiom.DisposableWrapper)1 Observable (rx.Observable)1