Skip to content

Commit

Permalink
cim 1.0.5 (#51)
Browse files Browse the repository at this point in the history
cim 1.0.5
  • Loading branch information
crossoverJie authored Aug 31, 2019
2 parents 3ae7d95 + b29a7ca commit 0f21d92
Show file tree
Hide file tree
Showing 38 changed files with 404 additions and 789 deletions.
6 changes: 6 additions & 0 deletions cim-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@
<artifactId>guava</artifactId>
</dependency>

<dependency>
<groupId>com.vdurmont</groupId>
<artifactId>emoji-java</artifactId>
<version>5.0.0</version>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.crossoverjie.cim.client.config.AppConfiguration;
import com.crossoverjie.cim.client.init.CIMClientHandleInitializer;
import com.crossoverjie.cim.client.service.EchoService;
import com.crossoverjie.cim.client.service.MsgHandle;
import com.crossoverjie.cim.client.service.RouteRequest;
import com.crossoverjie.cim.client.service.impl.ClientInfo;
Expand Down Expand Up @@ -50,6 +51,9 @@ public class CIMClient {

private SocketChannel channel;

@Autowired
private EchoService echoService ;

@Autowired
private RouteRequest routeRequest;

Expand Down Expand Up @@ -102,12 +106,13 @@ private void startClient(CIMServerResVO.ServerInfo cimServer) {
errorCount++;

if (errorCount >= configuration.getErrorCount()) {
LOGGER.error("链接失败次数达到上限[{}]次", errorCount);
LOGGER.error("连接失败次数达到上限[{}]次", errorCount);
msgHandle.shutdown();
}
LOGGER.error("连接失败", e);
}
if (future.isSuccess()) {
echoService.echo("start cim client success!");
LOGGER.info("启动 cim client 成功");
}
channel = (SocketChannel) future.channel();
Expand Down Expand Up @@ -137,7 +142,7 @@ private CIMServerResVO.ServerInfo userLogin() {
LOGGER.error("重连次数达到上限[{}]次", errorCount);
msgHandle.shutdown();
}
LOGGER.error("登录失败", e);
LOGGER.error("login fail", e);
}
return cimServer;
}
Expand All @@ -153,7 +158,8 @@ private void loginCIMServer() {
.build();
ChannelFuture future = channel.writeAndFlush(login);
future.addListener((ChannelFutureListener) channelFuture ->
LOGGER.info("注册成功={}", login.toString()));
echoService.echo("registry cim server success!")
);
}

/**
Expand Down Expand Up @@ -198,9 +204,9 @@ public void reconnect() throws Exception {
//首先清除路由信息,下线
routeRequest.offLine();

LOGGER.info("开始重连。。");
LOGGER.info("reconnect....");
start();
LOGGER.info("重连成功!!");
LOGGER.info("reconnect success");
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.crossoverjie.cim.client.constant;

/**
* Function:
*
* @author crossoverJie
* Date: 2019-08-24 22:53
* @since JDK 1.8
*/
public class Emoji {

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.crossoverjie.cim.common.protocol.CIMRequestProto;
import com.crossoverjie.cim.common.protocol.CIMResponseProto;
import com.crossoverjie.cim.common.util.NettyAttrUtil;
import com.vdurmont.emoji.EmojiParser;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
Expand Down Expand Up @@ -105,7 +106,9 @@ protected void channelRead0(ChannelHandlerContext ctx, CIMResponseProto.CIMResPr
//回调消息
callBackMsg(msg.getResMsg());

LOGGER.info(msg.getResMsg());
//将消息中的 emoji 表情格式化为 Unicode 编码以便在终端可以显示
String response = EmojiParser.parseToUnicode(msg.getResMsg());
System.out.println(response);
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.crossoverjie.cim.client.scanner;

import com.crossoverjie.cim.client.config.AppConfiguration;
import com.crossoverjie.cim.client.service.EchoService;
import com.crossoverjie.cim.client.service.MsgHandle;
import com.crossoverjie.cim.client.service.MsgLogger;
import com.crossoverjie.cim.client.util.SpringBeanFactory;
import com.vdurmont.emoji.EmojiParser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -29,10 +31,13 @@ public class Scan implements Runnable {

private MsgLogger msgLogger ;

private EchoService echoService ;

public Scan() {
this.configuration = SpringBeanFactory.getBean(AppConfiguration.class);
this.msgHandle = SpringBeanFactory.getBean(MsgHandle.class) ;
this.msgLogger = SpringBeanFactory.getBean(MsgLogger.class) ;
this.echoService = SpringBeanFactory.getBean(EchoService.class) ;
}

@Override
Expand All @@ -57,7 +62,7 @@ public void run() {
//写入聊天记录
msgLogger.log(msg) ;

LOGGER.info("{}:【{}】", configuration.getUserName(), msg);
echoService.echo(EmojiParser.parseToUnicode(msg));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.crossoverjie.cim.client.service;

/**
* Function:
*
* @author crossoverJie
* Date: 2019-08-27 22:35
* @since JDK 1.8
*/
public interface EchoService {

/**
* echo msg to terminal
* @param msg message
* @param replace
*/
void echo(String msg, Object... replace) ;
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public ClientInfo saveStartDate(){
return this;
}

private class Info{
public class Info{
private String userName;
private long userId ;
private String serviceInfo ;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.crossoverjie.cim.client.service.impl;

import com.crossoverjie.cim.client.config.AppConfiguration;
import com.crossoverjie.cim.client.service.EchoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* Function:
*
* @author crossoverJie
* Date: 2019-08-27 22:37
* @since JDK 1.8
*/
@Service
public class EchoServiceImpl implements EchoService {

private static final String PREFIX = "$";

@Autowired
private AppConfiguration appConfiguration;

@Override
public void echo(String msg,Object... replace) {
msg = "\033[31;4m" + appConfiguration.getUserName() + PREFIX + "\033[0m" + " " + msg;

String log = print(msg, replace);

System.out.println(log);
}


/**
* print msg
* @param msg
* @param place
* @return
*/
private String print(String msg, Object... place) {
StringBuilder sb = new StringBuilder();
int k = 0;
for (int i = 0; i < place.length; i++) {
int index = msg.indexOf("{}", k);

if (index == -1){
return msg;
}

if (index != 0) {
sb.append(msg, k, index);
sb.append(place[i]);

if (place.length == 1) {
sb.append(msg, index + 2, msg.length());
}

} else {
sb.append(place[i]);
if (place.length == 1) {
sb.append(msg, index + 2, msg.length());
}
}

k = index + 2;
}
if (sb.toString().equals("")){
return msg ;
}else {
return sb.toString();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@

import com.crossoverjie.cim.client.client.CIMClient;
import com.crossoverjie.cim.client.config.AppConfiguration;
import com.crossoverjie.cim.client.service.*;
import com.crossoverjie.cim.client.service.InnerCommand;
import com.crossoverjie.cim.client.service.InnerCommandContext;
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.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.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;
import java.util.concurrent.TimeUnit;

Expand Down Expand Up @@ -144,63 +143,6 @@ public boolean innerCommand(String msg) {

}


/**
* 模糊匹配
*
* @param msg
*/
private void prefixSearch(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);
}
}

/**
* 查询聊天记录
*
* @param msg
*/
private void queryChatHistory(String msg) {
String[] split = msg.split(" ");
String res = msgLogger.query(split[1]);
System.out.println(res);
}

/**
* 打印在线用户
*/
private void printOnlineUsers() {
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);
}
}

/**
* 关闭系统
*/
Expand Down Expand Up @@ -231,13 +173,4 @@ public void closeAIModel() {
aiModel = false ;
}

private void printAllCommand(Map<String, String> allStatusCode) {
LOGGER.warn("====================================");
for (Map.Entry<String, String> stringStringEntry : allStatusCode.entrySet()) {
String key = stringStringEntry.getKey();
String value = stringStringEntry.getValue();
LOGGER.warn(key + "----->" + value);
}
LOGGER.warn("====================================");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.crossoverjie.cim.client.config.AppConfiguration;
import com.crossoverjie.cim.client.service.EchoService;
import com.crossoverjie.cim.client.service.RouteRequest;
import com.crossoverjie.cim.client.vo.req.GroupReqVO;
import com.crossoverjie.cim.client.vo.req.LoginReqVO;
Expand Down Expand Up @@ -50,6 +51,8 @@ public class RouteRequestImpl implements RouteRequest {
@Value("${cim.server.online.user.url}")
private String onlineUserUrl;

@Autowired
private EchoService echoService ;


@Autowired
Expand Down Expand Up @@ -136,7 +139,7 @@ public CIMServerResVO.ServerInfo getCIMServer(LoginReqVO loginReqVO) throws Exce

//重复失败
if (!cimServerResVO.getCode().equals(StatusEnum.SUCCESS.getCode())){
LOGGER.error(appConfiguration.getUserName() + ":" + cimServerResVO.getMessage());
echoService.echo(cimServerResVO.getMessage());
System.exit(-1);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.crossoverjie.cim.client.service.impl.command;

import com.crossoverjie.cim.client.service.EchoService;
import com.crossoverjie.cim.client.service.InnerCommand;
import com.crossoverjie.cim.client.service.MsgHandle;
import org.slf4j.Logger;
Expand All @@ -22,9 +23,13 @@ public class CloseAIModelCommand implements InnerCommand {
@Autowired
private MsgHandle msgHandle ;

@Autowired
private EchoService echoService ;

@Override
public void process(String msg) {
msgHandle.closeAIModel();
System.out.println("\033[31;4m" + "。゚(゚´ω`゚)゚。 AI 下线了!" + "\033[0m");

echoService.echo("\033[31;4m" + "。゚(゚´ω`゚)゚。 AI 下线了!" + "\033[0m");
}
}
Loading

0 comments on commit 0f21d92

Please sign in to comment.