Skip to content

Commit

Permalink
Merge pull request #27 from crossoverJie/cim-1.0.3
Browse files Browse the repository at this point in the history
cim 1.0.3
  • Loading branch information
crossoverJie authored Jan 30, 2019
2 parents 845cc2d + 7015fed commit 37ef433
Show file tree
Hide file tree
Showing 17 changed files with 527 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ public void reconnect() throws Exception {
* @throws InterruptedException
*/
public void close() throws InterruptedException {
channel.close();
if (channel != null){
channel.close();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
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) ;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,14 @@ public interface MsgHandle {
* 关闭系统
*/
void shutdown() ;

/**
* 开启 AI 模式
*/
void openAIModel() ;

/**
* 关闭 AI 模式
*/
void closeAIModel() ;
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -130,42 +130,10 @@ public boolean checkMsg(String msg) {
@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;

Expand Down Expand Up @@ -253,6 +221,16 @@ public void shutdown() {
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()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
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");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
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("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
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");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
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);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
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("====================================");
}
}
Loading

0 comments on commit 37ef433

Please sign in to comment.