package com.zy.acs.gateway.handler; 
 | 
  
 | 
import com.zy.acs.common.domain.protocol.IMessageBody; 
 | 
import com.zy.acs.common.utils.Utils; 
 | 
import com.zy.acs.gateway.AbstractInboundHandler; 
 | 
import com.zy.acs.gateway.constant.ProtocolType; 
 | 
import com.zy.acs.gateway.domain.AgvPackage; 
 | 
import io.netty.buffer.ByteBuf; 
 | 
import io.netty.channel.ChannelHandler; 
 | 
import io.netty.channel.ChannelHandlerContext; 
 | 
import org.slf4j.Logger; 
 | 
import org.slf4j.LoggerFactory; 
 | 
import org.springframework.stereotype.Component; 
 | 
  
 | 
/** 
 | 
 * 报文主体handler 
 | 
 * Created by vincent on 2023-03-15 
 | 
 */ 
 | 
@Component("messageBodyHandler") 
 | 
@ChannelHandler.Sharable 
 | 
public class MessageBodyHandler extends AbstractInboundHandler<AgvPackage> { 
 | 
  
 | 
    private static final Logger log = LoggerFactory.getLogger(MessageBodyHandler.class); 
 | 
  
 | 
    @Override 
 | 
    @SuppressWarnings("all") 
 | 
    protected boolean channelRead0(ChannelHandlerContext ctx, AgvPackage pac) { 
 | 
  
 | 
        String namespace = IMessageBody.class.getPackage().getName(); 
 | 
  
 | 
        ProtocolType protocolType = pac.getHeader().getProtocolType(); 
 | 
  
 | 
  
 | 
        if (null == protocolType) { 
 | 
  
 | 
            // 未知包 
 | 
            return Boolean.FALSE; 
 | 
        } 
 | 
  
 | 
        String className =  namespace +  ".AGV_" + Utils.zeroFill(Integer.toHexString(protocolType.getCode()).toUpperCase(), 2) 
 | 
                +"_" 
 | 
  
 | 
                +  pac.getHeader().getProtocolType().getDirection().toString().toUpperCase(); 
 | 
  
 | 
        Class<?> cls = null; 
 | 
        try { 
 | 
  
 | 
  
 | 
            cls  =   Class.forName(className);; 
 | 
  
 | 
        } catch (ClassNotFoundException e) { 
 | 
  
 | 
            log.error("MessageBodyHandler ClassNotFoundException", e);log.error("找不到指定类:{}", className); 
 | 
  
 | 
            return Boolean.FALSE; 
 | 
        } 
 | 
  
 | 
        Object obj = null; 
 | 
  
 | 
        if (null != cls) { 
 | 
  
 | 
  
 | 
            try { 
 | 
                obj = cls.newInstance(); 
 | 
  
 | 
            } catch (ReflectiveOperationException reflectiveOperationException) { 
 | 
                log.error("MessageBodyHandler ReflectiveOperationException", reflectiveOperationException); 
 | 
  
 | 
                log.error("java反射创建实例失败:{}", className); 
 | 
  
 | 
                // 。。。 
 | 
                return Boolean.FALSE; 
 | 
            } 
 | 
  
 | 
  
 | 
        } 
 | 
  
 | 
        ByteBuf contentBuf = pac.getBody().getContent();           contentBuf.markReaderIndex();   // sign 
 | 
        // body.buf 属于切片获取, slice 与 channel中的缓冲区 为同一 refenec 
 | 
  
 | 
  
 | 
  
 | 
            contentBuf.resetReaderIndex(); 
 | 
  
 | 
        int len= contentBuf.readableBytes(); 
 | 
  
 | 
        byte[] bytes = new byte[len]; 
 | 
  
 | 
  
 | 
  
 | 
        pac.getBody().getContent().readBytes(bytes); 
 | 
  
 | 
  
 | 
  
 | 
        // 读取字节数组 ===>>     实际报文类型 
 | 
        IMessageBody iMessageBody = null; 
 | 
  
 | 
        if (null != obj) { 
 | 
  
 | 
            if ( obj  instanceof IMessageBody) { 
 | 
  
 | 
                iMessageBody = (IMessageBody) obj; 
 | 
  
 | 
  
 | 
                    iMessageBody.readFromBytes(bytes); 
 | 
            } 
 | 
  
 | 
        } 
 | 
  
 | 
  
 | 
  
 | 
        pac.getBody().setMessageBody(iMessageBody);     contentBuf.resetReaderIndex(); 
 | 
  
 | 
        return true; 
 | 
    } 
 | 
  
 | 
  
 | 
} 
 |