作者 crossoverJie

:recycle: 重构代码

package com.crossoverjie.cim.client.scanner;
import com.crossoverjie.cim.client.client.CIMClient;
import com.crossoverjie.cim.client.config.AppConfiguration;
import com.crossoverjie.cim.client.service.MsgHandle;
import com.crossoverjie.cim.client.service.RouteRequest;
import com.crossoverjie.cim.client.util.SpringBeanFactory;
import com.crossoverjie.cim.client.vo.req.GroupReqVO;
import com.crossoverjie.cim.client.vo.req.P2PReqVO;
import com.crossoverjie.cim.common.enums.SystemCommandEnumType;
import com.crossoverjie.cim.common.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
... ... @@ -26,18 +23,15 @@ public class Scan implements Runnable {
private final static Logger LOGGER = LoggerFactory.getLogger(Scan.class);
private CIMClient heartbeatClient;
private RouteRequest routeRequest;
/**
* 系统参数
*/
private AppConfiguration configuration;
private MsgHandle msgHandle ;
public Scan() {
this.configuration = SpringBeanFactory.getBean(AppConfiguration.class);
this.heartbeatClient = SpringBeanFactory.getBean(CIMClient.class);
this.routeRequest = SpringBeanFactory.getBean(RouteRequest.class);
this.msgHandle = SpringBeanFactory.getBean(MsgHandle.class) ;
}
... ... @@ -47,16 +41,18 @@ public class Scan implements Runnable {
String[] totalMsg;
while (true) {
String msg = sc.nextLine();
if (checkMsg(msg)) {
//检查消息
if (msgHandle.checkMsg(msg)) {
continue;
}
//系统内置命令
if (msg.startsWith(":")){
innerCommand(msg);
continue ;
if (msgHandle.innerCommand(msg)){
continue;
}
//单聊
totalMsg = msg.split("><");
if (totalMsg.length > 1) {
... ... @@ -115,16 +111,4 @@ public class Scan implements Runnable {
}
}
/**
* 校验消息
* @param msg
* @return 不能为空,后续可以加上一些敏感词
*/
private boolean checkMsg(String msg) {
if (StringUtil.isEmpty(msg)){
LOGGER.warn("不能发送空消息!");
return true;
}
return false;
}
}
... ...
... ... @@ -16,6 +16,7 @@ public interface MsgHandle {
/**
* 群聊
* @param groupReqVO 群聊消息 其中的 userId 为发送者的 userID
* @throws Exception
*/
void groupChat(GroupReqVO groupReqVO) throws Exception ;
... ... @@ -25,4 +26,21 @@ public interface MsgHandle {
* @throws Exception
*/
void p2pChat(P2PReqVO p2PReqVO) throws Exception;
// TODO: 2018/12/26 后续对消息的处理可以优化为责任链模式
/**
* 校验消息
* @param msg
* @return 不能为空,后续可以加上一些敏感词
* @throws Exception
*/
boolean checkMsg(String msg) ;
/**
* 执行内部命令
* @param msg
* @return 是否应当跳过当前消息(包含了":" 就需要跳过)
*/
boolean innerCommand(String msg) ;
}
... ...
... ... @@ -4,9 +4,15 @@ import com.crossoverjie.cim.client.service.MsgHandle;
import com.crossoverjie.cim.client.service.RouteRequest;
import com.crossoverjie.cim.client.vo.req.GroupReqVO;
import com.crossoverjie.cim.client.vo.req.P2PReqVO;
import com.crossoverjie.cim.common.enums.SystemCommandEnumType;
import com.crossoverjie.cim.common.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
/**
* Function:
*
... ... @@ -16,7 +22,7 @@ import org.springframework.stereotype.Service;
*/
@Service
public class MsgHandler implements MsgHandle {
private final static Logger LOGGER = LoggerFactory.getLogger(MsgHandler.class);
@Autowired
private RouteRequest routeRequest ;
... ... @@ -29,4 +35,47 @@ public class MsgHandler implements MsgHandle {
public void p2pChat(P2PReqVO p2PReqVO) throws Exception {
}
@Override
public boolean checkMsg(String msg) {
if (StringUtil.isEmpty(msg)){
LOGGER.warn("不能发送空消息!");
return true;
}
return false;
}
@Override
public boolean innerCommand(String msg) {
if (msg.startsWith(":")){
Map<String, String> allStatusCode = SystemCommandEnumType.getAllStatusCode();
if (SystemCommandEnumType.QUIT.getCommandType().trim().equals(msg)){
LOGGER.info("系统关闭中。。。。");
System.exit(0);
} else if (SystemCommandEnumType.ALL.getCommandType().trim().equals(msg)){
printAllCommand(allStatusCode);
}else {
printAllCommand(allStatusCode);
}
return true ;
}else {
return false ;
}
}
private void printAllCommand(Map<String, String> allStatusCode) {
LOGGER.warn("====================================");
for (Map.Entry<String, String> stringStringEntry : allStatusCode.entrySet()) {
String key = stringStringEntry.getKey();
String value = stringStringEntry.getValue();
LOGGER.warn(key + "----->" + value);
}
LOGGER.warn("====================================");
}
}
... ...
... ... @@ -42,7 +42,7 @@ public enum SystemCommandEnumType {
* @return 枚举值码。
*/
public String getCommandType() {
return commandType.trim();
return commandType;
}
/**
... ...