--- /dev/null
+package example.symphony;\r
+\r
+import peersim.config.Configuration;\r
+import peersim.core.CommonState;\r
+import peersim.core.Control;\r
+import peersim.core.Network;\r
+import peersim.core.Node;\r
+\r
+/**\r
+ *\r
+ * @author Andrea Esposito <and1989@gmail.com>\r
+ */\r
+public class LeaveTest implements Control {\r
+\r
+ private static final String PAR_NETMANAGER = "symphonynetworkmanager";\r
+ private static final String PAR_NUMBER_LEAVES = "n";\r
+ private static final String PAR_MIN_SIZE = "minsizeOnline";\r
+ private static final String PAR_WAIT_TARGET_SIZE = "waitTargetSizeToStart";\r
+ private final int networkManagerID;\r
+ private final double n;\r
+ private final int minSizeNetwork;\r
+ private int targetSize;\r
+\r
+ public LeaveTest(String prefix) {\r
+ networkManagerID = Configuration.getPid(prefix + "." + PAR_NETMANAGER);\r
+ double nAppo = Configuration.getDouble(prefix + "." + PAR_NUMBER_LEAVES);\r
+ if (!(nAppo > 0.0 && nAppo < 1.0)) {\r
+ n = (int) Math.round(nAppo);\r
+ } else {\r
+ n = nAppo;\r
+ }\r
+\r
+ minSizeNetwork = Configuration.getInt(prefix + "." + PAR_MIN_SIZE, -1);\r
+ targetSize = Configuration.getInt(prefix + "." + PAR_WAIT_TARGET_SIZE, -1);\r
+ }\r
+\r
+ public boolean execute() {\r
+\r
+ if (minSizeNetwork > 0) {\r
+\r
+ int onlineNode = 0;\r
+ AdapterIterableNetwork it = new AdapterIterableNetwork();\r
+ for (Node node : it) {\r
+ if (node.isUp()) {\r
+ onlineNode++;\r
+ }\r
+ }\r
+\r
+ if (targetSize <= 0 || targetSize <= onlineNode) {\r
+ targetSize = -1;\r
+\r
+ // verify if i have to remove an exact number of nodes or a percentage of them\r
+ int actualN = (int) (n < 1.0 ? Math.ceil(Network.size() * n) : n);\r
+\r
+ for (int i = 0; i < actualN && Network.size() > 0; i++) {\r
+ if (onlineNode > minSizeNetwork) {\r
+ Node leaveNode = Network.get(Math.abs(CommonState.r.nextInt()) % Network.size());\r
+\r
+ while (!leaveNode.isUp()) {\r
+ leaveNode = Network.get(Math.abs(CommonState.r.nextInt()) % Network.size());\r
+ }\r
+\r
+ SymphonyNetworkManager networkManager = (SymphonyNetworkManager) leaveNode.getProtocol(networkManagerID);\r
+\r
+ networkManager.leave(leaveNode);\r
+\r
+ onlineNode--;\r
+ } else {\r
+ break;\r
+ }\r
+ }\r
+ }\r
+ }\r
+\r
+ return false;\r
+ }\r
+}\r