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 pairs = options.getPairs(); if (CollectionUtils.isEmpty(pairs)) { return; } StationPair one = pairs.get(0); if (one == null || one.getInList() == null) { return; } // in port count int inSize = one.getInList().size(); if (inSize < 2) { return; } // circle count Integer count = options.getMaxIterCount(); if (count == null || count < 1) { return; } Integer step = options.getStep(); Integer endAttenuation = options.getEndAttenuation(); if (step == null || step <= 0) { return; } int dynamicCont = endAttenuation / step; for (int i = 0; i < count; i++) { // in port count == 2 if (inSize == 2) { for (int k = 0; k <= dynamicCont; k++) { int dynamic = step * k; List 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 { // in port count > 2 int rowCount = 1 + inSize * dynamicCont; // dynamic attenuation matrix int[][] attenuation = new int[rowCount][inSize]; // current dynamic in port index int current = 0; for (int k = 0; k < rowCount; k++) { // dynamic value 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; } } if (k != 0 && k % dynamicCont == 0) { current++; } } for (int x = 0; x < rowCount; x++) { List 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(); } } } }