package com.dy.testServer.forRmi; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import com.dy.testServer.ServerProperties; public class Manager { public static boolean enablemwTestStart = false ; public static int clientId = 1 ; public static long maxClient = 0 ; public static Map id2TokenMap = new HashMap<>() ; public static Map token2ClientMap = new HashMap<>() ; public static List rtuAddrList = new ArrayList<>() ; public static void init(){ maxClient = (ServerProperties.rtuAddrEnd - ServerProperties.rtuAddrStart)/ServerProperties.rtuAddrPerMwTest ; if((ServerProperties.rtuAddrEnd - ServerProperties.rtuAddrStart)%ServerProperties.rtuAddrPerMwTest > 0 ){ maxClient = maxClient + 1 ; } int count = 0 ; for(int i = 0; i < maxClient; i++){ long start = ServerProperties.rtuAddrStart + (i * ServerProperties.rtuAddrPerMwTest) ; long end = ServerProperties.rtuAddrStart + ((i + 1) * ServerProperties.rtuAddrPerMwTest - 1) ; if(end > ServerProperties.rtuAddrEnd){ end = ServerProperties.rtuAddrEnd ; } if(end < start){ break ; }else{ rtuAddrList.add(count++, new Object[]{start, end}); } } } public synchronized static String dealRequest(Object obj) throws Exception{ RmiResponseVo resVo ; if(obj == null){ resVo = new RmiResponseVo() ; resVo.success = false ; resVo.errorInfo = "收到的数据为null" ; }else if(!(obj instanceof String)){ resVo = new RmiResponseVo() ; resVo.success = false ; resVo.errorInfo = "收到的数据不是字符串类型" ; }else{ RmiRequestVo rqVo = RmiRequestVo.jsonToObject((String)obj) ; resVo = doDeal(rqVo) ; } return resVo.toJson() ; } private static RmiResponseVo doDeal(RmiRequestVo rqVo){ RmiResponseVo resVo = new RmiResponseVo() ; resVo.code = rqVo.code ; if(rqVo.code.equals(Code.cd1)){ if(rqVo.id == null || rqVo.id.trim().equals("")){ resVo.success = false ; resVo.errorInfo = "注册失败,必须提供客户端ID " ; }else{ ResRegisterVo vo = doDealRegister(rqVo) ; if(vo != null){ resVo.obj = vo ; }else{ resVo.success = false ; resVo.errorInfo = "rtuAddr已经分配完成,注册失败" ; } } }else if(rqVo.code.equals(Code.cd2)){ resVo.obj = doDealGetConfig(rqVo) ; }else if(rqVo.code.equals(Code.cd3)){ resVo.obj = doDealGetStart(rqVo) ; }else if(rqVo.code.equals(Code.cd4)){ doDealReportCount(rqVo) ; }else if(rqVo.code.equals(Code.cd5)){ doDealReportOver(rqVo) ; }else if(rqVo.code.equals(Code.cd6)){ doDealAllOver(rqVo) ; } return resVo ; } private static ResRegisterVo doDealRegister(RmiRequestVo rqVo){ ResRegisterVo resVo = null ; String token = id2TokenMap.get(rqVo.id) ; if(token == null){ if(clientId <= rtuAddrList.size()){ resVo = new ResRegisterVo() ; resVo.token = "" + clientId ; id2TokenMap.put(rqVo.id, resVo.token) ; clientId++ ; } }else{ resVo = new ResRegisterVo() ; resVo.token = token ; } return resVo ; } private static MwConfigVo doDealGetConfig(RmiRequestVo rqVo){ MwConfigVo conVo ; int token = Integer.parseInt(rqVo.token) ; MwTestClientStatus sta = token2ClientMap.get("" + token); if(sta == null){ Object[] rtuAddrs = rtuAddrList.get(token-1) ; conVo = new MwConfigVo() ; conVo.rtuAddrStart = (Long)rtuAddrs[0] ; conVo.rtuAddrEnd = (Long)rtuAddrs[1] ; conVo.tcpServerIp = ServerProperties.tcpServerIp ; conVo.tcpServerPort = ServerProperties.tcpServerPort ; conVo.sendInterval = ServerProperties.sendInterval ; conVo.heartbeatTimes = ServerProperties.heartbeatTimes ; conVo.sendTimes = ServerProperties.sendTimes ; sta = new MwTestClientStatus() ; sta.confVo = conVo ; token2ClientMap.put("" + token, sta); }else{ conVo = sta.confVo; } return conVo ; } private static ResStartVo doDealGetStart(RmiRequestVo rqVo){ ResStartVo rvo = new ResStartVo() ; rvo.start = enablemwTestStart ; int token = Integer.parseInt(rqVo.token) ; MwTestClientStatus sta = token2ClientMap.get("" + token); if(sta == null){ sta = new MwTestClientStatus() ; sta.startVo = rvo ; token2ClientMap.put("" + token, sta); }else{ sta.startVo = rvo ; } return rvo ; } private static void doDealReportCount(RmiRequestVo rqVo){ int token = Integer.parseInt(rqVo.token) ; MwTestClientStatus sta = token2ClientMap.get("" + token); if(sta == null){ sta = new MwTestClientStatus() ; sta.count = rqVo.count ; token2ClientMap.put("" + token, sta); }else{ sta.count = rqVo.count ; } } private static void doDealReportOver(RmiRequestVo rqVo){ int token = Integer.parseInt(rqVo.token) ; MwTestClientStatus sta = token2ClientMap.get("" + token); if(sta == null){ sta = new MwTestClientStatus() ; sta.overCount = rqVo.count ; token2ClientMap.put("" + token, sta); }else{ sta.count = rqVo.count ; } } private static void doDealAllOver(RmiRequestVo rqVo){ int token = Integer.parseInt(rqVo.token) ; MwTestClientStatus sta = token2ClientMap.get("" + token); if(sta == null){ sta = new MwTestClientStatus() ; if(rqVo.over != null && rqVo.over){ sta.over = true ; sta.seconds = rqVo.seconds ; } token2ClientMap.put("" + token, sta); }else{ if(rqVo.over != null && rqVo.over){ sta.over = true ; sta.seconds = rqVo.seconds ; } } } }