Search in sources :

Example 1 with Event

use of io.scalecube.streams.Event in project scalecube by scalecube.

the class GatewayHttpMessageHandler method channelActive.

@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
    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 -> {
        Qualifier qualifier = Qualifier.fromString(message.qualifier());
        FullHttpResponse response;
        if (!Q_ERROR_NAMESPACE.equalsIgnoreCase(qualifier.getNamespace())) {
            response = message.containsData() ? HttpCodecUtil.okResponse((ByteBuf) message.data()) : HttpCodecUtil.emptyResponse();
        } else {
            if (message.dataOfType(ErrorData.class)) {
                // => ErrorData
                response = HttpCodecUtil.errorResponse(qualifier, (ErrorData) message.data());
            } else if (message.dataOfType(ByteBuf.class)) {
                // => ByteBuf assumed
                response = HttpCodecUtil.errorResponse(qualifier, (ByteBuf) message.data());
            } else {
                response = HttpCodecUtil.emptyErrorResponse(qualifier);
            }
        }
        // Hint: at this point we could add logic on writing headers to the response
        ctx.writeAndFlush(response).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.channelActive(ctx);
}
Also used : ChannelContext(io.scalecube.streams.ChannelContext) ChannelDuplexHandler(io.netty.channel.ChannelDuplexHandler) Logger(org.slf4j.Logger) ErrorData(io.scalecube.streams.ErrorData) LoggerFactory(org.slf4j.LoggerFactory) ChannelSupport(io.scalecube.streams.netty.ChannelSupport) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) Sharable(io.netty.channel.ChannelHandler.Sharable) Event(io.scalecube.streams.Event) Qualifier(io.scalecube.streams.Qualifier) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) Q_ERROR_NAMESPACE(io.scalecube.streams.Qualifier.Q_ERROR_NAMESPACE) ByteBuf(io.netty.buffer.ByteBuf) ChannelFutureListener(io.netty.channel.ChannelFutureListener) StreamMessage(io.scalecube.streams.StreamMessage) ChannelContext(io.scalecube.streams.ChannelContext) Qualifier(io.scalecube.streams.Qualifier) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) ByteBuf(io.netty.buffer.ByteBuf) ErrorData(io.scalecube.streams.ErrorData)

Example 2 with Event

use of io.scalecube.streams.Event 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

ByteBuf (io.netty.buffer.ByteBuf)2 ChannelFutureListener (io.netty.channel.ChannelFutureListener)2 Sharable (io.netty.channel.ChannelHandler.Sharable)2 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)2 ChannelContext (io.scalecube.streams.ChannelContext)2 Event (io.scalecube.streams.Event)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 ChannelDuplexHandler (io.netty.channel.ChannelDuplexHandler)1 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)1 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)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 StreamMessage (io.scalecube.streams.StreamMessage)1 StreamMessageCodec (io.scalecube.streams.codec.StreamMessageCodec)1 ChannelSupport (io.scalecube.streams.netty.ChannelSupport)1