作者 crossoverJie

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

1 package com.crossoverjie.cim.client; 1 package com.crossoverjie.cim.client;
2 2
3 import com.crossoverjie.cim.client.scanner.Scan; 3 import com.crossoverjie.cim.client.scanner.Scan;
  4 +import com.crossoverjie.cim.client.service.impl.ClientInfo;
4 import org.slf4j.Logger; 5 import org.slf4j.Logger;
5 import org.slf4j.LoggerFactory; 6 import org.slf4j.LoggerFactory;
  7 +import org.springframework.beans.factory.annotation.Autowired;
6 import org.springframework.boot.CommandLineRunner; 8 import org.springframework.boot.CommandLineRunner;
7 import org.springframework.boot.SpringApplication; 9 import org.springframework.boot.SpringApplication;
8 import org.springframework.boot.autoconfigure.SpringBootApplication; 10 import org.springframework.boot.autoconfigure.SpringBootApplication;
@@ -15,6 +17,8 @@ public class CIMClientApplication implements CommandLineRunner{ @@ -15,6 +17,8 @@ public class CIMClientApplication implements CommandLineRunner{
15 17
16 private final static Logger LOGGER = LoggerFactory.getLogger(CIMClientApplication.class); 18 private final static Logger LOGGER = LoggerFactory.getLogger(CIMClientApplication.class);
17 19
  20 + @Autowired
  21 + private ClientInfo clientInfo ;
18 public static void main(String[] args) { 22 public static void main(String[] args) {
19 SpringApplication.run(CIMClientApplication.class, args); 23 SpringApplication.run(CIMClientApplication.class, args);
20 LOGGER.info("启动 Client 服务成功"); 24 LOGGER.info("启动 Client 服务成功");
@@ -26,5 +30,6 @@ public class CIMClientApplication implements CommandLineRunner{ @@ -26,5 +30,6 @@ public class CIMClientApplication implements CommandLineRunner{
26 Thread thread = new Thread(scan); 30 Thread thread = new Thread(scan);
27 thread.setName("scan-thread"); 31 thread.setName("scan-thread");
28 thread.start(); 32 thread.start();
  33 + clientInfo.saveStartDate();
29 } 34 }
30 } 35 }
@@ -4,6 +4,7 @@ import com.crossoverjie.cim.client.config.AppConfiguration; @@ -4,6 +4,7 @@ import com.crossoverjie.cim.client.config.AppConfiguration;
4 import com.crossoverjie.cim.client.init.CIMClientHandleInitializer; 4 import com.crossoverjie.cim.client.init.CIMClientHandleInitializer;
5 import com.crossoverjie.cim.client.service.MsgHandle; 5 import com.crossoverjie.cim.client.service.MsgHandle;
6 import com.crossoverjie.cim.client.service.RouteRequest; 6 import com.crossoverjie.cim.client.service.RouteRequest;
  7 +import com.crossoverjie.cim.client.service.impl.ClientInfo;
7 import com.crossoverjie.cim.client.vo.req.GoogleProtocolVO; 8 import com.crossoverjie.cim.client.vo.req.GoogleProtocolVO;
8 import com.crossoverjie.cim.client.vo.req.LoginReqVO; 9 import com.crossoverjie.cim.client.vo.req.LoginReqVO;
9 import com.crossoverjie.cim.client.vo.res.CIMServerResVO; 10 import com.crossoverjie.cim.client.vo.res.CIMServerResVO;
@@ -58,6 +59,9 @@ public class CIMClient { @@ -58,6 +59,9 @@ public class CIMClient {
58 @Autowired 59 @Autowired
59 private MsgHandle msgHandle; 60 private MsgHandle msgHandle;
60 61
  62 + @Autowired
  63 + private ClientInfo clientInfo;
  64 +
61 /** 65 /**
62 * 重试次数 66 * 重试次数
63 */ 67 */
@@ -120,6 +124,11 @@ public class CIMClient { @@ -120,6 +124,11 @@ public class CIMClient {
120 CIMServerResVO.ServerInfo cimServer = null; 124 CIMServerResVO.ServerInfo cimServer = null;
121 try { 125 try {
122 cimServer = routeRequest.getCIMServer(loginReqVO); 126 cimServer = routeRequest.getCIMServer(loginReqVO);
  127 +
  128 + //保存系统信息
  129 + clientInfo.saveServiceInfo(cimServer.getIp() + ":" + cimServer.getCimServerPort())
  130 + .saveUserInfo(userId, userName);
  131 +
123 LOGGER.info("cimServer=[{}]", cimServer.toString()); 132 LOGGER.info("cimServer=[{}]", cimServer.toString());
124 } catch (Exception e) { 133 } catch (Exception e) {
125 errorCount++; 134 errorCount++;
  1 +package com.crossoverjie.cim.client.service.impl;
  2 +
  3 +
  4 +import org.springframework.stereotype.Component;
  5 +
  6 +import java.util.Date;
  7 +
  8 +/**
  9 + * Function:
  10 + *
  11 + * @author crossoverJie
  12 + * Date: 2019-01-21 23:35
  13 + * @since JDK 1.8
  14 + */
  15 +@Component
  16 +public class ClientInfo {
  17 +
  18 + private Info info = new Info() ;
  19 +
  20 + public Info get(){
  21 + return info ;
  22 + }
  23 +
  24 + public ClientInfo saveUserInfo(long userId,String userName){
  25 + info.setUserId(userId);
  26 + info.setUserName(userName);
  27 + return this;
  28 + }
  29 +
  30 +
  31 + public ClientInfo saveServiceInfo(String serviceInfo){
  32 + info.setServiceInfo(serviceInfo);
  33 + return this;
  34 + }
  35 +
  36 + public ClientInfo saveStartDate(){
  37 + info.setStartDate(new Date());
  38 + return this;
  39 + }
  40 +
  41 + private class Info{
  42 + private String userName;
  43 + private long userId ;
  44 + private String serviceInfo ;
  45 + private Date startDate ;
  46 +
  47 + public Info() {
  48 + }
  49 +
  50 + public String getUserName() {
  51 + return userName;
  52 + }
  53 +
  54 + public void setUserName(String userName) {
  55 + this.userName = userName;
  56 + }
  57 +
  58 + public long getUserId() {
  59 + return userId;
  60 + }
  61 +
  62 + public void setUserId(long userId) {
  63 + this.userId = userId;
  64 + }
  65 +
  66 + public String getServiceInfo() {
  67 + return serviceInfo;
  68 + }
  69 +
  70 + public void setServiceInfo(String serviceInfo) {
  71 + this.serviceInfo = serviceInfo;
  72 + }
  73 +
  74 + public Date getStartDate() {
  75 + return startDate;
  76 + }
  77 +
  78 + public void setStartDate(Date startDate) {
  79 + this.startDate = startDate;
  80 + }
  81 + }
  82 +}
1 package com.crossoverjie.cim.client.service.impl; 1 package com.crossoverjie.cim.client.service.impl;
2 2
  3 +import com.alibaba.fastjson.JSON;
3 import com.crossoverjie.cim.client.client.CIMClient; 4 import com.crossoverjie.cim.client.client.CIMClient;
4 import com.crossoverjie.cim.client.config.AppConfiguration; 5 import com.crossoverjie.cim.client.config.AppConfiguration;
5 import com.crossoverjie.cim.client.service.MsgHandle; 6 import com.crossoverjie.cim.client.service.MsgHandle;
@@ -26,40 +27,44 @@ import java.util.concurrent.TimeUnit; @@ -26,40 +27,44 @@ import java.util.concurrent.TimeUnit;
26 * Function: 27 * Function:
27 * 28 *
28 * @author crossoverJie 29 * @author crossoverJie
29 - * Date: 2018/12/26 11:15 30 + * Date: 2018/12/26 11:15
30 * @since JDK 1.8 31 * @since JDK 1.8
31 */ 32 */
32 @Service 33 @Service
33 public class MsgHandler implements MsgHandle { 34 public class MsgHandler implements MsgHandle {
34 private final static Logger LOGGER = LoggerFactory.getLogger(MsgHandler.class); 35 private final static Logger LOGGER = LoggerFactory.getLogger(MsgHandler.class);
35 @Autowired 36 @Autowired
36 - private RouteRequest routeRequest ; 37 + private RouteRequest routeRequest;
37 38
38 @Autowired 39 @Autowired
39 private AppConfiguration configuration; 40 private AppConfiguration configuration;
40 41
41 @Resource(name = "callBackThreadPool") 42 @Resource(name = "callBackThreadPool")
42 - private ThreadPoolExecutor executor ; 43 + private ThreadPoolExecutor executor;
43 44
44 @Autowired 45 @Autowired
45 - private CIMClient cimClient ; 46 + private CIMClient cimClient;
46 47
47 @Autowired 48 @Autowired
48 - private MsgLogger msgLogger ; 49 + private MsgLogger msgLogger;
49 50
50 - private boolean aiModel = false ; 51 + @Autowired
  52 + private ClientInfo clientInfo ;
  53 +
  54 + private boolean aiModel = false;
51 55
52 @Override 56 @Override
53 public void sendMsg(String msg) { 57 public void sendMsg(String msg) {
54 - if (aiModel){ 58 + if (aiModel) {
55 aiChat(msg); 59 aiChat(msg);
56 - }else { 60 + } else {
57 normalChat(msg); 61 normalChat(msg);
58 } 62 }
59 } 63 }
60 64
61 /** 65 /**
62 * 正常聊天 66 * 正常聊天
  67 + *
63 * @param msg 68 * @param msg
64 */ 69 */
65 private void normalChat(String msg) { 70 private void normalChat(String msg) {
@@ -73,7 +78,7 @@ public class MsgHandler implements MsgHandle { @@ -73,7 +78,7 @@ public class MsgHandler implements MsgHandle {
73 try { 78 try {
74 p2pChat(p2PReqVO); 79 p2pChat(p2PReqVO);
75 } catch (Exception e) { 80 } catch (Exception e) {
76 - LOGGER.error("Exception",e); 81 + LOGGER.error("Exception", e);
77 } 82 }
78 83
79 } else { 84 } else {
@@ -82,21 +87,22 @@ public class MsgHandler implements MsgHandle { @@ -82,21 +87,22 @@ public class MsgHandler implements MsgHandle {
82 try { 87 try {
83 groupChat(groupReqVO); 88 groupChat(groupReqVO);
84 } catch (Exception e) { 89 } catch (Exception e) {
85 - LOGGER.error("Exception",e); 90 + LOGGER.error("Exception", e);
86 } 91 }
87 } 92 }
88 } 93 }
89 94
90 /** 95 /**
91 * AI model 96 * AI model
  97 + *
92 * @param msg 98 * @param msg
93 */ 99 */
94 private void aiChat(String msg) { 100 private void aiChat(String msg) {
95 - msg = msg.replace("吗","") ;  
96 - msg = msg.replace("嘛","") ;  
97 - msg = msg.replace("?","!");  
98 - msg = msg.replace("?","!");  
99 - msg = msg.replace("你","我"); 101 + msg = msg.replace("吗", "");
  102 + msg = msg.replace("嘛", "");
  103 + msg = msg.replace("?", "!");
  104 + msg = msg.replace("?", "!");
  105 + msg = msg.replace("你", "我");
100 System.out.println("AI:\033[31;4m" + msg + "\033[0m"); 106 System.out.println("AI:\033[31;4m" + msg + "\033[0m");
101 } 107 }
102 108
@@ -114,7 +120,7 @@ public class MsgHandler implements MsgHandle { @@ -114,7 +120,7 @@ public class MsgHandler implements MsgHandle {
114 120
115 @Override 121 @Override
116 public boolean checkMsg(String msg) { 122 public boolean checkMsg(String msg) {
117 - if (StringUtil.isEmpty(msg)){ 123 + if (StringUtil.isEmpty(msg)) {
118 LOGGER.warn("不能发送空消息!"); 124 LOGGER.warn("不能发送空消息!");
119 return true; 125 return true;
120 } 126 }
@@ -124,41 +130,46 @@ public class MsgHandler implements MsgHandle { @@ -124,41 +130,46 @@ public class MsgHandler implements MsgHandle {
124 @Override 130 @Override
125 public boolean innerCommand(String msg) { 131 public boolean innerCommand(String msg) {
126 132
127 - if (msg.startsWith(":")){ 133 + if (msg.startsWith(":")) {
128 Map<String, String> allStatusCode = SystemCommandEnumType.getAllStatusCode(); 134 Map<String, String> allStatusCode = SystemCommandEnumType.getAllStatusCode();
129 135
130 - if (SystemCommandEnumType.QUIT.getCommandType().trim().equals(msg)){ 136 + if (SystemCommandEnumType.QUIT.getCommandType().trim().equals(msg)) {
131 //关闭系统 137 //关闭系统
132 shutdown(); 138 shutdown();
133 - } else if (SystemCommandEnumType.ALL.getCommandType().trim().equals(msg)){ 139 + } else if (SystemCommandEnumType.ALL.getCommandType().trim().equals(msg)) {
134 printAllCommand(allStatusCode); 140 printAllCommand(allStatusCode);
135 141
136 - } else if (SystemCommandEnumType.ONLINE_USER.getCommandType().toLowerCase().trim().equals(msg.toLowerCase())){ 142 + } else if (SystemCommandEnumType.ONLINE_USER.getCommandType().toLowerCase().trim().equals(msg.toLowerCase())) {
137 //打印在线用户 143 //打印在线用户
138 printOnlineUsers(); 144 printOnlineUsers();
139 145
140 - } else if (msg.startsWith(SystemCommandEnumType.QUERY.getCommandType().trim() + " ")){ 146 + } else if (msg.startsWith(SystemCommandEnumType.QUERY.getCommandType().trim() + " ")) {
141 //查询聊天记录 147 //查询聊天记录
142 queryChatHistory(msg); 148 queryChatHistory(msg);
143 - }else if (SystemCommandEnumType.AI.getCommandType().trim().equals(msg.toLowerCase())){ 149 + } else if (SystemCommandEnumType.AI.getCommandType().trim().equals(msg.toLowerCase())) {
144 //开启 AI 模式 150 //开启 AI 模式
145 - aiModel = true ;  
146 - System.out.println("\033[31;4m" + "Hello,我是估值两亿的 AI 机器人!" + "\033[0m");  
147 - }else if (SystemCommandEnumType.QAI.getCommandType().trim().equals(msg.toLowerCase())){ 151 + aiModel = true;
  152 + System.out.println("\033[31;4m" + "Hello,我是估值两亿的 AI 机器人!" + "\033[0m");
  153 + } else if (SystemCommandEnumType.QAI.getCommandType().trim().equals(msg.toLowerCase())) {
148 //关闭 AI 模式 154 //关闭 AI 模式
149 - aiModel = false ;  
150 - System.out.println("\033[31;4m" + "。゚(゚´ω`゚)゚。 AI 下线了!" + "\033[0m");  
151 - }else if (msg.startsWith(SystemCommandEnumType.PREFIX.getCommandType().trim() + " ")){ 155 + aiModel = false;
  156 + System.out.println("\033[31;4m" + "。゚(゚´ω`゚)゚。 AI 下线了!" + "\033[0m");
  157 + } else if (msg.startsWith(SystemCommandEnumType.PREFIX.getCommandType().trim() + " ")) {
152 //模糊匹配 158 //模糊匹配
153 prefixSearch(msg); 159 prefixSearch(msg);
154 - }else { 160 + } else if (SystemCommandEnumType.INFO.getCommandType().trim().equals(msg.toLowerCase())) {
  161 + LOGGER.info("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
  162 + LOGGER.info("client info=[{}]", JSON.toJSONString(clientInfo.get()));
  163 + LOGGER.info("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
  164 +
  165 + } else {
155 printAllCommand(allStatusCode); 166 printAllCommand(allStatusCode);
156 } 167 }
157 168
158 - return true ; 169 + return true;
159 170
160 - }else {  
161 - return false ; 171 + } else {
  172 + return false;
162 } 173 }
163 174
164 175
@@ -167,12 +178,13 @@ public class MsgHandler implements MsgHandle { @@ -167,12 +178,13 @@ public class MsgHandler implements MsgHandle {
167 178
168 /** 179 /**
169 * 模糊匹配 180 * 模糊匹配
  181 + *
170 * @param msg 182 * @param msg
171 */ 183 */
172 private void prefixSearch(String msg) { 184 private void prefixSearch(String msg) {
173 try { 185 try {
174 List<OnlineUsersResVO.DataBodyBean> onlineUsers = routeRequest.onlineUsers(); 186 List<OnlineUsersResVO.DataBodyBean> onlineUsers = routeRequest.onlineUsers();
175 - TrieTree trieTree = new TrieTree() ; 187 + TrieTree trieTree = new TrieTree();
176 for (OnlineUsersResVO.DataBodyBean onlineUser : onlineUsers) { 188 for (OnlineUsersResVO.DataBodyBean onlineUser : onlineUsers) {
177 trieTree.insert(onlineUser.getUserName()); 189 trieTree.insert(onlineUser.getUserName());
178 } 190 }
@@ -187,16 +199,17 @@ public class MsgHandler implements MsgHandle { @@ -187,16 +199,17 @@ public class MsgHandler implements MsgHandle {
187 } 199 }
188 200
189 } catch (Exception e) { 201 } catch (Exception e) {
190 - LOGGER.error("Exception" ,e); 202 + LOGGER.error("Exception", e);
191 } 203 }
192 } 204 }
193 205
194 /** 206 /**
195 * 查询聊天记录 207 * 查询聊天记录
  208 + *
196 * @param msg 209 * @param msg
197 */ 210 */
198 private void queryChatHistory(String msg) { 211 private void queryChatHistory(String msg) {
199 - String[] split = msg.split(" ") ; 212 + String[] split = msg.split(" ");
200 String res = msgLogger.query(split[1]); 213 String res = msgLogger.query(split[1]);
201 System.out.println(res); 214 System.out.println(res);
202 } 215 }
@@ -210,12 +223,12 @@ public class MsgHandler implements MsgHandle { @@ -210,12 +223,12 @@ public class MsgHandler implements MsgHandle {
210 223
211 LOGGER.info("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); 224 LOGGER.info("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
212 for (OnlineUsersResVO.DataBodyBean onlineUser : onlineUsers) { 225 for (OnlineUsersResVO.DataBodyBean onlineUser : onlineUsers) {
213 - LOGGER.info("userId={}=====userName={}",onlineUser.getUserId(),onlineUser.getUserName()); 226 + LOGGER.info("userId={}=====userName={}", onlineUser.getUserId(), onlineUser.getUserName());
214 } 227 }
215 LOGGER.info("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); 228 LOGGER.info("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
216 229
217 } catch (Exception e) { 230 } catch (Exception e) {
218 - LOGGER.error("Exception" ,e); 231 + LOGGER.error("Exception", e);
219 } 232 }
220 } 233 }
221 234
@@ -234,7 +247,7 @@ public class MsgHandler implements MsgHandle { @@ -234,7 +247,7 @@ public class MsgHandler implements MsgHandle {
234 } 247 }
235 cimClient.close(); 248 cimClient.close();
236 } catch (InterruptedException e) { 249 } catch (InterruptedException e) {
237 - LOGGER.error("InterruptedException",e); 250 + LOGGER.error("InterruptedException", e);
238 } 251 }
239 System.exit(0); 252 System.exit(0);
240 } 253 }
@@ -20,7 +20,8 @@ public enum SystemCommandEnumType { @@ -20,7 +20,8 @@ public enum SystemCommandEnumType {
20 QUERY(":q ","【:q 关键字】查询聊天记录"), 20 QUERY(":q ","【:q 关键字】查询聊天记录"),
21 AI(":ai ","开启 AI 模式"), 21 AI(":ai ","开启 AI 模式"),
22 QAI(":qai ","关闭 AI 模式"), 22 QAI(":qai ","关闭 AI 模式"),
23 - PREFIX(":pu ","模糊匹配用户") 23 + PREFIX(":pu ","模糊匹配用户"),
  24 + INFO(":info ","获取客户端信息")
24 25
25 ; 26 ;
26 27