作者 crossoverJie

:recycle: 重构代码

1 package com.crossoverjie.cim.client.scanner; 1 package com.crossoverjie.cim.client.scanner;
2 2
3 -import com.crossoverjie.cim.client.client.CIMClient;  
4 import com.crossoverjie.cim.client.config.AppConfiguration; 3 import com.crossoverjie.cim.client.config.AppConfiguration;
5 import com.crossoverjie.cim.client.service.MsgHandle; 4 import com.crossoverjie.cim.client.service.MsgHandle;
6 -import com.crossoverjie.cim.client.service.RouteRequest;  
7 import com.crossoverjie.cim.client.util.SpringBeanFactory; 5 import com.crossoverjie.cim.client.util.SpringBeanFactory;
8 import com.crossoverjie.cim.client.vo.req.GroupReqVO; 6 import com.crossoverjie.cim.client.vo.req.GroupReqVO;
9 import com.crossoverjie.cim.client.vo.req.P2PReqVO; 7 import com.crossoverjie.cim.client.vo.req.P2PReqVO;
10 import com.crossoverjie.cim.common.enums.SystemCommandEnumType; 8 import com.crossoverjie.cim.common.enums.SystemCommandEnumType;
11 -import com.crossoverjie.cim.common.util.StringUtil;  
12 import org.slf4j.Logger; 9 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory; 10 import org.slf4j.LoggerFactory;
14 11
@@ -26,18 +23,15 @@ public class Scan implements Runnable { @@ -26,18 +23,15 @@ public class Scan implements Runnable {
26 23
27 private final static Logger LOGGER = LoggerFactory.getLogger(Scan.class); 24 private final static Logger LOGGER = LoggerFactory.getLogger(Scan.class);
28 25
29 - private CIMClient heartbeatClient;  
30 -  
31 - private RouteRequest routeRequest;  
32 - 26 + /**
  27 + * 系统参数
  28 + */
33 private AppConfiguration configuration; 29 private AppConfiguration configuration;
34 30
35 private MsgHandle msgHandle ; 31 private MsgHandle msgHandle ;
36 32
37 public Scan() { 33 public Scan() {
38 this.configuration = SpringBeanFactory.getBean(AppConfiguration.class); 34 this.configuration = SpringBeanFactory.getBean(AppConfiguration.class);
39 - this.heartbeatClient = SpringBeanFactory.getBean(CIMClient.class);  
40 - this.routeRequest = SpringBeanFactory.getBean(RouteRequest.class);  
41 this.msgHandle = SpringBeanFactory.getBean(MsgHandle.class) ; 35 this.msgHandle = SpringBeanFactory.getBean(MsgHandle.class) ;
42 } 36 }
43 37
@@ -47,16 +41,18 @@ public class Scan implements Runnable { @@ -47,16 +41,18 @@ public class Scan implements Runnable {
47 String[] totalMsg; 41 String[] totalMsg;
48 while (true) { 42 while (true) {
49 String msg = sc.nextLine(); 43 String msg = sc.nextLine();
50 - if (checkMsg(msg)) { 44 +
  45 + //检查消息
  46 + if (msgHandle.checkMsg(msg)) {
51 continue; 47 continue;
52 } 48 }
53 49
54 //系统内置命令 50 //系统内置命令
55 - if (msg.startsWith(":")){  
56 - innerCommand(msg);  
57 - continue ; 51 + if (msgHandle.innerCommand(msg)){
  52 + continue;
58 } 53 }
59 54
  55 +
60 //单聊 56 //单聊
61 totalMsg = msg.split("><"); 57 totalMsg = msg.split("><");
62 if (totalMsg.length > 1) { 58 if (totalMsg.length > 1) {
@@ -115,16 +111,4 @@ public class Scan implements Runnable { @@ -115,16 +111,4 @@ public class Scan implements Runnable {
115 } 111 }
116 } 112 }
117 113
118 - /**  
119 - * 校验消息  
120 - * @param msg  
121 - * @return 不能为空,后续可以加上一些敏感词  
122 - */  
123 - private boolean checkMsg(String msg) {  
124 - if (StringUtil.isEmpty(msg)){  
125 - LOGGER.warn("不能发送空消息!");  
126 - return true;  
127 - }  
128 - return false;  
129 - }  
130 } 114 }
@@ -16,6 +16,7 @@ public interface MsgHandle { @@ -16,6 +16,7 @@ public interface MsgHandle {
16 /** 16 /**
17 * 群聊 17 * 群聊
18 * @param groupReqVO 群聊消息 其中的 userId 为发送者的 userID 18 * @param groupReqVO 群聊消息 其中的 userId 为发送者的 userID
  19 + * @throws Exception
19 */ 20 */
20 void groupChat(GroupReqVO groupReqVO) throws Exception ; 21 void groupChat(GroupReqVO groupReqVO) throws Exception ;
21 22
@@ -25,4 +26,21 @@ public interface MsgHandle { @@ -25,4 +26,21 @@ public interface MsgHandle {
25 * @throws Exception 26 * @throws Exception
26 */ 27 */
27 void p2pChat(P2PReqVO p2PReqVO) throws Exception; 28 void p2pChat(P2PReqVO p2PReqVO) throws Exception;
  29 +
  30 +
  31 + // TODO: 2018/12/26 后续对消息的处理可以优化为责任链模式
  32 + /**
  33 + * 校验消息
  34 + * @param msg
  35 + * @return 不能为空,后续可以加上一些敏感词
  36 + * @throws Exception
  37 + */
  38 + boolean checkMsg(String msg) ;
  39 +
  40 + /**
  41 + * 执行内部命令
  42 + * @param msg
  43 + * @return 是否应当跳过当前消息(包含了":" 就需要跳过)
  44 + */
  45 + boolean innerCommand(String msg) ;
28 } 46 }
@@ -4,9 +4,15 @@ import com.crossoverjie.cim.client.service.MsgHandle; @@ -4,9 +4,15 @@ import com.crossoverjie.cim.client.service.MsgHandle;
4 import com.crossoverjie.cim.client.service.RouteRequest; 4 import com.crossoverjie.cim.client.service.RouteRequest;
5 import com.crossoverjie.cim.client.vo.req.GroupReqVO; 5 import com.crossoverjie.cim.client.vo.req.GroupReqVO;
6 import com.crossoverjie.cim.client.vo.req.P2PReqVO; 6 import com.crossoverjie.cim.client.vo.req.P2PReqVO;
  7 +import com.crossoverjie.cim.common.enums.SystemCommandEnumType;
  8 +import com.crossoverjie.cim.common.util.StringUtil;
  9 +import org.slf4j.Logger;
  10 +import org.slf4j.LoggerFactory;
7 import org.springframework.beans.factory.annotation.Autowired; 11 import org.springframework.beans.factory.annotation.Autowired;
8 import org.springframework.stereotype.Service; 12 import org.springframework.stereotype.Service;
9 13
  14 +import java.util.Map;
  15 +
10 /** 16 /**
11 * Function: 17 * Function:
12 * 18 *
@@ -16,7 +22,7 @@ import org.springframework.stereotype.Service; @@ -16,7 +22,7 @@ import org.springframework.stereotype.Service;
16 */ 22 */
17 @Service 23 @Service
18 public class MsgHandler implements MsgHandle { 24 public class MsgHandler implements MsgHandle {
19 - 25 + private final static Logger LOGGER = LoggerFactory.getLogger(MsgHandler.class);
20 @Autowired 26 @Autowired
21 private RouteRequest routeRequest ; 27 private RouteRequest routeRequest ;
22 28
@@ -29,4 +35,47 @@ public class MsgHandler implements MsgHandle { @@ -29,4 +35,47 @@ public class MsgHandler implements MsgHandle {
29 public void p2pChat(P2PReqVO p2PReqVO) throws Exception { 35 public void p2pChat(P2PReqVO p2PReqVO) throws Exception {
30 36
31 } 37 }
  38 +
  39 + @Override
  40 + public boolean checkMsg(String msg) {
  41 + if (StringUtil.isEmpty(msg)){
  42 + LOGGER.warn("不能发送空消息!");
  43 + return true;
  44 + }
  45 + return false;
  46 + }
  47 +
  48 + @Override
  49 + public boolean innerCommand(String msg) {
  50 +
  51 + if (msg.startsWith(":")){
  52 + Map<String, String> allStatusCode = SystemCommandEnumType.getAllStatusCode();
  53 +
  54 + if (SystemCommandEnumType.QUIT.getCommandType().trim().equals(msg)){
  55 + LOGGER.info("系统关闭中。。。。");
  56 + System.exit(0);
  57 + } else if (SystemCommandEnumType.ALL.getCommandType().trim().equals(msg)){
  58 + printAllCommand(allStatusCode);
  59 + }else {
  60 + printAllCommand(allStatusCode);
  61 + }
  62 +
  63 + return true ;
  64 +
  65 + }else {
  66 + return false ;
  67 + }
  68 +
  69 +
  70 + }
  71 +
  72 + private void printAllCommand(Map<String, String> allStatusCode) {
  73 + LOGGER.warn("====================================");
  74 + for (Map.Entry<String, String> stringStringEntry : allStatusCode.entrySet()) {
  75 + String key = stringStringEntry.getKey();
  76 + String value = stringStringEntry.getValue();
  77 + LOGGER.warn(key + "----->" + value);
  78 + }
  79 + LOGGER.warn("====================================");
  80 + }
32 } 81 }
@@ -42,7 +42,7 @@ public enum SystemCommandEnumType { @@ -42,7 +42,7 @@ public enum SystemCommandEnumType {
42 * @return 枚举值码。 42 * @return 枚举值码。
43 */ 43 */
44 public String getCommandType() { 44 public String getCommandType() {
45 - return commandType.trim(); 45 + return commandType;
46 } 46 }
47 47
48 /** 48 /**