126 lines
4.0 KiB
Java
126 lines
4.0 KiB
Java
package com.ifish.socketNew;
|
||
|
||
import java.util.Date;
|
||
import java.util.concurrent.atomic.AtomicInteger;
|
||
|
||
import org.apache.mina.core.service.IoHandlerAdapter;
|
||
import org.apache.mina.core.session.IdleStatus;
|
||
import org.apache.mina.core.session.IoSession;
|
||
import org.slf4j.Logger;
|
||
import org.slf4j.LoggerFactory;
|
||
import org.springframework.beans.factory.annotation.Autowired;
|
||
|
||
import com.ifish.quartz.JobGroup;
|
||
import com.ifish.quartz.ScheduleJob;
|
||
import com.ifish.util.IfishUtil;
|
||
|
||
/**
|
||
* mina服务器端事件处理
|
||
*
|
||
* @author
|
||
*/
|
||
public class MinaServerHandler extends IoHandlerAdapter {
|
||
|
||
private final AtomicInteger atomicInt = new AtomicInteger();
|
||
|
||
private static Logger log = LoggerFactory.getLogger(MinaServerHandler.class);
|
||
|
||
@Autowired
|
||
private SomeServer someServer;
|
||
@Autowired
|
||
private ScheduleJob scheduleJob;
|
||
|
||
@Override
|
||
public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
|
||
cause.printStackTrace();
|
||
log.error("{}服务器-客户端session异常:{}", session.getRemoteAddress(), cause.toString());
|
||
}
|
||
|
||
/**
|
||
* 服务端接收消息处理
|
||
*/
|
||
@Override
|
||
public void messageReceived(IoSession session, Object message) throws Exception {
|
||
someServer.doSome(session, message);
|
||
}
|
||
|
||
/**
|
||
* 客户端连接的会话创建
|
||
*/
|
||
@Override
|
||
public void sessionCreated(IoSession session) throws Exception {
|
||
log.info("{}:会话创建,在线数:{}", session.getRemoteAddress(), atomicInt.incrementAndGet());
|
||
}
|
||
|
||
/**
|
||
* 客户端连接关闭
|
||
*/
|
||
@Override
|
||
public void sessionClosed(IoSession session) throws Exception {
|
||
log.info("{}:会话关闭,连接数:{}", session.getRemoteAddress(), atomicInt.decrementAndGet());
|
||
//旧连接关闭,获取设备mac地址
|
||
String oldConnect = session.getRemoteAddress().toString();
|
||
String macAddress = SomeServer.remoteAddress.get(oldConnect);
|
||
if (macAddress != null) {
|
||
//移除关闭的连接
|
||
SomeServer.remoteAddress.remove(oldConnect);
|
||
//设备当前连接
|
||
IoSession nowIoSession = SomeServer.sessions_cz.get(macAddress);
|
||
if (nowIoSession != null && nowIoSession.getRemoteAddress() != null) {
|
||
String nowConnect = nowIoSession.getRemoteAddress().toString();
|
||
//如果当前连接和关闭的连接是相同的,说明设备没有重连,10分钟内还未连接,则推送通知用户
|
||
if (nowConnect.equals(oldConnect)) {
|
||
offlinePush(macAddress);
|
||
}
|
||
} //当前没有连接,10分钟内还未连接,则推送通知用户
|
||
else {
|
||
offlinePush(macAddress);
|
||
}
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 延时10分钟推送离线通知,10分钟内重新连接上则移除推送,详见SomeServer
|
||
*
|
||
* @param macAddress
|
||
*/
|
||
public void offlinePush(String macAddress) {
|
||
JobGroup jobGroup = new JobGroup();
|
||
jobGroup.setJobName(macAddress);
|
||
jobGroup.setTriggerName(macAddress);
|
||
jobGroup.setMacAddress(macAddress);
|
||
//离线时间
|
||
jobGroup.setTimestamp(IfishUtil.format(new Date()));
|
||
//10分钟后推送 update 30分钟
|
||
jobGroup.setStartTime(new Date(System.currentTimeMillis() + 600000L * 3));
|
||
scheduleJob.addJob(jobGroup);
|
||
}
|
||
|
||
/**
|
||
* 给客户端发送消息
|
||
*/
|
||
@Override
|
||
public void messageSent(IoSession session, Object message) {
|
||
//System.out.println("messageSent");
|
||
}
|
||
|
||
/**
|
||
* 空闲超时,主动关闭客户端连接
|
||
*/
|
||
@Override
|
||
public void sessionIdle(IoSession session, IdleStatus status)
|
||
throws Exception {
|
||
log.info("空闲超时,服务器主动关闭连接{}", session.getRemoteAddress());
|
||
session.close(true);
|
||
}
|
||
|
||
/**
|
||
* 客户端连接打开
|
||
*/
|
||
@Override
|
||
public void sessionOpened(IoSession session) throws Exception {
|
||
//log.info("session open!!!");
|
||
}
|
||
|
||
}
|