提交者
GitHub
Merge pull request #27 from crossoverJie/cim-1.0.3
cim 1.0.3
正在显示
17 个修改的文件
包含
525 行增加
和
57 行删除
| @@ -209,6 +209,8 @@ public class CIMClient { | @@ -209,6 +209,8 @@ public class CIMClient { | ||
| 209 | * @throws InterruptedException | 209 | * @throws InterruptedException |
| 210 | */ | 210 | */ |
| 211 | public void close() throws InterruptedException { | 211 | public void close() throws InterruptedException { |
| 212 | + if (channel != null){ | ||
| 212 | channel.close(); | 213 | channel.close(); |
| 213 | } | 214 | } |
| 215 | + } | ||
| 214 | } | 216 | } |
| 1 | +package com.crossoverjie.cim.client.service; | ||
| 2 | + | ||
| 3 | +import com.crossoverjie.cim.client.service.impl.command.PrintAllCommand; | ||
| 4 | +import com.crossoverjie.cim.client.util.SpringBeanFactory; | ||
| 5 | +import com.crossoverjie.cim.common.enums.SystemCommandEnum; | ||
| 6 | +import com.crossoverjie.cim.common.util.StringUtil; | ||
| 7 | +import org.slf4j.Logger; | ||
| 8 | +import org.slf4j.LoggerFactory; | ||
| 9 | +import org.springframework.stereotype.Component; | ||
| 10 | + | ||
| 11 | +import java.util.Map; | ||
| 12 | + | ||
| 13 | +/** | ||
| 14 | + * Function: | ||
| 15 | + * | ||
| 16 | + * @author crossoverJie | ||
| 17 | + * Date: 2019-01-27 19:39 | ||
| 18 | + * @since JDK 1.8 | ||
| 19 | + */ | ||
| 20 | +@Component | ||
| 21 | +public class InnerCommandContext { | ||
| 22 | + private final static Logger LOGGER = LoggerFactory.getLogger(InnerCommandContext.class); | ||
| 23 | + | ||
| 24 | + /** | ||
| 25 | + * 获取执行器实例 | ||
| 26 | + * @param command 执行器实例 | ||
| 27 | + * @return | ||
| 28 | + */ | ||
| 29 | + public InnerCommand getInstance(String command) { | ||
| 30 | + | ||
| 31 | + Map<String, String> allClazz = SystemCommandEnum.getAllClazz(); | ||
| 32 | + | ||
| 33 | + //兼容需要命令后接参数的数据 :q cross | ||
| 34 | + String[] trim = command.trim().split(" "); | ||
| 35 | + String clazz = allClazz.get(trim[0]); | ||
| 36 | + InnerCommand innerCommand = null; | ||
| 37 | + try { | ||
| 38 | + if (StringUtil.isEmpty(clazz)){ | ||
| 39 | + clazz = PrintAllCommand.class.getName() ; | ||
| 40 | + } | ||
| 41 | + innerCommand = (InnerCommand) SpringBeanFactory.getBean(Class.forName(clazz)); | ||
| 42 | + } catch (Exception e) { | ||
| 43 | + LOGGER.error("Exception", e); | ||
| 44 | + } | ||
| 45 | + | ||
| 46 | + return innerCommand; | ||
| 47 | + } | ||
| 48 | + | ||
| 49 | +} |
| @@ -54,4 +54,14 @@ public interface MsgHandle { | @@ -54,4 +54,14 @@ public interface MsgHandle { | ||
| 54 | * 关闭系统 | 54 | * 关闭系统 |
| 55 | */ | 55 | */ |
| 56 | void shutdown() ; | 56 | void shutdown() ; |
| 57 | + | ||
| 58 | + /** | ||
| 59 | + * 开启 AI 模式 | ||
| 60 | + */ | ||
| 61 | + void openAIModel() ; | ||
| 62 | + | ||
| 63 | + /** | ||
| 64 | + * 关闭 AI 模式 | ||
| 65 | + */ | ||
| 66 | + void closeAIModel() ; | ||
| 57 | } | 67 | } |
| 1 | package com.crossoverjie.cim.client.service.impl; | 1 | package com.crossoverjie.cim.client.service.impl; |
| 2 | 2 | ||
| 3 | -import com.alibaba.fastjson.JSON; | ||
| 4 | import com.crossoverjie.cim.client.client.CIMClient; | 3 | import com.crossoverjie.cim.client.client.CIMClient; |
| 5 | import com.crossoverjie.cim.client.config.AppConfiguration; | 4 | import com.crossoverjie.cim.client.config.AppConfiguration; |
| 6 | -import com.crossoverjie.cim.client.service.MsgHandle; | ||
| 7 | -import com.crossoverjie.cim.client.service.MsgLogger; | ||
| 8 | -import com.crossoverjie.cim.client.service.RouteRequest; | 5 | +import com.crossoverjie.cim.client.service.*; |
| 9 | import com.crossoverjie.cim.client.vo.req.GroupReqVO; | 6 | import com.crossoverjie.cim.client.vo.req.GroupReqVO; |
| 10 | import com.crossoverjie.cim.client.vo.req.P2PReqVO; | 7 | import com.crossoverjie.cim.client.vo.req.P2PReqVO; |
| 11 | import com.crossoverjie.cim.client.vo.res.OnlineUsersResVO; | 8 | import com.crossoverjie.cim.client.vo.res.OnlineUsersResVO; |
| 12 | import com.crossoverjie.cim.common.data.construct.TrieTree; | 9 | import com.crossoverjie.cim.common.data.construct.TrieTree; |
| 13 | -import com.crossoverjie.cim.common.enums.SystemCommandEnumType; | 10 | +import com.crossoverjie.cim.common.enums.SystemCommandEnum; |
| 14 | import com.crossoverjie.cim.common.util.StringUtil; | 11 | import com.crossoverjie.cim.common.util.StringUtil; |
| 15 | import org.slf4j.Logger; | 12 | import org.slf4j.Logger; |
| 16 | import org.slf4j.LoggerFactory; | 13 | import org.slf4j.LoggerFactory; |
| @@ -49,7 +46,10 @@ public class MsgHandler implements MsgHandle { | @@ -49,7 +46,10 @@ public class MsgHandler implements MsgHandle { | ||
| 49 | private MsgLogger msgLogger; | 46 | private MsgLogger msgLogger; |
| 50 | 47 | ||
| 51 | @Autowired | 48 | @Autowired |
| 52 | - private ClientInfo clientInfo ; | 49 | + private ClientInfo clientInfo; |
| 50 | + | ||
| 51 | + @Autowired | ||
| 52 | + private InnerCommandContext innerCommandContext ; | ||
| 53 | 53 | ||
| 54 | private boolean aiModel = false; | 54 | private boolean aiModel = false; |
| 55 | 55 | ||
| @@ -130,42 +130,10 @@ public class MsgHandler implements MsgHandle { | @@ -130,42 +130,10 @@ public class MsgHandler implements MsgHandle { | ||
| 130 | @Override | 130 | @Override |
| 131 | public boolean innerCommand(String msg) { | 131 | public boolean innerCommand(String msg) { |
| 132 | 132 | ||
| 133 | - // TODO: 2019-01-22 判断逻辑过多,需要重构。 | ||
| 134 | if (msg.startsWith(":")) { | 133 | if (msg.startsWith(":")) { |
| 135 | - Map<String, String> allStatusCode = SystemCommandEnumType.getAllStatusCode(); | ||
| 136 | - | ||
| 137 | - if (SystemCommandEnumType.QUIT.getCommandType().trim().equals(msg)) { | ||
| 138 | - //关闭系统 | ||
| 139 | - shutdown(); | ||
| 140 | - } else if (SystemCommandEnumType.ALL.getCommandType().trim().equals(msg)) { | ||
| 141 | - printAllCommand(allStatusCode); | ||
| 142 | - | ||
| 143 | - } else if (SystemCommandEnumType.ONLINE_USER.getCommandType().toLowerCase().trim().equals(msg.toLowerCase())) { | ||
| 144 | - //打印在线用户 | ||
| 145 | - printOnlineUsers(); | ||
| 146 | - | ||
| 147 | - } else if (msg.startsWith(SystemCommandEnumType.QUERY.getCommandType().trim() + " ")) { | ||
| 148 | - //查询聊天记录 | ||
| 149 | - queryChatHistory(msg); | ||
| 150 | - } else if (SystemCommandEnumType.AI.getCommandType().trim().equals(msg.toLowerCase())) { | ||
| 151 | - //开启 AI 模式 | ||
| 152 | - aiModel = true; | ||
| 153 | - System.out.println("\033[31;4m" + "Hello,我是估值两亿的 AI 机器人!" + "\033[0m"); | ||
| 154 | - } else if (SystemCommandEnumType.QAI.getCommandType().trim().equals(msg.toLowerCase())) { | ||
| 155 | - //关闭 AI 模式 | ||
| 156 | - aiModel = false; | ||
| 157 | - System.out.println("\033[31;4m" + "。゚(゚´ω`゚)゚。 AI 下线了!" + "\033[0m"); | ||
| 158 | - } else if (msg.startsWith(SystemCommandEnumType.PREFIX.getCommandType().trim() + " ")) { | ||
| 159 | - //模糊匹配 | ||
| 160 | - prefixSearch(msg); | ||
| 161 | - } else if (SystemCommandEnumType.INFO.getCommandType().trim().equals(msg.toLowerCase())) { | ||
| 162 | - LOGGER.info("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); | ||
| 163 | - LOGGER.info("client info=[{}]", JSON.toJSONString(clientInfo.get())); | ||
| 164 | - LOGGER.info("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); | ||
| 165 | 134 | ||
| 166 | - } else { | ||
| 167 | - printAllCommand(allStatusCode); | ||
| 168 | - } | 135 | + InnerCommand instance = innerCommandContext.getInstance(msg); |
| 136 | + instance.process(msg) ; | ||
| 169 | 137 | ||
| 170 | return true; | 138 | return true; |
| 171 | 139 | ||
| @@ -253,6 +221,16 @@ public class MsgHandler implements MsgHandle { | @@ -253,6 +221,16 @@ public class MsgHandler implements MsgHandle { | ||
| 253 | System.exit(0); | 221 | System.exit(0); |
| 254 | } | 222 | } |
| 255 | 223 | ||
| 224 | + @Override | ||
| 225 | + public void openAIModel() { | ||
| 226 | + aiModel = true; | ||
| 227 | + } | ||
| 228 | + | ||
| 229 | + @Override | ||
| 230 | + public void closeAIModel() { | ||
| 231 | + aiModel = false ; | ||
| 232 | + } | ||
| 233 | + | ||
| 256 | private void printAllCommand(Map<String, String> allStatusCode) { | 234 | private void printAllCommand(Map<String, String> allStatusCode) { |
| 257 | LOGGER.warn("===================================="); | 235 | LOGGER.warn("===================================="); |
| 258 | for (Map.Entry<String, String> stringStringEntry : allStatusCode.entrySet()) { | 236 | for (Map.Entry<String, String> stringStringEntry : allStatusCode.entrySet()) { |
cim-client/src/main/java/com/crossoverjie/cim/client/service/impl/command/CloseAIModelCommand.java
0 → 100644
| 1 | +package com.crossoverjie.cim.client.service.impl.command; | ||
| 2 | + | ||
| 3 | +import com.crossoverjie.cim.client.service.InnerCommand; | ||
| 4 | +import com.crossoverjie.cim.client.service.MsgHandle; | ||
| 5 | +import org.slf4j.Logger; | ||
| 6 | +import org.slf4j.LoggerFactory; | ||
| 7 | +import org.springframework.beans.factory.annotation.Autowired; | ||
| 8 | +import org.springframework.stereotype.Service; | ||
| 9 | + | ||
| 10 | +/** | ||
| 11 | + * Function: | ||
| 12 | + * | ||
| 13 | + * @author crossoverJie | ||
| 14 | + * Date: 2019-01-27 19:37 | ||
| 15 | + * @since JDK 1.8 | ||
| 16 | + */ | ||
| 17 | +@Service | ||
| 18 | +public class CloseAIModelCommand implements InnerCommand { | ||
| 19 | + private final static Logger LOGGER = LoggerFactory.getLogger(CloseAIModelCommand.class); | ||
| 20 | + | ||
| 21 | + | ||
| 22 | + @Autowired | ||
| 23 | + private MsgHandle msgHandle ; | ||
| 24 | + | ||
| 25 | + @Override | ||
| 26 | + public void process(String msg) { | ||
| 27 | + msgHandle.closeAIModel(); | ||
| 28 | + System.out.println("\033[31;4m" + "。゚(゚´ω`゚)゚。 AI 下线了!" + "\033[0m"); | ||
| 29 | + } | ||
| 30 | +} |
cim-client/src/main/java/com/crossoverjie/cim/client/service/impl/command/EchoInfoCommand.java
0 → 100644
| 1 | +package com.crossoverjie.cim.client.service.impl.command; | ||
| 2 | + | ||
| 3 | +import com.alibaba.fastjson.JSON; | ||
| 4 | +import com.crossoverjie.cim.client.service.InnerCommand; | ||
| 5 | +import com.crossoverjie.cim.client.service.impl.ClientInfo; | ||
| 6 | +import org.slf4j.Logger; | ||
| 7 | +import org.slf4j.LoggerFactory; | ||
| 8 | +import org.springframework.beans.factory.annotation.Autowired; | ||
| 9 | +import org.springframework.stereotype.Service; | ||
| 10 | + | ||
| 11 | +/** | ||
| 12 | + * Function: | ||
| 13 | + * | ||
| 14 | + * @author crossoverJie | ||
| 15 | + * Date: 2019-01-27 19:37 | ||
| 16 | + * @since JDK 1.8 | ||
| 17 | + */ | ||
| 18 | +@Service | ||
| 19 | +public class EchoInfoCommand implements InnerCommand { | ||
| 20 | + private final static Logger LOGGER = LoggerFactory.getLogger(EchoInfoCommand.class); | ||
| 21 | + | ||
| 22 | + | ||
| 23 | + @Autowired | ||
| 24 | + private ClientInfo clientInfo; | ||
| 25 | + | ||
| 26 | + @Override | ||
| 27 | + public void process(String msg) { | ||
| 28 | + LOGGER.info("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); | ||
| 29 | + LOGGER.info("client info=[{}]", JSON.toJSONString(clientInfo.get())); | ||
| 30 | + LOGGER.info("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); | ||
| 31 | + } | ||
| 32 | +} |
cim-client/src/main/java/com/crossoverjie/cim/client/service/impl/command/OpenAIModelCommand.java
0 → 100644
| 1 | +package com.crossoverjie.cim.client.service.impl.command; | ||
| 2 | + | ||
| 3 | +import com.crossoverjie.cim.client.service.InnerCommand; | ||
| 4 | +import com.crossoverjie.cim.client.service.MsgHandle; | ||
| 5 | +import org.slf4j.Logger; | ||
| 6 | +import org.slf4j.LoggerFactory; | ||
| 7 | +import org.springframework.beans.factory.annotation.Autowired; | ||
| 8 | +import org.springframework.stereotype.Service; | ||
| 9 | + | ||
| 10 | +/** | ||
| 11 | + * Function: | ||
| 12 | + * | ||
| 13 | + * @author crossoverJie | ||
| 14 | + * Date: 2019-01-27 19:37 | ||
| 15 | + * @since JDK 1.8 | ||
| 16 | + */ | ||
| 17 | +@Service | ||
| 18 | +public class OpenAIModelCommand implements InnerCommand { | ||
| 19 | + private final static Logger LOGGER = LoggerFactory.getLogger(OpenAIModelCommand.class); | ||
| 20 | + | ||
| 21 | + | ||
| 22 | + @Autowired | ||
| 23 | + private MsgHandle msgHandle ; | ||
| 24 | + | ||
| 25 | + @Override | ||
| 26 | + public void process(String msg) { | ||
| 27 | + msgHandle.openAIModel(); | ||
| 28 | + System.out.println("\033[31;4m" + "Hello,我是估值两亿的 AI 机器人!" + "\033[0m"); | ||
| 29 | + } | ||
| 30 | +} |
cim-client/src/main/java/com/crossoverjie/cim/client/service/impl/command/PrefixSearchCommand.java
0 → 100644
| 1 | +package com.crossoverjie.cim.client.service.impl.command; | ||
| 2 | + | ||
| 3 | +import com.crossoverjie.cim.client.service.InnerCommand; | ||
| 4 | +import com.crossoverjie.cim.client.service.RouteRequest; | ||
| 5 | +import com.crossoverjie.cim.client.vo.res.OnlineUsersResVO; | ||
| 6 | +import com.crossoverjie.cim.common.data.construct.TrieTree; | ||
| 7 | +import org.slf4j.Logger; | ||
| 8 | +import org.slf4j.LoggerFactory; | ||
| 9 | +import org.springframework.beans.factory.annotation.Autowired; | ||
| 10 | +import org.springframework.stereotype.Service; | ||
| 11 | + | ||
| 12 | +import java.util.List; | ||
| 13 | + | ||
| 14 | +/** | ||
| 15 | + * Function: | ||
| 16 | + * | ||
| 17 | + * @author crossoverJie | ||
| 18 | + * Date: 2019-01-27 19:37 | ||
| 19 | + * @since JDK 1.8 | ||
| 20 | + */ | ||
| 21 | +@Service | ||
| 22 | +public class PrefixSearchCommand implements InnerCommand { | ||
| 23 | + private final static Logger LOGGER = LoggerFactory.getLogger(PrefixSearchCommand.class); | ||
| 24 | + | ||
| 25 | + | ||
| 26 | + @Autowired | ||
| 27 | + private RouteRequest routeRequest ; | ||
| 28 | + | ||
| 29 | + @Override | ||
| 30 | + public void process(String msg) { | ||
| 31 | + try { | ||
| 32 | + List<OnlineUsersResVO.DataBodyBean> onlineUsers = routeRequest.onlineUsers(); | ||
| 33 | + TrieTree trieTree = new TrieTree(); | ||
| 34 | + for (OnlineUsersResVO.DataBodyBean onlineUser : onlineUsers) { | ||
| 35 | + trieTree.insert(onlineUser.getUserName()); | ||
| 36 | + } | ||
| 37 | + | ||
| 38 | + String[] split = msg.split(" "); | ||
| 39 | + String key = split[1]; | ||
| 40 | + List<String> list = trieTree.prefixSearch(key); | ||
| 41 | + | ||
| 42 | + for (String res : list) { | ||
| 43 | + res = res.replace(key, "\033[31;4m" + key + "\033[0m"); | ||
| 44 | + System.out.println(res); | ||
| 45 | + } | ||
| 46 | + | ||
| 47 | + } catch (Exception e) { | ||
| 48 | + LOGGER.error("Exception", e); | ||
| 49 | + } | ||
| 50 | + } | ||
| 51 | +} |
cim-client/src/main/java/com/crossoverjie/cim/client/service/impl/command/PrintAllCommand.java
0 → 100644
| 1 | +package com.crossoverjie.cim.client.service.impl.command; | ||
| 2 | + | ||
| 3 | +import com.crossoverjie.cim.client.service.InnerCommand; | ||
| 4 | +import com.crossoverjie.cim.common.enums.SystemCommandEnum; | ||
| 5 | +import org.slf4j.Logger; | ||
| 6 | +import org.slf4j.LoggerFactory; | ||
| 7 | +import org.springframework.stereotype.Service; | ||
| 8 | + | ||
| 9 | +import java.util.Map; | ||
| 10 | + | ||
| 11 | +/** | ||
| 12 | + * Function: | ||
| 13 | + * | ||
| 14 | + * @author crossoverJie | ||
| 15 | + * Date: 2019-01-27 19:37 | ||
| 16 | + * @since JDK 1.8 | ||
| 17 | + */ | ||
| 18 | +@Service | ||
| 19 | +public class PrintAllCommand implements InnerCommand { | ||
| 20 | + private final static Logger LOGGER = LoggerFactory.getLogger(PrintAllCommand.class); | ||
| 21 | + | ||
| 22 | + @Override | ||
| 23 | + public void process(String msg) { | ||
| 24 | + Map<String, String> allStatusCode = SystemCommandEnum.getAllStatusCode(); | ||
| 25 | + LOGGER.warn("===================================="); | ||
| 26 | + for (Map.Entry<String, String> stringStringEntry : allStatusCode.entrySet()) { | ||
| 27 | + String key = stringStringEntry.getKey(); | ||
| 28 | + String value = stringStringEntry.getValue(); | ||
| 29 | + LOGGER.warn(key + "----->" + value); | ||
| 30 | + } | ||
| 31 | + LOGGER.warn("===================================="); | ||
| 32 | + } | ||
| 33 | +} |
| 1 | +package com.crossoverjie.cim.client.service.impl.command; | ||
| 2 | + | ||
| 3 | +import com.crossoverjie.cim.client.service.InnerCommand; | ||
| 4 | +import com.crossoverjie.cim.client.service.RouteRequest; | ||
| 5 | +import com.crossoverjie.cim.client.vo.res.OnlineUsersResVO; | ||
| 6 | +import org.slf4j.Logger; | ||
| 7 | +import org.slf4j.LoggerFactory; | ||
| 8 | +import org.springframework.beans.factory.annotation.Autowired; | ||
| 9 | +import org.springframework.stereotype.Service; | ||
| 10 | + | ||
| 11 | +import java.util.List; | ||
| 12 | + | ||
| 13 | +/** | ||
| 14 | + * Function: | ||
| 15 | + * | ||
| 16 | + * @author crossoverJie | ||
| 17 | + * Date: 2019-01-27 19:37 | ||
| 18 | + * @since JDK 1.8 | ||
| 19 | + */ | ||
| 20 | +@Service | ||
| 21 | +public class PrintOnlineUsersCommand implements InnerCommand { | ||
| 22 | + private final static Logger LOGGER = LoggerFactory.getLogger(PrintOnlineUsersCommand.class); | ||
| 23 | + | ||
| 24 | + | ||
| 25 | + @Autowired | ||
| 26 | + private RouteRequest routeRequest ; | ||
| 27 | + | ||
| 28 | + @Override | ||
| 29 | + public void process(String msg) { | ||
| 30 | + try { | ||
| 31 | + List<OnlineUsersResVO.DataBodyBean> onlineUsers = routeRequest.onlineUsers(); | ||
| 32 | + | ||
| 33 | + LOGGER.info("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); | ||
| 34 | + for (OnlineUsersResVO.DataBodyBean onlineUser : onlineUsers) { | ||
| 35 | + LOGGER.info("userId={}=====userName={}", onlineUser.getUserId(), onlineUser.getUserName()); | ||
| 36 | + } | ||
| 37 | + LOGGER.info("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); | ||
| 38 | + | ||
| 39 | + } catch (Exception e) { | ||
| 40 | + LOGGER.error("Exception", e); | ||
| 41 | + } | ||
| 42 | + } | ||
| 43 | +} |
cim-client/src/main/java/com/crossoverjie/cim/client/service/impl/command/QueryHistoryCommand.java
0 → 100644
| 1 | +package com.crossoverjie.cim.client.service.impl.command; | ||
| 2 | + | ||
| 3 | +import com.crossoverjie.cim.client.service.InnerCommand; | ||
| 4 | +import com.crossoverjie.cim.client.service.MsgLogger; | ||
| 5 | +import org.slf4j.Logger; | ||
| 6 | +import org.slf4j.LoggerFactory; | ||
| 7 | +import org.springframework.beans.factory.annotation.Autowired; | ||
| 8 | +import org.springframework.stereotype.Service; | ||
| 9 | + | ||
| 10 | +/** | ||
| 11 | + * Function: | ||
| 12 | + * | ||
| 13 | + * @author crossoverJie | ||
| 14 | + * Date: 2019-01-27 19:37 | ||
| 15 | + * @since JDK 1.8 | ||
| 16 | + */ | ||
| 17 | +@Service | ||
| 18 | +public class QueryHistoryCommand implements InnerCommand { | ||
| 19 | + private final static Logger LOGGER = LoggerFactory.getLogger(QueryHistoryCommand.class); | ||
| 20 | + | ||
| 21 | + | ||
| 22 | + @Autowired | ||
| 23 | + private MsgLogger msgLogger ; | ||
| 24 | + | ||
| 25 | + @Override | ||
| 26 | + public void process(String msg) { | ||
| 27 | + String[] split = msg.split(" "); | ||
| 28 | + String res = msgLogger.query(split[1]); | ||
| 29 | + System.out.println(res); | ||
| 30 | + } | ||
| 31 | +} |
cim-client/src/main/java/com/crossoverjie/cim/client/service/impl/command/ShutDownCommand.java
0 → 100644
| 1 | +package com.crossoverjie.cim.client.service.impl.command; | ||
| 2 | + | ||
| 3 | +import com.crossoverjie.cim.client.client.CIMClient; | ||
| 4 | +import com.crossoverjie.cim.client.service.InnerCommand; | ||
| 5 | +import com.crossoverjie.cim.client.service.MsgLogger; | ||
| 6 | +import com.crossoverjie.cim.client.service.RouteRequest; | ||
| 7 | +import org.slf4j.Logger; | ||
| 8 | +import org.slf4j.LoggerFactory; | ||
| 9 | +import org.springframework.beans.factory.annotation.Autowired; | ||
| 10 | +import org.springframework.stereotype.Service; | ||
| 11 | + | ||
| 12 | +import javax.annotation.Resource; | ||
| 13 | +import java.util.concurrent.ThreadPoolExecutor; | ||
| 14 | +import java.util.concurrent.TimeUnit; | ||
| 15 | + | ||
| 16 | +/** | ||
| 17 | + * Function: | ||
| 18 | + * | ||
| 19 | + * @author crossoverJie | ||
| 20 | + * Date: 2019-01-27 19:28 | ||
| 21 | + * @since JDK 1.8 | ||
| 22 | + */ | ||
| 23 | +@Service | ||
| 24 | +public class ShutDownCommand implements InnerCommand { | ||
| 25 | + private final static Logger LOGGER = LoggerFactory.getLogger(ShutDownCommand.class); | ||
| 26 | + | ||
| 27 | + @Autowired | ||
| 28 | + private RouteRequest routeRequest ; | ||
| 29 | + | ||
| 30 | + @Autowired | ||
| 31 | + private CIMClient cimClient; | ||
| 32 | + | ||
| 33 | + @Autowired | ||
| 34 | + private MsgLogger msgLogger; | ||
| 35 | + | ||
| 36 | + @Resource(name = "callBackThreadPool") | ||
| 37 | + private ThreadPoolExecutor executor; | ||
| 38 | + | ||
| 39 | + @Override | ||
| 40 | + public void process(String msg) { | ||
| 41 | + LOGGER.info("系统关闭中。。。。"); | ||
| 42 | + routeRequest.offLine(); | ||
| 43 | + msgLogger.stop(); | ||
| 44 | + executor.shutdown(); | ||
| 45 | + try { | ||
| 46 | + while (!executor.awaitTermination(1, TimeUnit.SECONDS)) { | ||
| 47 | + LOGGER.info("线程池关闭中。。。。"); | ||
| 48 | + } | ||
| 49 | + cimClient.close(); | ||
| 50 | + } catch (InterruptedException e) { | ||
| 51 | + LOGGER.error("InterruptedException", e); | ||
| 52 | + } | ||
| 53 | + System.exit(0); | ||
| 54 | + } | ||
| 55 | +} |
| 1 | +package com.crossoverjie.cim.client.service; | ||
| 2 | + | ||
| 3 | +import com.crossoverjie.cim.client.CIMClientApplication; | ||
| 4 | +import com.crossoverjie.cim.common.enums.SystemCommandEnum; | ||
| 5 | +import org.junit.Test; | ||
| 6 | +import org.junit.runner.RunWith; | ||
| 7 | +import org.springframework.beans.factory.annotation.Autowired; | ||
| 8 | +import org.springframework.boot.test.context.SpringBootTest; | ||
| 9 | +import org.springframework.test.context.junit4.SpringRunner; | ||
| 10 | + | ||
| 11 | +@SpringBootTest(classes = CIMClientApplication.class) | ||
| 12 | +@RunWith(SpringRunner.class) | ||
| 13 | +public class InnerCommandContextTest { | ||
| 14 | + | ||
| 15 | + @Autowired | ||
| 16 | + private InnerCommandContext context; | ||
| 17 | + | ||
| 18 | + @Test | ||
| 19 | + public void execute() { | ||
| 20 | + String msg = ":all"; | ||
| 21 | + InnerCommand execute = context.getInstance(msg); | ||
| 22 | + execute.process(msg) ; | ||
| 23 | + } | ||
| 24 | + | ||
| 25 | + @Test | ||
| 26 | + public void execute3() { | ||
| 27 | + String msg = SystemCommandEnum.ONLINE_USER.getCommandType(); | ||
| 28 | + InnerCommand execute = context.getInstance(msg); | ||
| 29 | + execute.process(msg) ; | ||
| 30 | + } | ||
| 31 | + | ||
| 32 | + @Test | ||
| 33 | + public void execute4() { | ||
| 34 | + String msg = ":q 天气"; | ||
| 35 | + InnerCommand execute = context.getInstance(msg); | ||
| 36 | + execute.process(msg) ; | ||
| 37 | + } | ||
| 38 | + | ||
| 39 | + @Test | ||
| 40 | + public void execute5() { | ||
| 41 | + String msg = ":q crossoverJie"; | ||
| 42 | + InnerCommand execute = context.getInstance(msg); | ||
| 43 | + execute.process(msg) ; | ||
| 44 | + } | ||
| 45 | + | ||
| 46 | + @Test | ||
| 47 | + public void execute6() { | ||
| 48 | + String msg = SystemCommandEnum.AI.getCommandType(); | ||
| 49 | + InnerCommand execute = context.getInstance(msg); | ||
| 50 | + execute.process(msg) ; | ||
| 51 | + } | ||
| 52 | + | ||
| 53 | + @Test | ||
| 54 | + public void execute7() { | ||
| 55 | + String msg = SystemCommandEnum.QAI.getCommandType(); | ||
| 56 | + InnerCommand execute = context.getInstance(msg); | ||
| 57 | + execute.process(msg) ; | ||
| 58 | + } | ||
| 59 | + | ||
| 60 | + @Test | ||
| 61 | + public void execute8() { | ||
| 62 | + String msg = ":pu cross"; | ||
| 63 | + InnerCommand execute = context.getInstance(msg); | ||
| 64 | + execute.process(msg) ; | ||
| 65 | + } | ||
| 66 | + | ||
| 67 | + @Test | ||
| 68 | + public void execute9() { | ||
| 69 | + String msg = SystemCommandEnum.INFO.getCommandType(); | ||
| 70 | + InnerCommand execute = context.getInstance(msg); | ||
| 71 | + execute.process(msg) ; | ||
| 72 | + } | ||
| 73 | + | ||
| 74 | + @Test | ||
| 75 | + public void execute10() { | ||
| 76 | + String msg = "dsds"; | ||
| 77 | + InnerCommand execute = context.getInstance(msg); | ||
| 78 | + execute.process(msg) ; | ||
| 79 | + } | ||
| 80 | + | ||
| 81 | + | ||
| 82 | + | ||
| 83 | + // @Test | ||
| 84 | + public void quit() { | ||
| 85 | + String msg = ":q!"; | ||
| 86 | + InnerCommand execute = context.getInstance(msg); | ||
| 87 | + execute.process(msg) ; | ||
| 88 | + } | ||
| 89 | +} |
| 1 | package com.crossoverjie.cim.common.enums; | 1 | package com.crossoverjie.cim.common.enums; |
| 2 | 2 | ||
| 3 | -import java.util.ArrayList; | ||
| 4 | import java.util.HashMap; | 3 | import java.util.HashMap; |
| 5 | -import java.util.List; | ||
| 6 | import java.util.Map; | 4 | import java.util.Map; |
| 7 | 5 | ||
| 8 | /** | 6 | /** |
| @@ -12,16 +10,16 @@ import java.util.Map; | @@ -12,16 +10,16 @@ import java.util.Map; | ||
| 12 | * Date: 2018/12/26 18:38 | 10 | * Date: 2018/12/26 18:38 |
| 13 | * @since JDK 1.8 | 11 | * @since JDK 1.8 |
| 14 | */ | 12 | */ |
| 15 | -public enum SystemCommandEnumType { | 13 | +public enum SystemCommandEnum { |
| 16 | 14 | ||
| 17 | - ALL(":all ","获取所有命令"), | ||
| 18 | - ONLINE_USER(":olu ","获取所有在线用户"), | ||
| 19 | - QUIT(":q! ","退出程序"), | ||
| 20 | - QUERY(":q ","【:q 关键字】查询聊天记录"), | ||
| 21 | - AI(":ai ","开启 AI 模式"), | ||
| 22 | - QAI(":qai ","关闭 AI 模式"), | ||
| 23 | - PREFIX(":pu ","模糊匹配用户"), | ||
| 24 | - INFO(":info ","获取客户端信息") | 15 | + ALL(":all ","获取所有命令","PrintAllCommand"), |
| 16 | + ONLINE_USER(":olu ","获取所有在线用户","PrintOnlineUsersCommand"), | ||
| 17 | + QUIT(":q! ","退出程序","ShutDownCommand"), | ||
| 18 | + QUERY(":q ","【:q 关键字】查询聊天记录","QueryHistoryCommand"), | ||
| 19 | + AI(":ai ","开启 AI 模式","OpenAIModelCommand"), | ||
| 20 | + QAI(":qai ","关闭 AI 模式","CloseAIModelCommand"), | ||
| 21 | + PREFIX(":pu ","模糊匹配用户","PrefixSearchCommand"), | ||
| 22 | + INFO(":info ","获取客户端信息","EchoInfoCommand") | ||
| 25 | 23 | ||
| 26 | ; | 24 | ; |
| 27 | 25 | ||
| @@ -31,15 +29,21 @@ public enum SystemCommandEnumType { | @@ -31,15 +29,21 @@ public enum SystemCommandEnumType { | ||
| 31 | /** 枚举描述 */ | 29 | /** 枚举描述 */ |
| 32 | private final String desc; | 30 | private final String desc; |
| 33 | 31 | ||
| 32 | + /** | ||
| 33 | + * 实现类 | ||
| 34 | + */ | ||
| 35 | + private final String clazz ; | ||
| 36 | + | ||
| 34 | 37 | ||
| 35 | /** | 38 | /** |
| 36 | * 构建一个 。 | 39 | * 构建一个 。 |
| 37 | * @param commandType 枚举值码。 | 40 | * @param commandType 枚举值码。 |
| 38 | * @param desc 枚举描述。 | 41 | * @param desc 枚举描述。 |
| 39 | */ | 42 | */ |
| 40 | - private SystemCommandEnumType(String commandType, String desc) { | 43 | + private SystemCommandEnum(String commandType, String desc, String clazz) { |
| 41 | this.commandType = commandType; | 44 | this.commandType = commandType; |
| 42 | this.desc = desc; | 45 | this.desc = desc; |
| 46 | + this.clazz = clazz ; | ||
| 43 | } | 47 | } |
| 44 | 48 | ||
| 45 | /** | 49 | /** |
| @@ -49,6 +53,13 @@ public enum SystemCommandEnumType { | @@ -49,6 +53,13 @@ public enum SystemCommandEnumType { | ||
| 49 | public String getCommandType() { | 53 | public String getCommandType() { |
| 50 | return commandType; | 54 | return commandType; |
| 51 | } | 55 | } |
| 56 | + /** | ||
| 57 | + * 获取 class。 | ||
| 58 | + * @return class。 | ||
| 59 | + */ | ||
| 60 | + public String getClazz() { | ||
| 61 | + return clazz; | ||
| 62 | + } | ||
| 52 | 63 | ||
| 53 | /** | 64 | /** |
| 54 | * 得到枚举描述。 | 65 | * 得到枚举描述。 |
| @@ -80,13 +91,21 @@ public enum SystemCommandEnumType { | @@ -80,13 +91,21 @@ public enum SystemCommandEnumType { | ||
| 80 | * @return 全部枚举值码。 | 91 | * @return 全部枚举值码。 |
| 81 | */ | 92 | */ |
| 82 | public static Map<String,String> getAllStatusCode() { | 93 | public static Map<String,String> getAllStatusCode() { |
| 83 | - List<String> list = new ArrayList<String>(); | ||
| 84 | Map<String,String> map = new HashMap<String, String>(16) ; | 94 | Map<String,String> map = new HashMap<String, String>(16) ; |
| 85 | - for (SystemCommandEnumType status : values()) { | ||
| 86 | - list.add(status.code()); | 95 | + for (SystemCommandEnum status : values()) { |
| 87 | map.put(status.getCommandType(),status.getDesc()) ; | 96 | map.put(status.getCommandType(),status.getDesc()) ; |
| 88 | } | 97 | } |
| 89 | return map; | 98 | return map; |
| 90 | } | 99 | } |
| 91 | 100 | ||
| 101 | + public static Map<String,String> getAllClazz() { | ||
| 102 | + Map<String,String> map = new HashMap<String, String>(16) ; | ||
| 103 | + for (SystemCommandEnum status : values()) { | ||
| 104 | + map.put(status.getCommandType().trim(),"com.crossoverjie.cim.client.service.impl.command." + status.getClazz()) ; | ||
| 105 | + } | ||
| 106 | + return map; | ||
| 107 | + } | ||
| 108 | + | ||
| 109 | + | ||
| 110 | + | ||
| 92 | } | 111 | } |
| @@ -9,7 +9,7 @@ public class SystemCommandEnumTypeTest { | @@ -9,7 +9,7 @@ public class SystemCommandEnumTypeTest { | ||
| 9 | 9 | ||
| 10 | @Test | 10 | @Test |
| 11 | public void getAllStatusCode() throws Exception { | 11 | public void getAllStatusCode() throws Exception { |
| 12 | - Map<String, String> allStatusCode = SystemCommandEnumType.getAllStatusCode(); | 12 | + Map<String, String> allStatusCode = SystemCommandEnum.getAllStatusCode(); |
| 13 | for (Map.Entry<String, String> stringStringEntry : allStatusCode.entrySet()) { | 13 | for (Map.Entry<String, String> stringStringEntry : allStatusCode.entrySet()) { |
| 14 | String key = stringStringEntry.getKey(); | 14 | String key = stringStringEntry.getKey(); |
| 15 | String value = stringStringEntry.getValue(); | 15 | String value = stringStringEntry.getValue(); |
-
请 注册 或 登录 后发表评论