New file |
| | |
| | | package com.dy.testServer; |
| | | |
| | | import java.io.Console; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | |
| | | import com.dy.testServer.console.CommandConsole; |
| | | import com.dy.testServer.forRmi.Manager; |
| | | import org.jdom2.Document; |
| | | |
| | | import com.dy.common.mw.UnitInterface; |
| | | import com.dy.common.mw.UnitCallbackInterface; |
| | | import com.dy.common.mw.channel.rmi.RmiConfigVo; |
| | | import com.dy.common.mw.channel.rmi.RmiUnit; |
| | | import com.dy.common.util.NumUtil; |
| | | import com.dy.common.util.ConfigXmlNoSpringboot; |
| | | |
| | | public class Server{ |
| | | |
| | | private ConfigXmlNoSpringboot conf = null ; |
| | | private Document doc = null ; |
| | | private boolean showStartInfo = false ; |
| | | |
| | | private String RmiSvUrl ; |
| | | |
| | | private final List<UnitInterface> units = new ArrayList<>() ; |
| | | |
| | | /** |
| | | * @param args 参数 |
| | | */ |
| | | public static void main(String[] args) { |
| | | new Server().startServer(args); |
| | | } |
| | | |
| | | /** |
| | | * 启动服务 |
| | | */ |
| | | public void startServer(String[] args){ |
| | | Server sv = new Server(); |
| | | long start = System.currentTimeMillis() ; |
| | | try { |
| | | sv.conf = new ConfigXmlNoSpringboot() ; |
| | | sv.doc = sv.conf.createDom(sv.getClass(), "config.xml") ; |
| | | //////////////// |
| | | //服务 配置 |
| | | sv.showStartInfo = sv.conf.getSetAttrBoolean(sv.doc, "config.server", "showStartInfo", null, null) ; |
| | | |
| | | sv.startUnits() ; |
| | | |
| | | String svName ; |
| | | try{ |
| | | svName = sv.conf.getSetAttrTxt(sv.doc, "config.server", "name", null, false, null) ; |
| | | }catch(Exception e){ |
| | | svName = "" ; |
| | | } |
| | | |
| | | System.out.println("OOOOOOOOOO OOOOOOOO OOOOOOOO") ; |
| | | System.out.println("@@@@@@@@@@@@@@@@#O $@@@@@@@@& @@@@@@@@#") ; |
| | | System.out.println("@@@@@@@@@@@@@@@@@@@# @@@@@@@@# $@@@@@@@@&") ; |
| | | System.out.println("@@@@@@@@@@@@@@@@@@@@@# #@@@@@@@@@@@@@@@@O") ; |
| | | System.out.println("@@@@@@@@@@@@@@@@@@@@@@@ &@@@@@@@@@@@@@@") ; |
| | | System.out.println("@@@@@@$ $@@@@@@@@@& O@@@@@@@@@@@#") ; |
| | | System.out.println("@@@@@@$ @@@@@@@@@ @@@@@@@@@& " + svName + "mwTestSv " ) ; |
| | | System.out.println("@@@@@@$ @@@@@@@@@ &@@@@@@@@") ; |
| | | System.out.println("@@@@@@$ O@@@@@@@@@ &@@@@@@@@") ; |
| | | if(sv.RmiSvUrl != null){ |
| | | System.out.println("@@@@@@$ #@@@@@@@@@$ &@@@@@@@@ RmiSv " + sv.RmiSvUrl ) ; |
| | | }else{ |
| | | System.out.println("@@@@@@$ #@@@@@@@@@$ &@@@@@@@@" ) ; |
| | | } |
| | | System.out.println("@@@@@@@@@@@@@@@@@@@@@@# &@@@@@@@@ Runing in standalone mode" ) ; |
| | | System.out.println("@@@@@@@@@@@@@@@@@@@@@& &@@@@@@@@ Startup in " + (System.currentTimeMillis() - start) + " MS" ) ; |
| | | System.out.println("@@@@@@@@@@@@@@@@@@@# &@@@@@@@@") ; |
| | | System.out.println("@@@@@@@@@@@@@@@@#O &@@@@@@@@") ; |
| | | |
| | | |
| | | |
| | | Console console = System.console(); |
| | | if (console == null) { |
| | | throw new IllegalStateException("严重错误,未能得到控制台对象!"); |
| | | } |
| | | CommandConsole mwConsole = new CommandConsole(console) ; |
| | | mwConsole.init(args); |
| | | boolean exit = mwConsole.clientConsole() ; |
| | | if(exit){ |
| | | System.exit(-1); |
| | | } |
| | | }catch(Exception e){ |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | private void startUnits(){ |
| | | try { |
| | | /////////////// |
| | | //基本配置 |
| | | String txt = conf.getSetAttrTxt(doc, "config.base", "rtuAddrStart", null, false, null) ; |
| | | if(txt == null || txt.trim().equals("")){ |
| | | throw new Exception("元素config.base的属性rtuAddrStart的值为空字符串!"); |
| | | } |
| | | txt = txt.trim() ; |
| | | if(!NumUtil.isPlusIntNumber(txt)){ |
| | | throw new Exception("元素config.base的属性rtuAddrStart的值必须为数值!"); |
| | | } |
| | | if(txt.startsWith("0")){ |
| | | throw new Exception("元素config.base的属性rtuAddrStart的数值不能以0开头!"); |
| | | } |
| | | if(!(txt.length() == 10 || txt.length() == 12)){ |
| | | throw new Exception("元素config.base的属性rtuAddrStart的数值长度必须为10位或12位!"); |
| | | } |
| | | ServerProperties.rtuAddrStart = Long.valueOf(txt) ; |
| | | |
| | | txt = conf.getSetAttrTxt(doc, "config.base", "rtuAddrEnd", null, false, null) ; |
| | | if(txt == null || txt.trim().equals("")){ |
| | | throw new Exception("元素config.base的属性rtuAddrEnd的值为空字符串!"); |
| | | } |
| | | txt = txt.trim() ; |
| | | if(!NumUtil.isPlusIntNumber(txt)){ |
| | | throw new Exception("元素config.base的属性rtuAddrEnd的值必须为数值!"); |
| | | } |
| | | if(txt.startsWith("0")){ |
| | | throw new Exception("元素config.base的属性rtuAddrEnd的数值不能以0开头!"); |
| | | } |
| | | if(!(txt.length() == 10 || txt.length() == 12)){ |
| | | throw new Exception("元素config.base的属性rtuAddrEnd的数值长度必须为10位或12位!"); |
| | | } |
| | | ServerProperties.rtuAddrEnd = Long.valueOf(txt) ; |
| | | if(ServerProperties.rtuAddrEnd <= ServerProperties.rtuAddrStart){ |
| | | throw new Exception("元素config.base的属性rtuAddrEnd的数值必须大于rtuAddrStart!"); |
| | | } |
| | | |
| | | ServerProperties.rtuAddrPerMwTest = conf.getSetAttrPlusInt(doc, "config.base", "rtuAddrPerMwTest", null, 1, null, null) ; |
| | | if(ServerProperties.rtuAddrPerMwTest > ServerProperties.rtuAddrEnd - ServerProperties.rtuAddrStart){ |
| | | throw new Exception("元素config.base的属性rtuAddrPerMwTest的数值配置不正确!"); |
| | | } |
| | | ServerProperties.tcpServerIp = conf.getSetAttrTxt(doc, "config.base", "tcpServerIp", null, false, null) ; |
| | | ServerProperties.tcpServerPort = conf.getSetAttrPlusInt(doc, "config.base", "tcpServerPort", null, 1, 65535, null) ; |
| | | ServerProperties.sendInterval = conf.getSetAttrPlusInt(doc, "config.base", "sendInterval", null, 1, null, null) ; |
| | | ServerProperties.heartbeatTimes = conf.getSetAttrPlusInt(doc, "config.base", "heartbeatTimes", null, 1, null, null) ; |
| | | ServerProperties.sendTimes = conf.getSetAttrPlusInt(doc, "config.base", "sendTimes", null, 1, null, null) ; |
| | | ServerProperties.sendOverThenCloseConnect = conf.getSetAttrPlusInt(doc, "config.base", "sendOverThenCloseConnect", null, 0, 1, null) ; |
| | | |
| | | |
| | | ///////////////// |
| | | //RMI模块 |
| | | Manager.init(); |
| | | RmiConfigVo rmiVo = new RmiConfigVo(); |
| | | rmiVo.enable = conf.getSetAttrBoolean(doc, "config.rmi", "enable", null, null) ; |
| | | if(rmiVo.enable){ |
| | | rmiVo.port = conf.getSetAttrPlusInt(doc, "config.rmi", "port", null, 100, 65535, null); |
| | | rmiVo.context = conf.getSetAttrTxt(doc, "config.rmi", "context", null, false, null); |
| | | rmiVo.showStartInfo = showStartInfo ; |
| | | AdapterImp_RmiUnit rmiAdap = new AdapterImp_RmiUnit(); |
| | | rmiAdap.setConfig(rmiVo); |
| | | RmiUnit rmiUnit = RmiUnit.getInstance(); |
| | | rmiUnit.setAdapter(rmiAdap); |
| | | rmiUnit.start(new UnitCallbackInterface(){ |
| | | @Override |
| | | public void call(Object obj) { |
| | | } |
| | | }); |
| | | RmiSvUrl = "[ip]:" + rmiVo.port + "/" + rmiVo.context ; |
| | | units.add(rmiUnit) ; |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | } |