From 8f7789834852e93a88797c13de1067c802cbd30c 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 14:48:06 +0800 Subject: [PATCH] Update CIMSession.java --- .../farsunset/cim/nio/session/CIMSession.java | 484 +++++++++--------- 1 file changed, 242 insertions(+), 242 deletions(-) diff --git a/cim_for_mina/cim-core/src/com/farsunset/cim/nio/session/CIMSession.java b/cim_for_mina/cim-core/src/com/farsunset/cim/nio/session/CIMSession.java index c8083d2..b709218 100644 --- a/cim_for_mina/cim-core/src/com/farsunset/cim/nio/session/CIMSession.java +++ b/cim_for_mina/cim-core/src/com/farsunset/cim/nio/session/CIMSession.java @@ -1,243 +1,243 @@ -package com.farsunset.cim.nio.session; - -import java.io.Serializable; -import java.net.InetAddress; -import java.net.SocketAddress; -import java.net.UnknownHostException; -import java.util.concurrent.TimeUnit; -import org.apache.mina.core.future.WriteFuture; -import org.apache.mina.core.session.IoSession; - -import com.farsunset.cim.nio.constant.CIMConstant; - -/** - * IoSession包装类,集群时 将此对象存入表中 - * - * @author 3979434@qq.com - */ - -public class CIMSession implements Serializable{ - - /** - * - */ - private static final long serialVersionUID = 1L; - public static String ID = "ID"; - public static String HOST = "HOST"; - - private IoSession session; - - private String gid;//session全局ID - private Long nid;//session在本台服务器上的ID - private String deviceId;//客户端设备ID - private String host;//session绑定的服务器IP - private String account;//session绑定的账号 - private String channel;//终端设备类型 - private String deviceModel;//终端设备型号 - - private Long bindTime;//登录时间 - - private Long heartbeat;//心跳时间 - - public CIMSession(IoSession session) { - this.session = session; - this.nid = session.getId(); - } - - public CIMSession() - { - - } - - - - - public String getAccount() { - return account; - } - - public void setAccount(String account) { - if(session!=null) - { - session.setAttribute(CIMConstant.SESSION_KEY, account); - } - this.account = account; - } - - - - - - - public String getGid() { - return gid; - } - - public void setGid(String gid) { - this.gid = gid; - } - - public Long getNid() { - return nid; - } - - public void setNid(Long nid) { - this.nid = nid; - } - - public String getDeviceId() { - return deviceId; - } - - - public String getChannel() { - return channel; - } - - public void setChannel(String channel) { - this.channel = channel; - } - - public String getDeviceModel() { - return deviceModel; - } - - public void setDeviceModel(String deviceModel) { - this.deviceModel = deviceModel; - } - - public void setDeviceId(String deviceId) { - this.deviceId = deviceId; - } - - - - - public String getHost() { - return host; - } - - - - public Long getBindTime() { - return bindTime; - } - - public void setBindTime(Long bindTime) { - this.bindTime = bindTime; - } - - public Long getHeartbeat() { - return heartbeat; - } - - public void setHeartbeat(Long heartbeat) { - this.heartbeat = heartbeat; - if(session!=null) - { - session.setAttribute(CIMConstant.HEARTBEAT_KEY, heartbeat); - } - } - - public void setHost(String host) { - this.host = host; - } - - - - public void setAttribute(String key, Object value) { - if(session!=null) - session.setAttribute(key, value); - } - - - public boolean containsAttribute(String key) { - if(session!=null) - return session.containsAttribute(key); - return false; - } - - public Object getAttribute(String key) { - if(session!=null) - return session.getAttribute(key); - return null; - } - - public void removeAttribute(String key) { - if(session!=null) - session.removeAttribute(key); - } - - public SocketAddress getRemoteAddress() { - if(session!=null) - return session.getRemoteAddress(); - return null; - } - - public boolean write(Object msg) { - if(session!=null) - { - WriteFuture wf = session.write(msg); - wf.awaitUninterruptibly(5, TimeUnit.SECONDS); - return wf.isWritten(); - } - return false; - } - - public boolean isConnected() { - if(session!=null) - return session.isConnected(); - return false; - } - - public boolean isLocalhost() - { - - try { - String ip = InetAddress.getLocalHost().getHostAddress(); - return ip.equals(host) && session!=null; - } catch (UnknownHostException e) { - e.printStackTrace(); - } - return false; - - } - - - public void close(boolean immediately) { - if(session!=null) - session.close(immediately); - } - - - public boolean equals(Object o) { - - if (o instanceof CIMSession) { - - CIMSession t = (CIMSession) o; - if(!t.isLocalhost()) - { - return false; - } - if (t.session.getId() == session.getId()&& t.host.equals(host)) { - return true; - } - return false; - } else { - return false; - } - - } - - public void setIoSession(IoSession session) { - this.session = session; - } - - public IoSession getIoSession() { - return session; - } - - - - +package com.farsunset.cim.nio.session; + +import java.io.Serializable; +import java.net.InetAddress; +import java.net.SocketAddress; +import java.net.UnknownHostException; +import java.util.concurrent.TimeUnit; + +import org.apache.mina.core.future.WriteFuture; +import org.apache.mina.core.session.IoSession; + +import com.farsunset.cim.nio.constant.CIMConstant; + +/** + * IoSession包装类,集群时 将此对象存入表中 + * + * @author 3979434@qq.com + */ + +public class CIMSession implements Serializable{ + + /** + * + */ + private transient static final long serialVersionUID = 1L; + public transient static String ID = "ID"; + public transient static String HOST = "HOST"; + + private transient IoSession session; + + private String gid;//session全局ID + private Long nid;//session在本台服务器上的ID + private String deviceId;//客户端ID (设备号码+应用包名) + private String host;//session绑定的服务器IP + private String account;//session绑定的账号 + private String channel;//终端设备类型 + private String deviceModel;//终端设备型号 + private Long bindTime;//登录时间 + private Long heartbeat;//心跳时间 + + public CIMSession(IoSession session) { + this.session = session; + this.nid = session.getId(); + } + + public CIMSession() + { + + } + + + + + public String getAccount() { + return account; + } + + public void setAccount(String account) { + this.account = account; + + setAttribute(CIMConstant.SESSION_KEY, account); + } + + + + + + + public String getGid() { + return gid; + } + + public void setGid(String gid) { + + this.gid = gid; + + setAttribute("gid", gid); + } + + public Long getNid() { + return nid; + } + + public void setNid(Long nid) { + this.nid = nid; + } + + public String getDeviceId() { + return deviceId; + } + + + public String getChannel() { + return channel; + } + + public void setChannel(String channel) { + this.channel = channel; + + setAttribute("channel", channel); + } + + public String getDeviceModel() { + return deviceModel; + } + + public void setDeviceModel(String deviceModel) { + this.deviceModel = deviceModel; + + setAttribute("deviceModel", deviceModel); + } + + public void setDeviceId(String deviceId) { + this.deviceId = deviceId; + + setAttribute("deviceId", deviceId); + } + + + + + public String getHost() { + return host; + } + + + + public Long getBindTime() { + return bindTime; + } + + public void setBindTime(Long bindTime) { + this.bindTime = bindTime; + setAttribute("bindTime", bindTime); + } + + public Long getHeartbeat() { + return heartbeat; + } + + public void setHeartbeat(Long heartbeat) { + this.heartbeat = heartbeat; + setAttribute(CIMConstant.HEARTBEAT_KEY, heartbeat); + } + + public void setHost(String host) { + this.host = host; + + setAttribute("host", host); + } + + + public void setIoSession(IoSession session) { + this.session = session; + } + + public IoSession getIoSession() { + return session; + } + + + + public void setAttribute(String key, Object value) { + if(session!=null) + session.setAttribute(key, value); + } + + + public boolean containsAttribute(String key) { + if(session!=null) + return session.containsAttribute(key); + return false; + } + + public Object getAttribute(String key) { + if(session!=null) + return session.getAttribute(key); + return null; + } + + public void removeAttribute(String key) { + if(session!=null) + session.removeAttribute(key); + } + + public SocketAddress getRemoteAddress() { + if(session!=null) + return session.getRemoteAddress(); + return null; + } + + public boolean write(Object msg) { + if(session!=null) + { + WriteFuture wf = session.write(msg); + wf.awaitUninterruptibly(5, TimeUnit.SECONDS); + return wf.isWritten(); + } + return false; + } + + public boolean isConnected() { + if(session!=null) + return session.isConnected(); + return false; + } + + public boolean isLocalhost() + { + + try { + String ip = InetAddress.getLocalHost().getHostAddress(); + return ip.equals(host); + } catch (UnknownHostException e) { + e.printStackTrace(); + } + return false; + + } + + + public void close(boolean immediately) { + if(session!=null) + session.close(immediately); + } + + + public boolean equals(Object o) { + + if (o instanceof CIMSession) { + + CIMSession t = (CIMSession) o; + if(t.deviceId!=null && deviceId!=null && t.nid!=null && nid!=null) + { + return t.deviceId.equals(deviceId) && t.nid.longValue()==nid.longValue() && t.host.equals(host); + } + } + return false; + } + + + } \ No newline at end of file