mirror of
https://gitee.com/farsunset/cim.git
synced 2025-08-02 20:45:46 +08:00
Update CIMSession.java
This commit is contained in:
parent
4dcd82b8fb
commit
8f77898348
@ -1,243 +1,243 @@
|
|||||||
package com.farsunset.cim.nio.session;
|
package com.farsunset.cim.nio.session;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.net.SocketAddress;
|
import java.net.SocketAddress;
|
||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import org.apache.mina.core.future.WriteFuture;
|
|
||||||
import org.apache.mina.core.session.IoSession;
|
import org.apache.mina.core.future.WriteFuture;
|
||||||
|
import org.apache.mina.core.session.IoSession;
|
||||||
import com.farsunset.cim.nio.constant.CIMConstant;
|
|
||||||
|
import com.farsunset.cim.nio.constant.CIMConstant;
|
||||||
/**
|
|
||||||
* IoSession包装类,集群时 将此对象存入表中
|
/**
|
||||||
*
|
* IoSession包装类,集群时 将此对象存入表中
|
||||||
* @author 3979434@qq.com
|
*
|
||||||
*/
|
* @author 3979434@qq.com
|
||||||
|
*/
|
||||||
public class CIMSession implements Serializable{
|
|
||||||
|
public class CIMSession implements Serializable{
|
||||||
/**
|
|
||||||
*
|
/**
|
||||||
*/
|
*
|
||||||
private static final long serialVersionUID = 1L;
|
*/
|
||||||
public static String ID = "ID";
|
private transient static final long serialVersionUID = 1L;
|
||||||
public static String HOST = "HOST";
|
public transient static String ID = "ID";
|
||||||
|
public transient static String HOST = "HOST";
|
||||||
private IoSession session;
|
|
||||||
|
private transient IoSession session;
|
||||||
private String gid;//session全局ID
|
|
||||||
private Long nid;//session在本台服务器上的ID
|
private String gid;//session全局ID
|
||||||
private String deviceId;//客户端设备ID
|
private Long nid;//session在本台服务器上的ID
|
||||||
private String host;//session绑定的服务器IP
|
private String deviceId;//客户端ID (设备号码+应用包名)
|
||||||
private String account;//session绑定的账号
|
private String host;//session绑定的服务器IP
|
||||||
private String channel;//终端设备类型
|
private String account;//session绑定的账号
|
||||||
private String deviceModel;//终端设备型号
|
private String channel;//终端设备类型
|
||||||
|
private String deviceModel;//终端设备型号
|
||||||
private Long bindTime;//登录时间
|
private Long bindTime;//登录时间
|
||||||
|
private Long heartbeat;//心跳时间
|
||||||
private Long heartbeat;//心跳时间
|
|
||||||
|
public CIMSession(IoSession session) {
|
||||||
public CIMSession(IoSession session) {
|
this.session = session;
|
||||||
this.session = session;
|
this.nid = session.getId();
|
||||||
this.nid = session.getId();
|
}
|
||||||
}
|
|
||||||
|
public CIMSession()
|
||||||
public CIMSession()
|
{
|
||||||
{
|
|
||||||
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public String getAccount() {
|
||||||
public String getAccount() {
|
return account;
|
||||||
return account;
|
}
|
||||||
}
|
|
||||||
|
public void setAccount(String account) {
|
||||||
public void setAccount(String account) {
|
this.account = account;
|
||||||
if(session!=null)
|
|
||||||
{
|
setAttribute(CIMConstant.SESSION_KEY, account);
|
||||||
session.setAttribute(CIMConstant.SESSION_KEY, account);
|
}
|
||||||
}
|
|
||||||
this.account = account;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public String getGid() {
|
||||||
|
return gid;
|
||||||
|
}
|
||||||
public String getGid() {
|
|
||||||
return gid;
|
public void setGid(String gid) {
|
||||||
}
|
|
||||||
|
this.gid = gid;
|
||||||
public void setGid(String gid) {
|
|
||||||
this.gid = gid;
|
setAttribute("gid", gid);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Long getNid() {
|
public Long getNid() {
|
||||||
return nid;
|
return nid;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setNid(Long nid) {
|
public void setNid(Long nid) {
|
||||||
this.nid = nid;
|
this.nid = nid;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getDeviceId() {
|
public String getDeviceId() {
|
||||||
return deviceId;
|
return deviceId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public String getChannel() {
|
public String getChannel() {
|
||||||
return channel;
|
return channel;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setChannel(String channel) {
|
public void setChannel(String channel) {
|
||||||
this.channel = channel;
|
this.channel = channel;
|
||||||
}
|
|
||||||
|
setAttribute("channel", channel);
|
||||||
public String getDeviceModel() {
|
}
|
||||||
return deviceModel;
|
|
||||||
}
|
public String getDeviceModel() {
|
||||||
|
return deviceModel;
|
||||||
public void setDeviceModel(String deviceModel) {
|
}
|
||||||
this.deviceModel = deviceModel;
|
|
||||||
}
|
public void setDeviceModel(String deviceModel) {
|
||||||
|
this.deviceModel = deviceModel;
|
||||||
public void setDeviceId(String deviceId) {
|
|
||||||
this.deviceId = deviceId;
|
setAttribute("deviceModel", deviceModel);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setDeviceId(String deviceId) {
|
||||||
|
this.deviceId = deviceId;
|
||||||
|
|
||||||
public String getHost() {
|
setAttribute("deviceId", deviceId);
|
||||||
return host;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public Long getBindTime() {
|
public String getHost() {
|
||||||
return bindTime;
|
return host;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setBindTime(Long bindTime) {
|
|
||||||
this.bindTime = bindTime;
|
|
||||||
}
|
public Long getBindTime() {
|
||||||
|
return bindTime;
|
||||||
public Long getHeartbeat() {
|
}
|
||||||
return heartbeat;
|
|
||||||
}
|
public void setBindTime(Long bindTime) {
|
||||||
|
this.bindTime = bindTime;
|
||||||
public void setHeartbeat(Long heartbeat) {
|
setAttribute("bindTime", bindTime);
|
||||||
this.heartbeat = heartbeat;
|
}
|
||||||
if(session!=null)
|
|
||||||
{
|
public Long getHeartbeat() {
|
||||||
session.setAttribute(CIMConstant.HEARTBEAT_KEY, heartbeat);
|
return heartbeat;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
public void setHeartbeat(Long heartbeat) {
|
||||||
public void setHost(String host) {
|
this.heartbeat = heartbeat;
|
||||||
this.host = host;
|
setAttribute(CIMConstant.HEARTBEAT_KEY, heartbeat);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setHost(String host) {
|
||||||
|
this.host = host;
|
||||||
public void setAttribute(String key, Object value) {
|
|
||||||
if(session!=null)
|
setAttribute("host", host);
|
||||||
session.setAttribute(key, value);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
public void setIoSession(IoSession session) {
|
||||||
public boolean containsAttribute(String key) {
|
this.session = session;
|
||||||
if(session!=null)
|
}
|
||||||
return session.containsAttribute(key);
|
|
||||||
return false;
|
public IoSession getIoSession() {
|
||||||
}
|
return session;
|
||||||
|
}
|
||||||
public Object getAttribute(String key) {
|
|
||||||
if(session!=null)
|
|
||||||
return session.getAttribute(key);
|
|
||||||
return null;
|
public void setAttribute(String key, Object value) {
|
||||||
}
|
if(session!=null)
|
||||||
|
session.setAttribute(key, value);
|
||||||
public void removeAttribute(String key) {
|
}
|
||||||
if(session!=null)
|
|
||||||
session.removeAttribute(key);
|
|
||||||
}
|
public boolean containsAttribute(String key) {
|
||||||
|
if(session!=null)
|
||||||
public SocketAddress getRemoteAddress() {
|
return session.containsAttribute(key);
|
||||||
if(session!=null)
|
return false;
|
||||||
return session.getRemoteAddress();
|
}
|
||||||
return null;
|
|
||||||
}
|
public Object getAttribute(String key) {
|
||||||
|
if(session!=null)
|
||||||
public boolean write(Object msg) {
|
return session.getAttribute(key);
|
||||||
if(session!=null)
|
return null;
|
||||||
{
|
}
|
||||||
WriteFuture wf = session.write(msg);
|
|
||||||
wf.awaitUninterruptibly(5, TimeUnit.SECONDS);
|
public void removeAttribute(String key) {
|
||||||
return wf.isWritten();
|
if(session!=null)
|
||||||
}
|
session.removeAttribute(key);
|
||||||
return false;
|
}
|
||||||
}
|
|
||||||
|
public SocketAddress getRemoteAddress() {
|
||||||
public boolean isConnected() {
|
if(session!=null)
|
||||||
if(session!=null)
|
return session.getRemoteAddress();
|
||||||
return session.isConnected();
|
return null;
|
||||||
return false;
|
}
|
||||||
}
|
|
||||||
|
public boolean write(Object msg) {
|
||||||
public boolean isLocalhost()
|
if(session!=null)
|
||||||
{
|
{
|
||||||
|
WriteFuture wf = session.write(msg);
|
||||||
try {
|
wf.awaitUninterruptibly(5, TimeUnit.SECONDS);
|
||||||
String ip = InetAddress.getLocalHost().getHostAddress();
|
return wf.isWritten();
|
||||||
return ip.equals(host) && session!=null;
|
}
|
||||||
} catch (UnknownHostException e) {
|
return false;
|
||||||
e.printStackTrace();
|
}
|
||||||
}
|
|
||||||
return false;
|
public boolean isConnected() {
|
||||||
|
if(session!=null)
|
||||||
}
|
return session.isConnected();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
public void close(boolean immediately) {
|
|
||||||
if(session!=null)
|
public boolean isLocalhost()
|
||||||
session.close(immediately);
|
{
|
||||||
}
|
|
||||||
|
try {
|
||||||
|
String ip = InetAddress.getLocalHost().getHostAddress();
|
||||||
public boolean equals(Object o) {
|
return ip.equals(host);
|
||||||
|
} catch (UnknownHostException e) {
|
||||||
if (o instanceof CIMSession) {
|
e.printStackTrace();
|
||||||
|
}
|
||||||
CIMSession t = (CIMSession) o;
|
return false;
|
||||||
if(!t.isLocalhost())
|
|
||||||
{
|
}
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (t.session.getId() == session.getId()&& t.host.equals(host)) {
|
public void close(boolean immediately) {
|
||||||
return true;
|
if(session!=null)
|
||||||
}
|
session.close(immediately);
|
||||||
return false;
|
}
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
}
|
public boolean equals(Object o) {
|
||||||
|
|
||||||
}
|
if (o instanceof CIMSession) {
|
||||||
|
|
||||||
public void setIoSession(IoSession session) {
|
CIMSession t = (CIMSession) o;
|
||||||
this.session = session;
|
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);
|
||||||
public IoSession getIoSession() {
|
}
|
||||||
return session;
|
}
|
||||||
}
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user