liurunyu
2023-11-29 f2df44d7b3865cafe86baa48e19d1df0c0723702
pipIrr-platform/pipIrr-mw/pipIrr-mwTest-client/src/main/java/com/dy/testClient/tcpClient/TcpClUnit.java
@@ -10,6 +10,10 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
public class TcpClUnit  implements UnitInterface {
    private static final Logger log = LogManager.getLogger(TcpClUnit.class) ;
@@ -20,8 +24,10 @@
    public static TcpClUnitConfigVo confVo ;
    private static ThreadPool.Pool pool ;
    private static Map<String, MyThreadJob> jobMap = new HashMap<>() ;
    private static Integer totalRunedClientCount = 0;
    private static Integer totalSendDataCount = 0;
    private static Integer totalOverClientCount = 0;
    private static Long startTime = 0L ;
@@ -69,12 +75,13 @@
                            startTime = System.currentTimeMillis() ;
                            for(Long addr = ServerProperties.rtuAddrStart; addr <= ServerProperties.rtuAddrEnd; addr++){
                                totalRunedClientCount++ ;
                                startClient(addr) ;
                                createImitate(addr) ;
                            }
                            startJob() ;
                            while(true){
                                if(totalOverClientCount.longValue() >= totalRunedClientCount.longValue()){
                                    Long seconds = (System.currentTimeMillis() - startTime)/1000 ;
                                    RmiClUnit.getInstance().reportHadReportOver(seconds) ;
                                    RmiClUnit.getInstance().allOver(seconds) ;
                                    System.out.println("共用时" + seconds + "秒");
                                    break ;
                                }else{
@@ -91,20 +98,48 @@
        }).start();
    }
    private void startClient(Long rtuAddr){
        try {
            pool.putJob(new MyThreadJob("" + rtuAddr));
        } catch (Exception e) {
            log.error("TcpClUnit.startClient() ", e);
        }
    /**
     * 创建RTU模拟MyThreadJob
     * @param rtuAddr rtu地址
     */
    private void createImitate(Long rtuAddr){
        jobMap.put("" + rtuAddr, new MyThreadJob("" + rtuAddr, ServerProperties.tcpServerIp, ServerProperties.tcpServerPort)) ;
    }
    private void startJob(){
        new Thread(new Runnable(){
            @Override
            public void run() {
                try {
                    Thread.sleep(1000L);
                    while(true){
                        Collection<MyThreadJob> collection = jobMap.values() ;
                        for(MyThreadJob job : collection){
                            pool.putJob(job);
                        }
                        Thread.sleep(ServerProperties.sendInterval * 1000);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
    public static synchronized void clientSendData(){
        totalSendDataCount++;
        if(totalSendDataCount % 100 == 0){
            RmiClUnit.getInstance().reportHadReportCount(totalSendDataCount);
            System.out.println("已经发送" + totalSendDataCount + "条数据");
        }
    }
    public static synchronized void clientOver(){
        totalOverClientCount++;
        if(totalOverClientCount % 100 == 0){
            RmiClUnit.getInstance().reportHadReportCount(totalOverClientCount);
            System.out.println("已经发送" + totalOverClientCount * + "条数据");
            RmiClUnit.getInstance().reportHadReportOver(totalOverClientCount);
            System.out.println("已有" + totalOverClientCount + "完成了任务");
        }
    }
}