package com.dy.testClient.rmiClient; import com.dy.testClient.ServerProperties; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import com.dy.common.mw.UnitAdapterInterface; import com.dy.common.mw.UnitInterface; import com.dy.common.mw.UnitStartedCallbackInterface; import com.dy.common.mw.channel.rmi.RmiFrameWork; public class RmiClUnit implements UnitInterface { private static final Logger log = LogManager.getLogger(RmiClUnit.class) ; private static final String id = "" + System.nanoTime() ;//自己的id public static String token ;//服务端发的token private static final RmiClUnit instance = new RmiClUnit() ; private static RmiFrameWork frmWork = null ; public static RmiClUnitAdapter adapter ; public static RmiClUnitConfigVo confVo ; private RmiClUnit(){} public static RmiClUnit getInstance(){ return instance ; } @Override public void setAdapter(UnitAdapterInterface adapter) throws Exception { if(adapter == null){ throw new Exception("Rmi Client模块适配器对象不能为空!") ; } RmiClUnit.adapter = (RmiClUnitAdapter)adapter ; RmiClUnit.confVo = RmiClUnit.adapter.getConfig() ; if(RmiClUnit.confVo == null){ throw new Exception("Rmi Client模块配置对象不能为空!") ; } } @Override public void start(UnitStartedCallbackInterface callback) throws Exception { System.out.println("Rmi Client模块成功启动"); this.doStart(); callback.call(null) ; } @Override public void stop(UnitStartedCallbackInterface callback) throws Exception { callback.call(null); } private void doStart(){ new Thread(() -> { while(true){ try { Thread.sleep(100L); frmWork = getRmiFrameWork() ; if(frmWork != null){ break ; } } catch (Exception e) { log.error("得到rmiFramWork失败" + e.getMessage()); continue ; } } register(frmWork) ; }).start(); } public RmiFrameWork getRmiFrameWork(){ RmiClient rmiCl = new RmiClient(confVo.svUrl, confVo.svPort, confVo.svContext) ; return rmiCl.getRmiInterface() ; } //把上报数据的数量上报给mwTestServer public void reportHadReportCount(Integer count){ try { Thread.sleep(100L); RmiRequestVo rqVo = new RmiRequestVo() ; rqVo.id = id ; rqVo.token = token ; rqVo.code = Code.cd4 ; rqVo.count = count ; String json = rqVo.toJson() ; frmWork.syncRequest(json) ; } catch (Exception e) { log.error("把上报数据的数量上报给mwTestServer失败" + e.getMessage()); } } //把数据上报完成情况 上报给mwTestServer public void reportHadReportOver(Integer count){ try { Thread.sleep(100L); RmiRequestVo rqVo = new RmiRequestVo() ; rqVo.id = id ; rqVo.token = token ; rqVo.code = Code.cd5 ; rqVo.overCount = count ; String json = rqVo.toJson() ; frmWork.syncRequest(json) ; } catch (Exception e) { log.error("把数据上报完成情况 上报给mwTestServer失败" + e.getMessage()); } } //把数据上报完成情况 上报给mwTestServer public void allOver(Long seconds){ try { Thread.sleep(100L); RmiRequestVo rqVo = new RmiRequestVo() ; rqVo.id = id ; rqVo.token = token ; rqVo.code = Code.cd6 ; rqVo.over = true ; rqVo.seconds = seconds ; String json = rqVo.toJson() ; frmWork.syncRequest(json) ; } catch (Exception e) { log.error("把数据上报完成情况 上报给mwTestServer失败" + e.getMessage()); } } //向mwTestServer注册 private void register(RmiFrameWork frmWork){ boolean error = false ; while(true){ try { Thread.sleep(100L); RmiRequestVo rqVo = new RmiRequestVo() ; rqVo.id = id ; rqVo.code = Code.cd1 ; String json = rqVo.toJson() ; Object rObj = frmWork.syncRequest(json) ; if(rObj != null){ RmiResponseVo rspVo = RmiResponseVo.jsonToObject(String.valueOf(rObj), ResRegisterVo.class) ; if(rspVo != null){ if(rspVo.success){ if(rspVo.obj != null && rspVo.obj instanceof ResRegisterVo){ ResRegisterVo rVo = (ResRegisterVo)rspVo.obj ; token = rVo.token ; log.info("注册成功,得到token=" + token); break ; }else{ log.error("rmi注册失败:服务端返回ResRegisterVo为null"); error = true ; } }else{ log.error("rmi注册失败:服务端返回错误:" + rspVo.errorInfo); error = true ; } }else{ log.error("rmi注册失败:服务端返回的RmiResponseVo为null"); error = true ; } }else{ log.error("rmi注册失败:服务端返回json为null"); error = true ; } } catch (Exception e) { log.error("rmi注册失败" + e.getMessage()); error = true ; continue ; } } if(!error){ getConfig(frmWork) ; } } //从mwTestServer得到配置 private void getConfig(RmiFrameWork frmWork){ boolean error = false ; while(true){ try { Thread.sleep(100L); RmiRequestVo rqVo = new RmiRequestVo() ; rqVo.id = id ; rqVo.token = token ; rqVo.code = Code.cd2 ; String json = rqVo.toJson() ; Object rObj = frmWork.syncRequest(json) ; if(rObj != null){ RmiResponseVo rspVo = RmiResponseVo.jsonToObject(String.valueOf(rObj), ResConfigVo.class) ; if(rspVo != null){ if(rspVo.success){ if(rspVo.obj != null && rspVo.obj instanceof ResConfigVo){ ResConfigVo rVo = (ResConfigVo)rspVo.obj ; if(rVo != null){ ServerProperties.rtuAddrStart = rVo.rtuAddrStart; ServerProperties.rtuAddrEnd = rVo.rtuAddrEnd; ServerProperties.tcpServerIp = rVo.tcpServerIp; ServerProperties.tcpServerPort = rVo.tcpServerPort; ServerProperties.sendInterval = rVo.sendInterval ; ServerProperties.heartbeatTimes = rVo.heartbeatTimes ; ServerProperties.sendTimes = rVo.sendTimes ; log.info("得到配置成功"); log.info(" 开始RtuAddr=" + ServerProperties.rtuAddrStart); log.info(" 截止RtuAddr=" + ServerProperties.rtuAddrEnd); log.info(" mwAccept服务IP=" + ServerProperties.tcpServerIp); log.info(" mwAccept服务端口=" + ServerProperties.tcpServerPort); log.info(" 发送数据间隔=" + ServerProperties.sendTimes); log.info(" 每轮次发送心跳次数=" + ServerProperties.heartbeatTimes); log.info(" 每RTU上报数据轮次=" + ServerProperties.sendTimes); break ; }else{ log.error("rmi得到配置失败:json转ResConfigVo为null"); error = true ; } }else{ log.error("rmi得到配置失败:服务端返回ResConfigVo为null"); error = true ; } }else{ log.error("rmi得到配置失败:服务端返回错误:" + rspVo.errorInfo); error = true ; } }else{ log.error("rmi得到配置失败:服务端返回的RmiResponseVo为null"); error = true ; } }else{ log.error("rmi得到配置失败:服务端返回json为null"); error = true ; } } catch (Exception e) { log.error("rmi得到配置失败" + e.getMessage()); error = true ; continue ; } } if(!error){ getStart(frmWork) ; } } //从mwTestServer得到开始上报数据的请允许 private void getStart(RmiFrameWork frmWork){ log.info("等待服务端允许上报数据"); while(true){ try { Thread.sleep(100L); RmiRequestVo rqVo = new RmiRequestVo() ; rqVo.id = id ; rqVo.token = token ; rqVo.code = Code.cd3 ; String json = rqVo.toJson() ; Object rObj = frmWork.syncRequest(json) ; if(rObj != null){ RmiResponseVo rspVo = RmiResponseVo.jsonToObject(String.valueOf(rObj), ResStartVo.class) ; if(rspVo != null){ if(rspVo.success){ if(rspVo.obj != null && rspVo.obj instanceof ResStartVo){ ResStartVo rVo = (ResStartVo)rspVo.obj ; if(rVo != null){ if(rVo.start){ ServerProperties.startWork = true ; log.info("允许上报数据工作了( ^_^ )"); break ; } }else{ log.error("rmi请求启动失败:json转ResStartVo为null"); } }else{ log.error("rmi请求启动失败:服务端返回ResStartVo为null"); } }else{ log.error("rmi请求启动失败:服务端返回错误:" + rspVo.errorInfo); } }else{ log.error("rmi请求启动失败:服务端返回的RmiResponseVo为null"); } }else{ log.error("rmi请求启动失败:服务端返回json为null"); } } catch (Exception e) { log.error("rmi请求启动失败" + e.getMessage()); continue ; } } } }