diff --git a/src/main/java/com/github/unclecatmyself/Application.java b/src/main/java/com/github/unclecatmyself/Application.java new file mode 100644 index 0000000..5438cb0 --- /dev/null +++ b/src/main/java/com/github/unclecatmyself/Application.java @@ -0,0 +1,24 @@ +package com.github.unclecatmyself; + +import com.github.unclecatmyself.auto.ConfigManager; +import com.github.unclecatmyself.auto.InChatServer; +import com.github.unclecatmyself.service.FromServerServiceImpl; +import com.github.unclecatmyself.service.InChatInitializer; +import com.github.unclecatmyself.service.UserAsyncDataListener; +import com.github.unclecatmyself.service.VerifyServiceImpl; + +/** + * Created by MySelf on 2019/8/26. + */ +public class Application { + + + public static void main(String[] args) { + ConfigManager.initNetty = new InChatInitializer(); + ConfigManager.fromServerService = FromServerServiceImpl.TYPE2; + ConfigManager.asyncDataListener = new UserAsyncDataListener(); + ConfigManager.inChatVerifyService = new VerifyServiceImpl(); + InChatServer.open(); + } + +} diff --git a/src/main/java/com/github/unclecatmyself/application.java b/src/main/java/com/github/unclecatmyself/application.java deleted file mode 100644 index 7f65bbb..0000000 --- a/src/main/java/com/github/unclecatmyself/application.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.github.unclecatmyself; - -import com.github.unclecatmyself.auto.ConfigFactory; -import com.github.unclecatmyself.auto.InitServer; -import com.github.unclecatmyself.user.FromServerServiceImpl; -import com.github.unclecatmyself.user.MyInit; -import com.github.unclecatmyself.user.UserListenAsynData; -import com.github.unclecatmyself.user.VerifyServiceImpl; - -/** - * Created by MySelf on 2019/8/26. - */ -public class application { - - public static void main(String[] args) { - ConfigFactory.initNetty = new MyInit(); - ConfigFactory.fromServerService = FromServerServiceImpl.TYPE2; - ConfigFactory.listenAsynData = new UserListenAsynData(); - ConfigFactory.inChatVerifyService = new VerifyServiceImpl(); - InitServer.open(); - } - -} diff --git a/src/main/java/com/github/unclecatmyself/auto/ConfigFactory.java b/src/main/java/com/github/unclecatmyself/auto/ConfigManager.java similarity index 68% rename from src/main/java/com/github/unclecatmyself/auto/ConfigFactory.java rename to src/main/java/com/github/unclecatmyself/auto/ConfigManager.java index a4553ad..5f13ea1 100644 --- a/src/main/java/com/github/unclecatmyself/auto/ConfigFactory.java +++ b/src/main/java/com/github/unclecatmyself/auto/ConfigManager.java @@ -1,16 +1,15 @@ package com.github.unclecatmyself.auto; import com.github.unclecatmyself.bootstrap.channel.http.FromServerService; -import com.github.unclecatmyself.bootstrap.data.InChatToDataBaseService; -import com.github.unclecatmyself.bootstrap.verify.InChatVerifyService; +import com.github.unclecatmyself.bootstrap.channel.protocol.InChatVerifyService; import com.github.unclecatmyself.common.bean.InitNetty; -import com.github.unclecatmyself.task.ListenAsynData; +import com.github.unclecatmyself.task.AsyncDataListener; /** * 默认配置工厂 * Created by MySelf on 2018/12/21. */ -public class ConfigFactory { +public class ConfigManager { /** Redis的ip地址 */ public static String RedisIP; @@ -25,6 +24,6 @@ public class ConfigFactory { public static InitNetty initNetty; /** 用户监听异步数据伪接口 */ - public static ListenAsynData listenAsynData; + public static AsyncDataListener asyncDataListener; } diff --git a/src/main/java/com/github/unclecatmyself/auto/InitServer.java b/src/main/java/com/github/unclecatmyself/auto/InChatServer.java similarity index 57% rename from src/main/java/com/github/unclecatmyself/auto/InitServer.java rename to src/main/java/com/github/unclecatmyself/auto/InChatServer.java index 6d783ec..7b3b4d7 100644 --- a/src/main/java/com/github/unclecatmyself/auto/InitServer.java +++ b/src/main/java/com/github/unclecatmyself/auto/InChatServer.java @@ -1,8 +1,8 @@ package com.github.unclecatmyself.auto; -import com.github.unclecatmyself.bootstrap.BootstrapServer; -import com.github.unclecatmyself.bootstrap.NettyBootstrapServer; +import com.github.unclecatmyself.bootstrap.server.BootstrapServer; +import com.github.unclecatmyself.bootstrap.server.NettyBootstrapServer; import com.github.unclecatmyself.common.bean.InitNetty; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -11,27 +11,36 @@ * InChat项目启动服务 * Create by UncleCatMySelf in 2018/12/06 **/ -public abstract class InitServer { +public class InChatServer { + + private final static Logger LOGGER = LoggerFactory.getLogger(InChatServer.class); /** * 静态化处理,保证唯一,确保用户启动的是他自己指定的,不是框架的 * 一个数据配置集合 */ - private static InitNetty serverBean = ConfigFactory.initNetty; + private static InitNetty serverBean = ConfigManager.initNetty; /** * netty服务器启动切面 */ - static BootstrapServer bootstrapServer; + private static BootstrapServer bootstrapServer; /** * 主要还是这个{@link NettyBootstrapServer},实例化想要的netty配置服务 */ public static void open() { + /** + * 静态化处理,保证唯一,确保用户启动的是他自己指定的,不是框架的 + * 一个数据配置集合 + */ if (serverBean != null) { bootstrapServer = new NettyBootstrapServer(); bootstrapServer.setServerBean(serverBean); - int port = serverBean.getWebport(); + int port = serverBean.getWebPort(); + if(LOGGER.isInfoEnabled()){ + LOGGER.info("Server started, listening on " + port); + } bootstrapServer.start(); } } diff --git a/src/main/java/com/github/unclecatmyself/bootstrap/backmsg/InChatBackMapServiceImpl.java b/src/main/java/com/github/unclecatmyself/bootstrap/backmsg/InChatBackMapServiceImpl.java deleted file mode 100644 index 57a27e9..0000000 --- a/src/main/java/com/github/unclecatmyself/bootstrap/backmsg/InChatBackMapServiceImpl.java +++ /dev/null @@ -1,66 +0,0 @@ -package com.github.unclecatmyself.bootstrap.backmsg; - -import com.github.unclecatmyself.common.constant.Constans; - -import java.util.HashMap; -import java.util.Map; - -/** - * 列入项目中,默认返回实现 - * Created by MySelf on 2018/11/23. - */ -public class InChatBackMapServiceImpl implements InChatBackMapService { - - - public Map loginSuccess() { - Map backMap = new HashMap(); - backMap.put(Constans.TYPE,Constans.LOGIN); - backMap.put(Constans.SUCCESS,Constans.TRUE); - return backMap; - } - - - public Map loginError() { - Map backMap = new HashMap(); - backMap.put(Constans.TYPE,Constans.LOGIN); - backMap.put(Constans.SUCCESS,Constans.FALSE); - return backMap; - } - - - public Map sendMe(String value) { - Map backMap = new HashMap(); - backMap.put(Constans.TYPE,Constans.SENDME); - backMap.put(Constans.VALUE,value); - return backMap; - } - - - public Map sendBack(String otherOne, String value) { - Map backMap = new HashMap(); - backMap.put(Constans.TYPE,Constans.SENDTO); - backMap.put(Constans.VALUE,value); - backMap.put(Constans.ONE,otherOne); - return backMap; - } - - - public Map getMsg(String token, String value) { - Map backMap = new HashMap(); - backMap.put(Constans.TYPE,Constans.SENDTO); - backMap.put(Constans.FROM,token); - backMap.put(Constans.VALUE,value); - return backMap; - } - - - public Map sendGroup(String token, String value, String groupId) { - Map backMap = new HashMap(); - backMap.put(Constans.TYPE,Constans.SENDGROUP); - backMap.put(Constans.FROM,token); - backMap.put(Constans.VALUE,value); - backMap.put(Constans.GROUPID,groupId); - return backMap; - } - -} diff --git a/src/main/java/com/github/unclecatmyself/bootstrap/channel/HandlerServiceImpl.java b/src/main/java/com/github/unclecatmyself/bootstrap/channel/AbstractHandlerService.java similarity index 50% rename from src/main/java/com/github/unclecatmyself/bootstrap/channel/HandlerServiceImpl.java rename to src/main/java/com/github/unclecatmyself/bootstrap/channel/AbstractHandlerService.java index 9fd01ef..75892f5 100644 --- a/src/main/java/com/github/unclecatmyself/bootstrap/channel/HandlerServiceImpl.java +++ b/src/main/java/com/github/unclecatmyself/bootstrap/channel/AbstractHandlerService.java @@ -1,65 +1,60 @@ package com.github.unclecatmyself.bootstrap.channel; import com.alibaba.fastjson.JSONArray; -import com.github.unclecatmyself.bootstrap.backmsg.InChatBackMapService; -import com.github.unclecatmyself.bootstrap.backmsg.InChatBackMapServiceImpl; -import com.github.unclecatmyself.bootstrap.channel.http.HttpChannelService; -import com.github.unclecatmyself.bootstrap.channel.http.HttpChannelServiceImpl; -import com.github.unclecatmyself.bootstrap.channel.ws.WebSocketChannelService; -import com.github.unclecatmyself.common.base.HandlerService; +import com.github.unclecatmyself.bootstrap.channel.protocol.Response; +import com.github.unclecatmyself.service.InChatResponse; +import com.github.unclecatmyself.bootstrap.channel.protocol.HttpChannel; +import com.github.unclecatmyself.bootstrap.channel.http.HttpChannelImpl; +import com.github.unclecatmyself.bootstrap.channel.ws.WebSocketChannel; +import com.github.unclecatmyself.service.HandlerService; import com.github.unclecatmyself.common.bean.SendInChat; import com.github.unclecatmyself.common.bean.vo.SendServerVO; -import com.github.unclecatmyself.common.constant.Constans; -import com.github.unclecatmyself.task.ListenAsynData; +import com.github.unclecatmyself.common.constant.Constants; +import com.github.unclecatmyself.task.AsyncDataListener; import com.google.gson.Gson; -import com.github.unclecatmyself.bootstrap.channel.ws.WsChannelService; -import com.github.unclecatmyself.bootstrap.verify.InChatVerifyService; -import com.github.unclecatmyself.task.DataAsynchronousTask; +import com.github.unclecatmyself.bootstrap.channel.protocol.SocketChannel; +import com.github.unclecatmyself.bootstrap.channel.protocol.InChatVerifyService; import io.netty.channel.Channel; -import io.netty.channel.ChannelFuture; -import io.netty.channel.ChannelFutureListener; import io.netty.handler.codec.http.FullHttpMessage; import io.netty.handler.codec.http.websocketx.TextWebSocketFrame; import io.netty.util.CharsetUtil; -import java.util.ArrayList; -import java.util.List; import java.util.Map; /** * Created by MySelf on 2018/11/21. */ -public class HandlerServiceImpl extends HandlerService { +public class AbstractHandlerService extends HandlerService { private final InChatVerifyService inChatVerifyService; - private final InChatBackMapService inChatBackMapService = new InChatBackMapServiceImpl(); + private final Response response = new InChatResponse(); - private final HttpChannelService httpChannelService = new HttpChannelServiceImpl(); + private final HttpChannel httpChannel = new HttpChannelImpl(); - private final WsChannelService websocketChannelService = new WebSocketChannelService(); + private final SocketChannel webSocketChannel = new WebSocketChannel(); - private ListenAsynData listenAsynData; + private AsyncDataListener asyncDataListener; - public HandlerServiceImpl(InChatVerifyService inChatVerifyService,ListenAsynData listenAsynData) { + public AbstractHandlerService(InChatVerifyService inChatVerifyService, AsyncDataListener asyncDataListener) { this.inChatVerifyService = inChatVerifyService; - this.listenAsynData = listenAsynData; + this.asyncDataListener = asyncDataListener; } @Override public void getList(Channel channel) { - httpChannelService.getList(channel); + httpChannel.getList(channel); } @Override public void getSize(Channel channel) { - httpChannelService.getSize(channel); + httpChannel.getSize(channel); } @Override public void sendFromServer(Channel channel, SendServerVO serverVO) { - httpChannelService.sendFromServer(channel,serverVO); + httpChannel.sendFromServer(channel,serverVO); } @Override @@ -68,12 +63,12 @@ public void sendInChat(Channel channel, FullHttpMessage msg) { String content = msg.content().toString(CharsetUtil.UTF_8); Gson gson = new Gson(); SendInChat sendInChat = gson.fromJson(content,SendInChat.class); - httpChannelService.sendByInChat(channel,sendInChat); + httpChannel.sendByInChat(channel,sendInChat); } @Override public void notFindUri(Channel channel) { - httpChannelService.notFindUri(channel); + httpChannel.notFindUri(channel); } @Override @@ -86,74 +81,74 @@ public boolean login(Channel channel, Map maps) { public void sendMeText(Channel channel, Map maps) { Gson gson = new Gson(); channel.writeAndFlush(new TextWebSocketFrame( - gson.toJson(inChatBackMapService.sendMe((String) maps.get(Constans.VALUE))))); - listenAsynData.asynData(maps); + gson.toJson(response.sendMe((String) maps.get(Constants.VALUE))))); + asyncDataListener.asyncData(maps); } @Override public void sendToText(Channel channel, Map maps) { Gson gson = new Gson(); - String otherOne = (String) maps.get(Constans.ONE); - String value = (String) maps.get(Constans.VALUE); - String token = (String) maps.get(Constans.TOKEN); + String otherOne = (String) maps.get(Constants.ONE); + String value = (String) maps.get(Constants.VALUE); + String token = (String) maps.get(Constants.TOKEN); //返回给自己 channel.writeAndFlush(new TextWebSocketFrame( - gson.toJson(inChatBackMapService.sendBack(otherOne,value)))); - if (websocketChannelService.hasOther(otherOne)){ + gson.toJson(response.sendBack(otherOne,value)))); + if (webSocketChannel.hasOther(otherOne)){ //发送给对方--在线 - Channel other = websocketChannelService.getChannel(otherOne); + Channel other = webSocketChannel.getChannel(otherOne); if (other == null){ //转http分布式 - httpChannelService.sendInChat(otherOne,inChatBackMapService.getMsg(token,value)); + httpChannel.sendInChat(otherOne, response.getMessage(token,value)); }else{ other.writeAndFlush(new TextWebSocketFrame( - gson.toJson(inChatBackMapService.getMsg(token,value)))); + gson.toJson(response.getMessage(token,value)))); } }else { - maps.put(Constans.ON_ONLINE,otherOne); + maps.put(Constants.ON_ONLINE,otherOne); } - listenAsynData.asynData(maps); + asyncDataListener.asyncData(maps); } @Override public void sendGroupText(Channel channel, Map maps) { Gson gson = new Gson(); - String groupId = (String) maps.get(Constans.GROUPID); - String token = (String) maps.get(Constans.TOKEN); - String value = (String) maps.get(Constans.VALUE); + String groupId = (String) maps.get(Constants.GROUP_ID); + String token = (String) maps.get(Constants.TOKEN); + String value = (String) maps.get(Constants.VALUE); String no_online = ""; JSONArray array = inChatVerifyService.getArrayByGroupId(groupId); channel.writeAndFlush(new TextWebSocketFrame( - gson.toJson(inChatBackMapService.sendGroup(token,value,groupId)))); + gson.toJson(response.sendGroup(token,value,groupId)))); for (Object item:array) { if (!token.equals(item)){ - if (websocketChannelService.hasOther((String) item)){ - Channel other = websocketChannelService.getChannel((String) item); + if (webSocketChannel.hasOther((String) item)){ + Channel other = webSocketChannel.getChannel((String) item); if (other == null){ //转http分布式 - httpChannelService.sendInChat((String) item,inChatBackMapService.sendGroup(token,value,groupId)); + httpChannel.sendInChat((String) item, response.sendGroup(token,value,groupId)); }else{ other.writeAndFlush(new TextWebSocketFrame( - gson.toJson(inChatBackMapService.sendGroup(token,value,groupId)))); + gson.toJson(response.sendGroup(token,value,groupId)))); } }else{ no_online = (String) item + "、" + no_online; } } } - maps.put(Constans.ONLINE_GROUP,no_online.substring(0,no_online.length()-1)); - listenAsynData.asynData(maps); + maps.put(Constants.ONLINE_GROUP,no_online.substring(0,no_online.length()-1)); + asyncDataListener.asyncData(maps); } @Override public void verify(Channel channel, Map maps) { Gson gson = new Gson(); - String token = (String) maps.get(Constans.TOKEN); + String token = (String) maps.get(Constants.TOKEN); System.out.println(token); if (inChatVerifyService.verifyToken(token)){ return; }else{ - channel.writeAndFlush(new TextWebSocketFrame(gson.toJson(inChatBackMapService.loginError()))); + channel.writeAndFlush(new TextWebSocketFrame(gson.toJson(response.loginError()))); close(channel); } } @@ -161,27 +156,27 @@ public void verify(Channel channel, Map maps) { @Override public void sendPhotoToMe(Channel channel, Map maps) { Gson gson = new Gson(); - System.out.println(maps.get(Constans.VALUE)); + System.out.println(maps.get(Constants.VALUE)); channel.writeAndFlush(new TextWebSocketFrame( - gson.toJson(inChatBackMapService.sendMe((String) maps.get(Constans.VALUE))))); - listenAsynData.asynData(maps); + gson.toJson(response.sendMe((String) maps.get(Constants.VALUE))))); + asyncDataListener.asyncData(maps); } private Boolean check(Channel channel, Map maps){ Gson gson = new Gson(); - String token = (String) maps.get(Constans.TOKEN); + String token = (String) maps.get(Constants.TOKEN); if (inChatVerifyService.verifyToken(token)){ - channel.writeAndFlush(new TextWebSocketFrame(gson.toJson(inChatBackMapService.loginSuccess()))); - websocketChannelService.loginWsSuccess(channel,token); + channel.writeAndFlush(new TextWebSocketFrame(gson.toJson(response.loginSuccess()))); + webSocketChannel.loginWsSuccess(channel,token); return true; } - channel.writeAndFlush(new TextWebSocketFrame(gson.toJson(inChatBackMapService.loginError()))); + channel.writeAndFlush(new TextWebSocketFrame(gson.toJson(response.loginError()))); close(channel); return false; } @Override public void close(Channel channel) { - websocketChannelService.close(channel); + webSocketChannel.close(channel); } } diff --git a/src/main/java/com/github/unclecatmyself/bootstrap/channel/cache/CacheMap.java b/src/main/java/com/github/unclecatmyself/bootstrap/channel/cache/CacheMap.java index 3ed8d75..d3a9bca 100644 --- a/src/main/java/com/github/unclecatmyself/bootstrap/channel/cache/CacheMap.java +++ b/src/main/java/com/github/unclecatmyself/bootstrap/channel/cache/CacheMap.java @@ -10,7 +10,7 @@ **/ public class CacheMap { - private ConcurrentHashMap> datas = new ConcurrentHashMap<>(); + private ConcurrentHashMap> cacheMap = new ConcurrentHashMap<>(); public boolean putData(K[] topic, V v){ if(topic.length==1){ @@ -35,10 +35,10 @@ public boolean putData(K[] topic, V v){ public boolean delete(K[] ks,V v){ if(ks.length==1){ - return datas.get(ks[0]).delValue(v); + return cacheMap.get(ks[0]).delValue(v); } else{ - Node kvNode = datas.get(ks[0]); + Node kvNode = cacheMap.get(ks[0]); for(int i=1;i getData(K[] ks){ if(ks.length==1){ - return datas.get(ks[0]).get(); + return cacheMap.get(ks[0]).get(); } else{ - Node node = datas.get(ks[0]); + Node node = cacheMap.get(ks[0]); if(node!=null){ List all = new ArrayList<>(); all.addAll(node.get()); @@ -71,7 +71,7 @@ public List getData(K[] ks){ public Node buildOne(K k,V v){ - Node node = this.datas.computeIfAbsent(k, key -> { + Node node = this.cacheMap.computeIfAbsent(k, key -> { Node kObjectNode = new Node(k); return kObjectNode; }); diff --git a/src/main/java/com/github/unclecatmyself/bootstrap/channel/cache/WsCacheMap.java b/src/main/java/com/github/unclecatmyself/bootstrap/channel/cache/WebSocketCacheMap.java similarity index 71% rename from src/main/java/com/github/unclecatmyself/bootstrap/channel/cache/WsCacheMap.java rename to src/main/java/com/github/unclecatmyself/bootstrap/channel/cache/WebSocketCacheMap.java index a0a9099..e696a47 100644 --- a/src/main/java/com/github/unclecatmyself/bootstrap/channel/cache/WsCacheMap.java +++ b/src/main/java/com/github/unclecatmyself/bootstrap/channel/cache/WebSocketCacheMap.java @@ -1,10 +1,10 @@ package com.github.unclecatmyself.bootstrap.channel.cache; -import com.github.unclecatmyself.auto.AutoConfig; -import com.github.unclecatmyself.auto.ConfigFactory; -import com.github.unclecatmyself.auto.RedisConfig; -import com.github.unclecatmyself.common.exception.NotFindLoginChannlException; -import com.github.unclecatmyself.common.constant.NotInChatConstant; +import com.github.unclecatmyself.common.config.AutoConfig; +import com.github.unclecatmyself.auto.ConfigManager; +import com.github.unclecatmyself.common.config.RedisConfig; +import com.github.unclecatmyself.common.constant.UndefinedInChatConstant; +import com.github.unclecatmyself.common.exception.LoginChannelNotFoundException; import com.github.unclecatmyself.common.utils.RedisUtil; import io.netty.channel.Channel; import redis.clients.jedis.Jedis; @@ -17,17 +17,17 @@ * WebSocket链接实例本地存储 * Created by MySelf on 2018/11/26. */ -public class WsCacheMap { +public class WebSocketCacheMap { /** * 存储用户标识与链接实例 */ - private final static Map maps = new ConcurrentHashMap(); + private final static Map channelByTokenMap = new ConcurrentHashMap<>(); /** * 存储链接地址与用户标识 */ - private final static Map addMaps = new ConcurrentHashMap<>(); + private final static Map addressByTokenMap = new ConcurrentHashMap<>(); /** * Redis连接实例 @@ -37,7 +37,7 @@ public class WsCacheMap { /** * 是否启动分布式 */ - private final static Boolean isDistributed = ConfigFactory.initNetty.getDistributed(); + private final static Boolean isDistributed = ConfigManager.initNetty.getDistributed(); private final static String address = AutoConfig.address; @@ -47,7 +47,7 @@ public class WsCacheMap { * @param channel {@link Channel} 链接实例 */ public static void saveWs(String token,Channel channel){ - maps.put(token,channel); + channelByTokenMap.put(token,channel); if (isDistributed){ jedis.set(token, RedisUtil.convertMD5(address,token)); } @@ -59,7 +59,7 @@ public static void saveWs(String token,Channel channel){ * @param token 用户标签 */ public static void saveAd(String address,String token){ - addMaps.put(address, token); + addressByTokenMap.put(address, token); } /** @@ -69,12 +69,12 @@ public static void saveAd(String address,String token){ */ public static Channel getByToken(String token){ if (isDistributed){ - if (!maps.containsKey(token)){ + if (!channelByTokenMap.containsKey(token)){ //转分布式发送 return null; } } - return maps.get(token); + return channelByTokenMap.get(token); } /** @@ -83,7 +83,7 @@ public static Channel getByToken(String token){ * @return {@link String} */ public static String getByAddress(String address){ - return addMaps.get(address); + return addressByTokenMap.get(address); } /** @@ -92,12 +92,12 @@ public static String getByAddress(String address){ */ public static void deleteWs(String token){ try { - maps.remove(token); + channelByTokenMap.remove(token); if (isDistributed){ jedis.del(token); } }catch (NullPointerException e){ - throw new NotFindLoginChannlException(NotInChatConstant.Not_Login); + throw new LoginChannelNotFoundException(UndefinedInChatConstant.Not_Login); } } @@ -106,7 +106,7 @@ public static void deleteWs(String token){ * @param address */ public static void deleteAd(String address){ - addMaps.remove(address); + addressByTokenMap.remove(address); } /** @@ -117,7 +117,7 @@ public static Integer getSize(){ if (isDistributed){ return jedis.keys("*").size(); } - return maps.size(); + return channelByTokenMap.size(); } /** @@ -129,7 +129,7 @@ public static boolean hasToken(String token){ if (isDistributed){ return jedis.exists(token); } - return maps.containsKey(token); + return channelByTokenMap.containsKey(token); } /** @@ -140,7 +140,7 @@ public static Set getTokenList(){ if (isDistributed){ return jedis.keys("*"); } - Set keys = maps.keySet(); + Set keys = channelByTokenMap.keySet(); return keys; } diff --git a/src/main/java/com/github/unclecatmyself/bootstrap/channel/http/HttpChannelServiceImpl.java b/src/main/java/com/github/unclecatmyself/bootstrap/channel/http/HttpChannelImpl.java similarity index 78% rename from src/main/java/com/github/unclecatmyself/bootstrap/channel/http/HttpChannelServiceImpl.java rename to src/main/java/com/github/unclecatmyself/bootstrap/channel/http/HttpChannelImpl.java index 1063a44..c23aa61 100644 --- a/src/main/java/com/github/unclecatmyself/bootstrap/channel/http/HttpChannelServiceImpl.java +++ b/src/main/java/com/github/unclecatmyself/bootstrap/channel/http/HttpChannelImpl.java @@ -1,134 +1,136 @@ -package com.github.unclecatmyself.bootstrap.channel.http; - -import com.alibaba.fastjson.JSONObject; -import com.github.unclecatmyself.auto.ConfigFactory; -import com.github.unclecatmyself.bootstrap.channel.cache.WsCacheMap; -import com.github.unclecatmyself.common.bean.SendInChat; -import com.github.unclecatmyself.common.bean.vo.*; -import com.github.unclecatmyself.common.constant.HttpConstant; -import com.github.unclecatmyself.common.constant.LogConstant; -import com.github.unclecatmyself.common.constant.NotInChatConstant; -import com.github.unclecatmyself.common.utils.HttpUtil; -import com.github.unclecatmyself.common.utils.RedisUtil; -import com.google.gson.Gson; -import io.netty.buffer.ByteBuf; -import io.netty.buffer.Unpooled; -import io.netty.channel.Channel; -import io.netty.handler.codec.http.*; -import io.netty.handler.codec.http.websocketx.TextWebSocketFrame; -import io.netty.util.CharsetUtil; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.UnsupportedEncodingException; -import java.util.Date; -import java.util.Map; - -/** - * Create by UncleCatMySelf in 11:41 2018\12\31 0031 - */ -public class HttpChannelServiceImpl implements HttpChannelService { - - private static final Logger log = LoggerFactory.getLogger(HttpChannelServiceImpl.class); - - private static FromServerService fromServerService = ConfigFactory.fromServerService; - - @Override - public void getSize(Channel channel) { - FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); - response.headers().set(HttpConstant.CONTENT_TYPE,HttpConstant.APPLICATION_JSON); - GetSizeVO getSizeVO = new GetSizeVO(WsCacheMap.getSize(),new Date()); - ResultVO resultVO = new ResultVO<>(HttpResponseStatus.OK.code(),getSizeVO); - Gson gson = new Gson(); - ByteBuf buf = Unpooled.copiedBuffer(gson.toJson(resultVO), CharsetUtil.UTF_8); - response.content().writeBytes(buf); - channel.writeAndFlush(response); - close(channel); - } - - @Override - public void sendFromServer(Channel channel, SendServerVO serverVO) { - if (serverVO.getToken() == ""){ - notFindUri(channel); - } - Channel userChannel = WsCacheMap.getByToken(serverVO.getToken()); - if (userChannel == null){ - log.info(LogConstant.HTTPCHANNELSERVICEIMPL_NOTFINDLOGIN); - notFindToken(channel); - } - String value = fromServerService.findByCode(Integer.parseInt(serverVO.getValue())); - SendServer sendServer = new SendServer(value); - try { - userChannel.writeAndFlush(new TextWebSocketFrame(JSONObject.toJSONString(sendServer))); - sendServer(channel, NotInChatConstant.SEND_SUCCESS); - }catch (Exception e){ - log.info(LogConstant.HTTPCHANNELSERVICEIMPL_SEND_EXCEPTION); - } - } - - private void sendServer(Channel channel,String msg){ - FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.BAD_REQUEST); - response.headers().set(HttpConstant.CONTENT_TYPE,HttpConstant.APPLICATION_JSON); - NotFindUriVO notFindUriVO = new NotFindUriVO(msg); - ResultVO resultVO = new ResultVO<>(HttpResponseStatus.BAD_REQUEST.code(),notFindUriVO); - Gson gson = new Gson(); - ByteBuf buf = Unpooled.copiedBuffer(gson.toJson(resultVO), CharsetUtil.UTF_8); - response.content().writeBytes(buf); - channel.writeAndFlush(response); - close(channel); - } - - private void notFindToken(Channel channel) { - sendServer(channel,NotInChatConstant.NOT_FIND_LOGIN); - } - - @Override - public void notFindUri(Channel channel) { - sendServer(channel,NotInChatConstant.NOT_FIND_URI); - } - - @Override - public void close(Channel channel) { - log.info(LogConstant.HTTPCHANNELSERVICEIMPL_CLOSE); - channel.close(); - } - - @Override - public void getList(Channel channel) { - FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); - response.headers().set(HttpConstant.CONTENT_TYPE,HttpConstant.APPLICATION_JSON); - GetListVO getListVO = new GetListVO(WsCacheMap.getTokenList()); - ResultVO resultVO = new ResultVO<>(HttpResponseStatus.OK.code(),getListVO); - Gson gson = new Gson(); - ByteBuf buf = Unpooled.copiedBuffer(gson.toJson(resultVO), CharsetUtil.UTF_8); - response.content().writeBytes(buf); - channel.writeAndFlush(response); - close(channel); - } - - @Override - public void sendInChat(String token, Map msg) { - String address = RedisUtil.getAddress(RedisUtil.convertMD5(WsCacheMap.getByJedis(token))); - String[] str = address.split(":"); - try { - HttpClient.getInstance().send(str[0],Integer.parseInt(str[1]),token,msg); - } catch (Exception e) { - e.printStackTrace(); - } - } - - @Override - public void sendByInChat(Channel channel, SendInChat sendInChat) { - Gson gson = new Gson(); - Channel other = WsCacheMap.getByToken(sendInChat.getToken()); - try { - other.writeAndFlush(new TextWebSocketFrame(gson.toJson(sendInChat.getFrame()))); - }catch (NullPointerException e){ - e.printStackTrace(); - } -// FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.BAD_REQUEST); -// response.headers().set(HttpConstant.CONTENT_TYPE,HttpConstant.APPLICATION_JSON); -// channel.writeAndFlush(response); - close(channel); - } -} +package com.github.unclecatmyself.bootstrap.channel.http; + +import com.alibaba.fastjson.JSONObject; +import com.github.unclecatmyself.auto.ConfigManager; +import com.github.unclecatmyself.bootstrap.channel.cache.WebSocketCacheMap; +import com.github.unclecatmyself.bootstrap.channel.protocol.HttpChannel; +import com.github.unclecatmyself.common.bean.SendInChat; +import com.github.unclecatmyself.common.bean.vo.*; +import com.github.unclecatmyself.common.constant.HttpConstant; +import com.github.unclecatmyself.common.constant.LogConstant; +import com.github.unclecatmyself.common.constant.UndefinedInChatConstant; +import com.github.unclecatmyself.common.utils.RedisUtil; +import com.google.gson.Gson; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; +import io.netty.channel.Channel; +import io.netty.handler.codec.http.DefaultFullHttpResponse; +import io.netty.handler.codec.http.FullHttpResponse; +import io.netty.handler.codec.http.HttpResponseStatus; +import io.netty.handler.codec.http.HttpVersion; +import io.netty.handler.codec.http.websocketx.TextWebSocketFrame; +import io.netty.util.CharsetUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Date; +import java.util.Map; + +/** + * Create by UncleCatMySelf in 11:41 2018\12\31 0031 + */ +public class HttpChannelImpl implements HttpChannel { + + private static final Logger log = LoggerFactory.getLogger(HttpChannelImpl.class); + + private static FromServerService fromServerService = ConfigManager.fromServerService; + + @Override + public void getSize(Channel channel) { + FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); + response.headers().set(HttpConstant.CONTENT_TYPE,HttpConstant.APPLICATION_JSON); + GetSizeVO getSizeVO = new GetSizeVO(WebSocketCacheMap.getSize(),new Date()); + ResultVO resultVO = new ResultVO<>(HttpResponseStatus.OK.code(),getSizeVO); + Gson gson = new Gson(); + ByteBuf buf = Unpooled.copiedBuffer(gson.toJson(resultVO), CharsetUtil.UTF_8); + response.content().writeBytes(buf); + channel.writeAndFlush(response); + close(channel); + } + + @Override + public void sendFromServer(Channel channel, SendServerVO serverVO) { + if (serverVO.getToken() == ""){ + notFindUri(channel); + } + Channel userChannel = WebSocketCacheMap.getByToken(serverVO.getToken()); + if (userChannel == null){ + log.info(LogConstant.HTTPCHANNELSERVICEIMPL_NOTFINDLOGIN); + notFindToken(channel); + } + String value = fromServerService.findByCode(Integer.parseInt(serverVO.getValue())); + SendServer sendServer = new SendServer(value); + try { + userChannel.writeAndFlush(new TextWebSocketFrame(JSONObject.toJSONString(sendServer))); + sendServer(channel, UndefinedInChatConstant.SEND_SUCCESS); + }catch (Exception e){ + log.info(LogConstant.HTTPCHANNELSERVICEIMPL_SEND_EXCEPTION); + } + } + + private void sendServer(Channel channel,String msg){ + FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.BAD_REQUEST); + response.headers().set(HttpConstant.CONTENT_TYPE,HttpConstant.APPLICATION_JSON); + NotFindUriVO notFindUriVO = new NotFindUriVO(msg); + ResultVO resultVO = new ResultVO<>(HttpResponseStatus.BAD_REQUEST.code(),notFindUriVO); + Gson gson = new Gson(); + ByteBuf buf = Unpooled.copiedBuffer(gson.toJson(resultVO), CharsetUtil.UTF_8); + response.content().writeBytes(buf); + channel.writeAndFlush(response); + close(channel); + } + + private void notFindToken(Channel channel) { + sendServer(channel, UndefinedInChatConstant.NOT_FIND_LOGIN); + } + + @Override + public void notFindUri(Channel channel) { + sendServer(channel, UndefinedInChatConstant.NOT_FIND_URI); + } + + @Override + public void close(Channel channel) { + log.info(LogConstant.HTTPCHANNELSERVICEIMPL_CLOSE); + channel.close(); + } + + @Override + public void getList(Channel channel) { + FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); + response.headers().set(HttpConstant.CONTENT_TYPE,HttpConstant.APPLICATION_JSON); + GetListVO getListVO = new GetListVO(WebSocketCacheMap.getTokenList()); + ResultVO resultVO = new ResultVO<>(HttpResponseStatus.OK.code(),getListVO); + Gson gson = new Gson(); + ByteBuf buf = Unpooled.copiedBuffer(gson.toJson(resultVO), CharsetUtil.UTF_8); + response.content().writeBytes(buf); + channel.writeAndFlush(response); + close(channel); + } + + @Override + public void sendInChat(String token, Map msg) { + String address = RedisUtil.getAddress(RedisUtil.convertMD5(WebSocketCacheMap.getByJedis(token))); + String[] str = address.split(":"); + try { + HttpClient.getInstance().send(str[0],Integer.parseInt(str[1]),token,msg); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Override + public void sendByInChat(Channel channel, SendInChat sendInChat) { + Gson gson = new Gson(); + Channel other = WebSocketCacheMap.getByToken(sendInChat.getToken()); + try { + other.writeAndFlush(new TextWebSocketFrame(gson.toJson(sendInChat.getFrame()))); + }catch (NullPointerException e){ + e.printStackTrace(); + } +// FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.BAD_REQUEST); +// response.headers().set(HttpConstant.CONTENT_TYPE,HttpConstant.APPLICATION_JSON); +// channel.writeAndFlush(response); + close(channel); + } +} diff --git a/src/main/java/com/github/unclecatmyself/bootstrap/channel/http/HttpClient.java b/src/main/java/com/github/unclecatmyself/bootstrap/channel/http/HttpClient.java index cce9f11..10e00c9 100644 --- a/src/main/java/com/github/unclecatmyself/bootstrap/channel/http/HttpClient.java +++ b/src/main/java/com/github/unclecatmyself/bootstrap/channel/http/HttpClient.java @@ -11,7 +11,6 @@ import io.netty.bootstrap.Bootstrap; import io.netty.channel.socket.SocketChannel; import io.netty.handler.codec.http.*; -import io.netty.handler.ssl.SslContext; import io.netty.handler.ssl.SslHandler; import io.netty.util.CharsetUtil; import org.slf4j.Logger; @@ -67,7 +66,7 @@ public void send(String host, int port,String token,Map value) throws Exception // Start the client. ChannelFuture f = this.bootstrap.connect(host, port).sync(); - URI uri = new URI(HttpConstant.URI_SENDINCHAT); + URI uri = new URI(HttpConstant.URI_SEND_IN_CHAT); Gson gson = new Gson(); String content = gson.toJson(new SendInChat(token,value)); diff --git a/src/main/java/com/github/unclecatmyself/bootstrap/channel/http/HttpChannelService.java b/src/main/java/com/github/unclecatmyself/bootstrap/channel/protocol/HttpChannel.java similarity index 84% rename from src/main/java/com/github/unclecatmyself/bootstrap/channel/http/HttpChannelService.java rename to src/main/java/com/github/unclecatmyself/bootstrap/channel/protocol/HttpChannel.java index 0f730a1..daa0503 100644 --- a/src/main/java/com/github/unclecatmyself/bootstrap/channel/http/HttpChannelService.java +++ b/src/main/java/com/github/unclecatmyself/bootstrap/channel/protocol/HttpChannel.java @@ -1,30 +1,30 @@ -package com.github.unclecatmyself.bootstrap.channel.http; - -import com.github.unclecatmyself.common.bean.SendInChat; -import com.github.unclecatmyself.common.bean.vo.SendServerVO; -import io.netty.channel.Channel; -import io.netty.channel.ChannelConfig; -import io.netty.handler.codec.http.websocketx.TextWebSocketFrame; - -import java.util.Map; - -/** - * Create by UncleCatMySelf in 11:41 2018\12\31 0031 - */ -public interface HttpChannelService { - - void getSize(Channel channel); - - void sendFromServer(Channel channel,SendServerVO serverVO); - - void notFindUri(Channel channel); - - void close(Channel channel); - - void getList(Channel channel); - - void sendInChat(String token, Map msg); - - void sendByInChat(Channel channel,SendInChat sendInChat); - -} +package com.github.unclecatmyself.bootstrap.channel.protocol; + +import com.github.unclecatmyself.common.bean.SendInChat; +import com.github.unclecatmyself.common.bean.vo.SendServerVO; +import io.netty.channel.Channel; +import io.netty.channel.ChannelConfig; +import io.netty.handler.codec.http.websocketx.TextWebSocketFrame; + +import java.util.Map; + +/** + * Create by UncleCatMySelf in 11:41 2018\12\31 0031 + */ +public interface HttpChannel { + + void getSize(Channel channel); + + void sendFromServer(Channel channel,SendServerVO serverVO); + + void notFindUri(Channel channel); + + void close(Channel channel); + + void getList(Channel channel); + + void sendInChat(String token, Map msg); + + void sendByInChat(Channel channel,SendInChat sendInChat); + +} diff --git a/src/main/java/com/github/unclecatmyself/bootstrap/verify/InChatVerifyService.java b/src/main/java/com/github/unclecatmyself/bootstrap/channel/protocol/InChatVerifyService.java similarity index 79% rename from src/main/java/com/github/unclecatmyself/bootstrap/verify/InChatVerifyService.java rename to src/main/java/com/github/unclecatmyself/bootstrap/channel/protocol/InChatVerifyService.java index 0854bfd..0936dd9 100644 --- a/src/main/java/com/github/unclecatmyself/bootstrap/verify/InChatVerifyService.java +++ b/src/main/java/com/github/unclecatmyself/bootstrap/channel/protocol/InChatVerifyService.java @@ -1,4 +1,4 @@ -package com.github.unclecatmyself.bootstrap.verify; +package com.github.unclecatmyself.bootstrap.channel.protocol; import com.alibaba.fastjson.JSONArray; diff --git a/src/main/java/com/github/unclecatmyself/bootstrap/backmsg/InChatBackMapService.java b/src/main/java/com/github/unclecatmyself/bootstrap/channel/protocol/Response.java similarity index 88% rename from src/main/java/com/github/unclecatmyself/bootstrap/backmsg/InChatBackMapService.java rename to src/main/java/com/github/unclecatmyself/bootstrap/channel/protocol/Response.java index ffb7a07..1e2b3d2 100644 --- a/src/main/java/com/github/unclecatmyself/bootstrap/backmsg/InChatBackMapService.java +++ b/src/main/java/com/github/unclecatmyself/bootstrap/channel/protocol/Response.java @@ -1,4 +1,4 @@ -package com.github.unclecatmyself.bootstrap.backmsg; +package com.github.unclecatmyself.bootstrap.channel.protocol; import java.util.Map; @@ -7,7 +7,7 @@ * 消息返回 * Created by MySelf on 2018/11/23. */ -public interface InChatBackMapService { +public interface Response { /** * 登录成功返回信息 @@ -42,7 +42,7 @@ public interface InChatBackMapService { * @param value {@link String} 通讯消息 * @return */ - Map getMsg(String me, String value); + Map getMessage(String me, String value); /** * 发送消息到群里 diff --git a/src/main/java/com/github/unclecatmyself/bootstrap/channel/ws/WsChannelService.java b/src/main/java/com/github/unclecatmyself/bootstrap/channel/protocol/SocketChannel.java similarity index 92% rename from src/main/java/com/github/unclecatmyself/bootstrap/channel/ws/WsChannelService.java rename to src/main/java/com/github/unclecatmyself/bootstrap/channel/protocol/SocketChannel.java index e3b8113..36db7e8 100644 --- a/src/main/java/com/github/unclecatmyself/bootstrap/channel/ws/WsChannelService.java +++ b/src/main/java/com/github/unclecatmyself/bootstrap/channel/protocol/SocketChannel.java @@ -1,4 +1,4 @@ -package com.github.unclecatmyself.bootstrap.channel.ws; +package com.github.unclecatmyself.bootstrap.channel.protocol; import io.netty.channel.Channel; @@ -8,7 +8,7 @@ * WebSocket 聊天业务消息处理 * Created by MySelf on 2018/11/26. */ -public interface WsChannelService { +public interface SocketChannel { /** * 登录成功存储到本地缓存 diff --git a/src/main/java/com/github/unclecatmyself/bootstrap/channel/ws/WebSocketChannelService.java b/src/main/java/com/github/unclecatmyself/bootstrap/channel/ws/WebSocketChannel.java similarity index 57% rename from src/main/java/com/github/unclecatmyself/bootstrap/channel/ws/WebSocketChannelService.java rename to src/main/java/com/github/unclecatmyself/bootstrap/channel/ws/WebSocketChannel.java index f5ad892..28212ff 100644 --- a/src/main/java/com/github/unclecatmyself/bootstrap/channel/ws/WebSocketChannelService.java +++ b/src/main/java/com/github/unclecatmyself/bootstrap/channel/ws/WebSocketChannel.java @@ -1,7 +1,8 @@ package com.github.unclecatmyself.bootstrap.channel.ws; +import com.github.unclecatmyself.bootstrap.channel.protocol.SocketChannel; import com.google.gson.Gson; -import com.github.unclecatmyself.bootstrap.channel.cache.WsCacheMap; +import com.github.unclecatmyself.bootstrap.channel.cache.WebSocketCacheMap; import io.netty.channel.Channel; import io.netty.handler.codec.http.websocketx.TextWebSocketFrame; @@ -10,29 +11,29 @@ /** * Created by MySelf on 2018/11/26. */ -public class WebSocketChannelService implements WsChannelService { +public class WebSocketChannel implements SocketChannel { @Override public void loginWsSuccess(Channel channel, String token) { - WsCacheMap.saveWs(token,channel); - WsCacheMap.saveAd(channel.remoteAddress().toString(),token); + WebSocketCacheMap.saveWs(token,channel); + WebSocketCacheMap.saveAd(channel.remoteAddress().toString(),token); } @Override public boolean hasOther(String otherOne) { - return WsCacheMap.hasToken(otherOne); + return WebSocketCacheMap.hasToken(otherOne); } @Override public Channel getChannel(String otherOne) { - return WsCacheMap.getByToken(otherOne); + return WebSocketCacheMap.getByToken(otherOne); } @Override public void close(Channel channel) { - String token = WsCacheMap.getByAddress(channel.remoteAddress().toString()); - WsCacheMap.deleteAd(channel.remoteAddress().toString()); - WsCacheMap.deleteWs(token); + String token = WebSocketCacheMap.getByAddress(channel.remoteAddress().toString()); + WebSocketCacheMap.deleteAd(channel.remoteAddress().toString()); + WebSocketCacheMap.deleteWs(token); channel.close(); } diff --git a/src/main/java/com/github/unclecatmyself/common/base/Handler.java b/src/main/java/com/github/unclecatmyself/bootstrap/handler/AbstractHandler.java similarity index 79% rename from src/main/java/com/github/unclecatmyself/common/base/Handler.java rename to src/main/java/com/github/unclecatmyself/bootstrap/handler/AbstractHandler.java index cdcec7e..8be43e5 100644 --- a/src/main/java/com/github/unclecatmyself/common/base/Handler.java +++ b/src/main/java/com/github/unclecatmyself/bootstrap/handler/AbstractHandler.java @@ -1,7 +1,7 @@ -package com.github.unclecatmyself.common.base; +package com.github.unclecatmyself.bootstrap.handler; import com.github.unclecatmyself.common.constant.LogConstant; -import com.github.unclecatmyself.common.exception.NotFindLoginChannlException; +import com.github.unclecatmyself.common.exception.LoginChannelNotFoundException; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; import io.netty.handler.codec.http.FullHttpRequest; @@ -14,14 +14,14 @@ * Netty实现初始层 * Create by UncleCatMySelf in 2018/12/06 */ -public abstract class Handler extends SimpleChannelInboundHandler { +public abstract class AbstractHandler extends SimpleChannelInboundHandler { - private static final Logger log = LoggerFactory.getLogger(Handler.class); + private static final Logger log = LoggerFactory.getLogger(AbstractHandler.class); - HandlerApi handlerApi; + private Handler handler; - public Handler(HandlerApi handlerApi){ - this.handlerApi = handlerApi; + public AbstractHandler(Handler handler){ + this.handler = handler; } @Override @@ -48,8 +48,8 @@ protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Except public void channelInactive(ChannelHandlerContext ctx) throws Exception { log.info(LogConstant.CHANNELINACTIVE+ctx.channel().localAddress().toString()+LogConstant.CLOSE_SUCCESS); try { - handlerApi.close(ctx.channel()); - }catch (NotFindLoginChannlException e){ + handler.close(ctx.channel()); + }catch (LoginChannelNotFoundException e){ log.error(LogConstant.NOTFINDLOGINCHANNLEXCEPTION); } } diff --git a/src/main/java/com/github/unclecatmyself/bootstrap/handler/DefaultHandler.java b/src/main/java/com/github/unclecatmyself/bootstrap/handler/DefaultAbstractHandler.java similarity index 72% rename from src/main/java/com/github/unclecatmyself/bootstrap/handler/DefaultHandler.java rename to src/main/java/com/github/unclecatmyself/bootstrap/handler/DefaultAbstractHandler.java index c799238..f6bd814 100644 --- a/src/main/java/com/github/unclecatmyself/bootstrap/handler/DefaultHandler.java +++ b/src/main/java/com/github/unclecatmyself/bootstrap/handler/DefaultAbstractHandler.java @@ -1,15 +1,13 @@ package com.github.unclecatmyself.bootstrap.handler; import com.alibaba.fastjson.JSON; -import com.github.unclecatmyself.common.base.Handler; -import com.github.unclecatmyself.common.base.HandlerApi; -import com.github.unclecatmyself.common.base.HandlerService; +import com.github.unclecatmyself.service.HandlerService; import com.github.unclecatmyself.common.bean.vo.SendServerVO; -import com.github.unclecatmyself.common.constant.Constans; +import com.github.unclecatmyself.common.constant.Constants; import com.github.unclecatmyself.common.constant.HttpConstant; import com.github.unclecatmyself.common.constant.LogConstant; -import com.github.unclecatmyself.common.constant.NotInChatConstant; -import com.github.unclecatmyself.common.exception.NoFindHandlerException; +import com.github.unclecatmyself.common.constant.UndefinedInChatConstant; +import com.github.unclecatmyself.common.exception.HandlerNotFoundException; import com.github.unclecatmyself.common.utils.HttpUtil; import io.netty.channel.Channel; import io.netty.channel.ChannelHandler; @@ -28,25 +26,25 @@ * Create by UncleCatMySelf in 2018/12/06 */ @ChannelHandler.Sharable -public class DefaultHandler extends Handler { +public class DefaultAbstractHandler extends AbstractHandler { - private final Logger log = LoggerFactory.getLogger(DefaultHandler.class); + private final Logger log = LoggerFactory.getLogger(DefaultAbstractHandler.class); - private final HandlerApi handlerApi; + private final Handler handler; - public DefaultHandler(HandlerApi handlerApi) { - super(handlerApi); - this.handlerApi = handlerApi; + public DefaultAbstractHandler(Handler handler) { + super(handler); + this.handler = handler; } @Override protected void webdoMessage(ChannelHandlerContext ctx, WebSocketFrame msg) { Channel channel = ctx.channel(); HandlerService httpHandlerService; - if (handlerApi instanceof HandlerService){ - httpHandlerService = (HandlerService)handlerApi; + if (handler instanceof HandlerService){ + httpHandlerService = (HandlerService) handler; }else { - throw new NoFindHandlerException(NotInChatConstant.NOT_HANDLER); + throw new HandlerNotFoundException(UndefinedInChatConstant.NOT_HANDLER); } if (msg instanceof BinaryWebSocketFrame){ //TODO 实现图片处理 @@ -57,17 +55,17 @@ protected void webdoMessage(ChannelHandlerContext ctx, WebSocketFrame msg) { protected void httpdoMessage(ChannelHandlerContext ctx, FullHttpRequest msg) { Channel channel = ctx.channel(); HandlerService httpHandlerService; - if (handlerApi instanceof HandlerService){ - httpHandlerService = (HandlerService)handlerApi; + if (handler instanceof HandlerService){ + httpHandlerService = (HandlerService) handler; }else { - throw new NoFindHandlerException(NotInChatConstant.NOT_HANDLER); + throw new HandlerNotFoundException(UndefinedInChatConstant.NOT_HANDLER); } switch (HttpUtil.checkType(msg)){ - case HttpConstant.GETSIZE: + case HttpConstant.GET_SIZE: log.info(LogConstant.DEFAULTWEBSOCKETHANDLER_GETSIZE); httpHandlerService.getSize(channel); break; - case HttpConstant.SENDFROMSERVER: + case HttpConstant.SEND_FROM_SERVER: log.info(LogConstant.DEFAULTWEBSOCKETHANDLER_SENDFROMSERVER); SendServerVO serverVO = null; try { @@ -77,15 +75,15 @@ protected void httpdoMessage(ChannelHandlerContext ctx, FullHttpRequest msg) { } httpHandlerService.sendFromServer(channel,serverVO); break; - case HttpConstant.GETLIST: + case HttpConstant.GET_LIST: log.info(LogConstant.DEFAULTWEBSOCKETHANDLER_GETLIST); httpHandlerService.getList(channel); break; - case HttpConstant.SENDINCHAT: + case HttpConstant.SEND_IN_CHAT: log.info(LogConstant.DEFAULTWEBSOCKETHANDLER_SENDINCHAT); httpHandlerService.sendInChat(channel,msg); break; - case HttpConstant.NOTFINDURI: + case HttpConstant.NOT_FIND_URI: log.info(LogConstant.DEFAULTWEBSOCKETHANDLER_NOTFINDURI); httpHandlerService.notFindUri(channel); break; @@ -99,38 +97,38 @@ protected void httpdoMessage(ChannelHandlerContext ctx, FullHttpRequest msg) { protected void textdoMessage(ChannelHandlerContext ctx, TextWebSocketFrame msg) { Channel channel = ctx.channel(); HandlerService handlerService; - if (handlerApi instanceof HandlerService){ - handlerService = (HandlerService)handlerApi; + if (handler instanceof HandlerService){ + handlerService = (HandlerService) handler; }else{ - throw new NoFindHandlerException(NotInChatConstant.NOT_HANDLER); + throw new HandlerNotFoundException(UndefinedInChatConstant.NOT_HANDLER); } Map maps = (Map) JSON.parse(msg.text()); - maps.put(Constans.TIME, new Date()); - switch ((String)maps.get(Constans.TYPE)){ - case Constans.LOGIN: + maps.put(Constants.TIME, new Date()); + switch ((String)maps.get(Constants.TYPE)){ + case Constants.LOGIN: log.info(LogConstant.DEFAULTWEBSOCKETHANDLER_LOGIN); handlerService.login(channel,maps); break; //针对个人,发送给自己 - case Constans.SENDME: + case Constants.SEND_ME: log.info(LogConstant.DEFAULTWEBSOCKETHANDLER_SENDME); handlerService.verify(channel,maps); handlerService.sendMeText(channel,maps); break; //针对个人,发送给某人 - case Constans.SENDTO: + case Constants.SEND_TO: log.info(LogConstant.DefaultWebSocketHandler_SENDTO); handlerService.verify(channel,maps); handlerService.sendToText(channel,maps); break; //发送给群组 - case Constans.SENDGROUP: + case Constants.SEND_GROUP: log.info(LogConstant.DEFAULTWEBSOCKETHANDLER_SENDGROUP); handlerService.verify(channel,maps); handlerService.sendGroupText(channel,maps); break; //发送图片,发送给自己 - case Constans.SENDPHOTOTOME: + case Constants.SEND_PHOTO_TO_ME: log.info("图片到个人"); handlerService.verify(channel,maps); handlerService.sendPhotoToMe(channel,maps); diff --git a/src/main/java/com/github/unclecatmyself/common/base/HandlerApi.java b/src/main/java/com/github/unclecatmyself/bootstrap/handler/Handler.java similarity index 64% rename from src/main/java/com/github/unclecatmyself/common/base/HandlerApi.java rename to src/main/java/com/github/unclecatmyself/bootstrap/handler/Handler.java index 536c0ad..a6853a6 100644 --- a/src/main/java/com/github/unclecatmyself/common/base/HandlerApi.java +++ b/src/main/java/com/github/unclecatmyself/bootstrap/handler/Handler.java @@ -1,4 +1,4 @@ -package com.github.unclecatmyself.common.base; +package com.github.unclecatmyself.bootstrap.handler; import io.netty.channel.Channel; @@ -7,7 +7,7 @@ /** * Create by UncleCatMySelf in 2018/12/06 */ -public interface HandlerApi { +public interface Handler { void close(Channel channel); diff --git a/src/main/java/com/github/unclecatmyself/bootstrap/AbstractBootstrapServer.java b/src/main/java/com/github/unclecatmyself/bootstrap/server/AbstractBootstrapServer.java similarity index 78% rename from src/main/java/com/github/unclecatmyself/bootstrap/AbstractBootstrapServer.java rename to src/main/java/com/github/unclecatmyself/bootstrap/server/AbstractBootstrapServer.java index 7f272d5..0800d60 100644 --- a/src/main/java/com/github/unclecatmyself/bootstrap/AbstractBootstrapServer.java +++ b/src/main/java/com/github/unclecatmyself/bootstrap/server/AbstractBootstrapServer.java @@ -1,18 +1,16 @@ -package com.github.unclecatmyself.bootstrap; +package com.github.unclecatmyself.bootstrap.server; -import com.github.unclecatmyself.auto.ConfigFactory; -import com.github.unclecatmyself.bootstrap.channel.HandlerServiceImpl; -import com.github.unclecatmyself.bootstrap.handler.DefaultHandler; +import com.github.unclecatmyself.auto.ConfigManager; +import com.github.unclecatmyself.bootstrap.channel.AbstractHandlerService; +import com.github.unclecatmyself.bootstrap.handler.DefaultAbstractHandler; import com.github.unclecatmyself.common.bean.InitNetty; import com.github.unclecatmyself.common.constant.BootstrapConstant; -import com.github.unclecatmyself.common.constant.NotInChatConstant; -import com.github.unclecatmyself.common.ssl.SecureSocketSslContextFactory; +import com.github.unclecatmyself.common.constant.UndefinedInChatConstant; +import com.github.unclecatmyself.common.utils.ssl.SecureSocketSslContextFactory; import com.github.unclecatmyself.common.utils.SslUtil; import io.netty.channel.ChannelPipeline; import io.netty.handler.codec.http.HttpObjectAggregator; -import io.netty.handler.codec.http.HttpRequestDecoder; -import io.netty.handler.codec.http.HttpResponseEncoder; import io.netty.handler.codec.http.HttpServerCodec; import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler; import io.netty.handler.ssl.SslHandler; @@ -44,7 +42,7 @@ public abstract class AbstractBootstrapServer implements BootstrapServer { protected void initHandler(ChannelPipeline channelPipeline, InitNetty serverBean){ if (serverBean.isSsl()){ if (!ObjectUtils.allNotNull(serverBean.getJksCertificatePassword(),serverBean.getJksFile(),serverBean.getJksStorePassword())){ - throw new NullPointerException(NotInChatConstant.SSL_NOT_FIND); + throw new NullPointerException(UndefinedInChatConstant.SSL_NOT_FIND); } try { SSLContext context = SslUtil.createSSLContext("JKS",serverBean.getJksFile(),serverBean.getJksStorePassword()); @@ -59,16 +57,16 @@ protected void initHandler(ChannelPipeline channelPipeline, InitNetty serverBea } intProtocolHandler(channelPipeline,serverBean); channelPipeline.addLast(new IdleStateHandler(serverBean.getHeart(),0,0)); - channelPipeline.addLast(new DefaultHandler(new HandlerServiceImpl(ConfigFactory.inChatVerifyService,ConfigFactory.listenAsynData))); + channelPipeline.addLast(new DefaultAbstractHandler(new AbstractHandlerService(ConfigManager.inChatVerifyService, ConfigManager.asyncDataListener))); } private void intProtocolHandler(ChannelPipeline channelPipeline,InitNetty serverBean){ - channelPipeline.addLast(BootstrapConstant.HTTPCODE,new HttpServerCodec()); + channelPipeline.addLast(BootstrapConstant.HTTP_CODE,new HttpServerCodec()); // channelPipeline.addLast("http-decoder",new HttpRequestDecoder()); channelPipeline.addLast(BootstrapConstant.AGGREGATOR, new HttpObjectAggregator(serverBean.getMaxContext())); // channelPipeline.addLast("http-encoder",new HttpResponseEncoder()); - channelPipeline.addLast(BootstrapConstant.CHUNKEDWRITE,new ChunkedWriteHandler()); - channelPipeline.addLast(BootstrapConstant.WEBSOCKETHANDLER,new WebSocketServerProtocolHandler(serverBean.getWebSocketPath())); + channelPipeline.addLast(BootstrapConstant.CHUNKED_WRITE,new ChunkedWriteHandler()); + channelPipeline.addLast(BootstrapConstant.WEB_SOCKET_HANDLER,new WebSocketServerProtocolHandler(serverBean.getWebSocketPath())); } private void initSsl(InitNetty serverBean){ diff --git a/src/main/java/com/github/unclecatmyself/bootstrap/BootstrapServer.java b/src/main/java/com/github/unclecatmyself/bootstrap/server/BootstrapServer.java similarity index 81% rename from src/main/java/com/github/unclecatmyself/bootstrap/BootstrapServer.java rename to src/main/java/com/github/unclecatmyself/bootstrap/server/BootstrapServer.java index 00c36fa..b5fd002 100644 --- a/src/main/java/com/github/unclecatmyself/bootstrap/BootstrapServer.java +++ b/src/main/java/com/github/unclecatmyself/bootstrap/server/BootstrapServer.java @@ -1,4 +1,4 @@ -package com.github.unclecatmyself.bootstrap; +package com.github.unclecatmyself.bootstrap.server; import com.github.unclecatmyself.common.bean.InitNetty; diff --git a/src/main/java/com/github/unclecatmyself/bootstrap/NettyBootstrapServer.java b/src/main/java/com/github/unclecatmyself/bootstrap/server/NettyBootstrapServer.java similarity index 84% rename from src/main/java/com/github/unclecatmyself/bootstrap/NettyBootstrapServer.java rename to src/main/java/com/github/unclecatmyself/bootstrap/server/NettyBootstrapServer.java index 9282465..730aa02 100644 --- a/src/main/java/com/github/unclecatmyself/bootstrap/NettyBootstrapServer.java +++ b/src/main/java/com/github/unclecatmyself/bootstrap/server/NettyBootstrapServer.java @@ -1,9 +1,9 @@ -package com.github.unclecatmyself.bootstrap; +package com.github.unclecatmyself.bootstrap.server; -import com.github.unclecatmyself.auto.AutoConfig; -import com.github.unclecatmyself.auto.RedisConfig; -import com.github.unclecatmyself.common.ip.IpUtils; +import com.github.unclecatmyself.common.config.AutoConfig; +import com.github.unclecatmyself.common.config.RedisConfig; import com.github.unclecatmyself.common.bean.InitNetty; +import com.github.unclecatmyself.common.utils.UniqueIpUtils; import com.github.unclecatmyself.common.utils.RemotingUtil; import io.netty.bootstrap.ServerBootstrap; import io.netty.buffer.PooledByteBufAllocator; @@ -57,27 +57,27 @@ public void start() { .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) .option(ChannelOption.SO_RCVBUF, serverBean.getRevbuf()) .childHandler(new ChannelInitializer() { - protected void initChannel(SocketChannel ch) throws Exception { + protected void initChannel(SocketChannel ch) { initHandler(ch.pipeline(), serverBean); } }) .childOption(ChannelOption.TCP_NODELAY, serverBean.isNodelay()) - .childOption(ChannelOption.SO_KEEPALIVE, serverBean.isKeepalive()) + .childOption(ChannelOption.SO_KEEPALIVE, serverBean.isKeepAlive()) .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); - bootstrap.bind(IpUtils.getHost(), serverBean.getWebport()).addListener((ChannelFutureListener) channelFuture -> { + bootstrap.bind(UniqueIpUtils.getHost(), serverBean.getWebPort()).addListener((ChannelFutureListener) channelFuture -> { if (channelFuture.isSuccess()) { - log.info("服务端启动成功【" + IpUtils.getHost() + ":" + serverBean.getWebport() + "】"); - AutoConfig.address = IpUtils.getHost() + ":" + serverBean.getWebport(); + log.info("服务端启动成功【" + UniqueIpUtils.getHost() + ":" + serverBean.getWebPort() + "】"); + AutoConfig.address = UniqueIpUtils.getHost() + ":" + serverBean.getWebPort(); RedisConfig.getInstance(); } else { - log.info("服务端启动失败【" + IpUtils.getHost() + ":" + serverBean.getWebport() + "】"); + log.info("服务端启动失败【" + UniqueIpUtils.getHost() + ":" + serverBean.getWebPort() + "】"); } }); } } /** - * 初始化EnentPool 参数 + * 初始化EventPool 参数 */ private void initEventPool() { bootstrap = new ServerBootstrap(); @@ -125,7 +125,7 @@ public void shutdown() { bossGroup.shutdownGracefully().sync();// 优雅关闭 workGroup.shutdownGracefully().sync(); } catch (InterruptedException e) { - log.error("服务端关闭资源失败【" + IpUtils.getHost() + ":" + serverBean.getWebport() + "】"); + log.error("服务端关闭资源失败【" + UniqueIpUtils.getHost() + ":" + serverBean.getWebPort() + "】"); } } } diff --git a/src/main/java/com/github/unclecatmyself/common/bean/InChatMessage.java b/src/main/java/com/github/unclecatmyself/common/bean/InChatMessage.java index 96f4a15..c5e3b9d 100644 --- a/src/main/java/com/github/unclecatmyself/common/bean/InChatMessage.java +++ b/src/main/java/com/github/unclecatmyself/common/bean/InChatMessage.java @@ -1,6 +1,5 @@ package com.github.unclecatmyself.common.bean; -import java.util.ArrayList; import java.util.Date; /** @@ -22,7 +21,7 @@ public class InChatMessage { private String token; /** 群聊Id */ - private String groudId; + private String groupId; /** 是否在线-个人 */ private String online; @@ -73,12 +72,12 @@ public void setToken(String token) { this.token = token; } - public String getGroudId() { - return groudId; + public String getGroupId() { + return groupId; } - public void setGroudId(String groudId) { - this.groudId = groudId; + public void setGroupId(String groupId) { + this.groupId = groupId; } public String getOnline() { @@ -104,7 +103,7 @@ public String toString() { ", type='" + type + '\'' + ", value='" + value + '\'' + ", token='" + token + '\'' + - ", groudId='" + groudId + '\'' + + ", groupId='" + groupId + '\'' + ", online='" + online + '\'' + ", onlineGroup=" + onlineGroup + ", one='" + one + '\'' + diff --git a/src/main/java/com/github/unclecatmyself/common/bean/InitNetty.java b/src/main/java/com/github/unclecatmyself/common/bean/InitNetty.java index 7922764..7c1cf8b 100644 --- a/src/main/java/com/github/unclecatmyself/common/bean/InitNetty.java +++ b/src/main/java/com/github/unclecatmyself/common/bean/InitNetty.java @@ -1,6 +1,6 @@ package com.github.unclecatmyself.common.bean; -import com.github.unclecatmyself.bootstrap.handler.DefaultHandler; +import com.github.unclecatmyself.bootstrap.handler.DefaultAbstractHandler; /** * 初始化Netty配置 @@ -9,13 +9,13 @@ public abstract class InitNetty { /** 通信地址 */ - private int webport = 8090; + private int webPort = 8090; private int bossThread = 1; private int workerThread = 2; - private boolean keepalive = true; + private boolean keepAlive = true; private int backlog = 1024; @@ -51,7 +51,7 @@ public abstract class InitNetty { private String jksCertificatePassword = "123456"; - private Class webSocketHandler = DefaultHandler.class; + private Class webSocketHandler = DefaultAbstractHandler.class; public Boolean getDistributed() { return isDistributed; @@ -65,8 +65,8 @@ public void setDistributed(Boolean distributed) { * 返回WebSocket启动监听端口 * @return {@link Integer} WebSocket端口 */ - public int getWebport() { - return webport; + public int getWebPort() { + return webPort; } /** @@ -97,8 +97,8 @@ public void setWorkerThread(int workerThread) { * 是否保持链接 * @return {@link Boolean} 是否保持链接 */ - public boolean isKeepalive() { - return keepalive; + public boolean isKeepAlive() { + return keepAlive; } /** @@ -205,7 +205,7 @@ public String getWebSocketPath() { } - public Class getWebSocketHandler() { + public Class getWebSocketHandler() { return webSocketHandler; } diff --git a/src/main/java/com/github/unclecatmyself/common/bean/SendInChat.java b/src/main/java/com/github/unclecatmyself/common/bean/SendInChat.java index 3946077..60a5326 100644 --- a/src/main/java/com/github/unclecatmyself/common/bean/SendInChat.java +++ b/src/main/java/com/github/unclecatmyself/common/bean/SendInChat.java @@ -1,7 +1,5 @@ package com.github.unclecatmyself.common.bean; -import io.netty.handler.codec.http.websocketx.TextWebSocketFrame; - import java.util.Map; /** diff --git a/src/main/java/com/github/unclecatmyself/common/bean/vo/SendServer.java b/src/main/java/com/github/unclecatmyself/common/bean/vo/SendServer.java index e03a0ef..a45ede4 100644 --- a/src/main/java/com/github/unclecatmyself/common/bean/vo/SendServer.java +++ b/src/main/java/com/github/unclecatmyself/common/bean/vo/SendServer.java @@ -1,13 +1,13 @@ package com.github.unclecatmyself.common.bean.vo; -import com.github.unclecatmyself.common.constant.Constans; +import com.github.unclecatmyself.common.constant.Constants; /** * Created by MySelf on 2019/1/3. */ public class SendServer { - private String type = Constans.SERVER; + private String type = Constants.SERVER; private String value; diff --git a/src/main/java/com/github/unclecatmyself/auto/AutoConfig.java b/src/main/java/com/github/unclecatmyself/common/config/AutoConfig.java similarity index 77% rename from src/main/java/com/github/unclecatmyself/auto/AutoConfig.java rename to src/main/java/com/github/unclecatmyself/common/config/AutoConfig.java index 4af8a1f..8ebc924 100644 --- a/src/main/java/com/github/unclecatmyself/auto/AutoConfig.java +++ b/src/main/java/com/github/unclecatmyself/common/config/AutoConfig.java @@ -1,12 +1,12 @@ -package com.github.unclecatmyself.auto; - -/** - * 自动配置厂 - * Create by UncleCatMySelf in 14:33 2019\1\5 0005 - */ -public class AutoConfig { - - //地址配置,默认为空 - public static String address = ""; - -} +package com.github.unclecatmyself.common.config; + +/** + * 自动配置厂 + * Create by UncleCatMySelf in 14:33 2019\1\5 0005 + */ +public class AutoConfig { + + //地址配置,默认为空 + public static String address = ""; + +} diff --git a/src/main/java/com/github/unclecatmyself/auto/RedisConfig.java b/src/main/java/com/github/unclecatmyself/common/config/RedisConfig.java similarity index 67% rename from src/main/java/com/github/unclecatmyself/auto/RedisConfig.java rename to src/main/java/com/github/unclecatmyself/common/config/RedisConfig.java index e384287..493f7ef 100644 --- a/src/main/java/com/github/unclecatmyself/auto/RedisConfig.java +++ b/src/main/java/com/github/unclecatmyself/common/config/RedisConfig.java @@ -1,36 +1,38 @@ -package com.github.unclecatmyself.auto; - -import com.github.unclecatmyself.common.constant.LogConstant; -import com.sun.org.apache.regexp.internal.RE; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import redis.clients.jedis.Jedis; - -/** - * 关于redis一些基础东西 - * Create by UncleCatMySelf in 13:56 2019\1\5 0005 - */ -public class RedisConfig { - - private static final Logger log = LoggerFactory.getLogger(RedisConfig.class); - - /** 单例模式 */ - private static RedisConfig instance = new RedisConfig(); - - public static Jedis jedis; - - /** - * 如果配置启动分布式,则自动初始化jedis - * */ - private RedisConfig(){ - if (ConfigFactory.initNetty.getDistributed()){ - this.jedis = new Jedis(ConfigFactory.RedisIP); - log.info(LogConstant.REDIS_START + jedis.ping()); - } - } - - public static RedisConfig getInstance(){ - return instance; - } - -} +package com.github.unclecatmyself.common.config; + +import com.github.unclecatmyself.auto.ConfigManager; +import com.github.unclecatmyself.common.constant.LogConstant; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import redis.clients.jedis.Jedis; + +/** + * 关于redis一些基础东西 + * Create by UncleCatMySelf in 13:56 2019\1\5 0005 + */ +public class RedisConfig { + + private static final Logger log = LoggerFactory.getLogger(RedisConfig.class); + + /** 单例模式 */ + private static RedisConfig instance = new RedisConfig(); + + public static Jedis jedis; + + /** + * 如果配置启动分布式,则自动初始化jedis + * */ + private RedisConfig(){ + synchronized (this){ + if (ConfigManager.initNetty.getDistributed()){ + jedis = new Jedis(ConfigManager.RedisIP); + log.info(LogConstant.REDIS_START + jedis.ping()); + } + } + } + + public static RedisConfig getInstance(){ + return instance; + } + +} diff --git a/src/main/java/com/github/unclecatmyself/common/constant/BootstrapConstant.java b/src/main/java/com/github/unclecatmyself/common/constant/BootstrapConstant.java index a8e51ad..29c1533 100644 --- a/src/main/java/com/github/unclecatmyself/common/constant/BootstrapConstant.java +++ b/src/main/java/com/github/unclecatmyself/common/constant/BootstrapConstant.java @@ -5,13 +5,13 @@ */ public class BootstrapConstant { - public static final String HTTPCODE = "httpCode"; + public static final String HTTP_CODE = "httpCode"; public static final String AGGREGATOR = "aggregator"; - public static final String CHUNKEDWRITE = "chunkedWrite"; + public static final String CHUNKED_WRITE = "chunkedWrite"; - public static final String WEBSOCKETHANDLER = "webSocketHandler"; + public static final String WEB_SOCKET_HANDLER = "webSocketHandler"; public static final String SSL = "ssl"; diff --git a/src/main/java/com/github/unclecatmyself/common/constant/Constans.java b/src/main/java/com/github/unclecatmyself/common/constant/Constants.java similarity index 71% rename from src/main/java/com/github/unclecatmyself/common/constant/Constans.java rename to src/main/java/com/github/unclecatmyself/common/constant/Constants.java index f70c988..8db7cb4 100644 --- a/src/main/java/com/github/unclecatmyself/common/constant/Constans.java +++ b/src/main/java/com/github/unclecatmyself/common/constant/Constants.java @@ -3,7 +3,7 @@ /** * Created by MySelf on 2018/12/5. */ -public class Constans { +public class Constants { public static final String TOKEN = "token"; @@ -15,7 +15,7 @@ public class Constans { public static final String ONLINE_GROUP = "online_group"; - public static final String GROUPID = "groupId"; + public static final String GROUP_ID = "groupId"; public static final String TYPE = "type"; @@ -27,15 +27,15 @@ public class Constans { public static final String FALSE = "false"; - public static final String SENDME = "sendMe"; + public static final String SEND_ME = "sendMe"; - public static final String SENDTO = "sendTo"; + public static final String SEND_TO = "sendTo"; public static final String FROM = "from"; - public static final String SENDGROUP = "sendGroup"; + public static final String SEND_GROUP = "sendGroup"; - public static final String SENDPHOTOTOME = "sendPhotoToMe"; + public static final String SEND_PHOTO_TO_ME = "sendPhotoToMe"; public static final String TIME = "time"; diff --git a/src/main/java/com/github/unclecatmyself/common/constant/HttpConstant.java b/src/main/java/com/github/unclecatmyself/common/constant/HttpConstant.java index 971d94e..150dae0 100644 --- a/src/main/java/com/github/unclecatmyself/common/constant/HttpConstant.java +++ b/src/main/java/com/github/unclecatmyself/common/constant/HttpConstant.java @@ -9,23 +9,23 @@ public class HttpConstant { public static final String POST = "POST"; - public static final String GETSIZE = "get_size"; + public static final String GET_SIZE = "get_size"; - public static final String GETLIST = "get_list"; + public static final String GET_LIST = "get_list"; - public static final String SENDINCHAT = "send_inchat"; + public static final String SEND_IN_CHAT = "send_inChat"; - public static final String SENDFROMSERVER = "send_from_server"; + public static final String SEND_FROM_SERVER = "send_from_server"; - public static final String NOTFINDURI = "not_find_uri"; + public static final String NOT_FIND_URI = "not_find_uri"; - public static final String URI_GETSIZE = "/get_size"; + public static final String URI_GET_SIZE = "/uri_get_size"; - public static final String URI_GETLIST = "/get_list"; + public static final String URI_GET_LIST = "/uri_get_list"; - public static final String URI_SENDINCHAT = "/send_inchat"; + public static final String URI_SEND_IN_CHAT = "/uri_send_inChat"; - public static final String URI_SENDFROMSERVER = "/send_from_server"; + public static final String URI_SEND_FROM_SERVER = "/uri_send_from_server"; public static final String CONTENT_TYPE = "Content-Type"; diff --git a/src/main/java/com/github/unclecatmyself/common/constant/LogConstant.java b/src/main/java/com/github/unclecatmyself/common/constant/LogConstant.java index 87f667e..ef29f97 100644 --- a/src/main/java/com/github/unclecatmyself/common/constant/LogConstant.java +++ b/src/main/java/com/github/unclecatmyself/common/constant/LogConstant.java @@ -5,29 +5,29 @@ */ public class LogConstant { - public static final String HTTPCHANNELSERVICEIMPL_NOTFINDLOGIN = "[HttpChannelServiceImpl.sendFromServer] 未找到用户在线标识"; + public static final String HTTPCHANNELSERVICEIMPL_NOTFINDLOGIN = "[HttpChannelImpl.sendFromServer] 未找到用户在线标识"; - public static final String HTTPCHANNELSERVICEIMPL_CLOSE = "[HttpChannelServiceImpl.close] 关闭HTTP通道连接"; + public static final String HTTPCHANNELSERVICEIMPL_CLOSE = "[HttpChannelImpl.close] 关闭HTTP通道连接"; - public static final String HTTPCHANNELSERVICEIMPL_SEND_EXCEPTION = "[HttpChannelServiceImpl.sendFromServer] 发送通知异常"; + public static final String HTTPCHANNELSERVICEIMPL_SEND_EXCEPTION = "[HttpChannelImpl.sendFromServer] 发送通知异常"; - public static final String DEFAULTWEBSOCKETHANDLER_GETSIZE = "[DefaultWebSocketHandler.httpdoMessage.GETSIZE]"; + public static final String DEFAULTWEBSOCKETHANDLER_GETSIZE = "[DefaultWebSocketHandler.httpdoMessage.GET_SIZE]"; - public static final String DEFAULTWEBSOCKETHANDLER_SENDFROMSERVER = "[DefaultWebSocketHandler.httpdoMessage.SENDFROMSERVER]"; + public static final String DEFAULTWEBSOCKETHANDLER_SENDFROMSERVER = "[DefaultWebSocketHandler.httpdoMessage.SEND_FROM_SERVER]"; - public static final String DEFAULTWEBSOCKETHANDLER_NOTFINDURI = "[DefaultWebSocketHandler.httpdoMessage.NOTFINDURI]"; + public static final String DEFAULTWEBSOCKETHANDLER_NOTFINDURI = "[DefaultWebSocketHandler.httpdoMessage.NOT_FIND_URI]"; - public static final String DEFAULTWEBSOCKETHANDLER_GETLIST = "[DefaultWebSocketHandler.httpdoMessage.GETLIST]"; + public static final String DEFAULTWEBSOCKETHANDLER_GETLIST = "[DefaultWebSocketHandler.httpdoMessage.GET_LIST]"; - public static final String DEFAULTWEBSOCKETHANDLER_SENDINCHAT = "[DefaultWebSocketHandler.httpdoMessage.SENDINCHAT]"; + public static final String DEFAULTWEBSOCKETHANDLER_SENDINCHAT = "[DefaultWebSocketHandler.httpdoMessage.SEND_IN_CHAT]"; public static final String DEFAULTWEBSOCKETHANDLER_LOGIN = "[DefaultWebSocketHandler.textdoMessage.LOGIN]"; - public static final String DEFAULTWEBSOCKETHANDLER_SENDME = "[DefaultWebSocketHandler.textdoMessage.SENDME]"; + public static final String DEFAULTWEBSOCKETHANDLER_SENDME = "[DefaultWebSocketHandler.textdoMessage.SEND_ME]"; - public static final String DefaultWebSocketHandler_SENDTO = "[DefaultWebSocketHandler.textdoMessage.SENDTO]"; + public static final String DefaultWebSocketHandler_SENDTO = "[DefaultWebSocketHandler.textdoMessage.SEND_TO]"; - public static final String DEFAULTWEBSOCKETHANDLER_SENDGROUP = "[DefaultWebSocketHandler.textdoMessage.SENDGROUP]"; + public static final String DEFAULTWEBSOCKETHANDLER_SENDGROUP = "[DefaultWebSocketHandler.textdoMessage.SEND_GROUP]"; public static final String CHANNELACTIVE = "[DefaultWebSocketHandler.channelActive]"; @@ -37,11 +37,11 @@ public class LogConstant { public static final String EXCEPTIONCAUGHT = "[DefaultWebSocketHandler.exceptionCaught]"; - public static final String CHANNELINACTIVE = "[Handler:channelInactive]"; + public static final String CHANNELINACTIVE = "[AbstractHandler:channelInactive]"; public static final String CLOSE_SUCCESS = "关闭成功"; - public static final String NOTFINDLOGINCHANNLEXCEPTION = "[捕获异常:NotFindLoginChannlException]-[Handler:channelInactive] 关闭未正常注册链接!"; + public static final String NOTFINDLOGINCHANNLEXCEPTION = "[捕获异常:LoginChannelNotFoundException]-[AbstractHandler:channelInactive] 关闭未正常注册链接!"; public static final String DATAASYNCHRONOUSTASK_01 = "[DataAsynchronousTask.writeData]:数据外抛异常"; diff --git a/src/main/java/com/github/unclecatmyself/common/constant/NotInChatConstant.java b/src/main/java/com/github/unclecatmyself/common/constant/UndefinedInChatConstant.java similarity index 80% rename from src/main/java/com/github/unclecatmyself/common/constant/NotInChatConstant.java rename to src/main/java/com/github/unclecatmyself/common/constant/UndefinedInChatConstant.java index 497deb8..755966d 100644 --- a/src/main/java/com/github/unclecatmyself/common/constant/NotInChatConstant.java +++ b/src/main/java/com/github/unclecatmyself/common/constant/UndefinedInChatConstant.java @@ -3,7 +3,7 @@ /** * Created by MySelf on 2019/1/3. */ -public class NotInChatConstant { +public class UndefinedInChatConstant { public static final String Not_Login = "未找到正常注册的连接"; @@ -13,7 +13,7 @@ public class NotInChatConstant { public static final String NOT_FIND_URI = "未找到匹配任务URI"; - public static final String NOT_HANDLER = "Server Handler 不匹配"; + public static final String NOT_HANDLER = "Server AbstractHandler 不匹配"; public static final String SSL_NOT_FIND = "SSL file and password is null"; diff --git a/src/main/java/com/github/unclecatmyself/common/exception/NoFindHandlerException.java b/src/main/java/com/github/unclecatmyself/common/exception/HandlerNotFoundException.java similarity index 66% rename from src/main/java/com/github/unclecatmyself/common/exception/NoFindHandlerException.java rename to src/main/java/com/github/unclecatmyself/common/exception/HandlerNotFoundException.java index 99cf181..73a4860 100644 --- a/src/main/java/com/github/unclecatmyself/common/exception/NoFindHandlerException.java +++ b/src/main/java/com/github/unclecatmyself/common/exception/HandlerNotFoundException.java @@ -4,11 +4,11 @@ * 找不到对应处理类 * Create by UncleCatMySelf in 2018/12/06 **/ -public class NoFindHandlerException extends RuntimeException { +public class HandlerNotFoundException extends RuntimeException { private static final long serialVersionUID = 6724478022966267728L; - public NoFindHandlerException(String message) { + public HandlerNotFoundException(String message) { super(message); } diff --git a/src/main/java/com/github/unclecatmyself/common/exception/NotFindLoginChannlException.java b/src/main/java/com/github/unclecatmyself/common/exception/LoginChannelNotFoundException.java similarity index 65% rename from src/main/java/com/github/unclecatmyself/common/exception/NotFindLoginChannlException.java rename to src/main/java/com/github/unclecatmyself/common/exception/LoginChannelNotFoundException.java index 7670a6c..b573fb5 100644 --- a/src/main/java/com/github/unclecatmyself/common/exception/NotFindLoginChannlException.java +++ b/src/main/java/com/github/unclecatmyself/common/exception/LoginChannelNotFoundException.java @@ -1,14 +1,14 @@ -package com.github.unclecatmyself.common.exception; - -/** - * 未找到正常注册的连接异常 - * Create by UncleCatMySelf in 13:58 2018\12\30 0030 - */ -public class NotFindLoginChannlException extends RuntimeException { - - private static final long serialVersionUID = -2614068393411382075L; - - public NotFindLoginChannlException(String message) { - super(message); - } -} +package com.github.unclecatmyself.common.exception; + +/** + * 未找到正常注册的连接异常 + * Create by UncleCatMySelf in 13:58 2018\12\30 0030 + */ +public class LoginChannelNotFoundException extends RuntimeException { + + private static final long serialVersionUID = -2614068393411382075L; + + public LoginChannelNotFoundException(String message) { + super(message); + } +} diff --git a/src/main/java/com/github/unclecatmyself/common/pool/DefaultThreadFactory.java b/src/main/java/com/github/unclecatmyself/common/thread/DefaultThreadFactory.java similarity index 96% rename from src/main/java/com/github/unclecatmyself/common/pool/DefaultThreadFactory.java rename to src/main/java/com/github/unclecatmyself/common/thread/DefaultThreadFactory.java index 014999d..2e89087 100644 --- a/src/main/java/com/github/unclecatmyself/common/pool/DefaultThreadFactory.java +++ b/src/main/java/com/github/unclecatmyself/common/thread/DefaultThreadFactory.java @@ -1,4 +1,4 @@ -package com.github.unclecatmyself.common.pool; +package com.github.unclecatmyself.common.thread; import java.util.concurrent.ThreadFactory; import java.util.concurrent.atomic.AtomicInteger; diff --git a/src/main/java/com/github/unclecatmyself/common/pool/ExecutorQueue.java b/src/main/java/com/github/unclecatmyself/common/thread/ExecutorQueue.java similarity index 97% rename from src/main/java/com/github/unclecatmyself/common/pool/ExecutorQueue.java rename to src/main/java/com/github/unclecatmyself/common/thread/ExecutorQueue.java index 89cde71..4f35682 100644 --- a/src/main/java/com/github/unclecatmyself/common/pool/ExecutorQueue.java +++ b/src/main/java/com/github/unclecatmyself/common/thread/ExecutorQueue.java @@ -1,4 +1,4 @@ -package com.github.unclecatmyself.common.pool; +package com.github.unclecatmyself.common.thread; import java.util.concurrent.LinkedTransferQueue; import java.util.concurrent.RejectedExecutionException; diff --git a/src/main/java/com/github/unclecatmyself/common/pool/StandardThreadExecutor.java b/src/main/java/com/github/unclecatmyself/common/thread/StandardThreadExecutor.java similarity index 98% rename from src/main/java/com/github/unclecatmyself/common/pool/StandardThreadExecutor.java rename to src/main/java/com/github/unclecatmyself/common/thread/StandardThreadExecutor.java index 6fdab50..addfea7 100644 --- a/src/main/java/com/github/unclecatmyself/common/pool/StandardThreadExecutor.java +++ b/src/main/java/com/github/unclecatmyself/common/thread/StandardThreadExecutor.java @@ -1,4 +1,4 @@ -package com.github.unclecatmyself.common.pool; +package com.github.unclecatmyself.common.thread; import java.util.concurrent.*; import java.util.concurrent.atomic.AtomicInteger; diff --git a/src/main/java/com/github/unclecatmyself/common/utils/HttpUtil.java b/src/main/java/com/github/unclecatmyself/common/utils/HttpUtil.java index 5f47080..894aac8 100644 --- a/src/main/java/com/github/unclecatmyself/common/utils/HttpUtil.java +++ b/src/main/java/com/github/unclecatmyself/common/utils/HttpUtil.java @@ -1,7 +1,7 @@ package com.github.unclecatmyself.common.utils; import com.github.unclecatmyself.common.bean.vo.SendServerVO; -import com.github.unclecatmyself.common.constant.Constans; +import com.github.unclecatmyself.common.constant.Constants; import com.github.unclecatmyself.common.constant.HttpConstant; import io.netty.handler.codec.http.FullHttpRequest; import io.netty.handler.codec.http.HttpMethod; @@ -21,16 +21,16 @@ public static String checkType(FullHttpRequest msg){ System.out.println(url); HttpMethod method = msg.method(); String meName = method.name(); - if (url.equals(HttpConstant.URI_GETSIZE) && meName.equals(HttpConstant.GET)){ - return HttpConstant.GETSIZE; - }else if (url.equals(HttpConstant.URI_SENDFROMSERVER) && meName.equals(HttpConstant.POST)){ - return HttpConstant.SENDFROMSERVER; - }else if (url.equals(HttpConstant.URI_GETLIST) && meName.equals(HttpConstant.GET)){ - return HttpConstant.GETLIST; - }else if (url.equals(HttpConstant.URI_SENDINCHAT) && meName.equals(HttpConstant.POST)){ - return HttpConstant.SENDINCHAT; + if (url.equals(HttpConstant.URI_GET_SIZE) && meName.equals(HttpConstant.GET)){ + return HttpConstant.GET_SIZE; + }else if (url.equals(HttpConstant.URI_SEND_FROM_SERVER) && meName.equals(HttpConstant.POST)){ + return HttpConstant.SEND_FROM_SERVER; + }else if (url.equals(HttpConstant.URI_GET_LIST) && meName.equals(HttpConstant.GET)){ + return HttpConstant.GET_LIST; + }else if (url.equals(HttpConstant.URI_SEND_IN_CHAT) && meName.equals(HttpConstant.POST)){ + return HttpConstant.SEND_IN_CHAT; }else { - return HttpConstant.NOTFINDURI; + return HttpConstant.NOT_FIND_URI; } } @@ -43,10 +43,10 @@ public static SendServerVO getToken(FullHttpRequest msg) throws UnsupportedEncod String item = stars[i].toString(); String firstType = item.substring(0,5); String value = item.substring(6,item.length()); - if (Constans.TOKEN.equals(firstType)){ + if (Constants.TOKEN.equals(firstType)){ //Token sendServerVO.setToken(value); - }else if(Constans.VALUE.endsWith(firstType)){ + }else if(Constants.VALUE.endsWith(firstType)){ //value sendServerVO.setValue(value); } diff --git a/src/main/java/com/github/unclecatmyself/common/utils/MessageChangeUtil.java b/src/main/java/com/github/unclecatmyself/common/utils/MessageChangeUtil.java deleted file mode 100644 index 9223993..0000000 --- a/src/main/java/com/github/unclecatmyself/common/utils/MessageChangeUtil.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.github.unclecatmyself.common.utils; - -import com.github.unclecatmyself.common.bean.InChatMessage; -import com.github.unclecatmyself.common.constant.Constans; - -import java.util.ArrayList; -import java.util.Date; -import java.util.Map; - -/** - * Created by MySelf on 2018/12/19. - */ -public class MessageChangeUtil { - - public static InChatMessage Change(Map maps){ - InChatMessage message = new InChatMessage(); - if (maps.containsKey(Constans.TOKEN)){ - message.setToken((String) maps.get(Constans.TOKEN)); - } - if (maps.containsKey(Constans.TIME)){ - message.setTime((Date) maps.get(Constans.TIME)); - } - if (maps.containsKey(Constans.VALUE)){ - message.setValue((String)maps.get(Constans.VALUE)); - } - if (maps.containsKey(Constans.TYPE)){ - message.setType((String)maps.get(Constans.TYPE)); - } - if (maps.containsKey(Constans.ONE)){ - message.setOne((String)maps.get(Constans.ONE)); - } - if (maps.containsKey(Constans.GROUPID)){ - message.setGroudId((String)maps.get(Constans.GROUPID)); - } - if (maps.containsKey(Constans.ON_ONLINE)){ - message.setOnline((String)maps.get(Constans.ON_ONLINE)); - } - if (maps.containsKey(Constans.ONLINE_GROUP)){ - message.setOnlineGroup((String)maps.get(Constans.ONLINE_GROUP)); - } - return message; - } - -} diff --git a/src/main/java/com/github/unclecatmyself/common/utils/MessageConversionUtil.java b/src/main/java/com/github/unclecatmyself/common/utils/MessageConversionUtil.java new file mode 100644 index 0000000..94da01f --- /dev/null +++ b/src/main/java/com/github/unclecatmyself/common/utils/MessageConversionUtil.java @@ -0,0 +1,43 @@ +package com.github.unclecatmyself.common.utils; + +import com.github.unclecatmyself.common.bean.InChatMessage; +import com.github.unclecatmyself.common.constant.Constants; + +import java.util.Date; +import java.util.Map; + +/** + * Created by MySelf on 2018/12/19. + */ +public class MessageConversionUtil { + + public static InChatMessage convert(Map maps){ + InChatMessage message = new InChatMessage(); + if (maps.containsKey(Constants.TOKEN)){ + message.setToken((String) maps.get(Constants.TOKEN)); + } + if (maps.containsKey(Constants.TIME)){ + message.setTime((Date) maps.get(Constants.TIME)); + } + if (maps.containsKey(Constants.VALUE)){ + message.setValue((String)maps.get(Constants.VALUE)); + } + if (maps.containsKey(Constants.TYPE)){ + message.setType((String)maps.get(Constants.TYPE)); + } + if (maps.containsKey(Constants.ONE)){ + message.setOne((String)maps.get(Constants.ONE)); + } + if (maps.containsKey(Constants.GROUP_ID)){ + message.setGroupId((String)maps.get(Constants.GROUP_ID)); + } + if (maps.containsKey(Constants.ON_ONLINE)){ + message.setOnline((String)maps.get(Constants.ON_ONLINE)); + } + if (maps.containsKey(Constants.ONLINE_GROUP)){ + message.setOnlineGroup((String)maps.get(Constants.ONLINE_GROUP)); + } + return message; + } + +} diff --git a/src/main/java/com/github/unclecatmyself/common/utils/RedisUtil.java b/src/main/java/com/github/unclecatmyself/common/utils/RedisUtil.java index d659d91..f1ee4cd 100644 --- a/src/main/java/com/github/unclecatmyself/common/utils/RedisUtil.java +++ b/src/main/java/com/github/unclecatmyself/common/utils/RedisUtil.java @@ -7,7 +7,7 @@ */ public class RedisUtil { - private static final String STAL = "InChat"; + private static final String EXTRA = "InChat"; public static String string2MD5(String inStr) { MessageDigest md5 = null; @@ -33,7 +33,7 @@ public static String string2MD5(String inStr) { } public static String convertMD5(String address,String token){ - String inStr = address+"&"+token+"%"+STAL; + String inStr = address+"&"+token+"%"+ EXTRA; char[] a = inStr.toCharArray(); for (int i = 0; i < a.length; i++){ a[i] = (char) (a[i] ^ 't'); diff --git a/src/main/java/com/github/unclecatmyself/common/utils/SnowflakeIdWorker.java b/src/main/java/com/github/unclecatmyself/common/utils/SnowflakeIdWorker.java index 1d36335..1e292a6 100644 --- a/src/main/java/com/github/unclecatmyself/common/utils/SnowflakeIdWorker.java +++ b/src/main/java/com/github/unclecatmyself/common/utils/SnowflakeIdWorker.java @@ -1,11 +1,13 @@ package com.github.unclecatmyself.common.utils; + /** - * @author Noseparte - * @date 2019/8/27 18:05 - * @Description - *

ID生成器

- *

Twitter 雪花算法

+ * @Auther: Noseparte + * @Date: 2019/10/14 18:12 + * @Description: + * + *

ID生成器

+ *

Twitter 雪花算法

*/ public class SnowflakeIdWorker { diff --git a/src/main/java/com/github/unclecatmyself/common/utils/SpringContextUtils.java b/src/main/java/com/github/unclecatmyself/common/utils/SpringContextUtils.java index 0f4f6a2..c69d3f1 100644 --- a/src/main/java/com/github/unclecatmyself/common/utils/SpringContextUtils.java +++ b/src/main/java/com/github/unclecatmyself/common/utils/SpringContextUtils.java @@ -18,7 +18,7 @@ public class SpringContextUtils implements ApplicationContextAware { @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { - this.applicationContext = applicationContext; + SpringContextUtils.applicationContext = applicationContext; } /** @@ -55,12 +55,12 @@ public static T getBean(Class clazz) { * 通过name,以及Clazz返回指定的Bean * * @param name - * @param clazz + * @param requiredType * @param * @return */ - public static T getBean(String name, Class clazz) { - return getApplicationContext().getBean(name, clazz); + public static T getBean(String name, Class requiredType) { + return applicationContext.getBean(name, requiredType); } } diff --git a/src/main/java/com/github/unclecatmyself/common/utils/TokenGenerator.java b/src/main/java/com/github/unclecatmyself/common/utils/TokenGenerator.java index 5423b0b..7aa5608 100644 --- a/src/main/java/com/github/unclecatmyself/common/utils/TokenGenerator.java +++ b/src/main/java/com/github/unclecatmyself/common/utils/TokenGenerator.java @@ -6,11 +6,11 @@ import java.util.regex.Pattern; /** - * @author Noseparte - * @date 2019/8/27 18:05 - * @Description - *

Token

- *

生成器

+ * @Auther: Noseparte + * @Date: 2019/10/14 18:13 + * @Description: + * + *

Token 生成器

*/ public class TokenGenerator { diff --git a/src/main/java/com/github/unclecatmyself/common/ip/IpUtils.java b/src/main/java/com/github/unclecatmyself/common/utils/UniqueIpUtils.java similarity index 71% rename from src/main/java/com/github/unclecatmyself/common/ip/IpUtils.java rename to src/main/java/com/github/unclecatmyself/common/utils/UniqueIpUtils.java index b06e994..f398a21 100644 --- a/src/main/java/com/github/unclecatmyself/common/ip/IpUtils.java +++ b/src/main/java/com/github/unclecatmyself/common/utils/UniqueIpUtils.java @@ -1,14 +1,15 @@ -package com.github.unclecatmyself.common.ip; +package com.github.unclecatmyself.common.utils; import java.net.Inet4Address; import java.net.InetAddress; import java.net.NetworkInterface; import java.util.Enumeration; +import java.util.Objects; /** * Create by UncleCatMySelf in 2018/12/06 **/ -public class IpUtils { +public class UniqueIpUtils { /*** * 获取外网IP @@ -18,17 +19,17 @@ public static String internetIp() { try { Enumeration networks = NetworkInterface.getNetworkInterfaces(); - InetAddress inetAddress = null; - Enumeration inetAddresses = null; + InetAddress inetAddress; + Enumeration netAddresses; while (networks.hasMoreElements()) { - inetAddresses = networks.nextElement().getInetAddresses(); - while (inetAddresses.hasMoreElements()) { - inetAddress = inetAddresses.nextElement(); - if (inetAddress != null + netAddresses = networks.nextElement().getInetAddresses(); + while (netAddresses.hasMoreElements()) { + inetAddress = netAddresses.nextElement(); + if (Objects.nonNull(inetAddress) && inetAddress instanceof Inet4Address && !inetAddress.isSiteLocalAddress() && !inetAddress.isLoopbackAddress() - && inetAddress.getHostAddress().indexOf(":") == -1) { + && !inetAddress.getHostAddress().contains(":")) { return inetAddress.getHostAddress(); } } diff --git a/src/main/java/com/github/unclecatmyself/common/ssl/SecureSocketKeyStore.java b/src/main/java/com/github/unclecatmyself/common/utils/ssl/SecureSocketKeyStore.java similarity index 99% rename from src/main/java/com/github/unclecatmyself/common/ssl/SecureSocketKeyStore.java rename to src/main/java/com/github/unclecatmyself/common/utils/ssl/SecureSocketKeyStore.java index 322f796..dfc17d5 100644 --- a/src/main/java/com/github/unclecatmyself/common/ssl/SecureSocketKeyStore.java +++ b/src/main/java/com/github/unclecatmyself/common/utils/ssl/SecureSocketKeyStore.java @@ -1,4 +1,4 @@ -package com.github.unclecatmyself.common.ssl; +package com.github.unclecatmyself.common.utils.ssl; import java.io.ByteArrayInputStream; import java.io.InputStream; diff --git a/src/main/java/com/github/unclecatmyself/common/ssl/SecureSocketSslContextFactory.java b/src/main/java/com/github/unclecatmyself/common/utils/ssl/SecureSocketSslContextFactory.java similarity index 98% rename from src/main/java/com/github/unclecatmyself/common/ssl/SecureSocketSslContextFactory.java rename to src/main/java/com/github/unclecatmyself/common/utils/ssl/SecureSocketSslContextFactory.java index 8425072..e3d3585 100644 --- a/src/main/java/com/github/unclecatmyself/common/ssl/SecureSocketSslContextFactory.java +++ b/src/main/java/com/github/unclecatmyself/common/utils/ssl/SecureSocketSslContextFactory.java @@ -1,4 +1,4 @@ -package com.github.unclecatmyself.common.ssl; +package com.github.unclecatmyself.common.utils.ssl; import io.netty.util.internal.SystemPropertyUtil; diff --git a/src/main/java/com/github/unclecatmyself/common/ssl/SecureSokcetTrustManagerFactory.java b/src/main/java/com/github/unclecatmyself/common/utils/ssl/SecureSokcetTrustManagerFactory.java similarity index 97% rename from src/main/java/com/github/unclecatmyself/common/ssl/SecureSokcetTrustManagerFactory.java rename to src/main/java/com/github/unclecatmyself/common/utils/ssl/SecureSokcetTrustManagerFactory.java index c4d8975..4893162 100644 --- a/src/main/java/com/github/unclecatmyself/common/ssl/SecureSokcetTrustManagerFactory.java +++ b/src/main/java/com/github/unclecatmyself/common/utils/ssl/SecureSokcetTrustManagerFactory.java @@ -1,4 +1,4 @@ -package com.github.unclecatmyself.common.ssl; +package com.github.unclecatmyself.common.utils.ssl; import javax.net.ssl.ManagerFactoryParameters; import javax.net.ssl.TrustManager; diff --git a/src/main/java/com/github/unclecatmyself/common/ssl/StreamReader.java b/src/main/java/com/github/unclecatmyself/common/utils/ssl/StreamReader.java similarity index 89% rename from src/main/java/com/github/unclecatmyself/common/ssl/StreamReader.java rename to src/main/java/com/github/unclecatmyself/common/utils/ssl/StreamReader.java index 1f75cb5..cc851c5 100644 --- a/src/main/java/com/github/unclecatmyself/common/ssl/StreamReader.java +++ b/src/main/java/com/github/unclecatmyself/common/utils/ssl/StreamReader.java @@ -1,4 +1,4 @@ -package com.github.unclecatmyself.common.ssl; +package com.github.unclecatmyself.common.utils.ssl; import java.io.InputStream; diff --git a/src/main/java/com/github/unclecatmyself/common/ssl/X509CertTool.java b/src/main/java/com/github/unclecatmyself/common/utils/ssl/X509CertTool.java similarity index 97% rename from src/main/java/com/github/unclecatmyself/common/ssl/X509CertTool.java rename to src/main/java/com/github/unclecatmyself/common/utils/ssl/X509CertTool.java index 304e014..f5f81a1 100644 --- a/src/main/java/com/github/unclecatmyself/common/ssl/X509CertTool.java +++ b/src/main/java/com/github/unclecatmyself/common/utils/ssl/X509CertTool.java @@ -1,4 +1,4 @@ -package com.github.unclecatmyself.common.ssl; +package com.github.unclecatmyself.common.utils.ssl; diff --git a/src/main/java/com/github/unclecatmyself/user/FromServerServiceImpl.java b/src/main/java/com/github/unclecatmyself/service/FromServerServiceImpl.java similarity index 96% rename from src/main/java/com/github/unclecatmyself/user/FromServerServiceImpl.java rename to src/main/java/com/github/unclecatmyself/service/FromServerServiceImpl.java index d66a373..8192116 100644 --- a/src/main/java/com/github/unclecatmyself/user/FromServerServiceImpl.java +++ b/src/main/java/com/github/unclecatmyself/service/FromServerServiceImpl.java @@ -1,4 +1,4 @@ -package com.github.unclecatmyself.user; +package com.github.unclecatmyself.service; import com.github.unclecatmyself.bootstrap.channel.http.FromServerService; diff --git a/src/main/java/com/github/unclecatmyself/common/base/HandlerService.java b/src/main/java/com/github/unclecatmyself/service/HandlerService.java similarity index 93% rename from src/main/java/com/github/unclecatmyself/common/base/HandlerService.java rename to src/main/java/com/github/unclecatmyself/service/HandlerService.java index 0463e42..6738a08 100644 --- a/src/main/java/com/github/unclecatmyself/common/base/HandlerService.java +++ b/src/main/java/com/github/unclecatmyself/service/HandlerService.java @@ -1,5 +1,6 @@ -package com.github.unclecatmyself.common.base; +package com.github.unclecatmyself.service; +import com.github.unclecatmyself.bootstrap.handler.Handler; import com.github.unclecatmyself.common.bean.vo.SendServerVO; import io.netty.channel.Channel; import io.netty.handler.codec.http.FullHttpMessage; @@ -10,7 +11,7 @@ * 业务层伪接口 * Created by MySelf on 2018/11/21. */ -public abstract class HandlerService implements HandlerApi { +public abstract class HandlerService implements Handler { /** * HTTP获取在线用户标签列表 diff --git a/src/main/java/com/github/unclecatmyself/bootstrap/data/InChatToDataBaseService.java b/src/main/java/com/github/unclecatmyself/service/InChatDBManager.java similarity index 66% rename from src/main/java/com/github/unclecatmyself/bootstrap/data/InChatToDataBaseService.java rename to src/main/java/com/github/unclecatmyself/service/InChatDBManager.java index 623f7ca..4cf939b 100644 --- a/src/main/java/com/github/unclecatmyself/bootstrap/data/InChatToDataBaseService.java +++ b/src/main/java/com/github/unclecatmyself/service/InChatDBManager.java @@ -1,4 +1,4 @@ -package com.github.unclecatmyself.bootstrap.data; +package com.github.unclecatmyself.service; import com.github.unclecatmyself.common.bean.InChatMessage; @@ -7,8 +7,9 @@ /** * Created by MySelf on 2018/12/3. */ -public interface InChatToDataBaseService { +public interface InChatDBManager { Boolean writeMessage(InChatMessage message); } + diff --git a/src/main/java/com/github/unclecatmyself/user/MyInit.java b/src/main/java/com/github/unclecatmyself/service/InChatInitializer.java similarity index 67% rename from src/main/java/com/github/unclecatmyself/user/MyInit.java rename to src/main/java/com/github/unclecatmyself/service/InChatInitializer.java index 851e77f..7d8d2b4 100644 --- a/src/main/java/com/github/unclecatmyself/user/MyInit.java +++ b/src/main/java/com/github/unclecatmyself/service/InChatInitializer.java @@ -1,15 +1,15 @@ -package com.github.unclecatmyself.user; +package com.github.unclecatmyself.service; import com.github.unclecatmyself.common.bean.InitNetty; /** * Created by MySelf on 2019/8/26. */ -public class MyInit extends InitNetty { +public class InChatInitializer extends InitNetty { @Override - public int getWebport() { - return 8070; + public int getWebPort() { + return 8090; } //分布式 @Override diff --git a/src/main/java/com/github/unclecatmyself/service/InChatResponse.java b/src/main/java/com/github/unclecatmyself/service/InChatResponse.java new file mode 100644 index 0000000..874b0d3 --- /dev/null +++ b/src/main/java/com/github/unclecatmyself/service/InChatResponse.java @@ -0,0 +1,63 @@ +package com.github.unclecatmyself.service; + +import com.github.unclecatmyself.bootstrap.channel.protocol.Response; +import com.github.unclecatmyself.common.constant.Constants; + +import java.util.HashMap; +import java.util.Map; + +/** + * 列入项目中,默认返回实现 + * Created by MySelf on 2018/11/23. + */ +public class InChatResponse implements Response { + + public Map loginSuccess() { + Map backMap = new HashMap<>(); + backMap.put(Constants.TYPE, Constants.LOGIN); + backMap.put(Constants.SUCCESS, Constants.TRUE); + return backMap; + } + + public Map loginError() { + Map backMap = new HashMap<>(); + backMap.put(Constants.TYPE, Constants.LOGIN); + backMap.put(Constants.SUCCESS, Constants.FALSE); + return backMap; + } + + public Map sendMe(String value) { + Map backMap = new HashMap<>(); + backMap.put(Constants.TYPE, Constants.SEND_ME); + backMap.put(Constants.VALUE,value); + return backMap; + } + + public Map sendBack(String otherOne, String value) { + Map backMap = new HashMap<>(); + backMap.put(Constants.TYPE, Constants.SEND_TO); + backMap.put(Constants.VALUE,value); + backMap.put(Constants.ONE,otherOne); + return backMap; + } + + + public Map getMessage(String token, String value) { + Map backMap = new HashMap<>(); + backMap.put(Constants.TYPE, Constants.SEND_TO); + backMap.put(Constants.FROM,token); + backMap.put(Constants.VALUE,value); + return backMap; + } + + + public Map sendGroup(String token, String value, String groupId) { + Map backMap = new HashMap<>(); + backMap.put(Constants.TYPE, Constants.SEND_GROUP); + backMap.put(Constants.FROM,token); + backMap.put(Constants.VALUE,value); + backMap.put(Constants.GROUP_ID,groupId); + return backMap; + } + +} diff --git a/src/main/java/com/github/unclecatmyself/bootstrap/redisclient/testApplication.java b/src/main/java/com/github/unclecatmyself/service/RedisClient.java similarity index 83% rename from src/main/java/com/github/unclecatmyself/bootstrap/redisclient/testApplication.java rename to src/main/java/com/github/unclecatmyself/service/RedisClient.java index afcbcc0..992ebd6 100644 --- a/src/main/java/com/github/unclecatmyself/bootstrap/redisclient/testApplication.java +++ b/src/main/java/com/github/unclecatmyself/service/RedisClient.java @@ -1,22 +1,22 @@ -package com.github.unclecatmyself.bootstrap.redisclient; - -import redis.clients.jedis.Jedis; - -/** - * Create by UncleCatMySelf in 23:02 2019\1\4 0004 - */ -public class testApplication { - - public static void main(String[] args) { - //连接redis服务 - Jedis jedis = new Jedis("192.168.12.129"); - System.out.println("连接成功"); - //设置redis字符串数据 - //jedis.set("token","inchat"); - //查看服务是否运行 - System.out.println("服务正在运行:"+jedis.ping()); - //获取存储的数据并输出 - System.out.println("redis存储的字符串为:"+jedis.get("token")); - } - -} +package com.github.unclecatmyself.service; + +import redis.clients.jedis.Jedis; + +/** + * Create by UncleCatMySelf in 23:02 2019\1\4 0004 + */ +public class RedisClient { + + public static void main(String[] args) { + //连接redis服务 + Jedis jedis = new Jedis("192.168.12.129"); + System.out.println("连接成功"); + //设置redis字符串数据 + //jedis.set("token","inchat"); + //查看服务是否运行 + System.out.println("服务正在运行:"+jedis.ping()); + //获取存储的数据并输出 + System.out.println("redis存储的字符串为:"+jedis.get("token")); + } + +} diff --git a/src/main/java/com/github/unclecatmyself/service/UserAsyncDataListener.java b/src/main/java/com/github/unclecatmyself/service/UserAsyncDataListener.java new file mode 100644 index 0000000..e83f97e --- /dev/null +++ b/src/main/java/com/github/unclecatmyself/service/UserAsyncDataListener.java @@ -0,0 +1,20 @@ +package com.github.unclecatmyself.service; + +import com.github.unclecatmyself.common.bean.InChatMessage; +import com.github.unclecatmyself.common.utils.MessageConversionUtil; +import com.github.unclecatmyself.task.AsyncDataListener; + +import java.util.Map; + +/** + * Created by MySelf on 2019/8/26. + */ +public class UserAsyncDataListener extends AsyncDataListener { + + @Override + public void asyncData(Map asyncData) { + InChatMessage inChatMessage = MessageConversionUtil.convert(asyncData); + System.out.println(inChatMessage.toString()); + } + +} diff --git a/src/main/java/com/github/unclecatmyself/user/VerifyServiceImpl.java b/src/main/java/com/github/unclecatmyself/service/VerifyServiceImpl.java similarity index 76% rename from src/main/java/com/github/unclecatmyself/user/VerifyServiceImpl.java rename to src/main/java/com/github/unclecatmyself/service/VerifyServiceImpl.java index d0a9b75..0780460 100644 --- a/src/main/java/com/github/unclecatmyself/user/VerifyServiceImpl.java +++ b/src/main/java/com/github/unclecatmyself/service/VerifyServiceImpl.java @@ -1,7 +1,7 @@ -package com.github.unclecatmyself.user; +package com.github.unclecatmyself.service; import com.alibaba.fastjson.JSONArray; -import com.github.unclecatmyself.bootstrap.verify.InChatVerifyService; +import com.github.unclecatmyself.bootstrap.channel.protocol.InChatVerifyService; /** * Created by MySelf on 2019/8/26. diff --git a/src/main/java/com/github/unclecatmyself/task/ListenAsynData.java b/src/main/java/com/github/unclecatmyself/task/AsyncDataListener.java similarity index 54% rename from src/main/java/com/github/unclecatmyself/task/ListenAsynData.java rename to src/main/java/com/github/unclecatmyself/task/AsyncDataListener.java index 6e98984..5bc0b89 100644 --- a/src/main/java/com/github/unclecatmyself/task/ListenAsynData.java +++ b/src/main/java/com/github/unclecatmyself/task/AsyncDataListener.java @@ -6,8 +6,9 @@ * 监听异步数据 * Created by MySelf on 2019/8/20. */ -public abstract class ListenAsynData{ +public abstract class AsyncDataListener { - public abstract void asynData(Map maps); + public abstract void asyncData(Map asyncData); } + diff --git a/src/main/java/com/github/unclecatmyself/task/DataAsynchronousTask.java b/src/main/java/com/github/unclecatmyself/task/DataAsynchronousTask.java index b69b14e..1ef0d4f 100644 --- a/src/main/java/com/github/unclecatmyself/task/DataAsynchronousTask.java +++ b/src/main/java/com/github/unclecatmyself/task/DataAsynchronousTask.java @@ -1,7 +1,7 @@ package com.github.unclecatmyself.task; -import com.github.unclecatmyself.bootstrap.data.InChatToDataBaseService; import com.github.unclecatmyself.common.constant.LogConstant; +import com.github.unclecatmyself.service.InChatDBManager; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -20,22 +20,25 @@ public class DataAsynchronousTask { private final Logger log = LoggerFactory.getLogger(DataAsynchronousTask.class); - /** 用户读数据接口伪实现 */ - private final InChatToDataBaseService inChatToDataBaseService; + /** + * 用户读数据接口伪实现 + */ + private final InChatDBManager inChatDBManager; - public DataAsynchronousTask(InChatToDataBaseService inChatToDataBaseService){ - this.inChatToDataBaseService = inChatToDataBaseService; + public DataAsynchronousTask(InChatDBManager inChatToDataBaseService) { + this.inChatDBManager = inChatToDataBaseService; } /** * 将Netty数据消息借助这个方法已新线程发送给用户实现读方法 + * * @param maps {@link Map} 传递消息 */ - public void writeData(Map maps){ + public void writeData(Map maps) { Boolean result = false; ExecutorService service = Executors.newCachedThreadPool(); - final FutureTask future = new FutureTask(new DataCallable(inChatToDataBaseService,maps)); + final FutureTask future = new FutureTask(new DataCallable(inChatDBManager, maps)); service.execute(future); try { result = future.get(); @@ -45,7 +48,7 @@ public void writeData(Map maps){ } catch (ExecutionException e) { log.error(LogConstant.DATAASYNCHRONOUSTASK_02); } - if (!result){ + if (!result) { log.error(LogConstant.DATAASYNCHRONOUSTASK_03); } } diff --git a/src/main/java/com/github/unclecatmyself/task/DataCallable.java b/src/main/java/com/github/unclecatmyself/task/DataCallable.java index fef6d8a..932cc4a 100644 --- a/src/main/java/com/github/unclecatmyself/task/DataCallable.java +++ b/src/main/java/com/github/unclecatmyself/task/DataCallable.java @@ -1,7 +1,7 @@ package com.github.unclecatmyself.task; -import com.github.unclecatmyself.bootstrap.data.InChatToDataBaseService; -import com.github.unclecatmyself.common.utils.MessageChangeUtil; +import com.github.unclecatmyself.common.utils.MessageConversionUtil; +import com.github.unclecatmyself.service.InChatDBManager; import java.util.Map; import java.util.concurrent.Callable; @@ -14,18 +14,18 @@ public class DataCallable implements Callable{ /** 用户读数据接口伪实现 */ - private final InChatToDataBaseService inChatToDataBaseService; + private final InChatDBManager inChatDBManager; /** 消息数据 */ private final Map maps; - DataCallable(InChatToDataBaseService inChatToDataBaseService,Map maps) { - this.inChatToDataBaseService = inChatToDataBaseService; + DataCallable(InChatDBManager inChatToDataBaseService,Map maps) { + this.inChatDBManager = inChatToDataBaseService; this.maps = maps; } @Override public Boolean call() throws Exception { - inChatToDataBaseService.writeMessage(MessageChangeUtil.Change(maps)); + inChatDBManager.writeMessage(MessageConversionUtil.convert(maps)); return true; } } diff --git a/src/main/java/com/github/unclecatmyself/user/UserListenAsynData.java b/src/main/java/com/github/unclecatmyself/user/UserListenAsynData.java deleted file mode 100644 index 7d3465b..0000000 --- a/src/main/java/com/github/unclecatmyself/user/UserListenAsynData.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.github.unclecatmyself.user; - -import com.github.unclecatmyself.common.bean.InChatMessage; -import com.github.unclecatmyself.common.utils.MessageChangeUtil; -import com.github.unclecatmyself.task.ListenAsynData; - -import java.util.Map; - -/** - * Created by MySelf on 2019/8/26. - */ -public class UserListenAsynData extends ListenAsynData { - - @Override - public void asynData(Map maps) { - InChatMessage inChatMessage = MessageChangeUtil.Change(maps); - System.out.println(inChatMessage.toString()); - } - -}