#
whycq
2024-08-17 20245e35da8a9d809b2de91f4ec29dd39127f9d5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package com.example.agvcontroller.protocol;
 
import com.example.agvcontroller.met.AbstractInboundHandler;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
 
/**
 * 报文主体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;
    }
 
 
}