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("由子类来实现该方法");
|
// }
|
|
}
|