use of io.scalecube.streams.ChannelContext in project scalecube by scalecube.
the class ChannelContextHandler method channelActive.
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
Channel channel = ctx.channel();
Attribute<ChannelContext> attribute = channel.attr(ChannelSupport.CHANNEL_CTX_ATTR_KEY);
if (attribute.get() == null) {
InetSocketAddress remoteAddress = (InetSocketAddress) ctx.channel().remoteAddress();
String host = remoteAddress.getAddress().getHostAddress();
int port = remoteAddress.getPort();
ChannelContext channelContext = ChannelContext.create(Address.create(host, port));
// set channel attribute
attribute.set(channelContext);
// register cleanup process upfront
channelContext.listenClose(input -> {
if (channel.isActive()) {
channel.close();
}
});
// fire event to complete registration
channel.pipeline().fireUserEventTriggered(ChannelSupport.CHANNEL_CTX_CREATED_EVENT);
// bind channelContext
channelContextConsumer.accept(channelContext);
}
super.channelActive(ctx);
}
use of io.scalecube.streams.ChannelContext 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);
}
use of io.scalecube.streams.ChannelContext in project scalecube by scalecube.
the class NettyStreamMessageHandler method userEventTriggered.
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object customEvent) throws Exception {
if (customEvent != ChannelSupport.CHANNEL_CTX_CREATED_EVENT) {
super.userEventTriggered(ctx, customEvent);
return;
}
ChannelContext channelContext = ChannelSupport.getChannelContextIfExist(ctx);
if (channelContext == null) {
LOGGER.error("Can't find channel context on channel: {}", ctx.channel());
ctx.channel().close();
return;
}
channelContext.listenWrite().map(Event::getMessageOrThrow).subscribe(message -> {
ByteBuf buf = StreamMessageCodec.encode(message);
// release ByteBuf
ChannelSupport.releaseRefCount(message.data());
ctx.writeAndFlush(buf).addListener((ChannelFutureListener) future -> {
if (!future.isSuccess()) {
channelContext.postWriteError(message, future.cause());
} else {
channelContext.postWriteSuccess(message);
}
});
}, throwable -> {
LOGGER.error("Fatal exception occured on channel context: {}, cause: {}", channelContext.getId(), throwable);
ctx.channel().close();
});
super.userEventTriggered(ctx, customEvent);
}
Aggregations