File was renamed from pipIrr-platform/pipIrr-mw/pipIrr-mw-accept/src/main/java/com/dy/aceMw/server/forTcp/TcpSessionCach.java |
| | |
| | | package com.dy.aceMw.server.forTcp; |
| | | |
| | | import java.net.InetAddress; |
| | | import java.net.InetSocketAddress; |
| | | import java.util.ArrayList; |
| | | import java.util.HashMap; |
| | | import java.util.Hashtable; |
| | | import java.util.Iterator; |
| | | import java.util.List; |
| | | import java.util.Map.Entry; |
| | | |
| | | import com.dy.aceMw.server.ServerProperties; |
| | | import org.apache.mina.core.session.IoSession; |
| | | |
| | | import com.dy.aceMw.server.ServerProperties; |
| | | import java.net.InetAddress; |
| | | import java.net.InetSocketAddress; |
| | | import java.util.*; |
| | | import java.util.Map.Entry; |
| | | |
| | | |
| | | public class TcpSessionCach { |
| | | /** |
| | | * 用Hashtable实现,但实测发现Hashtable并不线程安全 |
| | | */ |
| | | public class TcpSessionCacheBk { |
| | | |
| | | /** |
| | | * 用Hashtable而不用HashMap原因: |
| | | * Hashtable线程安全的 |
| | | * HashMap线程不安全的 |
| | | * 多线程对sessionTable读出或存入,可能产生异常 |
| | | * 虽然是单个主线程,但在Jgroups web成员查询在线情况时,Jgroups的线程直接侵入,从而形成多线程环境,多线程遍历时,Map成员数量有变化就会产生异常 |
| | | * TcpSessionCache是在多线程环境下运行 |
| | | */ |
| | | private static Hashtable<String, TcpSession> sessionTable = new Hashtable<String, TcpSession>() ; |
| | | |
| | |
| | | * @param ioSession |
| | | */ |
| | | public static void changeRtuAddr(String oldRtuAddr, String newRtuAddr, String protocolName, IoSession ioSession){ |
| | | TcpSession tcpSe = sessionTable.get(oldRtuAddr) ; |
| | | if(tcpSe == null){ |
| | | putNewTcpSession(newRtuAddr, protocolName, ioSession) ; |
| | | }else{ |
| | | sessionTable.remove(oldRtuAddr) ; |
| | | sessionTable.put(newRtuAddr, tcpSe) ; |
| | | if(oldRtuAddr != null && newRtuAddr != null && !oldRtuAddr.equals(newRtuAddr)){ |
| | | TcpSession tcpSe = sessionTable.get(oldRtuAddr) ; |
| | | if(tcpSe == null){ |
| | | putNewTcpSession(newRtuAddr, protocolName, ioSession) ; |
| | | }else{ |
| | | sessionTable.remove(oldRtuAddr) ; |
| | | sessionTable.put(newRtuAddr, tcpSe) ; |
| | | } |
| | | } |
| | | } |
| | | |
| | |
| | | * 得到所有RTU连接状态情况 |
| | | * @return |
| | | */ |
| | | public static List<RtuSessionStatus> allConnectStauts(){ |
| | | public static List<RtuSessionStatus> allConnectStatus(){ |
| | | List<RtuSessionStatus> list = new ArrayList<RtuSessionStatus>(); |
| | | Iterator<Entry<String, TcpSession>> it = sessionTable.entrySet().iterator() ; |
| | | Entry<String, TcpSession> entry = null ; |
| | |
| | | * 设置上行数据时刻 |
| | | * @param rtuAddr |
| | | */ |
| | | public static void cachUpDataTime(String rtuAddr){ |
| | | public static void cacheUpDataTime(String rtuAddr){ |
| | | TcpSession tcpSe = sessionTable.get(rtuAddr) ; |
| | | if(tcpSe != null){ |
| | | tcpSe.lastUpDataTime = System.currentTimeMillis() ; |