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));
}
};
}
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));
}
};
}
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);
}
}
}
});
}
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);
}
Aggregations