作者 crossoverJie

:white_check_mark: Adding tests.

... ... @@ -2,6 +2,7 @@ package com.crossoverjie.cim.client.client;
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;
... ... @@ -51,6 +52,9 @@ public class CIMClient {
private SocketChannel channel;
@Autowired
private EchoService echoService ;
@Autowired
private RouteRequest routeRequest;
@Autowired
... ... @@ -108,6 +112,7 @@ public class CIMClient {
LOGGER.error("连接失败", e);
}
if (future.isSuccess()) {
echoService.echo("start cim client success!");
LOGGER.info("启动 cim client 成功");
}
channel = (SocketChannel) future.channel();
... ... @@ -137,7 +142,7 @@ public class CIMClient {
LOGGER.error("重连次数达到上限[{}]次", errorCount);
msgHandle.shutdown();
}
LOGGER.error("登录失败", e);
LOGGER.error("login fail", e);
}
return cimServer;
}
... ... @@ -153,7 +158,8 @@ public class CIMClient {
.build();
ChannelFuture future = channel.writeAndFlush(login);
future.addListener((ChannelFutureListener) channelFuture ->
LOGGER.info("注册成功={}", login.toString()));
echoService.echo("registry cim server success!")
);
}
/**
... ...
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;
... ... @@ -30,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
... ... @@ -58,7 +62,7 @@ public class Scan implements Runnable {
//写入聊天记录
msgLogger.log(msg) ;
System.out.println(configuration.getUserName() + ":" + EmojiParser.parseToUnicode(msg));
echoService.echo(EmojiParser.parseToUnicode(msg));
}
}
... ...
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, String... replace) ;
}
... ...
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,String... replace) {
msg = "\033[31;4m" + appConfiguration.getUserName() + PREFIX + "\033[0m" + " " + msg;
for (String str : replace) {
// msg.replaceAll("{}",str) ;
}
System.out.println(msg);
}
}
... ...
... ... @@ -16,7 +16,6 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Map;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
... ... @@ -174,13 +173,4 @@ public class MsgHandler implements MsgHandle {
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("====================================");
}
}
... ...
... ... @@ -3,6 +3,7 @@ package com.crossoverjie.cim.client.service.impl;
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;
... ... @@ -50,6 +51,8 @@ public class RouteRequestImpl implements RouteRequest {
@Value("${cim.server.online.user.url}")
private String onlineUserUrl;
@Autowired
private EchoService echoService ;
@Autowired
... ... @@ -136,7 +139,7 @@ public class RouteRequestImpl implements RouteRequest {
//重复失败
if (!cimServerResVO.getCode().equals(StatusEnum.SUCCESS.getCode())){
LOGGER.error(appConfiguration.getUserName() + ":" + cimServerResVO.getMessage());
echoService.echo(cimServerResVO.getMessage());
System.exit(-1);
}
... ...
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.common.enums.SystemCommandEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
... ... @@ -19,15 +21,18 @@ import java.util.Map;
public class PrintAllCommand implements InnerCommand {
private final static Logger LOGGER = LoggerFactory.getLogger(PrintAllCommand.class);
@Autowired
private EchoService echoService ;
@Override
public void process(String msg) {
Map<String, String> allStatusCode = SystemCommandEnum.getAllStatusCode();
LOGGER.warn("====================================");
echoService.echo("====================================");
for (Map.Entry<String, String> stringStringEntry : allStatusCode.entrySet()) {
String key = stringStringEntry.getKey();
String value = stringStringEntry.getValue();
LOGGER.warn(key + "----->" + value);
echoService.echo(key + "----->" + value);
}
LOGGER.warn("====================================");
echoService.echo("====================================");
}
}
... ...
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.RouteRequest;
import com.crossoverjie.cim.client.vo.res.OnlineUsersResVO;
... ... @@ -25,16 +26,19 @@ public class PrintOnlineUsersCommand implements InnerCommand {
@Autowired
private RouteRequest routeRequest ;
@Autowired
private EchoService echoService ;
@Override
public void process(String msg) {
try {
List<OnlineUsersResVO.DataBodyBean> onlineUsers = routeRequest.onlineUsers();
LOGGER.info("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
echoService.echo("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
for (OnlineUsersResVO.DataBodyBean onlineUser : onlineUsers) {
LOGGER.info("userId={}=====userName={}", onlineUser.getUserId(), onlineUser.getUserName());
}
LOGGER.info("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
echoService.echo("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
} catch (Exception e) {
LOGGER.error("Exception", e);
... ...
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.MsgLogger;
import org.slf4j.Logger;
... ... @@ -22,6 +23,9 @@ public class QueryHistoryCommand implements InnerCommand {
@Autowired
private MsgLogger msgLogger ;
@Autowired
private EchoService echoService ;
@Override
public void process(String msg) {
String[] split = msg.split(" ");
... ... @@ -29,6 +33,6 @@ public class QueryHistoryCommand implements InnerCommand {
return;
}
String res = msgLogger.query(split[1]);
System.out.println(res);
echoService.echo(res);
}
}
... ...
package com.crossoverjie.cim.client.service.impl.command;
import com.crossoverjie.cim.client.client.CIMClient;
import com.crossoverjie.cim.client.service.EchoService;
import com.crossoverjie.cim.client.service.InnerCommand;
import com.crossoverjie.cim.client.service.MsgLogger;
import com.crossoverjie.cim.client.service.RouteRequest;
... ... @@ -37,25 +38,29 @@ public class ShutDownCommand implements InnerCommand {
@Resource(name = "callBackThreadPool")
private ThreadPoolExecutor executor;
@Autowired
private EchoService echoService ;
@Autowired
private ShutDownMsg shutDownMsg ;
@Override
public void process(String msg) {
LOGGER.info("系统关闭中。。。。");
echoService.echo("cim client closing...");
shutDownMsg.shutdown();
routeRequest.offLine();
msgLogger.stop();
executor.shutdown();
try {
while (!executor.awaitTermination(1, TimeUnit.SECONDS)) {
LOGGER.info("线程池关闭中。。。。");
echoService.echo("thread pool closing");
}
cimClient.close();
} catch (InterruptedException e) {
LOGGER.error("InterruptedException", e);
}
echoService.echo("cim close success!");
System.exit(0);
}
}
... ...
... ... @@ -13,40 +13,40 @@ cim.msg.logger.path = /opt/logs/cim/
###=======生产模拟======###
# 群发消息
cim.group.route.request.url=http://45.78.28.220:8083/groupRoute
# 私聊消息
cim.p2p.route.request.url=http://45.78.28.220:8083/p2pRoute
# 登录并获取服务器ip+port
cim.server.route.request.url=http://45.78.28.220:8083/login
# 在线用户
cim.server.online.user.url=http://45.78.28.220:8083/onlineUser
# 清除路由信息
cim.clear.route.request.url=http://45.78.28.220:8083/offLine
###=======本地模拟======###
## 群发消息
#cim.group.route.request.url=http://localhost:8083/groupRoute
#cim.group.route.request.url=http://45.78.28.220:8083/groupRoute
#
## 私聊消息
#cim.p2p.route.request.url=http://localhost:8083/p2pRoute
#cim.p2p.route.request.url=http://45.78.28.220:8083/p2pRoute
#
## 登录并获取服务器ip+port
#cim.server.route.request.url=http://localhost:8083/login
#cim.server.route.request.url=http://45.78.28.220:8083/login
#
## 在线用户
#cim.server.online.user.url=http://localhost:8083/onlineUser
#cim.server.online.user.url=http://45.78.28.220:8083/onlineUser
#
## 清除路由信息
#cim.clear.route.request.url=http://45.78.28.220:8083/offLine
###=======本地模拟======###
# 群发消息
cim.group.route.request.url=http://localhost:8083/groupRoute
# 私聊消息
cim.p2p.route.request.url=http://localhost:8083/p2pRoute
# 登录并获取服务器ip+port
cim.server.route.request.url=http://localhost:8083/login
# 在线用户
cim.server.online.user.url=http://localhost:8083/onlineUser
# 清除路由信息
#cim.clear.route.request.url=http://localhost:8083/offLine
清除路由信息
cim.clear.route.request.url=http://localhost:8083/offLine
# 客户端唯一ID
cim.user.id=1551267098213
cim.user.userName=test3
cim.user.id=1566914867344
cim.user.userName=zhangsan
# 回调线程队列大小
cim.callback.thread.queue.size = 2
... ...
package com.crossoverjie.cim.server.test;
import org.junit.Assert;
import org.junit.Test;
/**
* Function:
*
* @author crossoverJie
* Date: 2019-08-28 01:47
* @since JDK 1.8
*/
public class EchoTest {
@Test
public void echo() {
String msg = "{} say,you {}";
String[] place = {"zhangsan", "haha"};
String log = log(msg, place);
System.out.println(log);
Assert.assertEquals(log,"zhangsan say,you haha");
}
@Test
public void echo2() {
String msg = "{} say,you {},zhangsan say {}";
String[] place = {"zhangsan", "haha", "nihao"};
String log = log(msg, place);
System.out.println(log);
Assert.assertEquals(log,"zhangsan say,you haha,zhangsan say nihao");
}
@Test
public void echo3() {
String msg = "see you {},zhangsan say";
String[] place = {"zhangsan"};
String log = log(msg, place);
System.out.println(log);
Assert.assertEquals(log,"see you zhangsan,zhangsan say");
}
@Test
public void echo4() {
String msg = "{}see you,zhangsan say";
String[] place = {"!!!"};
String log = log(msg, place);
System.out.println(log);
Assert.assertEquals(log,"!!!see you,zhangsan say");
}
@Test
public void echo5() {
String msg = "see you,zhangsan say{}";
String[] place = {"!!!"};
String log = log(msg, place);
System.out.println(log);
Assert.assertEquals(log,"see you,zhangsan say!!!");
}
@Test
public void echo6() {
String msg = "see you,zhangsan say";
String[] place = {""};
String log = log(msg, place);
System.out.println(log);
Assert.assertEquals(log,"see you,zhangsan say");
}
private String log(String msg, String... 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;
}
return sb.toString();
}
}
... ...
... ... @@ -25,7 +25,7 @@ monitor.channel.map.key=channelMap
app.zk.switch=true
# zk 地址
app.zk.addr=47.98.194.60:2182
app.zk.addr=ip:port
# zk 连接超时时限
app.zk.connect.timeout=15000
... ... @@ -36,5 +36,5 @@ app.zk.root=/route
# 清除路由信息
cim.clear.route.request.url=http://localhost:8083/offLine
# 检测多少秒没有收到客户端心跳后服务端关闭连接
# 检测多少秒没有收到客户端心跳后服务端关闭连接 单位秒
cim.heartbeat.time = 30
\ No newline at end of file
... ...