作者 crossoverJie

:sparkles: Introducing new features.cim support delay msg

... ... @@ -3,6 +3,7 @@ package com.crossoverjie.cim.client.config;
import com.crossoverjie.cim.client.handle.MsgHandleCaller;
import com.crossoverjie.cim.client.service.impl.MsgCallBackListener;
import com.crossoverjie.cim.common.constant.Constants;
import com.crossoverjie.cim.common.data.construct.RingBufferWheel;
import com.crossoverjie.cim.common.protocol.CIMRequestProto;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import okhttp3.OkHttpClient;
... ... @@ -104,4 +105,11 @@ public class BeanConfig {
return caller ;
}
@Bean
public RingBufferWheel bufferWheel(){
ExecutorService executorService = Executors.newFixedThreadPool(2) ;
return new RingBufferWheel(executorService) ;
}
}
... ...
package com.crossoverjie.cim.client.handle;
import com.crossoverjie.cim.client.service.EchoService;
import com.crossoverjie.cim.client.service.ShutDownMsg;
import com.crossoverjie.cim.client.service.impl.EchoServiceImpl;
import com.crossoverjie.cim.client.thread.ReConnectJob;
import com.crossoverjie.cim.client.util.SpringBeanFactory;
import com.crossoverjie.cim.common.constant.Constants;
... ... @@ -41,6 +43,8 @@ public class CIMClientHandle extends SimpleChannelInboundHandler<CIMResponseProt
private ShutDownMsg shutDownMsg ;
private EchoService echoService ;
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
... ... @@ -68,7 +72,6 @@ public class CIMClientHandle extends SimpleChannelInboundHandler<CIMResponseProt
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
//客户端和服务端建立连接时调用
LOGGER.info("cim server connect success!");
}
... ... @@ -95,6 +98,10 @@ public class CIMClientHandle extends SimpleChannelInboundHandler<CIMResponseProt
@Override
protected void channelRead0(ChannelHandlerContext ctx, CIMResponseProto.CIMResProtocol msg) throws Exception {
if (echoService == null){
echoService = SpringBeanFactory.getBean(EchoServiceImpl.class) ;
}
//心跳更新时间
if (msg.getType() == Constants.CommandType.PING){
... ... @@ -108,7 +115,7 @@ public class CIMClientHandle extends SimpleChannelInboundHandler<CIMResponseProt
//将消息中的 emoji 表情格式化为 Unicode 编码以便在终端可以显示
String response = EmojiParser.parseToUnicode(msg.getResMsg());
System.out.println(response);
echoService.echo(response);
}
... ...
... ... @@ -5,6 +5,9 @@ import com.crossoverjie.cim.client.service.EchoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDate;
import java.time.LocalTime;
/**
* Function:
*
... ... @@ -21,8 +24,10 @@ public class EchoServiceImpl implements EchoService {
private AppConfiguration appConfiguration;
@Override
public void echo(String msg,Object... replace) {
msg = "\033[31;4m" + appConfiguration.getUserName() + PREFIX + "\033[0m" + " " + msg;
public void echo(String msg, Object... replace) {
String date = LocalDate.now().toString() + " " + LocalTime.now().withNano(0).toString();
msg = "[" + date + "] \033[31;4m" + appConfiguration.getUserName() + PREFIX + "\033[0m" + " " + msg;
String log = print(msg, replace);
... ... @@ -32,6 +37,7 @@ public class EchoServiceImpl implements EchoService {
/**
* print msg
*
* @param msg
* @param place
* @return
... ... @@ -42,7 +48,7 @@ public class EchoServiceImpl implements EchoService {
for (int i = 0; i < place.length; i++) {
int index = msg.indexOf("{}", k);
if (index == -1){
if (index == -1) {
return msg;
}
... ... @@ -63,9 +69,9 @@ public class EchoServiceImpl implements EchoService {
k = index + 2;
}
if (sb.toString().equals("")){
return msg ;
}else {
if (sb.toString().equals("")) {
return msg;
} else {
return sb.toString();
}
}
... ...
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 com.crossoverjie.cim.common.data.construct.RingBufferWheel;
import com.vdurmont.emoji.EmojiParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* Function:
*
* @author crossoverJie
* Date: 2019-09-25 00:37
* @since JDK 1.8
*/
@Service
public class DelayMsgCommand implements InnerCommand {
@Autowired
private EchoService echoService ;
@Autowired
private MsgHandle msgHandle ;
@Autowired
private RingBufferWheel ringBufferWheel ;
@Override
public void process(String msg) {
if (msg.split(" ").length <=2){
echoService.echo("incorrect commond, :delay [msg] [delayTime]") ;
return ;
}
String message = msg.split(" ")[1] ;
Integer delayTime = Integer.valueOf(msg.split(" ")[2]);
RingBufferWheel.Task task = new DelayMsgJob(message) ;
task.setKey(delayTime);
ringBufferWheel.addTask(task);
ringBufferWheel.start();
echoService.echo(EmojiParser.parseToUnicode(msg));
}
private class DelayMsgJob extends RingBufferWheel.Task{
private String msg ;
public DelayMsgJob(String msg) {
this.msg = msg;
}
@Override
public void run() {
msgHandle.sendMsg(msg);
}
}
}
... ...
... ... @@ -6,6 +6,7 @@ import com.crossoverjie.cim.client.service.InnerCommand;
import com.crossoverjie.cim.client.service.MsgLogger;
import com.crossoverjie.cim.client.service.RouteRequest;
import com.crossoverjie.cim.client.service.ShutDownMsg;
import com.crossoverjie.cim.common.data.construct.RingBufferWheel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
... ... @@ -45,6 +46,9 @@ public class ShutDownCommand implements InnerCommand {
@Autowired
private ShutDownMsg shutDownMsg ;
@Autowired
private RingBufferWheel ringBufferWheel ;
@Override
public void process(String msg) {
echoService.echo("cim client closing...");
... ... @@ -52,6 +56,7 @@ public class ShutDownCommand implements InnerCommand {
routeRequest.offLine();
msgLogger.stop();
executor.shutdown();
ringBufferWheel.stop(false);
try {
while (!executor.awaitTermination(1, TimeUnit.SECONDS)) {
echoService.echo("thread pool closing");
... ...
... ... @@ -130,14 +130,16 @@ public final class RingBufferWheel {
executorService.shutdownNow();
} else {
logger.info("delay task is stopping");
try {
lock.lock();
condition.await();
stop = true;
} catch (InterruptedException e) {
logger.error("InterruptedException", e);
} finally {
lock.unlock();
if (taskSize() > 0){
try {
lock.lock();
condition.await();
stop = true;
} catch (InterruptedException e) {
logger.error("InterruptedException", e);
} finally {
lock.unlock();
}
}
executorService.shutdown();
}
... ...
... ... @@ -20,7 +20,8 @@ public enum SystemCommandEnum {
QAI(":qai ","关闭 AI 模式","CloseAIModelCommand"),
PREFIX(":pu ","模糊匹配用户","PrefixSearchCommand"),
EMOJI(":emoji ","emoji 表情列表","EmojiCommand"),
INFO(":info ","获取客户端信息","EchoInfoCommand")
INFO(":info ","获取客户端信息","EchoInfoCommand"),
DELAY_MSG(":delay ","delay message, :delay [msg] [delayTime]","DelayMsgCommand")
;
... ...
... ... @@ -2,6 +2,8 @@ package com.crossoverjie.cim.common;
import org.junit.Test;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.concurrent.TimeUnit;
/**
... ... @@ -13,6 +15,13 @@ import java.util.concurrent.TimeUnit;
*/
public class CommonTest {
@Test
public void test2(){
System.out.println(LocalDate.now().toString());
System.out.println(LocalTime.now().withNano(0).toString());
}
@Test
public void test() throws InterruptedException {
... ...