HandoverExperiment.java 5.46 KB
package com.example.mina.client.experiment;

import com.example.mina.client.base.Experiment;
import com.example.mina.client.base.ExperimentOptions;
import com.example.mina.client.base.MatrixClient;
import com.example.mina.client.base.MatrixCommand;
import com.example.mina.client.base.MatrixConstants;
import com.example.mina.client.base.StationPair;
import java.util.ArrayList;
import java.util.List;
import org.springframework.util.CollectionUtils;

public class HandoverExperiment extends Experiment {

    public HandoverExperiment(MatrixClient client, ExperimentOptions options) {
        super(client, options);
    }

    @Override
    public String getExperimentName() {
        return MatrixConstants.EXPERIMENT_HANDOVER;
    }

    @Override
    public void schedule() {
        List<StationPair> pairs = options.getPairs();
        if (CollectionUtils.isEmpty(pairs)) {
            return;
        }
        StationPair one = pairs.get(0);
        if (one == null || one.getInList() == null) {
            return;
        }
        // 输入端口的个数
        int inSize = one.getInList().size();
        if (inSize < 2) {
            return;
        }
        // 循环次数
        Integer count = options.getMaxIterCount();
        if (count == null || count < 1) {
            return;
        }
        Integer step = options.getStep();
        Integer endAttenuation = options.getEndAttenuation();
        int dynamicCont = endAttenuation / step;
        if (step == null || step <= 0) {
            return;
        }
        for (int i = 0; i < count; i++) {
            // 输入端口只有 2 个
            if (inSize == 2) {
                for (int k = 0; k <= dynamicCont; k++) {
                    int dynamic = step * k;
                    List<MatrixCommand> commandList = new ArrayList<>();
                    for (StationPair pair : pairs) {
                        Integer out = pair.getOut();
                        Integer inOne = pair.getInList().get(0);
                        Integer inTwo = pair.getInList().get(1);
                        MatrixCommand commandOne = MatrixCommand.builder().command(MatrixConstants.COMMAND_SET_ATTN)
                                .col(inOne).row(out).attn(dynamic).build();
                        MatrixCommand commandTwo = MatrixCommand.builder().command(MatrixConstants.COMMAND_SET_ATTN)
                                .col(inTwo).row(out).attn(endAttenuation - dynamic).build();
                        commandList.add(commandOne);
                        commandList.add(commandTwo);
                    }
                    commandList.forEach(this::sendCommand);
                    commandList.clear();
                    try {
                        Thread.sleep(options.getPeriod() * 1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            } else {
                // 输入端口大于 2
                // 行数
                int rowCount = 1 + inSize * dynamicCont;
                // 动态衰减值 矩阵
                int[][] attenuation = new int[rowCount][inSize];
                // 当前动态输入端口指针
                int current = 0;
                for (int k = 0; k < rowCount; k++) {
                    // 动态衰减值
                    int dynamic = step * (k != 0 && k % dynamicCont == 0 ? dynamicCont : k % dynamicCont);
                    for (int j = 0; j < inSize; j++) {
                        if (j == current && j != inSize - 1) {
                            attenuation[k][j++] = dynamic;
                            attenuation[k][j] = endAttenuation - dynamic;
                        } else if (j == current && j == inSize - 1) {
                            attenuation[k][j] = dynamic;
                            attenuation[k][0] = endAttenuation - dynamic;
                        } else {
                            attenuation[k][j] = endAttenuation;
                        }
                    }
                    // 一轮衰减完成后,当前动态输入端口指针 加 1
                    if (k != 0 && k % dynamicCont == 0) {
                        current++;
                    }
                }
                for (int x = 0; x < rowCount; x++) {
                    // 当前行 --> 时间点,需要发送的 命令
                    List<MatrixCommand> commandList = new ArrayList<>();
                    for (int y = 0; y < inSize; y++) {
                        int att = attenuation[x][y];
                        for (StationPair pair : pairs) {
                            Integer out = pair.getOut();
                            Integer in = pair.getInList().get(y);
                            commandList.add(MatrixCommand.builder().command(MatrixConstants.COMMAND_SET_ATTN)
                                    .col(in).row(out).attn(att).build());
                        }
                    }
                    commandList.forEach(this::sendCommand);
                    // 发送完成后,清空集合。
                    commandList.clear();
                    try {
                        Thread.sleep(options.getPeriod() * 1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
            try {
                Thread.sleep(options.getPause() * 1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}