| | |
| | | |
| | | import java.net.InetAddress; |
| | | import java.net.InetSocketAddress; |
| | | import java.util.ArrayList; |
| | | import java.util.HashMap; |
| | | import java.util.Iterator; |
| | | import java.util.List; |
| | | import java.util.*; |
| | | import java.util.Map.Entry; |
| | | |
| | | import com.dy.common.util.DateTime; |
| | | import org.apache.mina.core.session.IoSession; |
| | | |
| | | import com.dy.rtuMw.server.ServerProperties; |
| | |
| | | * 2023-12-19实测,发现Hashtable并不线程安全,所以应用了HashMap和synchronized |
| | | */ |
| | | private static HashMap<String, TcpSession> sessionTable = new HashMap<String, TcpSession>() ; |
| | | |
| | | |
| | | |
| | | /** |
| | | * 得到信息 |
| | | * @return |
| | | */ |
| | | public static Integer[] info(){ |
| | | Integer rtuTotalConnect = 0 ;//已经连接过中间件的RTU总数(包括在线与离线的) |
| | | Integer rtuTotalOnLine = 0 ;//在线RTU总数 |
| | | Integer rtuTotalOffLine = 0 ;//离线RTU总数 |
| | | synchronized (sessionTable){ |
| | | rtuTotalConnect = sessionTable.size() ; |
| | | Collection<TcpSession> col = sessionTable.values() ; |
| | | for(TcpSession se : col){ |
| | | if(se.ioSession.isConnected()){ |
| | | rtuTotalOnLine ++ ; |
| | | }else{ |
| | | rtuTotalOffLine ++ ; |
| | | } |
| | | } |
| | | } |
| | | return new Integer[] {rtuTotalConnect, rtuTotalOnLine, rtuTotalOffLine} ; |
| | | } |
| | | |
| | | /** |
| | | * 关闭所有网络连接 |
| | | */ |
| | | public static void closeAllSessions(){ |
| | | synchronized (sessionTable){ |
| | | Collection<TcpSession> col = sessionTable.values() ; |
| | | for(TcpSession se : col){ |
| | | se.ioSession.closeNow() ; |
| | | } |
| | | sessionTable.clear(); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 加入新的IoSession |
| | | * @param rtuAddr |
| | |
| | | TcpSession tcpSe = sessionTable.get(rtuAddr) ; |
| | | if(tcpSe != null){ |
| | | tcpSe.lastUpDataTime = System.currentTimeMillis() ; |
| | | tcpSe.lastUpDataTimeForOnlineCtrl = System.currentTimeMillis() ; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 更新上行数据时刻 |
| | | * 当上行数据时刻已经过去一定时长,上行数据时刻清空 |
| | | * 当一定时间内没有上行数据,则认为RTU离线 |
| | | */ |
| | | public static void updateUpDataTime(Long now){ |
| | | public static void updateRtuStatus(Long now){ |
| | | synchronized (sessionTable){ |
| | | Iterator<TcpSession> it = sessionTable.values().iterator() ; |
| | | Set<Map.Entry<String, TcpSession>> entrySet = sessionTable.entrySet() ; |
| | | Iterator<Map.Entry<String, TcpSession>> it = entrySet.iterator() ; |
| | | Map.Entry<String, TcpSession> entry ; |
| | | TcpSession tcpSe ; |
| | | while(it.hasNext()){ |
| | | tcpSe = it.next() ; |
| | | entry = it.next() ; |
| | | tcpSe = entry.getValue(); |
| | | if(tcpSe.lastUpDataTime != null){ |
| | | if(now - tcpSe.lastUpDataTime > ServerProperties.lastUpDataTimeLive){ |
| | | tcpSe.lastUpDataTime = null ; |
| | | } |
| | | } |
| | | if(tcpSe.lastUpDataTimeForOnlineCtrl != null){ |
| | | if(tcpSe.ioSession != null && tcpSe.ioSession.isConnected()){ |
| | | if(now - tcpSe.lastUpDataTimeForOnlineCtrl > ServerProperties.disconnectedByNoUpDataMinutes){ |
| | | tcpSe.ioSession.closeNow() ; |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | // public static void updateRtuStatus(Long now){ |
| | | // synchronized (sessionTable){ |
| | | // Iterator<TcpSession> it = sessionTable.values().iterator() ; |
| | | // TcpSession tcpSe ; |
| | | // while(it.hasNext()){ |
| | | // tcpSe = it.next() ; |
| | | // if(tcpSe.lastUpDataTime != null){ |
| | | // if(now - tcpSe.lastUpDataTime > ServerProperties.lastUpDataTimeLive){ |
| | | // tcpSe.lastUpDataTime = null ; |
| | | // } |
| | | // } |
| | | // } |
| | | // } |
| | | // } |
| | | |
| | | |
| | | } |