1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
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.UnitStartedCallbackInterface;
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 UnitStartedCallbackInterface(){
                    @Override
                    public void call(Object obj) {
                    }
                });
                RmiSvUrl = "[ip]:" + rmiVo.port + "/" + rmiVo.context ;
                units.add(rmiUnit) ;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }        
    }
    
}