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