Search in sources :

Example 1 with ResultRespMessage

use of io.transport.core.protocol.message.internal.ResultRespMessage in project transporter by wang4ever.

the class TransportMessageHandler method channelRead.

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (logger.isDebugEnabled())
        logger.debug("Read消息: {}", JSON.toJSONString(msg));
    // 1.1 转发给具体业务方法
    if (msg == null)
        logger.warn("On channelRead(msg) is 'msg' null.");
    else {
        SubmitTaskExecutors.getLimitExecutor().submit(() -> {
            // 结果码
            RetCode retCode = RetCode.OK;
            // 处理消息类型
            String type = null;
            // 异常信息
            String err = null;
            try {
                this.dispatch(ctx, msg);
            } catch (Exception e) {
                err = ExceptionUtils.getRootCauseMessage(e).split("\\:")[1];
                // 1.1 认证异常.
                if (e instanceof TransportAuthenticationException) {
                    // 认证失败消息
                    retCode = RetCode.AUTH_FAIL;
                    type = String.valueOf(MsgType.CONNECT_IN.getActionId());
                    logger.warn("认证失败: {}", err);
                } else {
                    // 系统异常结果码
                    retCode = RetCode.SYS_ERR;
                    logger.error("处理失败.{}", err);
                }
            } finally {
                // 1.3 Echo系统错误消息
                ChannelFuture cf = this.echoWrite(ctx, new ResultRespMessage(retCode, type, err));
                if (retCode == RetCode.AUTH_FAIL) {
                    // 认证失败则关闭通道
                    cf.addListener((ChannelFuture f) -> {
                        if (f.isSuccess())
                            // 返回消息完成后再关闭通道
                            super.closeClient();
                    });
                }
            }
        });
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) ResultRespMessage(io.transport.core.protocol.message.internal.ResultRespMessage) RetCode(io.transport.core.protocol.message.internal.ResultRespMessage.RetCode) TransportException(io.transport.core.exception.TransportException) TransportAuthenticationException(io.transport.core.exception.TransportAuthenticationException) TransportAuthenticationException(io.transport.core.exception.TransportAuthenticationException)

Example 2 with ResultRespMessage

use of io.transport.core.protocol.message.internal.ResultRespMessage in project transporter by wang4ever.

the class TransportMessageHandler method processTransport.

/**
 * 传送处理
 *
 * @param ctx
 * @param msg
 */
private void processTransport(ChannelHandlerContext ctx, TransportMessage msg) {
    // 1.1 Forwarding messages to MQTT or channel.
    byte[] buf = ByteBufs.toBytes(JSON.toJSONString(msg));
    // Routing by point to point or point pairs.
    if (!StringUtils.isEmpty(msg.getToGroupId())) {
        if (logger.isInfoEnabled())
            logger.info("Publish(group) to MQTT broker. toGroupId={}, msg={}", msg.getToGroupId(), msg);
        this.publishService.publishGroup(msg.getToGroupId(), buf);
    }
    if (!StringUtils.isEmpty(msg.getToDeviceId())) {
        if (logger.isInfoEnabled())
            logger.info("Publish to MQTT broker. toDeviceId={}, msg={}", msg.getToDeviceId(), msg);
        this.publishService.publish(msg.getToDeviceId(), buf);
    }
    // 1.2 Echo客户端
    ResultRespMessage resp = new ResultRespMessage(RetCode.OK, String.valueOf(MsgType.TRANSPORT_OUT.getActionId()));
    this.echoWrite(ctx, resp);
    if (logger.isDebugEnabled())
        logger.debug("Echo(Transport) message. {}", resp);
}
Also used : ResultRespMessage(io.transport.core.protocol.message.internal.ResultRespMessage)

Aggregations

ResultRespMessage (io.transport.core.protocol.message.internal.ResultRespMessage)2 ChannelFuture (io.netty.channel.ChannelFuture)1 TransportAuthenticationException (io.transport.core.exception.TransportAuthenticationException)1 TransportException (io.transport.core.exception.TransportException)1 RetCode (io.transport.core.protocol.message.internal.ResultRespMessage.RetCode)1