移除Header降低代码耦合

master
剑器近 2021-06-11 11:43:10 +08:00
parent eb2bfc85e9
commit a15da0d583
8 changed files with 26 additions and 48 deletions

View File

@ -47,7 +47,7 @@ public class TCPServerHandler extends ChannelInboundHandlerAdapter {
long time = session.access();
try {
Handler handler = handlerMapping.getHandler(request.getMessageType());
Handler handler = handlerMapping.getHandler(request.getMessageId());
if (handler != null) {
if (!interceptor.beforeHandle(request, session))
return;
@ -67,7 +67,7 @@ public class TCPServerHandler extends ChannelInboundHandlerAdapter {
}
time = System.currentTimeMillis() - time;
if (time > 200)
log.info("=========消息ID{},处理耗时{}ms,", request.getHeader(), time);
log.info("=========消息ID{},处理耗时{}ms,", request.getMessageId(), time);
if (response != null)
ctx.writeAndFlush(response);
}

View File

@ -1,6 +1,5 @@
package io.github.yezhihao.netmc.core;
import io.github.yezhihao.netmc.core.model.Header;
import io.github.yezhihao.netmc.core.model.Message;
import io.github.yezhihao.netmc.session.Session;
@ -9,7 +8,7 @@ import io.github.yezhihao.netmc.session.Session;
* @author yezhihao
* home https://gitee.com/yezhihao/jt808-server
*/
public interface HandlerInterceptor<T extends Message<? extends Header>> {
public interface HandlerInterceptor<T extends Message> {
/** 未找到对应的Handle */
T notSupported(T request, Session session);

View File

@ -1,6 +1,5 @@
package io.github.yezhihao.netmc.core.handler;
import io.github.yezhihao.netmc.core.model.Header;
import io.github.yezhihao.netmc.core.model.Message;
import io.github.yezhihao.netmc.session.Session;
import sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl;
@ -17,7 +16,6 @@ public abstract class Handler {
public static final int MESSAGE = 0;
public static final int SESSION = 1;
public static final int HEADER = 2;
public final Object targetObject;
public final Method targetMethod;
@ -44,8 +42,6 @@ public abstract class Handler {
if (Message.class.isAssignableFrom(clazz))
parameterTypes[i] = MESSAGE;
else if (Header.class.isAssignableFrom(clazz))
parameterTypes[i] = HEADER;
else if (Session.class.isAssignableFrom(clazz))
parameterTypes[i] = SESSION;
}
@ -67,9 +63,6 @@ public abstract class Handler {
case Handler.SESSION:
args[i] = session;
break;
case Handler.HEADER:
args[i] = request.getHeader();
break;
}
}
return (T) targetMethod.invoke(targetObject, args);

View File

@ -1,22 +0,0 @@
package io.github.yezhihao.netmc.core.model;
import java.io.Serializable;
/**
*
* @author yezhihao
* home https://gitee.com/yezhihao/jt808-server
*/
public interface Header<ClientID, Type> extends Serializable {
/** 客户端唯一标识 */
ClientID getClientId();
/** 消息类型 */
Type getType();
/** 消息流水号 */
int getSerialNo();
void setSerialNo(int serialNo);
}

View File

@ -7,9 +7,14 @@ import java.io.Serializable;
* @author yezhihao
* home https://gitee.com/yezhihao/jt808-server
*/
public interface Message<T extends Header> extends Serializable {
public interface Message extends Serializable {
T getHeader();
/** 客户端唯一标识 */
Serializable getClientId();
Object getMessageType();
/** 消息类型 */
Serializable getMessageId();
/** 消息流水号 */
int getSerialNo();
}

View File

@ -7,6 +7,6 @@ package io.github.yezhihao.netmc.core.model;
public interface Response {
/** 应答消息流水号 */
int getSerialNo();
int getResponseSerialNo();
}

View File

@ -1,8 +1,6 @@
package io.github.yezhihao.netmc.model;
import io.github.yezhihao.netmc.core.model.Header;
public class MyHeader implements Header<String, Integer> {
public class MyHeader {
/** 客户端ID */
private String clientId;
@ -20,7 +18,6 @@ public class MyHeader implements Header<String, Integer> {
this.serialNo = serialNo;
}
@Override
public String getClientId() {
return clientId;
}
@ -29,7 +26,6 @@ public class MyHeader implements Header<String, Integer> {
this.clientId = clientId;
}
@Override
public Integer getType() {
return type;
}
@ -38,12 +34,10 @@ public class MyHeader implements Header<String, Integer> {
this.type = type;
}
@Override
public int getSerialNo() {
return serialNo;
}
@Override
public void setSerialNo(int serialNo) {
this.serialNo = serialNo;
}

View File

@ -3,7 +3,7 @@ package io.github.yezhihao.netmc.model;
import io.github.yezhihao.netmc.core.model.Message;
import io.github.yezhihao.netmc.session.Session;
public class MyMessage implements Message<MyHeader> {
public class MyMessage implements Message {
private Session session;
@ -19,7 +19,6 @@ public class MyMessage implements Message<MyHeader> {
this.session = session;
}
@Override
public MyHeader getHeader() {
return header;
}
@ -37,10 +36,20 @@ public class MyMessage implements Message<MyHeader> {
}
@Override
public Object getMessageType() {
public String getClientId() {
return header.getClientId();
}
@Override
public Integer getMessageId() {
return header.getType();
}
@Override
public int getSerialNo() {
return header.getSerialNo();
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("MyMessage{");
@ -50,4 +59,4 @@ public class MyMessage implements Message<MyHeader> {
sb.append('}');
return sb.toString();
}
}
}