Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Incorporate simgrid-java in simgrid-java/.
[simgrid.git] / simgrid-java / examples / kademlia / Answer.java
1 /* Copyright (c) 2012. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 package kademlia;
8
9 import java.util.ArrayList;
10 import java.util.Collections;
11
12 /**
13  * Answer to a "FIND_NODE" query. Contains the nodes closest to
14  * an id given.
15  */
16 public class Answer {
17         /**
18          * Id of the node we're trying to find
19          */
20         private int destinationId;
21         /**
22          * Closest nodes in the answer.
23          */
24         private ArrayList<Contact> nodes;
25         
26         /**
27          * Constructor
28          */
29         public Answer(int destinationId) {
30                 this.destinationId = destinationId;
31                 nodes = new ArrayList<Contact>();
32         }
33         /**
34          * Returns the destination id
35          */
36         int getDestinationId() {
37                 return destinationId;
38         }
39         /**
40          * Returns the list of the nodes in the answer
41          */
42         ArrayList<Contact> getNodes() {
43                 return nodes;
44         }
45         /**
46          * Returns the answer array size
47          */
48         int size() {
49                 return nodes.size();
50         }
51         /**
52          * Remove an element from the answer.
53          */
54         public void remove(int index) {
55                 nodes.remove(index);
56         }
57         /**
58          * Add a contact to the answer.
59          */
60         public void add(Contact contact) {
61                 nodes.add(contact);
62         }
63         /**
64          * Merge the contents of this answer with another answer
65          */
66         public int merge(Answer answer) {
67                 int nbAdded = 0;
68                 
69                 for (Contact c: answer.getNodes()) {
70                         if (!nodes.contains(c)) {
71                                 nbAdded++;
72                                 nodes.add(c);
73                         }
74                 }
75                 Collections.sort(nodes);
76                 //Trim the list
77                 while (answer.size() > Common.BUCKET_SIZE) {
78                         answer.remove(answer.size() - 1);
79                 }
80                 return nbAdded;
81         }
82         /**
83          * Returns if the destination has been found
84          */
85         public boolean destinationFound() {
86                 if (nodes.size() < 1) {
87                         return false;
88                 }
89                 Contact tail = nodes.get(0);
90                 return tail.getDistance() == 0;
91         }
92         @Override
93         public String toString() {
94                 return "Answer [destinationId=" + destinationId + ", nodes=" + nodes
95                                 + "]";
96         }
97         
98 }