作者 crossoverJie
提交者 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 {
* @throws InterruptedException
*/
public void close() throws InterruptedException {
if (channel != null){
channel.close();
}
}
}
... ...
package com.crossoverjie.cim.client.service;
/**
* Function:
*
* @author crossoverJie
* Date: 2019-01-27 19:26
* @since JDK 1.8
*/
public interface InnerCommand {
/**
* 执行
* @param msg
*/
void process(String msg) ;
}
... ...
package com.crossoverjie.cim.client.service;
import com.crossoverjie.cim.client.service.impl.command.PrintAllCommand;
import com.crossoverjie.cim.client.util.SpringBeanFactory;
import com.crossoverjie.cim.common.enums.SystemCommandEnum;
import com.crossoverjie.cim.common.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.util.Map;
/**
* Function:
*
* @author crossoverJie
* Date: 2019-01-27 19:39
* @since JDK 1.8
*/
@Component
public class InnerCommandContext {
private final static Logger LOGGER = LoggerFactory.getLogger(InnerCommandContext.class);
/**
* 获取执行器实例
* @param command 执行器实例
* @return
*/
public InnerCommand getInstance(String command) {
Map<String, String> allClazz = SystemCommandEnum.getAllClazz();
//兼容需要命令后接参数的数据 :q cross
String[] trim = command.trim().split(" ");
String clazz = allClazz.get(trim[0]);
InnerCommand innerCommand = null;
try {
if (StringUtil.isEmpty(clazz)){
clazz = PrintAllCommand.class.getName() ;
}
innerCommand = (InnerCommand) SpringBeanFactory.getBean(Class.forName(clazz));
} catch (Exception e) {
LOGGER.error("Exception", e);
}
return innerCommand;
}
}
... ...
... ... @@ -54,4 +54,14 @@ public interface MsgHandle {
* 关闭系统
*/
void shutdown() ;
/**
* 开启 AI 模式
*/
void openAIModel() ;
/**
* 关闭 AI 模式
*/
void closeAIModel() ;
}
... ...
package com.crossoverjie.cim.client.service.impl;
import com.alibaba.fastjson.JSON;
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.MsgLogger;
import com.crossoverjie.cim.client.service.RouteRequest;
import com.crossoverjie.cim.client.service.*;
import com.crossoverjie.cim.client.vo.req.GroupReqVO;
import com.crossoverjie.cim.client.vo.req.P2PReqVO;
import com.crossoverjie.cim.client.vo.res.OnlineUsersResVO;
import com.crossoverjie.cim.common.data.construct.TrieTree;
import com.crossoverjie.cim.common.enums.SystemCommandEnumType;
import com.crossoverjie.cim.common.enums.SystemCommandEnum;
import com.crossoverjie.cim.common.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
... ... @@ -49,7 +46,10 @@ public class MsgHandler implements MsgHandle {
private MsgLogger msgLogger;
@Autowired
private ClientInfo clientInfo ;
private ClientInfo clientInfo;
@Autowired
private InnerCommandContext innerCommandContext ;
private boolean aiModel = false;
... ... @@ -130,42 +130,10 @@ public class MsgHandler implements MsgHandle {
@Override
public boolean innerCommand(String msg) {
// TODO: 2019-01-22 判断逻辑过多,需要重构。
if (msg.startsWith(":")) {
Map<String, String> allStatusCode = SystemCommandEnumType.getAllStatusCode();
if (SystemCommandEnumType.QUIT.getCommandType().trim().equals(msg)) {
//关闭系统
shutdown();
} else if (SystemCommandEnumType.ALL.getCommandType().trim().equals(msg)) {
printAllCommand(allStatusCode);
} else if (SystemCommandEnumType.ONLINE_USER.getCommandType().toLowerCase().trim().equals(msg.toLowerCase())) {
//打印在线用户
printOnlineUsers();
} else if (msg.startsWith(SystemCommandEnumType.QUERY.getCommandType().trim() + " ")) {
//查询聊天记录
queryChatHistory(msg);
} else if (SystemCommandEnumType.AI.getCommandType().trim().equals(msg.toLowerCase())) {
//开启 AI 模式
aiModel = true;
System.out.println("\033[31;4m" + "Hello,我是估值两亿的 AI 机器人!" + "\033[0m");
} else if (SystemCommandEnumType.QAI.getCommandType().trim().equals(msg.toLowerCase())) {
//关闭 AI 模式
aiModel = false;
System.out.println("\033[31;4m" + "。゚(゚´ω`゚)゚。 AI 下线了!" + "\033[0m");
} else if (msg.startsWith(SystemCommandEnumType.PREFIX.getCommandType().trim() + " ")) {
//模糊匹配
prefixSearch(msg);
} else if (SystemCommandEnumType.INFO.getCommandType().trim().equals(msg.toLowerCase())) {
LOGGER.info("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
LOGGER.info("client info=[{}]", JSON.toJSONString(clientInfo.get()));
LOGGER.info("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
} else {
printAllCommand(allStatusCode);
}
InnerCommand instance = innerCommandContext.getInstance(msg);
instance.process(msg) ;
return true;
... ... @@ -253,6 +221,16 @@ public class MsgHandler implements MsgHandle {
System.exit(0);
}
@Override
public void openAIModel() {
aiModel = true;
}
@Override
public void closeAIModel() {
aiModel = false ;
}
private void printAllCommand(Map<String, String> allStatusCode) {
LOGGER.warn("====================================");
for (Map.Entry<String, String> stringStringEntry : allStatusCode.entrySet()) {
... ...
package com.crossoverjie.cim.client.service.impl.command;
import com.crossoverjie.cim.client.service.InnerCommand;
import com.crossoverjie.cim.client.service.MsgHandle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* Function:
*
* @author crossoverJie
* Date: 2019-01-27 19:37
* @since JDK 1.8
*/
@Service
public class CloseAIModelCommand implements InnerCommand {
private final static Logger LOGGER = LoggerFactory.getLogger(CloseAIModelCommand.class);
@Autowired
private MsgHandle msgHandle ;
@Override
public void process(String msg) {
msgHandle.closeAIModel();
System.out.println("\033[31;4m" + "。゚(゚´ω`゚)゚。 AI 下线了!" + "\033[0m");
}
}
... ...
package com.crossoverjie.cim.client.service.impl.command;
import com.alibaba.fastjson.JSON;
import com.crossoverjie.cim.client.service.InnerCommand;
import com.crossoverjie.cim.client.service.impl.ClientInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* Function:
*
* @author crossoverJie
* Date: 2019-01-27 19:37
* @since JDK 1.8
*/
@Service
public class EchoInfoCommand implements InnerCommand {
private final static Logger LOGGER = LoggerFactory.getLogger(EchoInfoCommand.class);
@Autowired
private ClientInfo clientInfo;
@Override
public void process(String msg) {
LOGGER.info("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
LOGGER.info("client info=[{}]", JSON.toJSONString(clientInfo.get()));
LOGGER.info("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
}
}
... ...
package com.crossoverjie.cim.client.service.impl.command;
import com.crossoverjie.cim.client.service.InnerCommand;
import com.crossoverjie.cim.client.service.MsgHandle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* Function:
*
* @author crossoverJie
* Date: 2019-01-27 19:37
* @since JDK 1.8
*/
@Service
public class OpenAIModelCommand implements InnerCommand {
private final static Logger LOGGER = LoggerFactory.getLogger(OpenAIModelCommand.class);
@Autowired
private MsgHandle msgHandle ;
@Override
public void process(String msg) {
msgHandle.openAIModel();
System.out.println("\033[31;4m" + "Hello,我是估值两亿的 AI 机器人!" + "\033[0m");
}
}
... ...
package com.crossoverjie.cim.client.service.impl.command;
import com.crossoverjie.cim.client.service.InnerCommand;
import com.crossoverjie.cim.client.service.RouteRequest;
import com.crossoverjie.cim.client.vo.res.OnlineUsersResVO;
import com.crossoverjie.cim.common.data.construct.TrieTree;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* Function:
*
* @author crossoverJie
* Date: 2019-01-27 19:37
* @since JDK 1.8
*/
@Service
public class PrefixSearchCommand implements InnerCommand {
private final static Logger LOGGER = LoggerFactory.getLogger(PrefixSearchCommand.class);
@Autowired
private RouteRequest routeRequest ;
@Override
public void process(String msg) {
try {
List<OnlineUsersResVO.DataBodyBean> onlineUsers = routeRequest.onlineUsers();
TrieTree trieTree = new TrieTree();
for (OnlineUsersResVO.DataBodyBean onlineUser : onlineUsers) {
trieTree.insert(onlineUser.getUserName());
}
String[] split = msg.split(" ");
String key = split[1];
List<String> list = trieTree.prefixSearch(key);
for (String res : list) {
res = res.replace(key, "\033[31;4m" + key + "\033[0m");
System.out.println(res);
}
} catch (Exception e) {
LOGGER.error("Exception", e);
}
}
}
... ...
package com.crossoverjie.cim.client.service.impl.command;
import com.crossoverjie.cim.client.service.InnerCommand;
import com.crossoverjie.cim.common.enums.SystemCommandEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import java.util.Map;
/**
* Function:
*
* @author crossoverJie
* Date: 2019-01-27 19:37
* @since JDK 1.8
*/
@Service
public class PrintAllCommand implements InnerCommand {
private final static Logger LOGGER = LoggerFactory.getLogger(PrintAllCommand.class);
@Override
public void process(String msg) {
Map<String, String> allStatusCode = SystemCommandEnum.getAllStatusCode();
LOGGER.warn("====================================");
for (Map.Entry<String, String> stringStringEntry : allStatusCode.entrySet()) {
String key = stringStringEntry.getKey();
String value = stringStringEntry.getValue();
LOGGER.warn(key + "----->" + value);
}
LOGGER.warn("====================================");
}
}
... ...
package com.crossoverjie.cim.client.service.impl.command;
import com.crossoverjie.cim.client.service.InnerCommand;
import com.crossoverjie.cim.client.service.RouteRequest;
import com.crossoverjie.cim.client.vo.res.OnlineUsersResVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* Function:
*
* @author crossoverJie
* Date: 2019-01-27 19:37
* @since JDK 1.8
*/
@Service
public class PrintOnlineUsersCommand implements InnerCommand {
private final static Logger LOGGER = LoggerFactory.getLogger(PrintOnlineUsersCommand.class);
@Autowired
private RouteRequest routeRequest ;
@Override
public void process(String msg) {
try {
List<OnlineUsersResVO.DataBodyBean> onlineUsers = routeRequest.onlineUsers();
LOGGER.info("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
for (OnlineUsersResVO.DataBodyBean onlineUser : onlineUsers) {
LOGGER.info("userId={}=====userName={}", onlineUser.getUserId(), onlineUser.getUserName());
}
LOGGER.info("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
} catch (Exception e) {
LOGGER.error("Exception", e);
}
}
}
... ...
package com.crossoverjie.cim.client.service.impl.command;
import com.crossoverjie.cim.client.service.InnerCommand;
import com.crossoverjie.cim.client.service.MsgLogger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* Function:
*
* @author crossoverJie
* Date: 2019-01-27 19:37
* @since JDK 1.8
*/
@Service
public class QueryHistoryCommand implements InnerCommand {
private final static Logger LOGGER = LoggerFactory.getLogger(QueryHistoryCommand.class);
@Autowired
private MsgLogger msgLogger ;
@Override
public void process(String msg) {
String[] split = msg.split(" ");
String res = msgLogger.query(split[1]);
System.out.println(res);
}
}
... ...
package com.crossoverjie.cim.client.service.impl.command;
import com.crossoverjie.cim.client.client.CIMClient;
import com.crossoverjie.cim.client.service.InnerCommand;
import com.crossoverjie.cim.client.service.MsgLogger;
import com.crossoverjie.cim.client.service.RouteRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* Function:
*
* @author crossoverJie
* Date: 2019-01-27 19:28
* @since JDK 1.8
*/
@Service
public class ShutDownCommand implements InnerCommand {
private final static Logger LOGGER = LoggerFactory.getLogger(ShutDownCommand.class);
@Autowired
private RouteRequest routeRequest ;
@Autowired
private CIMClient cimClient;
@Autowired
private MsgLogger msgLogger;
@Resource(name = "callBackThreadPool")
private ThreadPoolExecutor executor;
@Override
public void process(String msg) {
LOGGER.info("系统关闭中。。。。");
routeRequest.offLine();
msgLogger.stop();
executor.shutdown();
try {
while (!executor.awaitTermination(1, TimeUnit.SECONDS)) {
LOGGER.info("线程池关闭中。。。。");
}
cimClient.close();
} catch (InterruptedException e) {
LOGGER.error("InterruptedException", e);
}
System.exit(0);
}
}
... ...
package com.crossoverjie.cim.client.service;
import com.crossoverjie.cim.client.CIMClientApplication;
import com.crossoverjie.cim.common.enums.SystemCommandEnum;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@SpringBootTest(classes = CIMClientApplication.class)
@RunWith(SpringRunner.class)
public class InnerCommandContextTest {
@Autowired
private InnerCommandContext context;
@Test
public void execute() {
String msg = ":all";
InnerCommand execute = context.getInstance(msg);
execute.process(msg) ;
}
@Test
public void execute3() {
String msg = SystemCommandEnum.ONLINE_USER.getCommandType();
InnerCommand execute = context.getInstance(msg);
execute.process(msg) ;
}
@Test
public void execute4() {
String msg = ":q 天气";
InnerCommand execute = context.getInstance(msg);
execute.process(msg) ;
}
@Test
public void execute5() {
String msg = ":q crossoverJie";
InnerCommand execute = context.getInstance(msg);
execute.process(msg) ;
}
@Test
public void execute6() {
String msg = SystemCommandEnum.AI.getCommandType();
InnerCommand execute = context.getInstance(msg);
execute.process(msg) ;
}
@Test
public void execute7() {
String msg = SystemCommandEnum.QAI.getCommandType();
InnerCommand execute = context.getInstance(msg);
execute.process(msg) ;
}
@Test
public void execute8() {
String msg = ":pu cross";
InnerCommand execute = context.getInstance(msg);
execute.process(msg) ;
}
@Test
public void execute9() {
String msg = SystemCommandEnum.INFO.getCommandType();
InnerCommand execute = context.getInstance(msg);
execute.process(msg) ;
}
@Test
public void execute10() {
String msg = "dsds";
InnerCommand execute = context.getInstance(msg);
execute.process(msg) ;
}
// @Test
public void quit() {
String msg = ":q!";
InnerCommand execute = context.getInstance(msg);
execute.process(msg) ;
}
}
\ No newline at end of file
... ...
package com.crossoverjie.cim.common.enums;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
... ... @@ -12,16 +10,16 @@ import java.util.Map;
* Date: 2018/12/26 18:38
* @since JDK 1.8
*/
public enum SystemCommandEnumType {
public enum SystemCommandEnum {
ALL(":all ","获取所有命令"),
ONLINE_USER(":olu ","获取所有在线用户"),
QUIT(":q! ","退出程序"),
QUERY(":q ","【:q 关键字】查询聊天记录"),
AI(":ai ","开启 AI 模式"),
QAI(":qai ","关闭 AI 模式"),
PREFIX(":pu ","模糊匹配用户"),
INFO(":info ","获取客户端信息")
ALL(":all ","获取所有命令","PrintAllCommand"),
ONLINE_USER(":olu ","获取所有在线用户","PrintOnlineUsersCommand"),
QUIT(":q! ","退出程序","ShutDownCommand"),
QUERY(":q ","【:q 关键字】查询聊天记录","QueryHistoryCommand"),
AI(":ai ","开启 AI 模式","OpenAIModelCommand"),
QAI(":qai ","关闭 AI 模式","CloseAIModelCommand"),
PREFIX(":pu ","模糊匹配用户","PrefixSearchCommand"),
INFO(":info ","获取客户端信息","EchoInfoCommand")
;
... ... @@ -31,15 +29,21 @@ public enum SystemCommandEnumType {
/** 枚举描述 */
private final String desc;
/**
* 实现类
*/
private final String clazz ;
/**
* 构建一个 。
* @param commandType 枚举值码。
* @param desc 枚举描述。
*/
private SystemCommandEnumType(String commandType, String desc) {
private SystemCommandEnum(String commandType, String desc, String clazz) {
this.commandType = commandType;
this.desc = desc;
this.clazz = clazz ;
}
/**
... ... @@ -49,6 +53,13 @@ public enum SystemCommandEnumType {
public String getCommandType() {
return commandType;
}
/**
* 获取 class。
* @return class。
*/
public String getClazz() {
return clazz;
}
/**
* 得到枚举描述。
... ... @@ -80,13 +91,21 @@ public enum SystemCommandEnumType {
* @return 全部枚举值码。
*/
public static Map<String,String> getAllStatusCode() {
List<String> list = new ArrayList<String>();
Map<String,String> map = new HashMap<String, String>(16) ;
for (SystemCommandEnumType status : values()) {
list.add(status.code());
for (SystemCommandEnum status : values()) {
map.put(status.getCommandType(),status.getDesc()) ;
}
return map;
}
public static Map<String,String> getAllClazz() {
Map<String,String> map = new HashMap<String, String>(16) ;
for (SystemCommandEnum status : values()) {
map.put(status.getCommandType().trim(),"com.crossoverjie.cim.client.service.impl.command." + status.getClazz()) ;
}
return map;
}
}
\ No newline at end of file
... ...
... ... @@ -9,7 +9,7 @@ public class SystemCommandEnumTypeTest {
@Test
public void getAllStatusCode() throws Exception {
Map<String, String> allStatusCode = SystemCommandEnumType.getAllStatusCode();
Map<String, String> allStatusCode = SystemCommandEnum.getAllStatusCode();
for (Map.Entry<String, String> stringStringEntry : allStatusCode.entrySet()) {
String key = stringStringEntry.getKey();
String value = stringStringEntry.getValue();
... ...
... ... @@ -81,7 +81,6 @@ public class RouteController {
return res;
}
// TODO: 2018/12/26 这些基于 HTTP 接口的远程通信都可以换为 SpringCloud
/**
* 私聊路由
... ...