| | |
| | | import java.util.*; |
| | | import java.util.Map.Entry; |
| | | |
| | | import com.dy.common.mw.channel.tcp.TcpIoSessionAttrIdIsRtuAddr; |
| | | import org.apache.mina.core.session.IoSession; |
| | | |
| | | import com.dy.rtuMw.server.ServerProperties; |
| | |
| | | /** |
| | | * 加入新的IoSession |
| | | * @param rtuAddr |
| | | * @param protocolName |
| | | * @param ioSession |
| | | */ |
| | | public static void putNewTcpSession(String rtuAddr, String protocolName, Short protocolVersion, IoSession ioSession){ |
| | | //public static void putNewTcpSession(String rtuAddr, String protocolName, Short protocolVersion, IoSession ioSession){ |
| | | public static void putNewTcpSession(String rtuAddr, IoSession ioSession){ |
| | | synchronized (map){ |
| | | TcpSession tcpSe = map.get(rtuAddr) ; |
| | | if(tcpSe == null){ |
| | | tcpSe = new TcpSession() ; |
| | | tcpSe.protocolName = protocolName ; |
| | | tcpSe.protocolVersion = protocolVersion ; |
| | | //tcpSe.protocolName = protocolName ; |
| | | //tcpSe.protocolVersion = protocolVersion ; |
| | | tcpSe.ioSession = ioSession ; |
| | | map.put(rtuAddr, tcpSe) ; |
| | | }else{ |
| | |
| | | * 更新IoSession对应的rtuAddr |
| | | * @param oldRtuAddr |
| | | * @param newRtuAddr |
| | | * @param protocolName |
| | | * @param protocolVersion |
| | | * @param ioSession |
| | | */ |
| | | public static void changeRtuAddr(String oldRtuAddr, String newRtuAddr, String protocolName, Short protocolVersion, IoSession ioSession){ |
| | | //public static void changeRtuAddr(String oldRtuAddr, String newRtuAddr, String protocolName, Short protocolVersion, IoSession ioSession){ |
| | | public static void changeRtuAddr(String oldRtuAddr, String newRtuAddr, IoSession ioSession){ |
| | | if(oldRtuAddr != null && newRtuAddr != null && !oldRtuAddr.equals(newRtuAddr)){ |
| | | synchronized (map){ |
| | | TcpSession tcpSe = map.get(oldRtuAddr) ; |
| | | if(tcpSe == null){ |
| | | putNewTcpSession(newRtuAddr, protocolName, protocolVersion, ioSession) ; |
| | | putNewTcpSession(newRtuAddr, ioSession) ; |
| | | }else{ |
| | | map.remove(oldRtuAddr) ; |
| | | map.put(newRtuAddr, tcpSe) ; |
| | |
| | | * 得到Tcp通信协议名称 |
| | | * @param rtuAddr |
| | | * @return |
| | | |
| | | public static String getTcpProtocolName(String rtuAddr){ |
| | | TcpSession tcpSe = sessionTable.get(rtuAddr) ; |
| | | if(tcpSe != null){ |
| | | return tcpSe.protocolName ; |
| | | }else{ |
| | | return null ; |
| | | } |
| | | } |
| | | */ |
| | | /** |
| | | * 得到Tcp通信协议名称 |
| | | * @param rtuAddr |
| | | * @return |
| | | */ |
| | | * */ |
| | | public static Object[] getTcpProtocolNameVersion(String rtuAddr){ |
| | | TcpSession tcpSe = map.get(rtuAddr) ; |
| | | if(tcpSe != null){ |
| | | return new Object[]{tcpSe.protocolName, tcpSe.protocolVersion}; |
| | | return new Object[]{ |
| | | tcpSe.ioSession.getAttribute(TcpIoSessionAttrIdIsRtuAddr.sessionArrProtocolName), |
| | | tcpSe.ioSession.getAttribute(TcpIoSessionAttrIdIsRtuAddr.sessionArrProtocolVersion)}; |
| | | }else{ |
| | | return null ; |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 得到所有在线与离线数量统计 |
| | |
| | | return map ; |
| | | } |
| | | } |
| | | // |
| | | // /** |
| | | // * 得到所有RTU连接状态情况 |
| | | // * @return |
| | | // */ |
| | | // public static List<RtuSessionStatus> allConnectStatus(){ |
| | | // synchronized (sessionTable){ |
| | | // List<RtuSessionStatus> list = new ArrayList<RtuSessionStatus>(); |
| | | // Iterator<Entry<String, TcpSession>> it = sessionTable.entrySet().iterator() ; |
| | | // Entry<String, TcpSession> entry = null ; |
| | | // while(it.hasNext()){ |
| | | // entry = it.next() ; |
| | | // RtuSessionStatus vo = new RtuSessionStatus() ; |
| | | // vo.rtuAddr = entry.getKey() ; |
| | | // IoSession se = entry.getValue().ioSession ; |
| | | // vo.onTrueOffLine = se.isConnected() ; |
| | | // InetSocketAddress sa = (InetSocketAddress)se.getRemoteAddress() ; |
| | | // if(sa != null){ |
| | | // InetAddress inetAddr = sa.getAddress() ; |
| | | // if(inetAddr != null){ |
| | | // vo.ip = inetAddr.getHostAddress() ; |
| | | // vo.port = sa.getPort() ; |
| | | // } |
| | | // } |
| | | // list.add(vo) ; |
| | | // } |
| | | // return list ; |
| | | // } |
| | | // } |
| | | // |
| | | |
| | | /** |
| | | * 得到IoSession |
| | | * @param rtuAddr |
| | | * @return |
| | | */ |
| | | // public IoSession getIoSession(String rtuAddr){ |
| | | // TcpSession tcpSe = sessionMap.get(rtuAddr) ; |
| | | // if(tcpSe != null){ |
| | | // return tcpSe.ioSession ; |
| | | // } |
| | | // return null ; |
| | | // } |
| | | |
| | | |
| | | /** |
| | | * 网络是否连接 |
| | | * @param rtuAddr |
| | |
| | | } |
| | | } |
| | | } |
| | | |
| | | // 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 ; |
| | | // } |
| | | // } |
| | | // } |
| | | // } |
| | | // } |
| | | |
| | | |
| | | } |