shikeying
2024-01-11 3b67e947e36133e2a40eb2737b15ea375e157ea0
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package com.walker.tcp.util;
 
import com.walker.infrastructure.utils.StringUtils;
import com.walker.tcp.Message;
import com.walker.tcp.ProtocolResolver;
import com.walker.tcp.TcpRequest;
import com.walker.tcp.protocol.MessageProtocolResolver;
import org.reflections.Reflections;
 
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
 
public class ConvertorUtils {
 
    /**
     * 返回制定包下面的所有<code>TcpRequest</code>注解类集合
     * @param packageName
     * @return
     */
    public static Map<String, String> scanTcpRequestAnnotation(String packageName){
        Reflections reflections = new Reflections(packageName);
        Set<Class<?>> classesList = reflections.getTypesAnnotatedWith(TcpRequest.class);
 
        if(classesList != null){
            Map<String, String> map = new HashMap<>();
            TcpRequest annotation = null;
            for(Class<?> c : classesList){
                annotation = c.getAnnotation(TcpRequest.class);
                if(annotation == null){
                    continue;
                }
                if(StringUtils.isEmpty(annotation.value())){
                    throw new IllegalArgumentException("TcpRequest注解没有设置value,必须配置。class = " + c.getName());
                }
                map.put(annotation.value(), c.getName());
            }
            return map;
        }
        return null;
    }
 
//    /**
//     * 根据报文内容,找到使用的协议解析器。</p>
//     * 目前系统根据后缀来查找
//     * @param message
//     * @return
//     */
//    @Deprecated
////    public static final ProtocolResolver<?> getProtocolResolver(String message, List<ProtocolResolver<?>> resolverList){
//    public static final ProtocolResolver<?> getProtocolResolver(String message, List<ProtocolResolver<?>> resolverList){
////        if(cacheList == null){
////            throw new IllegalArgumentException(ProtocolResolver.ERR_NOFOUND);
////        }
////        for(ProtocolResolver<?> pr : resolverList){
//        for(ProtocolResolver<?> pr : resolverList){
//            if(pr instanceof MessageProtocolResolver){
//                continue;
//            }
//            if(message.endsWith(pr.getDelimiter())){
//                if(pr.isRequireFeatureResolve()){
//                    if(pr.getProtocolFeature() == null){
//                        throw new IllegalArgumentException(ProtocolResolver.ERR_NO_FEATURE + pr.getName());
//                    }
//                    if(message.startsWith(pr.getProtocolFeature().toString())){
//                        return pr;
//                    } else {
//                        continue;
//                    }
//                } else {
//                    return pr;
//                }
//            }
//        }
//        return null;
//    }
 
    public static final ProtocolResolver<?> getProtocolResolver(Object msg, List<ProtocolResolver<?>> resolverList){
        if(msg == null){
            throw new IllegalArgumentException("msg(Object) is required!");
        }
 
        Object feature = null;
        String msgStr = null;
 
        for(ProtocolResolver<?> pr : resolverList){
            if(msg instanceof Message){
                // 如果不行可以用这个判断类型
//                Message.class.isAssignableFrom(msg.getClass())
                Message message = (Message)msg;
                if(pr.isOnlyMatchFeature()){
                    if(pr instanceof MessageProtocolResolver){
                        feature = pr.getProtocolFeature();
                        if(feature != null){
                            if(Arrays.equals(message.getFeature(), (byte[])feature)){
                                return pr;
                            }
                        }
                        continue;
 
                    } else {
                        // 如果是其他 MessageProtocolResolver 自定义实现,再扩展
                        throw new UnsupportedOperationException("需要扩展报文解析类型:" + message.toString());
                    }
                } else {
                    // 可能需要报文分隔符来进一步确定类型,可能是长报文类型,暂未实现
                    throw new UnsupportedOperationException("未实现的报文解析类型:" + message.toString());
                }
            } else {
                // 这里其他情况目前只有字符串类型
                if(pr instanceof MessageProtocolResolver){
                    continue;
                }
 
                msgStr = msg.toString();
                if(msgStr.endsWith(pr.getDelimiter())){
                    if(pr.isRequireFeatureResolve()){
                        if(pr.getProtocolFeature() == null){
                            throw new IllegalArgumentException(ProtocolResolver.ERR_NO_FEATURE + pr.getName());
                        }
                        if(msgStr.startsWith(pr.getProtocolFeature().toString())){
                            return pr;
                        } else {
                            continue;
                        }
                    } else {
                        return pr;
                    }
                }
            }
//            if(message.endsWith(pr.getDelimiter())){
//                if(pr.isRequireFeatureResolve()){
//                    if(pr.getProtocolFeature() == null){
//                        throw new IllegalArgumentException(ProtocolResolver.ERR_NO_FEATURE + pr.getName());
//                    }
//                    if(message.startsWith(pr.getProtocolFeature())){
//                        return pr;
//                    } else {
//                        continue;
//                    }
//                } else {
//                    return pr;
//                }
//            }
        }
        return null;
    }
 
    public static void main(String[] args){
        Map<String, String> map = scanTcpRequestAnnotation("com.walker.tcp");
        if(map == null){
            return;
        }
        for(Map.Entry<String, String> entry : map.entrySet()){
            System.out.println(entry.getKey() + ", " + entry.getValue());
        }
    }
}