1 package example.symphony;
\r
3 import peersim.config.Configuration;
\r
4 import peersim.core.CommonState;
\r
5 import peersim.core.Control;
\r
6 import peersim.core.Network;
\r
7 import peersim.core.Node;
\r
11 * @author Andrea Esposito <and1989@gmail.com>
\r
13 public class LeaveTest implements Control {
\r
15 private static final String PAR_NETMANAGER = "symphonynetworkmanager";
\r
16 private static final String PAR_NUMBER_LEAVES = "n";
\r
17 private static final String PAR_MIN_SIZE = "minsizeOnline";
\r
18 private static final String PAR_WAIT_TARGET_SIZE = "waitTargetSizeToStart";
\r
19 private final int networkManagerID;
\r
20 private final double n;
\r
21 private final int minSizeNetwork;
\r
22 private int targetSize;
\r
24 public LeaveTest(String prefix) {
\r
25 networkManagerID = Configuration.getPid(prefix + "." + PAR_NETMANAGER);
\r
26 double nAppo = Configuration.getDouble(prefix + "." + PAR_NUMBER_LEAVES);
\r
27 if (!(nAppo > 0.0 && nAppo < 1.0)) {
\r
28 n = (int) Math.round(nAppo);
\r
33 minSizeNetwork = Configuration.getInt(prefix + "." + PAR_MIN_SIZE, -1);
\r
34 targetSize = Configuration.getInt(prefix + "." + PAR_WAIT_TARGET_SIZE, -1);
\r
37 public boolean execute() {
\r
39 if (minSizeNetwork > 0) {
\r
42 AdapterIterableNetwork it = new AdapterIterableNetwork();
\r
43 for (Node node : it) {
\r
49 if (targetSize <= 0 || targetSize <= onlineNode) {
\r
52 // verify if i have to remove an exact number of nodes or a percentage of them
\r
53 int actualN = (int) (n < 1.0 ? Math.ceil(Network.size() * n) : n);
\r
55 for (int i = 0; i < actualN && Network.size() > 0; i++) {
\r
56 if (onlineNode > minSizeNetwork) {
\r
57 Node leaveNode = Network.get(Math.abs(CommonState.r.nextInt()) % Network.size());
\r
59 while (!leaveNode.isUp()) {
\r
60 leaveNode = Network.get(Math.abs(CommonState.r.nextInt()) % Network.size());
\r
63 SymphonyNetworkManager networkManager = (SymphonyNetworkManager) leaveNode.getProtocol(networkManagerID);
\r
65 networkManager.leave(leaveNode);
\r