重构Processor相关

This commit is contained in:
王超 2020-03-31 01:52:01 +08:00
parent 9793baf93d
commit 96b5806891
34 changed files with 246 additions and 191 deletions

View File

@ -24,22 +24,22 @@
<groupId>nl.basjes.parse.useragent</groupId>
<artifactId>yauaa</artifactId>
</dependency>
<dependency>
<groupId>com.typesafe</groupId>
<artifactId>config</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
</dependency>
<!-- slf4j-logback绑定 -->
<dependency>
<groupId>ch.qos.logback</groupId>
@ -92,9 +92,9 @@
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>

View File

@ -14,5 +14,5 @@ import org.tio.core.ChannelContext;
*/
public interface ImDecoder {
public ImPacket decode(ByteBuffer buffer, ChannelContext channelContext) throws ImDecodeException;
ImPacket decode(ByteBuffer buffer, ChannelContext channelContext) throws ImDecodeException;
}

View File

@ -73,9 +73,6 @@ public class HttpResponse extends HttpPacket {
addHeader(Http.ResponseHeaderKey.Keep_Alive, "timeout=10, max=20");
}
}
//暂时先设置为短连接...防止服务器一直不释放资源;
addHeader(Http.ResponseHeaderKey.Connection, Http.ResponseHeaderValue.Connection.close);
if (httpConfig != null) {
addHeader(Http.ResponseHeaderKey.Server, httpConfig.getServerInfo());
}

View File

@ -34,10 +34,10 @@ public class ImServerDemoStart {
/*****************start 以下处理器根据业务需要自行添加与扩展每个Command都可以添加扩展,此处为demo中处理**********************************/
HandshakeReqHandler handshakeReqHandler = CommandManager.getCommand(Command.COMMAND_HANDSHAKE_REQ, HandshakeReqHandler.class);
//添加自定义握手处理器;
handshakeReqHandler.addProcessor(new DemoWsHandshakeProcessor());
handshakeReqHandler.addMultiProtocolProcessor(new DemoWsHandshakeProcessor());
LoginReqHandler loginReqHandler = CommandManager.getCommand(Command.COMMAND_LOGIN_REQ,LoginReqHandler.class);
//添加登录业务处理器;
loginReqHandler.addProcessor(new LoginServiceProcessor());
loginReqHandler.setSingleProcessor(new LoginServiceProcessor());
/*****************end *******************************************************************************************/
imServerStarter.start();
}
@ -60,4 +60,5 @@ public class ImServerDemoStart {
}
}
}
}

View File

@ -159,16 +159,4 @@ public class LoginServiceProcessor implements LoginCmdProcessor {
}
}
}
@Override
public boolean isProtocol(ImChannelContext channelContext) {
return true;
}
@Override
public String name() {
return "default";
}
}

View File

@ -3,7 +3,7 @@
context.name=j-im-server-demo
log.dir=/logs/j-im-server-demo
log.dir=./logs/j-im-server-demo
rolling.policy.file.name.pattern=yyyy-MM-dd
max.file.size=100MB

View File

@ -3,14 +3,15 @@
*/
package org.jim.server.command;
import org.apache.commons.collections4.CollectionUtils;
import org.jim.common.ImChannelContext;
import org.jim.common.ImConst;
import org.jim.common.config.ImConfig;
import org.jim.server.command.handler.processor.CmdProcessor;
import org.jim.server.command.handler.processor.SingleProtocolCmdProcessor;
import org.jim.server.command.handler.processor.MultiProtocolCmdProcessor;
import org.jim.server.config.ImServerConfig;
import java.util.*;
import java.util.Map.Entry;
import java.util.ArrayList;
import java.util.List;
/**
* 版本: [1.0]
@ -19,18 +20,39 @@ import java.util.Map.Entry;
*/
public abstract class AbstractCmdHandler implements CmdHandler, ImConst {
/**
* 不同协议cmd处理命令如(wssocket自定义协议)握手心跳命令等.
* 单协议业务处理器
*/
protected Map<String,CmdProcessor> processors = new HashMap<String,CmdProcessor>();
private SingleProtocolCmdProcessor singleProcessor;
/**
* 多协议业务处理器
*/
private List<MultiProtocolCmdProcessor> multiProcessors = new ArrayList<>();
/**
* IM相关配置类
*/
protected ImServerConfig imConfig = ImConfig.Global.get();
private ImServerConfig imConfig = ImConfig.Global.get();
public AbstractCmdHandler() {};
public AbstractCmdHandler addProcessor(CmdProcessor processor){
this.processors.put(processor.name(), processor);
public ImServerConfig getImConfig() {
return imConfig;
}
public SingleProtocolCmdProcessor getSingleProcessor() {
return singleProcessor;
}
public AbstractCmdHandler setSingleProcessor(SingleProtocolCmdProcessor singleProcessor) {
this.singleProcessor = singleProcessor;
return this;
}
public <T> T getSingleProcessor(Class<T> clazz) {
return (T)singleProcessor;
}
public AbstractCmdHandler addMultiProtocolProcessor(MultiProtocolCmdProcessor processor) {
this.multiProcessors.add(processor);
return this;
}
@ -39,68 +61,14 @@ public abstract class AbstractCmdHandler implements CmdHandler, ImConst {
* @param imChannelContext
* @return
*/
public <T> List<T> getProcessor(ImChannelContext imChannelContext, Class<T> clazz){
List<T> processorList = null;
for(Entry<String,CmdProcessor> processorEntry : processors.entrySet()){
CmdProcessor processor = processorEntry.getValue();
if(processor.isProtocol(imChannelContext)){
if(CollectionUtils.isEmpty(processorList)){
processorList = new ArrayList<>();
}
processorList.add((T)processor);
public <T> T getMultiProcessor(ImChannelContext imChannelContext, Class<T> clazz){
T multiCmdProcessor = null;
for(MultiProtocolCmdProcessor multiProcessor : multiProcessors){
if(multiProcessor.isProtocol(imChannelContext)){
multiCmdProcessor = (T)multiProcessor;
}
}
return processorList;
}
/**
* 根据cmdProcessor名字获取cmd业务处理器
* @param name
* @return
*/
public <T> List<T> getProcessor(String name, Class<T> clazz){
List<T> processorList = null;
for(Entry<String,CmdProcessor> processorEntry : processors.entrySet()){
CmdProcessor processor = processorEntry.getValue();
if(name.equals(processor.name())){
if(CollectionUtils.isEmpty(processorList)){
processorList = new ArrayList<>();
}
processorList.add((T)processor);
}
}
return processorList;
}
/**
* 获取不包含指定名字的cmdProcessor
* @param names
* @param clazz
* @return
*/
public <T> List<T> getProcessorNotEqualName(Set<String> names, Class<T> clazz){
List<T> processorList = null;
for(Entry<String,CmdProcessor> processorEntry : processors.entrySet()){
CmdProcessor processor = processorEntry.getValue();
if(CollectionUtils.isEmpty(processorList)){
processorList = new ArrayList<>();
}
if(CollectionUtils.isEmpty(names)){
processorList.add((T)processor);
}else {
if(!names.contains(processor.name())){
processorList.add((T)processor);
}
}
}
return processorList;
}
public CmdProcessor removeProcessor(String name){
return processors.remove(name);
}
public ImServerConfig getImConfig() {
return imConfig;
return multiCmdProcessor;
}
}

View File

@ -9,7 +9,7 @@ import java.util.Properties;
public class CommandConfiguration {
private int cmd ;
private String cmdHandler ;
private List<String> proCmdHandlers = new ArrayList<String>();
private List<String> cmdProcessors = new ArrayList<String>();
public CommandConfiguration(){}
@ -21,7 +21,7 @@ public class CommandConfiguration {
if(values.length >1){
for(int i = 0 ; i < values.length ; i++){
if(i > 0) {
proCmdHandlers.add(values[i]);
cmdProcessors.add(values[i]);
}
}
}
@ -44,13 +44,11 @@ public class CommandConfiguration {
this.cmdHandler = cmdHandler;
}
public List<String> getProCmdHandlers() {
return proCmdHandlers;
public List<String> getCmdProcessors() {
return cmdProcessors;
}
public void setProCmdHandlers(List<String> proCmdHandlers) {
this.proCmdHandlers = proCmdHandlers;
public void setCmdProcessors(List<String> cmdProcessors) {
this.cmdProcessors = cmdProcessors;
}
}

View File

@ -4,15 +4,14 @@
package org.jim.server.command;
import org.jim.common.packets.Command;
import org.jim.server.command.handler.processor.CmdProcessor;
import org.jim.server.config.ImServerConfig;
import org.jim.server.command.handler.processor.MultiProtocolCmdProcessor;
import org.jim.server.command.handler.processor.SingleProtocolCmdProcessor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
/**
* 版本: [1.0]
* 功能说明: 命令执行管理器;
@ -38,19 +37,22 @@ public class CommandManager{
private static void init(List<CommandConfiguration> configurations) throws Exception{
for(CommandConfiguration configuration : configurations){
Class<AbstractCmdHandler> cmdHandlerClazz = (Class<AbstractCmdHandler>)Class.forName(configuration.getCmdHandler());
AbstractCmdHandler cmdHandler = cmdHandlerClazz.newInstance();
List<String> proCmdHandlerList = configuration.getProCmdHandlers();
if(!proCmdHandlerList.isEmpty()){
for(String proCmdHandlerClass : proCmdHandlerList){
Class<CmdProcessor> proCmdHandlerClazz = (Class<CmdProcessor>)Class.forName(proCmdHandlerClass);
CmdProcessor proCmdHandler = proCmdHandlerClazz.newInstance();
cmdHandler.addProcessor(proCmdHandler);
AbstractCmdHandler cmdHandler = ((Class<AbstractCmdHandler>)Class.forName(configuration.getCmdHandler())).newInstance();
List<String> cmdProcessors = configuration.getCmdProcessors();
if(!cmdProcessors.isEmpty()){
for(String cmdProcessor : cmdProcessors){
Object cmdProcessorObj = Class.forName(cmdProcessor).newInstance();
if(cmdProcessorObj instanceof MultiProtocolCmdProcessor){
cmdHandler.addMultiProtocolProcessor((MultiProtocolCmdProcessor)cmdProcessorObj);
}else if(cmdProcessorObj instanceof SingleProtocolCmdProcessor){
cmdHandler.setSingleProcessor((SingleProtocolCmdProcessor)cmdProcessorObj);
}
}
}
registerCommand(cmdHandler);
}
}
public static AbstractCmdHandler registerCommand(AbstractCmdHandler imCommandHandler) throws Exception{
if(imCommandHandler == null || imCommandHandler.command() == null) {
return null;

View File

@ -50,7 +50,7 @@ public class ChatReqHandler extends AbstractCmdHandler {
//私聊
if(ChatType.CHAT_TYPE_PRIVATE.getNumber() == chatBody.getChatType()){
String toId = chatBody.getTo();
if(ChatKit.isOnline(toId,imConfig)){
if(ChatKit.isOnline(toId, getImConfig())){
Jim.sendToUser(toId, chatPacket);
//发送成功响应包
return ProtocolManager.Packet.success(channelContext);

View File

@ -11,6 +11,7 @@ import org.jim.common.ws.WsSessionContext;
import org.jim.server.command.AbstractCmdHandler;
import org.jim.server.command.handler.processor.handshake.HandshakeCmdProcessor;
import java.util.List;
import java.util.Objects;
/**
* 版本: [1.0]
@ -21,13 +22,13 @@ public class HandshakeReqHandler extends AbstractCmdHandler {
@Override
public ImPacket handler(ImPacket packet, ImChannelContext channelContext) throws ImException {
List<HandshakeCmdProcessor> handshakeProcessors = this.getProcessor(channelContext,HandshakeCmdProcessor.class);
if(CollectionUtils.isEmpty(handshakeProcessors)){
Jim.remove(channelContext, "没有对应的握手协议处理器HandshakeProCmd...");
HandshakeCmdProcessor handshakeProcessor = this.getMultiProcessor(channelContext,HandshakeCmdProcessor.class);
if(Objects.isNull(handshakeProcessor)){
Jim.remove(channelContext, "没有对应的握手协议处理器HandshakeCmdProcessor...");
return null;
}
HandshakeCmdProcessor handShakeProCmdHandler = handshakeProcessors.get(0);
ImPacket handShakePacket = handShakeProCmdHandler.handshake(packet, channelContext);
ImPacket handShakePacket = handshakeProcessor.handshake(packet, channelContext);
if (handShakePacket == null) {
Jim.remove(channelContext, "业务层不同意握手");
return null;
@ -35,7 +36,7 @@ public class HandshakeReqHandler extends AbstractCmdHandler {
Jim.send(channelContext, handShakePacket);
WsSessionContext wsSessionContext = (WsSessionContext) channelContext.getSessionContext();
HttpRequest request = wsSessionContext.getHandshakeRequestPacket();
handShakeProCmdHandler.onAfterHandshake(request, channelContext);
handshakeProcessor.onAfterHandshake(request, channelContext);
return null;
}

View File

@ -1,6 +1,5 @@
package org.jim.server.command.handler;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.jim.common.*;
import org.jim.common.exception.ImException;
@ -17,7 +16,7 @@ import org.jim.common.packets.RespBody;
import org.jim.common.packets.User;
import org.jim.common.utils.JsonKit;
import org.jim.server.command.AbstractCmdHandler;
import java.util.List;
import java.util.Objects;
/**
*
@ -75,12 +74,11 @@ public class JoinGroupReqHandler extends AbstractCmdHandler {
return null;
}
//实际绑定之前执行处理器动作
List<GroupCmdProcessor> groupCmdProcessors = this.getProcessor(imChannelContext, GroupCmdProcessor.class);
GroupCmdProcessor groupCmdProcessor = (GroupCmdProcessor)this.getSingleProcessor();
JoinGroupRespBody joinGroupRespBody = new JoinGroupRespBody(Command.COMMAND_JOIN_GROUP_RESP,ImStatus.C10011);
//当有群组处理器时候才会去处理
if(CollectionUtils.isNotEmpty(groupCmdProcessors)){
GroupCmdProcessor groupCmdProcessor = groupCmdProcessors.get(0);
if(Objects.nonNull(groupCmdProcessor)){
joinGroupRespBody = groupCmdProcessor.join(joinGroup, imChannelContext);
if (joinGroupRespBody == null || JoinGroupResult.JOIN_GROUP_RESULT_OK.getNumber() != joinGroupRespBody.getResult().getNumber()) {
RespBody joinRespBody = new RespBody(Command.COMMAND_JOIN_GROUP_RESP, ImStatus.C10012).setData(joinGroupRespBody);

View File

@ -1,6 +1,5 @@
package org.jim.server.command.handler;
import org.apache.commons.collections4.CollectionUtils;
import org.jim.common.*;
import org.jim.common.config.ImConfig;
import org.jim.common.exception.ImException;
@ -11,7 +10,6 @@ import org.jim.common.packets.LoginReqBody;
import org.jim.common.packets.LoginRespBody;
import org.jim.common.packets.User;
import org.jim.common.protocol.IProtocol;
import org.jim.common.utils.ImKit;
import org.jim.common.utils.JsonKit;
import org.jim.server.ImServerChannelContext;
import org.jim.server.command.AbstractCmdHandler;
@ -20,10 +18,8 @@ import org.jim.server.command.handler.processor.login.LoginCmdProcessor;
import org.jim.server.handler.ProtocolManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.core.ChannelContext;
import org.tio.core.Tio;
import java.util.List;
import java.util.Objects;
/**
* 登录消息命令处理器
@ -31,6 +27,7 @@ import java.util.List;
* @date 2018年4月10日 下午2:40:07
*/
public class LoginReqHandler extends AbstractCmdHandler {
private static Logger log = LoggerFactory.getLogger(LoginReqHandler.class);
@Override
@ -40,17 +37,16 @@ public class LoginReqHandler extends AbstractCmdHandler {
Jim.remove(imChannelContext, "body is null");
return null;
}
List<LoginCmdProcessor> loginProcessors = this.getProcessor(imChannelContext, LoginCmdProcessor.class);
if(CollectionUtils.isEmpty(loginProcessors)){
LoginCmdProcessor loginProcessor = this.getSingleProcessor(LoginCmdProcessor.class);
if(Objects.isNull(loginProcessor)){
log.info("登录失败,没有登录命令业务处理器!");
Jim.remove(imChannelContext, "no login serviceHandler processor!");
return null;
}
LoginCmdProcessor loginServiceHandler = loginProcessors.get(0);
ImSessionContext imSessionContext = imChannelContext.getSessionContext();
LoginReqBody loginReqBody = JsonKit.toBean(packet.getBody(),LoginReqBody.class);
LoginRespBody loginRespBody = loginServiceHandler.doLogin(loginReqBody, imChannelContext);
LoginRespBody loginRespBody = loginProcessor.doLogin(loginReqBody, imChannelContext);
if (loginRespBody == null || loginRespBody.getUser() == null) {
log.info("登录失败, loginName:{}, password:{}", loginReqBody.getLoginname(), loginReqBody.getPassword());
if(loginRespBody == null){
@ -70,7 +66,7 @@ public class LoginReqHandler extends AbstractCmdHandler {
Jim.bindUser(imServerChannelContext, user.getId());
//初始化绑定或者解绑群组;
bindUnbindGroup(imChannelContext, user);
loginServiceHandler.onSuccess(imChannelContext);
loginProcessor.onSuccess(imChannelContext);
loginRespBody.clear();
return ProtocolManager.Converter.respPacket(loginRespBody, imChannelContext);
}
@ -81,11 +77,11 @@ public class LoginReqHandler extends AbstractCmdHandler {
String userId = user.getId();
List<Group> groups = user.getGroups();
if( groups != null){
boolean isStore = ImConfig.Const.ON.equals(imConfig.getIsStore());
boolean isStore = ImConfig.Const.ON.equals(getImConfig().getIsStore());
MessageHelper messageHelper = null;
List<String> groupIds = null;
if(isStore){
messageHelper = imConfig.getMessageHelper();
messageHelper = getImConfig().getMessageHelper();
groupIds = messageHelper.getGroups(userId);
}
//绑定群组

View File

@ -40,7 +40,7 @@ public class MessageReqHandler extends AbstractCmdHandler {
return getMessageFailedPacket(imChannelContext);
}
UserMessageData messageData = null;
MessageHelper messageHelper = imConfig.getMessageHelper();
MessageHelper messageHelper = getImConfig().getMessageHelper();
//群组ID;
String groupId = messageReqBody.getGroupId();
//当前用户ID;
@ -58,7 +58,7 @@ public class MessageReqHandler extends AbstractCmdHandler {
//消息类型;
int type = messageReqBody.getType();
//如果用户ID为空或者type格式不正确获取消息失败;
if(StringUtils.isEmpty(userId) || (0 != type && 1 != type) || !ImServerConfig.Const.ON.equals(imConfig.getIsStore())){
if(StringUtils.isEmpty(userId) || (0 != type && 1 != type) || !ImServerConfig.Const.ON.equals(getImConfig().getIsStore())){
return getMessageFailedPacket(imChannelContext);
}
if(type == 0){

View File

@ -72,9 +72,9 @@ public class UserReqHandler extends AbstractCmdHandler {
public User getUserInfo(String userId , Integer type){
User user = null;
//是否开启持久化;
boolean isStore = ImConfig.Const.ON.equals(imConfig.getIsStore());
boolean isStore = ImConfig.Const.ON.equals(getImConfig().getIsStore());
//消息持久化助手;
MessageHelper messageHelper = imConfig.getMessageHelper();
MessageHelper messageHelper = getImConfig().getMessageHelper();
if(isStore){
user = messageHelper.getUserByType(userId, 2);
if(user == null) {

View File

@ -1,4 +1,17 @@
package org.jim.server.command.handler.processor;
public class MultiprotocolProcessor {
import org.jim.common.ImChannelContext;
import org.jim.common.ImConst;
/**
* @date 2020-03-19
* @author WChao
*/
public interface MultiProtocolCmdProcessor extends ImConst {
/**
* 不同协议判断方法
* @param imChannelContext
* @return
*/
boolean isProtocol(ImChannelContext imChannelContext);
}

View File

@ -10,6 +10,7 @@ import org.jim.common.ImConst;
* @author WChao
*
*/
public interface CmdProcessor extends ImConst {
public interface SingleProtocolCmdProcessor extends ImConst {
}

View File

@ -12,7 +12,7 @@ package org.jim.server.command.handler.processor.chat;
import org.jim.common.ImChannelContext;
import org.jim.common.packets.ChatBody;
import org.jim.server.command.handler.processor.CmdProcessor;
import org.jim.server.command.handler.processor.SingleProtocolCmdProcessor;
/**
*
* 聊天消息异步业务处理器
@ -21,7 +21,7 @@ import org.jim.server.command.handler.processor.CmdProcessor;
* @date 2018/11/18 上午1:36
*
*/
public interface AsyncChatMessageProcessor extends CmdProcessor{
public interface AsyncChatMessageProcessor extends SingleProtocolCmdProcessor {
/**
* 聊天消息异步业务处理器执行方法;
* @param chatBody

View File

@ -23,16 +23,6 @@ public abstract class BaseAsyncChatMessageProcessor implements AsyncChatMessageP
*/
protected abstract void doHandler(ChatBody chatBody, ImChannelContext imChannelContext);
@Override
public boolean isProtocol(ImChannelContext imChannelContext) {
return true;
}
@Override
public String name() {
return BASE_ASYNC_CHAT_MESSAGE_PROCESSOR;
}
@Override
public void handler(ChatBody chatBody, ImChannelContext imChannelContext){
//开启持久化

View File

@ -2,14 +2,13 @@ package org.jim.server.command.handler.processor.chat;
import org.jim.common.ImChannelContext;
import org.jim.common.ImPacket;
import org.tio.core.ChannelContext;
import org.jim.server.command.handler.processor.CmdProcessor;
import org.jim.server.command.handler.processor.SingleProtocolCmdProcessor;
/**
* 聊天请求cmd业务处理器接口
* @author WChao
* @date 2018年4月2日 下午3:21:01
*/
public interface ChatCmdProcessor extends CmdProcessor {
public interface ChatCmdProcessor extends SingleProtocolCmdProcessor {
/**
* 聊天cmd业务处理器处理方法;
* @param chatPacket

View File

@ -38,7 +38,7 @@ public class MsgQueueRunnable extends AbstractQueueRunnable<ImPacket> {
super(executor);
this.imChannelContext = imChannelContext;
ChatReqHandler chatReqHandler = CommandManager.getCommand(Command.COMMAND_CHAT_REQ,ChatReqHandler.class);
chatMessageProcessor = chatReqHandler.getProcessor(ImConst.BASE_ASYNC_CHAT_MESSAGE_PROCESSOR,BaseAsyncChatMessageProcessor.class).get(0);
chatMessageProcessor = chatReqHandler.getSingleProcessor(BaseAsyncChatMessageProcessor.class);
}
@Override

View File

@ -3,11 +3,11 @@ package org.jim.server.command.handler.processor.group;
import org.jim.common.ImChannelContext;
import org.jim.common.packets.Group;
import org.jim.common.packets.JoinGroupRespBody;
import org.jim.server.command.handler.processor.CmdProcessor;
import org.jim.server.command.handler.processor.SingleProtocolCmdProcessor;
/**
* @author ensheng
*/
public interface GroupCmdProcessor extends CmdProcessor {
public interface GroupCmdProcessor extends SingleProtocolCmdProcessor {
/**
* 加入群组处理
* @param joinGroup

View File

@ -6,7 +6,8 @@ package org.jim.server.command.handler.processor.handshake;
import org.jim.common.ImChannelContext;
import org.jim.common.ImPacket;
import org.jim.common.exception.ImException;
import org.jim.server.command.handler.processor.CmdProcessor;
import org.jim.server.command.handler.processor.MultiProtocolCmdProcessor;
/**
* @ClassName HandshakeCmdProcessor
* @Description TODO
@ -14,7 +15,7 @@ import org.jim.server.command.handler.processor.CmdProcessor;
* @Date 2019/6/13 3:57
* @Version 1.0
**/
public interface HandshakeCmdProcessor extends CmdProcessor {
public interface HandshakeCmdProcessor extends MultiProtocolCmdProcessor {
/**
* 对httpResponsePacket参数进行补充并返回如果返回null表示不想和对方建立连接框架会断开连接如果返回非null框架会把这个对象发送给对方
* @param packet
@ -32,4 +33,5 @@ public interface HandshakeCmdProcessor extends CmdProcessor {
* @author Wchao
*/
void onAfterHandshake(ImPacket packet, ImChannelContext imChannelContext) throws ImException;
}

View File

@ -38,6 +38,7 @@ public class TcpHandshakeProcessor implements HandshakeCmdProcessor {
public void onAfterHandshake(ImPacket packet, ImChannelContext channelContext)throws ImException {
}
@Override
public boolean isProtocol(ImChannelContext channelContext){
ImSessionContext sessionContext = channelContext.getSessionContext();
@ -48,12 +49,5 @@ public class TcpHandshakeProcessor implements HandshakeCmdProcessor {
}
return false;
}
@Override
public String name() {
return Protocol.TCP;
}
}

View File

@ -11,6 +11,7 @@ import org.jim.common.packets.Command;
import org.jim.common.ws.WsRequestPacket;
import org.jim.common.ws.WsResponsePacket;
import org.jim.common.ws.WsSessionContext;
/**
* 版本: [1.0]
* 功能说明:
@ -69,16 +70,4 @@ public class WsHandshakeProcessor implements HandshakeCmdProcessor {
return false;
}
/**
* @Author WChao
* @Description 协议名称
* @param
* @return java.lang.String
**/
@Override
public String name() {
return Protocol.WEB_SOCKET;
}
}

View File

@ -6,12 +6,12 @@ package org.jim.server.command.handler.processor.login;
import org.jim.common.ImChannelContext;
import org.jim.common.packets.LoginReqBody;
import org.jim.common.packets.LoginRespBody;
import org.jim.server.command.handler.processor.CmdProcessor;
import org.jim.server.command.handler.processor.SingleProtocolCmdProcessor;
/**
*
* @author WChao
*/
public interface LoginCmdProcessor extends CmdProcessor {
public interface LoginCmdProcessor extends SingleProtocolCmdProcessor {
/**
* 执行登录操作接口方法
* @param loginReqBody

View File

@ -64,11 +64,9 @@ public class DefaultImServerHandler implements ImServerHandler{
@Override
public ImPacket decode(ByteBuffer buffer, int limit, int position, int readableLength, ImChannelContext imChannelContext) throws ImDecodeException {
ImServerChannelContext imServerChannelContext = (ImServerChannelContext)imChannelContext;
AbstractProtocolHandler handler;
AbstractProtocolHandler handler = imServerChannelContext.getProtocolHandler();
if(Objects.isNull(imServerChannelContext.getSessionContext())){
handler = ProtocolManager.initProtocolHandler(buffer, imServerChannelContext);
}else{
handler = imServerChannelContext.getProtocolHandler();
}
if(handler != null){
return handler.decode(buffer, limit, position, readableLength, imServerChannelContext);

View File

@ -13,7 +13,6 @@ import org.jim.common.exception.ImException;
import org.jim.common.http.*;
import org.jim.common.http.handler.IHttpRequestHandler;
import org.jim.common.protocol.AbstractProtocol;
import org.jim.common.protocol.IProtocol;
import org.jim.common.session.id.impl.UUIDSessionIdGenerator;
import org.jim.server.ImServerStarter;
import org.jim.server.config.ImServerConfig;

View File

View File

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,121 @@
2020-03-31 01:44:07,824 INFO o.j.s.tcp.TcpProtocolHandler[43]: J-IM TCP协议初始化完毕...
2020-03-31 01:44:08,312 INFO org.jim.server.http.mvc.Routes[170]: class mapping
{
"/webim":"org.jim.server.http.api.WebImController",
"/api":"org.jim.server.http.api.HttpApiController",
"/test":"org.jim.server.http.api.TestController"
}
2020-03-31 01:44:08,314 INFO org.jim.server.http.mvc.Routes[172]: method mapping
{
"/test/html":"org.jim.server.http.api.TestController.html(request)",
"/test/filetest":"org.jim.server.http.api.TestController.filetest(request)",
"/test/abtest":"org.jim.server.http.api.TestController.abtest(request)",
"/test/txt":"org.jim.server.http.api.TestController.txt(request)",
"/test/upload":"org.jim.server.http.api.TestController.upload(uploadFile,before,end,request)",
"/test/bean":"org.jim.server.http.api.TestController.bean(user,request)",
"/test/json":"org.jim.server.http.api.TestController.json(request)",
"/test/post":"org.jim.server.http.api.TestController.post(before,end,request)",
"/test/plain":"org.jim.server.http.api.TestController.plain(before,end,request)",
"/api/user/online":"org.jim.server.http.api.HttpApiController.online(request,httpConfig,channelContext)",
"/api/message/send":"org.jim.server.http.api.HttpApiController.chat(request,httpConfig,channelContext)",
"/test/putsession":"org.jim.server.http.api.TestController.putsession(value,request)",
"/api/user/close":"org.jim.server.http.api.HttpApiController.close(request,httpConfig,channelContext)",
"/test/filetest.zip":"org.jim.server.http.api.TestController.filetest_zip(request)",
"/test/getsession":"org.jim.server.http.api.TestController.getsession(request)"
}
2020-03-31 01:44:08,317 INFO o.j.s.http.HttpProtocolHandler[77]: J-IM Http协议初始化完毕,耗时:478ms
2020-03-31 01:44:08,319 INFO o.j.s.ws.WsProtocolHandler[67]: J-IM WebSocket协议初始化完毕...
2020-03-31 01:44:08,405 INFO org.tio.server.TioServer[352]:
|----------------------------------------------------------------------------------------|
| t-io site | https://www.t-io.org |
| t-io on gitee | https://gitee.com/tywo45/t-io |
| t-io on github | https://github.com/tywo45/t-io |
| t-io version | 3.5.8.v20191228-RELEASE |
| ---------------------------------------------------------------------------------------|
| TioConfig name | j-im |
| Started at | 2020-03-31 01:44:08 |
| Listen on | 127.0.0.1:8888 |
| Main Class | org.jim.server.demo.ImServerDemoStart |
| Jvm start time | 2302ms |
| Tio start time | 68ms |
| Pid | 97007 |
|----------------------------------------------------------------------------------------|
2020-03-31 01:44:09,754 INFO org.tio.server.TioServer[435]: t-io latest version:3.6.0.v20200315-RELEASEyour version:3.5.8.v20191228-RELEASE
2020-03-31 01:44:09,757 INFO org.tio.server.TioServer[446]: You haven't upgraded in 78 days
2020-03-31 01:44:17,783 INFO org.tio.server.ServerTioConfig[314]: j-im, 用户取消了框架层面的心跳检测,如果业务需要,请用户自己去完成心跳检测
2020-03-31 01:44:21,651 INFO o.j.s.h.DefaultHttpRequestHandler[335]: org.jim.server.ImServerChannelContext@2fd05977 创建会话Cookie, jimIxO=739778f2e1a947e083984fb91387a3e0; Domain=localhost:8888; Max-Age=1800; Path=/
2020-03-31 01:44:21,657 INFO o.j.c.http.HttpResponseEncoder[74]: org.jim.server.ImServerChannelContext@2fd05977, set-cookie:jimIxO=739778f2e1a947e083984fb91387a3e0; Domain=localhost:8888; Max-Age=1800; Path=/
2020-03-31 01:44:21,997 INFO o.j.s.h.DefaultHttpRequestHandler[335]: org.jim.server.ImServerChannelContext@2fd05977 创建会话Cookie, jimIxO=b15ea5d5e82542a391e059a9308039f0; Domain=localhost:8888; Max-Age=1800; Path=/
2020-03-31 01:44:21,998 INFO o.j.c.http.HttpResponseEncoder[74]: org.jim.server.ImServerChannelContext@2fd05977, set-cookie:jimIxO=b15ea5d5e82542a391e059a9308039f0; Domain=localhost:8888; Max-Age=1800; Path=/
2020-03-31 01:44:22,194 INFO o.j.s.h.DefaultHttpRequestHandler[335]: org.jim.server.ImServerChannelContext@2fd05977 创建会话Cookie, jimIxO=e2a6c7b767204c588c87430cc214c895; Domain=localhost:8888; Max-Age=1800; Path=/
2020-03-31 01:44:22,194 INFO o.j.c.http.HttpResponseEncoder[74]: org.jim.server.ImServerChannelContext@2fd05977, set-cookie:jimIxO=e2a6c7b767204c588c87430cc214c895; Domain=localhost:8888; Max-Age=1800; Path=/
2020-03-31 01:44:23,048 INFO o.j.s.h.DefaultHttpRequestHandler[335]: org.jim.server.ImServerChannelContext@2fd05977 创建会话Cookie, jimIxO=f30bd29ca61a4c05abd44684d4dda50b; Domain=localhost:8888; Max-Age=1800; Path=/
2020-03-31 01:44:23,049 INFO o.j.c.http.HttpResponseEncoder[74]: org.jim.server.ImServerChannelContext@2fd05977, set-cookie:jimIxO=f30bd29ca61a4c05abd44684d4dda50b; Domain=localhost:8888; Max-Age=1800; Path=/
2020-03-31 01:44:23,103 INFO o.j.s.h.DefaultHttpRequestHandler[335]: org.jim.server.ImServerChannelContext@2fd05977 创建会话Cookie, jimIxO=e6ce30a5432143a7bb1f0ebc130e710f; Domain=localhost:8888; Max-Age=1800; Path=/
2020-03-31 01:44:23,103 INFO o.j.c.http.HttpResponseEncoder[74]: org.jim.server.ImServerChannelContext@2fd05977, set-cookie:jimIxO=e6ce30a5432143a7bb1f0ebc130e710f; Domain=localhost:8888; Max-Age=1800; Path=/
2020-03-31 01:44:23,126 INFO o.j.s.h.DefaultHttpRequestHandler[335]: org.jim.server.ImServerChannelContext@2fd05977 创建会话Cookie, jimIxO=5c7100c809f842388b30f5eb71747aa8; Domain=localhost:8888; Max-Age=1800; Path=/
2020-03-31 01:44:23,127 INFO o.j.c.http.HttpResponseEncoder[74]: org.jim.server.ImServerChannelContext@2fd05977, set-cookie:jimIxO=5c7100c809f842388b30f5eb71747aa8; Domain=localhost:8888; Max-Age=1800; Path=/
2020-03-31 01:44:23,682 INFO o.j.s.h.DefaultHttpRequestHandler[335]: org.jim.server.ImServerChannelContext@2fd05977 创建会话Cookie, jimIxO=663bca4acf504991ae43673bc49169ae; Domain=localhost:8888; Max-Age=1800; Path=/
2020-03-31 01:44:23,682 INFO o.j.c.http.HttpResponseEncoder[74]: org.jim.server.ImServerChannelContext@2fd05977, set-cookie:jimIxO=663bca4acf504991ae43673bc49169ae; Domain=localhost:8888; Max-Age=1800; Path=/
2020-03-31 01:44:23,721 INFO o.j.s.h.DefaultHttpRequestHandler[335]: org.jim.server.ImServerChannelContext@2fd05977 创建会话Cookie, jimIxO=d205a4d408114608ae83df5bf370b709; Domain=localhost:8888; Max-Age=1800; Path=/
2020-03-31 01:44:23,722 INFO o.j.c.http.HttpResponseEncoder[74]: org.jim.server.ImServerChannelContext@2fd05977, set-cookie:jimIxO=d205a4d408114608ae83df5bf370b709; Domain=localhost:8888; Max-Age=1800; Path=/
2020-03-31 01:44:23,750 INFO o.j.s.h.DefaultHttpRequestHandler[335]: org.jim.server.ImServerChannelContext@2fd05977 创建会话Cookie, jimIxO=3cf19702a2884faca8e253b46e7f44d6; Domain=localhost:8888; Max-Age=1800; Path=/
2020-03-31 01:44:23,750 INFO o.j.c.http.HttpResponseEncoder[74]: org.jim.server.ImServerChannelContext@2fd05977, set-cookie:jimIxO=3cf19702a2884faca8e253b46e7f44d6; Domain=localhost:8888; Max-Age=1800; Path=/
2020-03-31 01:44:24,090 INFO o.j.s.h.DefaultHttpRequestHandler[335]: org.jim.server.ImServerChannelContext@2fd05977 创建会话Cookie, jimIxO=a2425996401742f0877c85c1c6f1fdfe; Domain=localhost:8888; Max-Age=1800; Path=/
2020-03-31 01:44:24,091 INFO o.j.c.http.HttpResponseEncoder[74]: org.jim.server.ImServerChannelContext@2fd05977, set-cookie:jimIxO=a2425996401742f0877c85c1c6f1fdfe; Domain=localhost:8888; Max-Age=1800; Path=/
2020-03-31 01:44:24,126 INFO o.j.s.h.DefaultHttpRequestHandler[335]: org.jim.server.ImServerChannelContext@2fd05977 创建会话Cookie, jimIxO=7ad17158c1f1490dbc2bd6e04d299846; Domain=localhost:8888; Max-Age=1800; Path=/
2020-03-31 01:44:24,127 INFO o.j.c.http.HttpResponseEncoder[74]: org.jim.server.ImServerChannelContext@2fd05977, set-cookie:jimIxO=7ad17158c1f1490dbc2bd6e04d299846; Domain=localhost:8888; Max-Age=1800; Path=/
2020-03-31 01:44:24,151 INFO o.j.s.h.DefaultHttpRequestHandler[335]: org.jim.server.ImServerChannelContext@2fd05977 创建会话Cookie, jimIxO=4f5e6af22257418bb0de09b8ab068232; Domain=localhost:8888; Max-Age=1800; Path=/
2020-03-31 01:44:24,152 INFO o.j.c.http.HttpResponseEncoder[74]: org.jim.server.ImServerChannelContext@2fd05977, set-cookie:jimIxO=4f5e6af22257418bb0de09b8ab068232; Domain=localhost:8888; Max-Age=1800; Path=/
2020-03-31 01:44:26,324 INFO o.j.s.d.s.LoginServiceProcessor[146]: 登录成功回调方法
2020-03-31 01:44:28,006 INFO o.tio.core.task.CloseRunnable[284]: ServerTioConfig [name=j-im], server:127.0.0.1:8888, client:127.0.0.1:55420 准备关闭连接, isNeedRemove:true, 收到关闭请求
2020-03-31 01:44:28,007 INFO org.tio.core.ChannelContext[592]: 关闭前server:127.0.0.1:8888, client:127.0.0.1:55420, 关闭后server:127.0.0.1:8888, client:$UNKNOWN:1
2020-03-31 01:44:35,008 INFO o.tio.core.task.CloseRunnable[284]: ServerTioConfig [name=j-im], server:127.0.0.1:8888, client:127.0.0.1:55349 准备关闭连接, isNeedRemove:true, 对方关闭了连接
2020-03-31 01:44:35,008 INFO org.tio.core.ChannelContext[592]: 关闭前server:127.0.0.1:8888, client:127.0.0.1:55349, 关闭后server:127.0.0.1:8888, client:$UNKNOWN:2
2020-03-31 01:45:07,397 INFO o.j.s.tcp.TcpProtocolHandler[43]: J-IM TCP协议初始化完毕...
2020-03-31 01:45:07,802 INFO org.jim.server.http.mvc.Routes[170]: class mapping
{
"/webim":"org.jim.server.http.api.WebImController",
"/api":"org.jim.server.http.api.HttpApiController",
"/test":"org.jim.server.http.api.TestController"
}
2020-03-31 01:45:07,807 INFO org.jim.server.http.mvc.Routes[172]: method mapping
{
"/test/html":"org.jim.server.http.api.TestController.html(request)",
"/test/filetest":"org.jim.server.http.api.TestController.filetest(request)",
"/test/abtest":"org.jim.server.http.api.TestController.abtest(request)",
"/test/txt":"org.jim.server.http.api.TestController.txt(request)",
"/test/upload":"org.jim.server.http.api.TestController.upload(uploadFile,before,end,request)",
"/test/bean":"org.jim.server.http.api.TestController.bean(user,request)",
"/test/json":"org.jim.server.http.api.TestController.json(request)",
"/test/post":"org.jim.server.http.api.TestController.post(before,end,request)",
"/test/plain":"org.jim.server.http.api.TestController.plain(before,end,request)",
"/api/user/online":"org.jim.server.http.api.HttpApiController.online(request,httpConfig,channelContext)",
"/api/message/send":"org.jim.server.http.api.HttpApiController.chat(request,httpConfig,channelContext)",
"/test/putsession":"org.jim.server.http.api.TestController.putsession(value,request)",
"/api/user/close":"org.jim.server.http.api.HttpApiController.close(request,httpConfig,channelContext)",
"/test/filetest.zip":"org.jim.server.http.api.TestController.filetest_zip(request)",
"/test/getsession":"org.jim.server.http.api.TestController.getsession(request)"
}
2020-03-31 01:45:07,810 INFO o.j.s.http.HttpProtocolHandler[77]: J-IM Http协议初始化完毕,耗时:409ms
2020-03-31 01:45:07,811 INFO o.j.s.ws.WsProtocolHandler[67]: J-IM WebSocket协议初始化完毕...
2020-03-31 01:45:45,425 INFO org.tio.server.ServerTioConfig[314]: j-im, 用户取消了框架层面的心跳检测,如果业务需要,请用户自己去完成心跳检测
2020-03-31 01:46:01,987 INFO org.tio.server.TioServer[352]:
|----------------------------------------------------------------------------------------|
| t-io site | https://www.t-io.org |
| t-io on gitee | https://gitee.com/tywo45/t-io |
| t-io on github | https://github.com/tywo45/t-io |
| t-io version | 3.5.8.v20191228-RELEASE |
| ---------------------------------------------------------------------------------------|
| TioConfig name | j-im |
| Started at | 2020-03-31 01:46:01 |
| Listen on | 127.0.0.1:8888 |
| Main Class | org.jim.server.demo.ImServerDemoStart |
| Jvm start time | 56204ms |
| Tio start time | 123ms |
| Pid | 97190 |
|----------------------------------------------------------------------------------------|
2020-03-31 01:46:02,726 INFO org.tio.server.TioServer[435]: t-io latest version:3.6.0.v20200315-RELEASEyour version:3.5.8.v20191228-RELEASE
2020-03-31 01:46:02,729 INFO org.tio.server.TioServer[446]: You haven't upgraded in 78 days

View File