作者 crossoverJie

:sparkles: Introducing new features.新增一个客户端命令

package com.crossoverjie.cim.client;
import com.crossoverjie.cim.client.scanner.Scan;
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.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
... ... @@ -15,6 +17,8 @@ public class CIMClientApplication implements CommandLineRunner{
private final static Logger LOGGER = LoggerFactory.getLogger(CIMClientApplication.class);
@Autowired
private ClientInfo clientInfo ;
public static void main(String[] args) {
SpringApplication.run(CIMClientApplication.class, args);
LOGGER.info("启动 Client 服务成功");
... ... @@ -26,5 +30,6 @@ public class CIMClientApplication implements CommandLineRunner{
Thread thread = new Thread(scan);
thread.setName("scan-thread");
thread.start();
clientInfo.saveStartDate();
}
}
\ No newline at end of file
... ...
... ... @@ -4,6 +4,7 @@ import com.crossoverjie.cim.client.config.AppConfiguration;
import com.crossoverjie.cim.client.init.CIMClientHandleInitializer;
import com.crossoverjie.cim.client.service.MsgHandle;
import com.crossoverjie.cim.client.service.RouteRequest;
import com.crossoverjie.cim.client.service.impl.ClientInfo;
import com.crossoverjie.cim.client.vo.req.GoogleProtocolVO;
import com.crossoverjie.cim.client.vo.req.LoginReqVO;
import com.crossoverjie.cim.client.vo.res.CIMServerResVO;
... ... @@ -58,6 +59,9 @@ public class CIMClient {
@Autowired
private MsgHandle msgHandle;
@Autowired
private ClientInfo clientInfo;
/**
* 重试次数
*/
... ... @@ -120,6 +124,11 @@ public class CIMClient {
CIMServerResVO.ServerInfo cimServer = null;
try {
cimServer = routeRequest.getCIMServer(loginReqVO);
//保存系统信息
clientInfo.saveServiceInfo(cimServer.getIp() + ":" + cimServer.getCimServerPort())
.saveUserInfo(userId, userName);
LOGGER.info("cimServer=[{}]", cimServer.toString());
} catch (Exception e) {
errorCount++;
... ...
package com.crossoverjie.cim.client.service.impl;
import org.springframework.stereotype.Component;
import java.util.Date;
/**
* Function:
*
* @author crossoverJie
* Date: 2019-01-21 23:35
* @since JDK 1.8
*/
@Component
public class ClientInfo {
private Info info = new Info() ;
public Info get(){
return info ;
}
public ClientInfo saveUserInfo(long userId,String userName){
info.setUserId(userId);
info.setUserName(userName);
return this;
}
public ClientInfo saveServiceInfo(String serviceInfo){
info.setServiceInfo(serviceInfo);
return this;
}
public ClientInfo saveStartDate(){
info.setStartDate(new Date());
return this;
}
private class Info{
private String userName;
private long userId ;
private String serviceInfo ;
private Date startDate ;
public Info() {
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public long getUserId() {
return userId;
}
public void setUserId(long userId) {
this.userId = userId;
}
public String getServiceInfo() {
return serviceInfo;
}
public void setServiceInfo(String serviceInfo) {
this.serviceInfo = serviceInfo;
}
public Date getStartDate() {
return startDate;
}
public void setStartDate(Date startDate) {
this.startDate = startDate;
}
}
}
... ...
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;
... ... @@ -33,33 +34,37 @@ import java.util.concurrent.TimeUnit;
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 ;
private ThreadPoolExecutor executor;
@Autowired
private CIMClient cimClient ;
private CIMClient cimClient;
@Autowired
private MsgLogger msgLogger ;
private MsgLogger msgLogger;
private boolean aiModel = false ;
@Autowired
private ClientInfo clientInfo ;
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) {
... ... @@ -73,7 +78,7 @@ public class MsgHandler implements MsgHandle {
try {
p2pChat(p2PReqVO);
} catch (Exception e) {
LOGGER.error("Exception",e);
LOGGER.error("Exception", e);
}
} else {
... ... @@ -82,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");
}
... ... @@ -114,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;
}
... ... @@ -124,41 +130,46 @@ public class MsgHandler implements MsgHandle {
@Override
public boolean innerCommand(String msg) {
if (msg.startsWith(":")){
if (msg.startsWith(":")) {
Map<String, String> allStatusCode = SystemCommandEnumType.getAllStatusCode();
if (SystemCommandEnumType.QUIT.getCommandType().trim().equals(msg)){
if (SystemCommandEnumType.QUIT.getCommandType().trim().equals(msg)) {
//关闭系统
shutdown();
} else if (SystemCommandEnumType.ALL.getCommandType().trim().equals(msg)){
} else if (SystemCommandEnumType.ALL.getCommandType().trim().equals(msg)) {
printAllCommand(allStatusCode);
} else if (SystemCommandEnumType.ONLINE_USER.getCommandType().toLowerCase().trim().equals(msg.toLowerCase())){
} else if (SystemCommandEnumType.ONLINE_USER.getCommandType().toLowerCase().trim().equals(msg.toLowerCase())) {
//打印在线用户
printOnlineUsers();
} else if (msg.startsWith(SystemCommandEnumType.QUERY.getCommandType().trim() + " ")){
} else if (msg.startsWith(SystemCommandEnumType.QUERY.getCommandType().trim() + " ")) {
//查询聊天记录
queryChatHistory(msg);
}else if (SystemCommandEnumType.AI.getCommandType().trim().equals(msg.toLowerCase())){
} else if (SystemCommandEnumType.AI.getCommandType().trim().equals(msg.toLowerCase())) {
//开启 AI 模式
aiModel = true ;
aiModel = true;
System.out.println("\033[31;4m" + "Hello,我是估值两亿的 AI 机器人!" + "\033[0m");
}else if (SystemCommandEnumType.QAI.getCommandType().trim().equals(msg.toLowerCase())){
} else if (SystemCommandEnumType.QAI.getCommandType().trim().equals(msg.toLowerCase())) {
//关闭 AI 模式
aiModel = false ;
aiModel = false;
System.out.println("\033[31;4m" + "。゚(゚´ω`゚)゚。 AI 下线了!" + "\033[0m");
}else if (msg.startsWith(SystemCommandEnumType.PREFIX.getCommandType().trim() + " ")){
} else if (msg.startsWith(SystemCommandEnumType.PREFIX.getCommandType().trim() + " ")) {
//模糊匹配
prefixSearch(msg);
}else {
} else if (SystemCommandEnumType.INFO.getCommandType().trim().equals(msg.toLowerCase())) {
LOGGER.info("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
LOGGER.info("client info=[{}]", JSON.toJSONString(clientInfo.get()));
LOGGER.info("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
} else {
printAllCommand(allStatusCode);
}
return true ;
return true;
}else {
return false ;
} else {
return false;
}
... ... @@ -167,12 +178,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());
}
... ... @@ -187,16 +199,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);
}
... ... @@ -210,12 +223,12 @@ 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);
}
}
... ... @@ -234,7 +247,7 @@ public class MsgHandler implements MsgHandle {
}
cimClient.close();
} catch (InterruptedException e) {
LOGGER.error("InterruptedException",e);
LOGGER.error("InterruptedException", e);
}
System.exit(0);
}
... ...
... ... @@ -20,7 +20,8 @@ public enum SystemCommandEnumType {
QUERY(":q ","【:q 关键字】查询聊天记录"),
AI(":ai ","开启 AI 模式"),
QAI(":qai ","关闭 AI 模式"),
PREFIX(":pu ","模糊匹配用户")
PREFIX(":pu ","模糊匹配用户"),
INFO(":info ","获取客户端信息")
;
... ...