|
...
|
...
|
@@ -2,20 +2,19 @@ package com.crossoverjie.cim.client.service.impl; |
|
|
|
|
|
|
|
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;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.concurrent.ThreadPoolExecutor;
|
|
...
|
...
|
@@ -25,40 +24,47 @@ import java.util.concurrent.TimeUnit; |
|
|
|
* Function:
|
|
|
|
*
|
|
|
|
* @author crossoverJie
|
|
|
|
* Date: 2018/12/26 11:15
|
|
|
|
* Date: 2018/12/26 11:15
|
|
|
|
* @since JDK 1.8
|
|
|
|
*/
|
|
|
|
@Service
|
|
|
|
public class MsgHandler implements MsgHandle {
|
|
|
|
private final static Logger LOGGER = LoggerFactory.getLogger(MsgHandler.class);
|
|
|
|
@Autowired
|
|
|
|
private RouteRequest routeRequest ;
|
|
|
|
private RouteRequest routeRequest;
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private AppConfiguration configuration;
|
|
|
|
|
|
|
|
@Resource(name = "callBackThreadPool")
|
|
|
|
private ThreadPoolExecutor executor;
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private CIMClient cimClient;
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private ThreadPoolExecutor executor ;
|
|
|
|
private MsgLogger msgLogger;
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private CIMClient cimClient ;
|
|
|
|
private ClientInfo clientInfo;
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private MsgLogger msgLogger ;
|
|
|
|
private InnerCommandContext innerCommandContext ;
|
|
|
|
|
|
|
|
private boolean aiModel = false ;
|
|
|
|
private boolean aiModel = false;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void sendMsg(String msg) {
|
|
|
|
if (aiModel){
|
|
|
|
if (aiModel) {
|
|
|
|
aiChat(msg);
|
|
|
|
}else {
|
|
|
|
} else {
|
|
|
|
normalChat(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 正常聊天
|
|
|
|
*
|
|
|
|
* @param msg
|
|
|
|
*/
|
|
|
|
private void normalChat(String msg) {
|
|
...
|
...
|
@@ -72,7 +78,7 @@ public class MsgHandler implements MsgHandle { |
|
|
|
try {
|
|
|
|
p2pChat(p2PReqVO);
|
|
|
|
} catch (Exception e) {
|
|
|
|
LOGGER.error("Exception",e);
|
|
|
|
LOGGER.error("Exception", e);
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
...
|
...
|
@@ -81,21 +87,22 @@ public class MsgHandler implements MsgHandle { |
|
|
|
try {
|
|
|
|
groupChat(groupReqVO);
|
|
|
|
} catch (Exception e) {
|
|
|
|
LOGGER.error("Exception",e);
|
|
|
|
LOGGER.error("Exception", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* AI model
|
|
|
|
*
|
|
|
|
* @param msg
|
|
|
|
*/
|
|
|
|
private void aiChat(String msg) {
|
|
|
|
msg = msg.replace("吗","") ;
|
|
|
|
msg = msg.replace("嘛","") ;
|
|
|
|
msg = msg.replace("?","!");
|
|
|
|
msg = msg.replace("?","!");
|
|
|
|
msg = msg.replace("你","我");
|
|
|
|
msg = msg.replace("吗", "");
|
|
|
|
msg = msg.replace("嘛", "");
|
|
|
|
msg = msg.replace("?", "!");
|
|
|
|
msg = msg.replace("?", "!");
|
|
|
|
msg = msg.replace("你", "我");
|
|
|
|
System.out.println("AI:\033[31;4m" + msg + "\033[0m");
|
|
|
|
}
|
|
|
|
|
|
...
|
...
|
@@ -113,7 +120,7 @@ public class MsgHandler implements MsgHandle { |
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean checkMsg(String msg) {
|
|
|
|
if (StringUtil.isEmpty(msg)){
|
|
|
|
if (StringUtil.isEmpty(msg)) {
|
|
|
|
LOGGER.warn("不能发送空消息!");
|
|
|
|
return true;
|
|
|
|
}
|
|
...
|
...
|
@@ -123,41 +130,15 @@ public class MsgHandler implements MsgHandle { |
|
|
|
@Override
|
|
|
|
public boolean innerCommand(String msg) {
|
|
|
|
|
|
|
|
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 {
|
|
|
|
printAllCommand(allStatusCode);
|
|
|
|
}
|
|
|
|
if (msg.startsWith(":")) {
|
|
|
|
|
|
|
|
InnerCommand instance = innerCommandContext.getInstance(msg);
|
|
|
|
instance.process(msg) ;
|
|
|
|
|
|
|
|
return true ;
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}else {
|
|
|
|
return false ;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
...
|
...
|
@@ -166,12 +147,13 @@ public class MsgHandler implements MsgHandle { |
|
|
|
|
|
|
|
/**
|
|
|
|
* 模糊匹配
|
|
|
|
*
|
|
|
|
* @param msg
|
|
|
|
*/
|
|
|
|
private void prefixSearch(String msg) {
|
|
|
|
try {
|
|
|
|
List<OnlineUsersResVO.DataBodyBean> onlineUsers = routeRequest.onlineUsers();
|
|
|
|
TrieTree trieTree = new TrieTree() ;
|
|
|
|
TrieTree trieTree = new TrieTree();
|
|
|
|
for (OnlineUsersResVO.DataBodyBean onlineUser : onlineUsers) {
|
|
|
|
trieTree.insert(onlineUser.getUserName());
|
|
|
|
}
|
|
...
|
...
|
@@ -186,16 +168,17 @@ public class MsgHandler implements MsgHandle { |
|
|
|
}
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
LOGGER.error("Exception" ,e);
|
|
|
|
LOGGER.error("Exception", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 查询聊天记录
|
|
|
|
*
|
|
|
|
* @param msg
|
|
|
|
*/
|
|
|
|
private void queryChatHistory(String msg) {
|
|
|
|
String[] split = msg.split(" ") ;
|
|
|
|
String[] split = msg.split(" ");
|
|
|
|
String res = msgLogger.query(split[1]);
|
|
|
|
System.out.println(res);
|
|
|
|
}
|
|
...
|
...
|
@@ -209,20 +192,22 @@ public class MsgHandler implements MsgHandle { |
|
|
|
|
|
|
|
LOGGER.info("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
|
|
|
|
for (OnlineUsersResVO.DataBodyBean onlineUser : onlineUsers) {
|
|
|
|
LOGGER.info("userId={}=====userName={}",onlineUser.getUserId(),onlineUser.getUserName());
|
|
|
|
LOGGER.info("userId={}=====userName={}", onlineUser.getUserId(), onlineUser.getUserName());
|
|
|
|
}
|
|
|
|
LOGGER.info("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
LOGGER.error("Exception" ,e);
|
|
|
|
LOGGER.error("Exception", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 关闭系统
|
|
|
|
*/
|
|
|
|
private void shutdown() {
|
|
|
|
@Override
|
|
|
|
public void shutdown() {
|
|
|
|
LOGGER.info("系统关闭中。。。。");
|
|
|
|
routeRequest.offLine();
|
|
|
|
msgLogger.stop();
|
|
|
|
executor.shutdown();
|
|
|
|
try {
|
|
...
|
...
|
@@ -231,11 +216,21 @@ public class MsgHandler implements MsgHandle { |
|
|
|
}
|
|
|
|
cimClient.close();
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
LOGGER.error("InterruptedException",e);
|
|
|
|
LOGGER.error("InterruptedException", e);
|
|
|
|
}
|
|
|
|
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()) {
|
...
|
...
|
|