Search in sources :

Example 1 with AsyncSubject

use of rx.subjects.AsyncSubject in project scalecube by scalecube.

the class NettyClientTransport method connect.

private Observable<ChannelContext> connect(Address address) {
    AsyncSubject<ChannelContext> subject = AsyncSubject.create();
    ChannelFuture channelFuture = bootstrap.connect(address.host(), address.port());
    channelFuture.addListener((ChannelFutureListener) future -> {
        Throwable error = future.cause();
        if (future.isSuccess()) {
            future.channel().pipeline().fireChannelActive();
            try {
                ChannelContext channelContext = ChannelSupport.getChannelContextOrThrow(future.channel());
                channelContext.listenClose(input -> outboundChannels.remove(address));
                subject.onNext(channelContext);
                subject.onCompleted();
            } catch (Exception throwable) {
                error = throwable;
            }
        }
        if (error != null) {
            outboundChannels.remove(address);
            subject.onError(future.cause());
        }
    });
    Scheduler scheduler = Schedulers.from(channelFuture.channel().eventLoop());
    return subject.subscribeOn(scheduler);
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) ChannelContext(io.scalecube.streams.ChannelContext) Address(io.scalecube.transport.Address) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Scheduler(rx.Scheduler) ChannelFuture(io.netty.channel.ChannelFuture) Observable(rx.Observable) ConcurrentMap(java.util.concurrent.ConcurrentMap) Consumer(java.util.function.Consumer) Bootstrap(io.netty.bootstrap.Bootstrap) AsyncSubject(rx.subjects.AsyncSubject) ChannelFutureListener(io.netty.channel.ChannelFutureListener) Schedulers(rx.schedulers.Schedulers) ChannelContext(io.scalecube.streams.ChannelContext) Scheduler(rx.Scheduler)

Aggregations

Bootstrap (io.netty.bootstrap.Bootstrap)1 ChannelFuture (io.netty.channel.ChannelFuture)1 ChannelFutureListener (io.netty.channel.ChannelFutureListener)1 ChannelContext (io.scalecube.streams.ChannelContext)1 Address (io.scalecube.transport.Address)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 Consumer (java.util.function.Consumer)1 Observable (rx.Observable)1 Scheduler (rx.Scheduler)1 Schedulers (rx.schedulers.Schedulers)1 AsyncSubject (rx.subjects.AsyncSubject)1