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
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<String, String> id2TokenMap = new HashMap<>() ;
 
    public static Map<String, MwTestClientStatus> token2ClientMap = new HashMap<>() ;
    
    public static List<Object[]> 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 ;
            }
        }
    }
 
 
 
}