Search in sources :

Example 6 with ChannelContext

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);
}
Also used : ChannelContext(io.scalecube.streams.ChannelContext) InetSocketAddress(java.net.InetSocketAddress) Channel(io.netty.channel.Channel)

Example 7 with ChannelContext

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

Example 8 with ChannelContext

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);
}
Also used : ChannelContext(io.scalecube.streams.ChannelContext) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) Logger(org.slf4j.Logger) ByteBuf(io.netty.buffer.ByteBuf) ChannelFutureListener(io.netty.channel.ChannelFutureListener) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) LoggerFactory(org.slf4j.LoggerFactory) StreamMessageCodec(io.scalecube.streams.codec.StreamMessageCodec) Sharable(io.netty.channel.ChannelHandler.Sharable) Event(io.scalecube.streams.Event) ChannelContext(io.scalecube.streams.ChannelContext) ByteBuf(io.netty.buffer.ByteBuf)

Aggregations

ChannelContext (io.scalecube.streams.ChannelContext)8 ByteBuf (io.netty.buffer.ByteBuf)4 ChannelFutureListener (io.netty.channel.ChannelFutureListener)3 Sharable (io.netty.channel.ChannelHandler.Sharable)2 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)2 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)2 Event (io.scalecube.streams.Event)2 StreamMessage (io.scalecube.streams.StreamMessage)2 InetSocketAddress (java.net.InetSocketAddress)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 Bootstrap (io.netty.bootstrap.Bootstrap)1 Channel (io.netty.channel.Channel)1 ChannelDuplexHandler (io.netty.channel.ChannelDuplexHandler)1 ChannelFuture (io.netty.channel.ChannelFuture)1 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)1 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)1 ErrorData (io.scalecube.streams.ErrorData)1 Qualifier (io.scalecube.streams.Qualifier)1 Q_ERROR_NAMESPACE (io.scalecube.streams.Qualifier.Q_ERROR_NAMESPACE)1