1 package example.symphony;
\r
4 import peersim.config.Configuration;
\r
5 import peersim.core.Network;
\r
6 import peersim.core.Node;
\r
10 * @author Andrea Esposito <and1989@gmail.com>
\r
12 public class SymphonyEstimationProtocol implements NetworkSizeEstimatorProtocolInterface {
\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
19 public SymphonyEstimationProtocol(String prefix) {
\r
20 symphonyID = Configuration.getPid(prefix + "." + PAR_SYMPHONY);
\r
21 s = Configuration.getInt(prefix + "." + PAR_S, -1);
\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
29 public int getNetworkSize(Node node) {
\r
30 SymphonyProtocol symphony = (SymphonyProtocol) node.getProtocol(symphonyID);
\r
32 // If the node is not yet inside the ring i return the minimum size (2 nodes)
\r
33 if (!symphony.isBootstrapped()) {
\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
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
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
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
58 rightList.removeAll(offlineNeighbors);
\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
65 // Calculate the variables to estimated the network size
\r
69 List<Tuple<Node, SymphonyProtocol.BootstrapStatus>> appoList[] = new List[2];
\r
70 appoList[0] = leftList;
\r
71 appoList[1] = rightList;
\r
73 double[] appoPrecIdentifier = new double[]{symphony.getIdentifier(), symphony.getIdentifier()};
\r
74 int[] appoCurrentIndex = new int[]{0, 0};
\r
76 int realS = (int) (s <= 0 ? Math.log(Network.size() / Math.log(2)) : s);
\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
84 double currentIdentifier = ((SymphonyProtocol) currentList.get(currentIndex).x.getProtocol(symphonyID)).getIdentifier();
\r
86 appoPrecIdentifier[i % 2] = currentIdentifier;
\r
87 appoCurrentIndex[i % 2] = appoCurrentIndex[i % 2] + 1;
\r
89 double distance = Math.abs(currentIdentifier - precIdentifier);
\r
90 Xs += Math.min(distance, 1.0 - distance);
\r
92 } catch (IndexOutOfBoundsException ex) {
\r
93 // Simply i skip the counting
\r
97 int ret = Xs == 0 ? 0 : (int) Math.round(countS / Xs);
\r
103 public Object clone() {
\r