1 /* Copyright (c) 2012-2014. The SimGrid Team.
2 * All rights reserved. */
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. */
9 import org.simgrid.msg.Host;
11 import org.simgrid.msg.Comm;
12 import org.simgrid.msg.Msg;
13 import org.simgrid.msg.MsgException;
14 import org.simgrid.msg.Process;
15 import org.simgrid.msg.Task;
17 * Main class of the simulation, contains the logic of a node.
19 public class Node extends Process {
27 protected RoutingTable table;
31 protected int deadline;
33 * FIND_NODE which have succeeded.
35 protected int findNodeSuccedded = 0;
37 * FIND_NODE which have failed
39 protected int findNodeFailed = 0;
43 public Node(Host host, String name, String[]args) {
44 super(host,name,args);
48 public void main(String[] args) throws MsgException {
49 //Check the number of arguments.
50 if (args.length != 2 && args.length != 3) {
51 Msg.info("Wrong argument count.");
54 this.id = Integer.valueOf(args[0]);
55 this.table = new RoutingTable(this.id);
57 if (args.length == 3) {
58 this.deadline = Integer.valueOf(args[2]).intValue();
59 Msg.info("Hi, I'm going to join the network with the id " + id + "!");
60 if (joinNetwork(Integer.valueOf(args[1]))) {
64 Msg.info("I couldn't join the network :(");
68 this.deadline = Integer.valueOf(args[1]).intValue();
69 Msg.info("Hi, I'm going to create the network with the id " + id + "!");
70 table.update(this.id);
73 Msg.debug("I'm leaving the network");
74 Msg.debug("Here is my routing table:" + table);
79 public void mainLoop() {
80 double next_lookup_time = Msg.getClock() + Common.RANDOM_LOOKUP_INTERVAL;
81 while (Msg.getClock() < this.deadline) {
84 comm = Task.irecv(Integer.toString(id));
87 if (Msg.getClock() >= next_lookup_time) {
89 next_lookup_time += Common.RANDOM_LOOKUP_INTERVAL;
96 Task task = comm.getTask();
101 catch (Exception e) {
105 Msg.info(findNodeSuccedded + "/" + (findNodeSuccedded + findNodeFailed) + " FIND_NODE have succedded.");
108 * @brief Try to make the node join the network
109 * @param idKnown Id of someone we know in the system
111 public boolean joinNetwork(int idKnown) {
112 boolean answerGot = false;
113 double timeBegin = Msg.getClock();
114 Msg.debug("Joining the network knowing " + idKnown);
115 //Add ourselves and the node we know to our routing table
116 table.update(this.id);
117 table.update(idKnown);
118 //Send a "FIND_NODE" to the node we know.
119 sendFindNode(idKnown,this.id);
120 //Wait for the answer.
126 comm = Task.irecv(Integer.toString(id));
133 Task task = comm.getTask();
134 if (task instanceof FindNodeAnswerTask) {
136 //Retrieve the node list and ping them
137 FindNodeAnswerTask answerTask = (FindNodeAnswerTask)task;
138 Answer answer = answerTask.getAnswer();
141 if (answer.getDestinationId() == this.id) {
142 //Ping everyone in the list
143 for (Contact c : answer.getNodes()) {
144 table.update(c.getId());
156 catch (Exception ex) {
158 Msg.info("FIND_NODE failed");
160 } while (!answerGot && trials < Common.MAX_JOIN_TRIALS);
161 /* Second step: Send a FIND_NODE in a node in each bucket */
162 int bucketId = table.findBucket(idKnown).getId();
163 for (int i = 0; ((bucketId - i) > 0 ||
164 (bucketId + i) <= Common.IDENTIFIER_SIZE) &&
165 i < Common.JOIN_BUCKETS_QUERIES; i++) {
166 if (bucketId - i > 0) {
167 int idInBucket = table.getIdInPrefix(this.id,bucketId - i);
168 this.findNode(idInBucket,false);
170 if (bucketId + i <= Common.IDENTIFIER_SIZE) {
171 int idInBucket = table.getIdInPrefix(this.id,bucketId + i);
172 findNode(idInBucket,false);
175 Msg.debug("Time spent:" + (Msg.getClock() - timeBegin));
179 * Send a request to find a node in the node's routing table.
181 public boolean findNode(int destination, boolean counts) {
182 int queries, answers;
184 boolean destinationFound = false;
186 double timeBeginReceive;
187 double timeout, globalTimeout = Msg.getClock() + Common.FIND_NODE_GLOBAL_TIMEOUT;
188 //Build a list of the closest nodes we already know.
189 Answer nodeList = table.findClosest(destination);
190 Msg.verb("Doing a FIND_NODE on " + destination);
192 timeBeginReceive = Msg.getClock();
194 queries = this.sendFindNodeToBest(nodeList);
196 timeout = Msg.getClock() + Common.FIND_NODE_TIMEOUT;
201 comm = Task.irecv(Integer.toString(id));
207 Task task = comm.getTask();
208 if (task instanceof FindNodeAnswerTask) {
209 FindNodeAnswerTask answerTask = (FindNodeAnswerTask)task;
210 //Check if we received what we are looking for.
211 if (answerTask.getDestinationId() == destination) {
212 table.update(answerTask.getSenderId());
213 //Add the answer to our routing table
214 for (Contact c: answerTask.getAnswer().getNodes()) {
215 table.update(c.getId());
219 nodesAdded = nodeList.merge(answerTask.getAnswer());
221 /* If it's not our answer, we answer to the node that
222 * has queried us anyway
226 //Update the timeout if it's not our answer.
227 timeout += Msg.getClock() - timeBeginReceive;
228 timeBeginReceive = Msg.getClock();
233 timeout += Msg.getClock() - timeBeginReceive;
234 timeBeginReceive = Msg.getClock();
239 catch (Exception e) {
242 } while (answers < queries && Msg.getClock() < timeout);
243 destinationFound = nodeList.destinationFound();
244 } while (!destinationFound && (nodesAdded > 0 || answers == 0) && Msg.getClock() < globalTimeout && steps < Common.MAX_STEPS);
246 if (destinationFound) {
250 Msg.debug("Find node on " + destination + " succedded");
253 Msg.debug("Find node on " + destination + " failed");
254 Msg.debug("Queried " + queries + " nodes to find " + destination);
255 Msg.debug(nodeList.toString());
260 return destinationFound;
263 * Sends a "PING" request to a node
264 * @param destination Ping destination id.
266 public void ping(int destination) {
267 boolean destinationFound = false;
268 double timeout = Msg.getClock() + Common.PING_TIMEOUT;
269 PingTask pingTask = new PingTask(this.id);
270 /* Sending the ping task */
271 pingTask.dsend(Integer.toString(destination));
275 Task task = Task.receive(Integer.toString(this.id),Common.PING_TIMEOUT);
276 if (task instanceof PingAnswerTask) {
277 PingAnswerTask answerTask = (PingAnswerTask)task;
278 if (answerTask.getSenderId() == destination) {
279 this.table.update(destination);
280 destinationFound = true;
291 catch (Exception ex) {
293 } while (Msg.getClock() < timeout && !destinationFound);
296 * Sends a "FIND_NODE" request (task) to a node we know.
297 * @brief id Id of the node we are querying
298 * @brief destination id of the node we are trying to find.
300 public void sendFindNode(int id, int destination) {
301 Msg.debug("Sending a FIND_NODE to " + Integer.toString(id) + " to find " + destination );
302 FindNodeTask task = new FindNodeTask(this.id,destination);
303 task.dsend(Integer.toString(id));
306 * Sends a "FIND_NODE" request to the best "alpha" nodes in a node
309 public int sendFindNodeToBest(Answer nodeList) {
310 int destination = nodeList.getDestinationId();
312 for (i = 0; i < Common.alpha && i < nodeList.size(); i++) {
313 Contact node = nodeList.getNodes().get(i);
314 if (node.getId() != this.id) {
315 this.sendFindNode(node.getId(),destination);
321 * Does a random lookup
323 public void randomLookup() {
327 * Handles an incomming task
328 * @param task The task we need to handle
330 public void handleTask(Task task) {
331 if (task instanceof KademliaTask) {
332 table.update(((KademliaTask) task).getSenderId());
333 if (task instanceof FindNodeTask) {
334 handleFindNode((FindNodeTask)task);
336 else if (task instanceof PingTask) {
337 handlePing((PingTask)task);
341 public void handleFindNode(FindNodeTask task) {
342 Msg.debug("Received a FIND_NODE from " + task.getSenderId());
343 Answer answer = table.findClosest(task.getDestination());
344 FindNodeAnswerTask taskToSend = new FindNodeAnswerTask(this.id,task.getDestination(),answer);
345 taskToSend.dsend(Integer.toString(task.getSenderId()));
347 public void handlePing(PingTask task) {
348 Msg.debug("Received a PING from " + task.getSenderId());
349 PingAnswerTask taskToSend = new PingAnswerTask(this.id);
350 taskToSend.dsend(Integer.toString(task.getSenderId()));