Update BindHandler.java

This commit is contained in:
远方夕阳 2014-10-23 15:05:52 +08:00
parent 38d8ef58e8
commit ec645fb9a2

View File

@ -1,96 +1,89 @@
package com.farsunset.ichat.cim.handler;
package com.farsunset.ichat.cim.handler;
import java.net.InetAddress;
import java.net.InetAddress; import java.util.UUID;
import java.util.UUID;
import org.apache.log4j.Logger;
import org.apache.log4j.Logger;
import com.farsunset.cim.nio.constant.CIMConstant;
import com.farsunset.cim.nio.constant.CIMConstant; import com.farsunset.cim.nio.handler.CIMRequestHandler;
import com.farsunset.cim.nio.handler.CIMRequestHandler; import com.farsunset.cim.nio.mutual.Message;
import com.farsunset.cim.nio.mutual.Message; import com.farsunset.cim.nio.mutual.ReplyBody;
import com.farsunset.cim.nio.mutual.ReplyBody; import com.farsunset.cim.nio.mutual.SentBody;
import com.farsunset.cim.nio.mutual.SentBody; import com.farsunset.cim.nio.session.CIMSession;
import com.farsunset.cim.nio.session.CIMSession; import com.farsunset.cim.nio.session.DefaultSessionManager;
import com.farsunset.cim.nio.session.DefaultSessionManager; import com.farsunset.ichat.common.util.ContextHolder;
import com.farsunset.ichat.common.util.ContextHolder;
/**
/** * 账号绑定实现
* 账号绑定实现 *
* * @author
* @author */
*/ public class BindHandler implements CIMRequestHandler {
public class BindHandler implements CIMRequestHandler {
protected final Logger logger = Logger.getLogger(BindHandler.class);
protected final Logger logger = Logger.getLogger(BindHandler.class); public ReplyBody process(CIMSession newSession, SentBody message) {
public ReplyBody process(CIMSession newSession, SentBody message) {
ReplyBody reply = new ReplyBody();
ReplyBody reply = new ReplyBody(); DefaultSessionManager sessionManager= ((DefaultSessionManager) ContextHolder.getBean("defaultSessionManager"));
DefaultSessionManager sessionManager= ((DefaultSessionManager) ContextHolder.getBean("defaultSessionManager")); try {
try {
String account = message.get("account");
String account = message.get("account");
newSession.setAccount(account);
newSession.setAccount(account); newSession.setDeviceId(message.get("deviceId"));
newSession.setDeviceId(message.get("deviceId")); newSession.setGid(UUID.randomUUID().toString());
newSession.setGid(UUID.randomUUID().toString()); newSession.setHost(InetAddress.getLocalHost().getHostAddress());
newSession.setHost(InetAddress.getLocalHost().getHostAddress()); newSession.setChannel( message.get("channel"));
newSession.setChannel( message.get("channel")); newSession.setDeviceModel(message.get("device"));
newSession.setDeviceModel(message.get("device")); //第一次设置心跳时间为登录时间
/** newSession.setBindTime(System.currentTimeMillis());
* 由于客户端断线服务端可能会无法获知的情况客户端重连时需要关闭旧的连接 newSession.setHeartbeat(System.currentTimeMillis());
*/ /**
CIMSession oldSession = sessionManager.getSession(account); * 由于客户端断线服务端可能会无法获知的情况客户端重连时需要关闭旧的连接
if(oldSession!=null) */
{ //如果是账号已经在另一台终端登录则让另一个终端下线
if(oldSession!=null&&!oldSession.equals(newSession))
//如果是账号已经在另一台终端登录则让另一个终端下线 {
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);//强行下线消息类型
oldSession.removeAttribute(CIMConstant.SESSION_KEY); msg.setReceiver(account);
Message msg = new Message();
msg.setType(CIMConstant.MessageType.TYPE_999);//强行下线消息类型 if(!oldSession.isLocalhost())
msg.setReceiver(account); {
if(!oldSession.isLocalhost()) /*
{ 判断当前session是否连接于本台服务器如不是发往目标服务器处理
MessageDispatcher.execute(msg, oldSession.getHost());
/* */
判断当前session是否连接于本台服务器如不是发往目标服务器处理 }else
MessageDispatcher.execute(msg, oldSession.getHost()); {
*/ oldSession.write(msg);
}else oldSession.close(true);
{ oldSession = null;
oldSession.write(msg); }
oldSession.close(true); oldSession = null;
oldSession = null;
}
oldSession = null; }
} if(oldSession==null)
{
} sessionManager.addSession(account, newSession);
if(oldSession==null)
{ }
//第一次设置心跳时间为登录时间
newSession.setBindTime(System.currentTimeMillis()); reply.setCode(CIMConstant.ReturnCode.CODE_200);
newSession.setHeartbeat(System.currentTimeMillis());
} catch (Exception e) {
sessionManager.addSession(account, newSession); reply.setCode(CIMConstant.ReturnCode.CODE_500);
e.printStackTrace();
} }
logger.debug("bind :account:" +message.get("account")+"-----------------------------" +reply.getCode());
reply.setCode(CIMConstant.ReturnCode.CODE_200); return reply;
}
} catch (Exception e) {
reply.setCode(CIMConstant.ReturnCode.CODE_500);
e.printStackTrace();
}
logger.debug("bind :account:" +message.get("account")+"-----------------------------" +reply.getCode());
return reply;
}
} }