From ec645fb9a21736abbf721f0a502518f4ec755948 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=BF=9C=E6=96=B9=E5=A4=95=E9=98=B3?= Date: Thu, 23 Oct 2014 15:05:52 +0800 Subject: [PATCH] Update BindHandler.java --- .../ichat/cim/handler/BindHandler.java | 183 +++++++++--------- 1 file changed, 88 insertions(+), 95 deletions(-) diff --git a/cim_for_netty/ichat-server/src/main/java/com/farsunset/ichat/cim/handler/BindHandler.java b/cim_for_netty/ichat-server/src/main/java/com/farsunset/ichat/cim/handler/BindHandler.java index 3cdb53d..682b258 100644 --- a/cim_for_netty/ichat-server/src/main/java/com/farsunset/ichat/cim/handler/BindHandler.java +++ b/cim_for_netty/ichat-server/src/main/java/com/farsunset/ichat/cim/handler/BindHandler.java @@ -1,96 +1,89 @@ - -package com.farsunset.ichat.cim.handler; - -import java.net.InetAddress; -import java.util.UUID; - -import org.apache.log4j.Logger; - -import com.farsunset.cim.nio.constant.CIMConstant; -import com.farsunset.cim.nio.handler.CIMRequestHandler; -import com.farsunset.cim.nio.mutual.Message; -import com.farsunset.cim.nio.mutual.ReplyBody; -import com.farsunset.cim.nio.mutual.SentBody; -import com.farsunset.cim.nio.session.CIMSession; -import com.farsunset.cim.nio.session.DefaultSessionManager; -import com.farsunset.ichat.common.util.ContextHolder; - - -/** - * 账号绑定实现 - * - * @author - */ -public class BindHandler implements CIMRequestHandler { - - protected final Logger logger = Logger.getLogger(BindHandler.class); - public ReplyBody process(CIMSession newSession, SentBody message) { - - ReplyBody reply = new ReplyBody(); - DefaultSessionManager sessionManager= ((DefaultSessionManager) ContextHolder.getBean("defaultSessionManager")); - try { - - String account = message.get("account"); - - newSession.setAccount(account); - newSession.setDeviceId(message.get("deviceId")); - newSession.setGid(UUID.randomUUID().toString()); - newSession.setHost(InetAddress.getLocalHost().getHostAddress()); - newSession.setChannel( message.get("channel")); - newSession.setDeviceModel(message.get("device")); - /** - * 由于客户端断线服务端可能会无法获知的情况,客户端重连时,需要关闭旧的连接 - */ - CIMSession oldSession = sessionManager.getSession(account); - if(oldSession!=null) - { - - //如果是账号已经在另一台终端登录。则让另一个终端下线 - if((oldSession.getDeviceId()!=null&&!oldSession.getDeviceId().equals(newSession.getDeviceId()) - ||!oldSession.equals(newSession))) - { - - - oldSession.removeAttribute(CIMConstant.SESSION_KEY); - Message msg = new Message(); - msg.setType(CIMConstant.MessageType.TYPE_999);//强行下线消息类型 - msg.setReceiver(account); - - if(!oldSession.isLocalhost()) - { - - /* - 判断当前session是否连接于本台服务器,如不是发往目标服务器处理 - MessageDispatcher.execute(msg, oldSession.getHost()); - */ - }else - { - oldSession.write(msg); - oldSession.close(true); - oldSession = null; - } - oldSession = null; - } - - } - if(oldSession==null) - { - //第一次设置心跳时间为登录时间 - newSession.setBindTime(System.currentTimeMillis()); - newSession.setHeartbeat(System.currentTimeMillis()); - - sessionManager.addSession(account, newSession); - - } - - reply.setCode(CIMConstant.ReturnCode.CODE_200); - - } catch (Exception e) { - reply.setCode(CIMConstant.ReturnCode.CODE_500); - e.printStackTrace(); - } - logger.debug("bind :account:" +message.get("account")+"-----------------------------" +reply.getCode()); - return reply; - } - +package com.farsunset.ichat.cim.handler; + +import java.net.InetAddress; +import java.util.UUID; + +import org.apache.log4j.Logger; + +import com.farsunset.cim.nio.constant.CIMConstant; +import com.farsunset.cim.nio.handler.CIMRequestHandler; +import com.farsunset.cim.nio.mutual.Message; +import com.farsunset.cim.nio.mutual.ReplyBody; +import com.farsunset.cim.nio.mutual.SentBody; +import com.farsunset.cim.nio.session.CIMSession; +import com.farsunset.cim.nio.session.DefaultSessionManager; +import com.farsunset.ichat.common.util.ContextHolder; + + +/** + * 账号绑定实现 + * + * @author + */ +public class BindHandler implements CIMRequestHandler { + + protected final Logger logger = Logger.getLogger(BindHandler.class); + public ReplyBody process(CIMSession newSession, SentBody message) { + + ReplyBody reply = new ReplyBody(); + DefaultSessionManager sessionManager= ((DefaultSessionManager) ContextHolder.getBean("defaultSessionManager")); + try { + + String account = message.get("account"); + + newSession.setAccount(account); + newSession.setDeviceId(message.get("deviceId")); + newSession.setGid(UUID.randomUUID().toString()); + newSession.setHost(InetAddress.getLocalHost().getHostAddress()); + newSession.setChannel( message.get("channel")); + newSession.setDeviceModel(message.get("device")); + //第一次设置心跳时间为登录时间 + newSession.setBindTime(System.currentTimeMillis()); + newSession.setHeartbeat(System.currentTimeMillis()); + /** + * 由于客户端断线服务端可能会无法获知的情况,客户端重连时,需要关闭旧的连接 + */ + //如果是账号已经在另一台终端登录。则让另一个终端下线 + if(oldSession!=null&&!oldSession.equals(newSession)) + { + + + oldSession.removeAttribute(CIMConstant.SESSION_KEY); + Message msg = new Message(); + msg.setType(CIMConstant.MessageType.TYPE_999);//强行下线消息类型 + msg.setReceiver(account); + + if(!oldSession.isLocalhost()) + { + + /* + 判断当前session是否连接于本台服务器,如不是发往目标服务器处理 + MessageDispatcher.execute(msg, oldSession.getHost()); + */ + }else + { + oldSession.write(msg); + oldSession.close(true); + oldSession = null; + } + oldSession = null; + + + } + if(oldSession==null) + { + sessionManager.addSession(account, newSession); + + } + + reply.setCode(CIMConstant.ReturnCode.CODE_200); + + } catch (Exception e) { + reply.setCode(CIMConstant.ReturnCode.CODE_500); + e.printStackTrace(); + } + logger.debug("bind :account:" +message.get("account")+"-----------------------------" +reply.getCode()); + return reply; + } + } \ No newline at end of file