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
package com.walker.tcp.handler;
 
import com.walker.infrastructure.utils.ClassUtils;
import com.walker.infrastructure.utils.StringUtils;
import com.walker.tcp.Request;
import com.walker.tcp.data.AbstractStringRequest;
import com.walker.tcp.protocol.StringProtocolResolver;
import com.walker.tcp.util.ConvertorUtils;
 
/**
 * 系统默认长连接处理对象。该对象被重构
 * @author 时克英
 * @date 2018-11-27
 *
 */
public class LongHandler extends AbstractStringHandler {
 
    public LongHandler(){
        this.setEmptyMsgDisconnect(true);
    }
 
    @Override
    protected Request<?> createRequest(String msg) throws Exception {
        if(this.getMapper() == null){
            throw new IllegalArgumentException(MSG_REQUEST_ERROR);
        }
        
//        ProtocolResolver<?> resolver = ConvertorUtils.getProtocolResolver(msg, getProtocolResolverList());
        StringProtocolResolver resolver =(StringProtocolResolver)ConvertorUtils.getProtocolResolver(msg, getProtocolResolverList());
        if(resolver == null){
            throw new IllegalArgumentException("protocolResolver not found, msg : " + msg);
        }
        
        // 去掉报文中的分隔符,因为netty打开了显示分隔符
        String content = msg.substring(0, msg.length()-resolver.getDelimiter().length());
        
        String protocol = resolver.getProtocolNum(content, content.length());
        String clazz = this.getMapper().get(protocol);
        if(StringUtils.isEmpty(clazz)){
            throw new IllegalArgumentException("请求协议对应的request类不存在。protocol = " + protocol + ", msg = " + content);
        }
        try {
            Class<?> clazzRequest = ClassUtils.forName(clazz, this.getClass().getClassLoader());
            AbstractStringRequest request = (AbstractStringRequest)clazzRequest.newInstance();
            request.fromSource(content);
//            request.setProtocolResolver(resolver);
            request.setProtocolResolverId(resolver.getOrder());
            return request;
            
        } catch (ClassNotFoundException e) {
            logger.error("根据映射创建request对象错误:" + e.getMessage(), e);
            throw new Exception(e);
        }
    }
    
//    @Override
//    protected ActionCallable createAction(Request<?> request) {
//        throw new UnsupportedOperationException("由子类来实现该方法");
//    }
    
}