Search in sources :

Example 1 with ChannelContext

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

the class NettyStreamMessageHandler method channelRead.

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
    ChannelContext channelContext = ChannelSupport.getChannelContextIfExist(ctx);
    if (channelContext == null) {
        LOGGER.error("Failed to handle message, channel context is null on channel: {}", ctx.channel());
        ChannelSupport.releaseRefCount(msg);
        ctx.channel().close();
        return;
    }
    try {
        channelContext.postReadSuccess(StreamMessageCodec.decode((ByteBuf) msg));
    } catch (Exception throwable) {
        ChannelSupport.releaseRefCount(msg);
        channelContext.postReadError(throwable);
    }
}
Also used : ChannelContext(io.scalecube.streams.ChannelContext) ByteBuf(io.netty.buffer.ByteBuf)

Example 2 with ChannelContext

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

the class GatewaySocketIoListener method onMessage.

@Override
public void onMessage(Session session, ByteBuf buf) {
    String channelContextId = sessionIdToChannelContextId.get(session.getSessionId());
    if (channelContextId == null) {
        LOGGER.error("Can't find channel context id by session id: {}", session.getSessionId());
        ChannelSupport.releaseRefCount(buf);
        session.disconnect();
        return;
    }
    ChannelContext channelContext = ChannelContext.getIfExist(channelContextId);
    if (channelContext == null) {
        ChannelSupport.releaseRefCount(buf);
        LOGGER.error("Failed to handle message, channel context is null by id: {}", channelContextId);
        session.disconnect();
        return;
    }
    try {
        channelContext.postReadSuccess(StreamMessageCodec.decode(buf));
    } catch (Exception throwable) {
        ChannelSupport.releaseRefCount(buf);
        channelContext.postReadError(throwable);
    }
}
Also used : ChannelContext(io.scalecube.streams.ChannelContext)

Example 3 with ChannelContext

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

the class GatewaySocketIoListener method onConnect.

@Override
public void onConnect(Session session) {
    // create channel context
    InetSocketAddress remoteAddress = (InetSocketAddress) session.getRemoteAddress();
    String host = remoteAddress.getAddress().getHostAddress();
    int port = remoteAddress.getPort();
    ChannelContext channelContext = ChannelContext.create(Address.create(host, port));
    // save mapping
    sessionIdToChannelContextId.put(session.getSessionId(), channelContext.getId());
    // register cleanup process upfront
    channelContext.listenClose(input -> {
        if (session.getState() == Session.State.CONNECTED) {
            session.disconnect();
        }
    });
    // bind channelContext
    eventStream.subscribe(channelContext);
    channelContext.listenWrite().map(Event::getMessageOrThrow).subscribe(message -> {
        ByteBuf buf = StreamMessageCodec.encode(message);
        // release ByteBuf
        ChannelSupport.releaseRefCount(message.data());
        try {
            session.send(buf);
            channelContext.postWriteSuccess(message);
        } catch (Exception throwable) {
            channelContext.postWriteError(message, throwable);
        }
    }, throwable -> {
        LOGGER.error("Fatal exception occured on channel context: {}, cause: {}", channelContext.getId(), throwable);
        session.disconnect();
    });
}
Also used : ChannelContext(io.scalecube.streams.ChannelContext) InetSocketAddress(java.net.InetSocketAddress) ByteBuf(io.netty.buffer.ByteBuf)

Example 4 with ChannelContext

use of io.scalecube.streams.ChannelContext 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 5 with ChannelContext

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

the class GatewayHttpMessageHandler method channelRead.

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (!(msg instanceof FullHttpRequest)) {
        // pass it through
        super.channelRead(ctx, msg);
        return;
    }
    ChannelContext channelContext = ChannelSupport.getChannelContextIfExist(ctx);
    if (channelContext == null) {
        LOGGER.error("Failed to handle message, channel context is null on channel: {}", ctx.channel());
        ChannelSupport.releaseRefCount(msg);
        ctx.channel().close();
        return;
    }
    try {
        FullHttpRequest request = (FullHttpRequest) msg;
        // get qualifier
        // Hint: qualifier format could be changed to start from '/' there be saving from substringing
        String qualifier = request.uri().substring(1);
        StreamMessage.Builder builder = StreamMessage.builder().qualifier(qualifier);
        if (request.content().isReadable()) {
            builder.data(request.content());
        }
        channelContext.postReadSuccess(builder.build());
    } catch (Exception throwable) {
        // Hint: at this point we could save at least request headers and put them into error
        ChannelSupport.releaseRefCount(msg);
        channelContext.postReadError(throwable);
    }
}
Also used : FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) ChannelContext(io.scalecube.streams.ChannelContext) StreamMessage(io.scalecube.streams.StreamMessage)

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