Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
peersimgrid release 1.0
[simgrid.git] / contrib / psg / src / example / symphony / SymphonyEstimationProtocol.java
1 package example.symphony;\r
2 \r
3 import java.util.*;\r
4 import peersim.config.Configuration;\r
5 import peersim.core.Network;\r
6 import peersim.core.Node;\r
7 \r
8 /**\r
9  *\r
10  * @author Andrea Esposito <and1989@gmail.com>\r
11  */\r
12 public class SymphonyEstimationProtocol implements NetworkSizeEstimatorProtocolInterface {\r
13 \r
14     private static final String PAR_SYMPHONY = "symphony";\r
15     private static final String PAR_S = "s";\r
16     private final int symphonyID;\r
17     private final int s;\r
18 \r
19     public SymphonyEstimationProtocol(String prefix) {\r
20         symphonyID = Configuration.getPid(prefix + "." + PAR_SYMPHONY);\r
21         s = Configuration.getInt(prefix + "." + PAR_S, -1);\r
22     }\r
23 \r
24     /**\r
25      * Implementation of the estimated network size as a variant of the paper one. It use anyway the\r
26      * idea to calculate the size from the length segments but without exchanging the information\r
27      * with the neighbours instead using only the local information.\r
28      */\r
29     public int getNetworkSize(Node node) {\r
30         SymphonyProtocol symphony = (SymphonyProtocol) node.getProtocol(symphonyID);\r
31 \r
32         // If the node is not yet inside the ring i return the minimum size (2 nodes)\r
33         if (!symphony.isBootstrapped()) {\r
34             return 2;\r
35         }\r
36 \r
37         /*\r
38          * I clone the short range links views (wrapped into an ArrayList because the returned list\r
39          * 'Arrays.asList doesn't support the "removeAll" method or better its size is fixed)\r
40          */\r
41         ArrayList<Tuple<Node, SymphonyProtocol.BootstrapStatus>> leftList = new ArrayList<Tuple<Node, SymphonyProtocol.BootstrapStatus>>(Arrays.asList((Tuple<Node, SymphonyProtocol.BootstrapStatus>[]) symphony.leftShortRangeLinks.toArray(new Tuple[0])));\r
42         ArrayList<Tuple<Node, SymphonyProtocol.BootstrapStatus>> rightList = new ArrayList<Tuple<Node, SymphonyProtocol.BootstrapStatus>>(Arrays.asList((Tuple<Node, SymphonyProtocol.BootstrapStatus>[]) symphony.rightShortRangeLinks.toArray(new Tuple[0])));\r
43 \r
44         // Remove the neighbours that are offline\r
45         LinkedList<Tuple<Node, SymphonyProtocol.BootstrapStatus>> offlineNeighbors = new LinkedList<Tuple<Node, SymphonyProtocol.BootstrapStatus>>();\r
46         for (Tuple<Node, SymphonyProtocol.BootstrapStatus> tuple : leftList) {\r
47             if (tuple.y == SymphonyProtocol.BootstrapStatus.OFFLINE) {\r
48                 offlineNeighbors.add(tuple);\r
49             }\r
50         }\r
51         leftList.removeAll(offlineNeighbors);\r
52         offlineNeighbors.clear();\r
53         for (Tuple<Node, SymphonyProtocol.BootstrapStatus> tuple : rightList) {\r
54             if (tuple.y == SymphonyProtocol.BootstrapStatus.OFFLINE) {\r
55                 offlineNeighbors.add(tuple);\r
56             }\r
57         }\r
58         rightList.removeAll(offlineNeighbors);\r
59 \r
60         // Sort the neighbours based on the distance from me\r
61         Comparator<Tuple<Node, SymphonyProtocol.BootstrapStatus>> comparator = new AdapterSymphonyNodeComparator(new SymphonyNodeComparator(symphonyID, symphony.getIdentifier()));\r
62         Collections.sort(leftList, comparator);\r
63         Collections.sort(rightList, comparator);\r
64 \r
65         // Calculate the variables to estimated the network size\r
66         double Xs = 0;\r
67         int countS = 0;\r
68 \r
69         List<Tuple<Node, SymphonyProtocol.BootstrapStatus>> appoList[] = new List[2];\r
70         appoList[0] = leftList;\r
71         appoList[1] = rightList;\r
72 \r
73         double[] appoPrecIdentifier = new double[]{symphony.getIdentifier(), symphony.getIdentifier()};\r
74         int[] appoCurrentIndex = new int[]{0, 0};\r
75 \r
76         int realS = (int) (s <= 0 ? Math.log(Network.size() / Math.log(2)) : s);\r
77 \r
78         for (int i = 0; i < realS; i++) {\r
79             double precIdentifier = appoPrecIdentifier[i % 2];\r
80             int currentIndex = appoCurrentIndex[i % 2];\r
81             List<Tuple<Node, SymphonyProtocol.BootstrapStatus>> currentList = appoList[i % 2];\r
82 \r
83             try {\r
84                 double currentIdentifier = ((SymphonyProtocol) currentList.get(currentIndex).x.getProtocol(symphonyID)).getIdentifier();\r
85 \r
86                 appoPrecIdentifier[i % 2] = currentIdentifier;\r
87                 appoCurrentIndex[i % 2] = appoCurrentIndex[i % 2] + 1;\r
88 \r
89                 double distance = Math.abs(currentIdentifier - precIdentifier);\r
90                 Xs += Math.min(distance, 1.0 - distance);\r
91                 countS++;\r
92             } catch (IndexOutOfBoundsException ex) {\r
93                 // Simply i skip the counting\r
94             }\r
95         }\r
96 \r
97         int ret = Xs == 0 ? 0 : (int) Math.round(countS / Xs);\r
98 \r
99         return ret;\r
100     }\r
101 \r
102     @Override\r
103     public Object clone() {\r
104         return this;\r
105     }\r
106 }\r