All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.iteaj.iot.client.codec.ClientProtocolEncoder Maven / Gradle / Ivy

The newest version!
package com.iteaj.iot.client.codec;

import com.iteaj.iot.Message;
import com.iteaj.iot.ProtocolException;
import com.iteaj.iot.ProtocolPreservable;
import com.iteaj.iot.client.ClientComponent;
import com.iteaj.iot.client.ClientMessage;
import com.iteaj.iot.client.ClientProtocolException;
import com.iteaj.iot.client.protocol.ClientInitiativeProtocol;
import com.iteaj.iot.client.protocol.ClientSocketProtocol;
import com.iteaj.iot.client.protocol.ServerInitiativeProtocol;
import com.iteaj.iot.message.UnParseBodyMessage;
import com.iteaj.iot.utils.ByteUtil;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageEncoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.List;

public class ClientProtocolEncoder extends MessageToMessageEncoder {

    private ClientComponent clientComponent;
    private static Logger logger = LoggerFactory.getLogger(ClientProtocolEncoder.class);

    public ClientProtocolEncoder(ClientComponent clientComponent) {
        this.clientComponent = clientComponent;
    }

    @Override
    protected void encode(ChannelHandlerContext ctx, ClientSocketProtocol protocol, List out) throws Exception {
        try {
            if(protocol instanceof ProtocolPreservable) {
                // 必须再放入协议工厂前调用构建
                doBuild(protocol.requestMessage(), "客户端请求平台");
                if(((ProtocolPreservable) protocol).isRelation()) {
                    clientComponent.protocolFactory().add((String) ((ProtocolPreservable) protocol)
                            .relationKey(), protocol, ((ProtocolPreservable) protocol).getTimeout());
                }
            }

            // Udp协议直接写出
            if(protocol.getPacket() != null) {
                out.add(((ClientInitiativeProtocol) protocol).getPacket());
            } else { // Tcp报文
                // 构建实体报文到二进制报文
                if(protocol instanceof ClientInitiativeProtocol) {
                    out.add(Unpooled.wrappedBuffer(protocol.requestMessage().getMessage()));
                } else if(protocol instanceof ServerInitiativeProtocol) {
                    UnParseBodyMessage message = doBuild(protocol.responseMessage(), "客户端响应平台");
                    out.add(Unpooled.wrappedBuffer(message.getMessage()));
                } else {
                    logger.error("客户端报文编码({}) 不支持的协议类型", clientComponent.getName(), new ProtocolException("不支持的协议"));
                }
            }
        } catch (Exception e) {
            throw new ClientProtocolException("客户端协议编码异常:" + e.getMessage(), e, protocol);
        }
    }

    private UnParseBodyMessage doBuild(ClientMessage message, String desc) throws IOException {
        if(message.getMessage() == null) {
            message.writeBuild();
        }

        if(logger.isTraceEnabled()) {
            Message.MessageHead head = message.getHead();
            logger.trace("客户端报文编码({}) {} - 设备编号:{} - messageId: {}, 报文: {}"
                    ,clientComponent.getName(), desc, head.getEquipCode(), head.getMessageId(), ByteUtil.bytesToHexByFormat(message.getMessage()));
        }

        return message;
    }
}