liurunyu
2024-09-07 9ad6b0179be4d351105f1507779d4f41a3a41953
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
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.UnitCallbackInterface;
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(UnitCallbackInterface callback) throws Exception {
        System.out.println("Rmi Client模块成功启动");
        this.doStart();
        callback.call(null) ;
    }
 
    @Override
    public void stop(UnitCallbackInterface 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(){
        System.setProperty("java.rmi.server.hostname", confVo.svUrl) ;
        System.out.println("Rmi建立连接请求服务端:" + confVo.svUrl + ":" + confVo.svPort + "/" + confVo.svContext);
        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.cd5;
            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.cd6;
            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.cd7;
            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 ;
                                    ServerProperties.sendOverThenCloseConnect = rVo.sendOverThenCloseConnect ;
                                    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);
                                    log.info("    发送完数据后,是否关闭TCP连接(1是,0否)=" + ServerProperties.sendOverThenCloseConnect);
                                    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){
            getStartTcpConnect(frmWork) ;
        }
    }
    
    //从mwTestServer得到开始TCP连接的请允许
    private void getStartTcpConnect(RmiFrameWork frmWork){
        log.info("等待服务端允许网络连接");
        boolean error = false ;
        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), ResStartTcpConnectVo.class) ;
                    if(rspVo != null){
                        if(rspVo.success){
                            if(rspVo.obj != null && rspVo.obj instanceof ResStartTcpConnectVo){
                                ResStartTcpConnectVo rVo = (ResStartTcpConnectVo)rspVo.obj ;
                                if(rVo != null){
                                    if(rVo.start){
                                        ServerProperties.startTcpConnectWork = true ;
                                        log.info("允许TCP网络连接了( ^_^ )");
                                        error = false ;
                                        break ;
                                    }
                                }else{
                                    error = true ;
                                    log.error("rmi请求TCP网络连接失败:json转ResStartTcpConnectVo为null");
                                }
                            }else{
                                error = true ;
                                log.error("rmi请求TCP网络连接失败:服务端返回ResStartTcpConnectVo为null");
                            }
                        }else{
                            error = true ;
                            log.error("rmi请求TCP网络连接失败:服务端返回错误:" + rspVo.errorInfo);
                        }
                    }else{
                        error = true ;
                        log.error("rmi请求TCP网络连接失败:服务端返回的RmiResponseVo为null");
                    }
                }else{
                    error = true ;
                    log.error("rmi请求TCP网络连接失败:服务端返回json为null");
                }
            } catch (Exception e) {
                error = true ;
                log.error("rmi请求TCP网络连接失败" + e.getMessage());
                continue ;
            }
        }
        if(!error){
            getStartRtuReport(frmWork) ;
        }
    }
 
    //从mwTestServer得到开始RTU上报数据的请允许
    private void getStartRtuReport(RmiFrameWork frmWork){
        log.info("等待服务端允许上报数据");
        while(true){
            try {
                Thread.sleep(100L);
                RmiRequestVo rqVo = new RmiRequestVo() ;
                rqVo.id = id ;
                rqVo.token = token ;
                rqVo.code = Code.cd4 ;
                String json = rqVo.toJson() ;
                Object rObj = frmWork.syncRequest(json) ;
                if(rObj != null){
                    RmiResponseVo rspVo = RmiResponseVo.jsonToObject(String.valueOf(rObj), ResStartRtuReportVo.class) ;
                    if(rspVo != null){
                        if(rspVo.success){
                            if(rspVo.obj != null && rspVo.obj instanceof ResStartRtuReportVo){
                                ResStartRtuReportVo rVo = (ResStartRtuReportVo)rspVo.obj ;
                                if(rVo != null){
                                    if(rVo.report){
                                        ServerProperties.startRtuReportWork = true ;
                                        log.info("允许RTU上报数据工作了( ^_^ )");
                                        break ;
                                    }
                                }else{
                                    log.error("rmi请求Rtu上报数据失败:json转ResStartRtuReportVo为null");
                                }
                            }else{
                                log.error("rmi请求Rtu上报数据失败:服务端返回ResStartRtuReportVo为null");
                            }
                        }else{
                            log.error("rmi请求Rtu上报数据失败:服务端返回错误:" + rspVo.errorInfo);
                        }
                    }else{
                        log.error("rmi请求Rtu上报数据失败:服务端返回的RmiResponseVo为null");
                    }
                }else{
                    log.error("rmi请求Rtu上报数据失败:服务端返回json为null");
                }
            } catch (Exception e) {
                log.error("rmi请求Rtu上报数据失败" + e.getMessage());
                continue ;
            }
        }
    }
 
}