HandoverExperiment.java
5.19 KB
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
package com.example.mina.client.experiment;
import com.example.mina.client.base.Experiment;
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<MatrixClient, HandOverExperimentOptions> {
public HandoverExperiment(MatrixClient client, HandOverExperimentOptions 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;
}
// in port count
int inSize = one.getInList().size();
if (inSize < 2) {
return;
}
// circle count
Integer count = options.getIterations();
if (count == null || count < 1) {
return;
}
Integer step = options.getStep();
Integer endAttenuation = options.getEndAttn();
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<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 {
// 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<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();
}
}
}
}