1 /* Copyright (c) 2012-2013. 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. */
8 import org.simgrid.msg.Host;
10 import org.simgrid.msg.Comm;
11 import org.simgrid.msg.Msg;
12 import org.simgrid.msg.MsgException;
13 import org.simgrid.msg.Process;
14 import org.simgrid.msg.Task;
16 * Main class of the simulation, contains the logic of a node.
18 public class Node extends Process {
26 protected RoutingTable table;
30 protected int deadline;
32 * FIND_NODE which have succeeded.
34 protected int findNodeSuccedded = 0;
36 * FIND_NODE which have failed
38 protected int findNodeFailed = 0;
42 public Node(Host host, String name, String[]args) {
43 super(host,name,args);
47 public void main(String[] args) throws MsgException {
48 //Check the number of arguments.
49 if (args.length != 2 && args.length != 3) {
50 Msg.info("Wrong argument count.");
53 this.id = Integer.valueOf(args[0]);
54 this.table = new RoutingTable(this.id);
56 if (args.length == 3) {
57 this.deadline = Integer.valueOf(args[2]).intValue();
58 Msg.info("Hi, I'm going to join the network with the id " + id + "!");
59 if (joinNetwork(Integer.valueOf(args[1]))) {
63 Msg.info("I couldn't join the network :(");
67 this.deadline = Integer.valueOf(args[1]).intValue();
68 Msg.info("Hi, I'm going to create the network with the id " + id + "!");
69 table.update(this.id);
72 Msg.debug("I'm leaving the network");
73 Msg.debug("Here is my routing table:" + table);
78 public void mainLoop() {
79 double next_lookup_time = Msg.getClock() + Common.RANDOM_LOOKUP_INTERVAL;
80 while (Msg.getClock() < this.deadline) {
83 comm = Task.irecv(Integer.toString(id));
86 if (Msg.getClock() >= next_lookup_time) {
88 next_lookup_time += Common.RANDOM_LOOKUP_INTERVAL;
95 Task task = comm.getTask();
100 catch (Exception e) {
104 Msg.info(findNodeSuccedded + "/" + (findNodeSuccedded + findNodeFailed) + " FIND_NODE have succedded.");
107 * @brief Try to make the node join the network
108 * @param idKnown Id of someone we know in the system
110 public boolean joinNetwork(int idKnown) {
111 boolean answerGot = false;
112 double timeBegin = Msg.getClock();
113 Msg.debug("Joining the network knowing " + idKnown);
114 //Add ourselves and the node we know to our routing table
115 table.update(this.id);
116 table.update(idKnown);
117 //Send a "FIND_NODE" to the node we know.
118 sendFindNode(idKnown,this.id);
119 //Wait for the answer.
125 comm = Task.irecv(Integer.toString(id));
132 Task task = comm.getTask();
133 if (task instanceof FindNodeAnswerTask) {
135 //Retrieve the node list and ping them
136 FindNodeAnswerTask answerTask = (FindNodeAnswerTask)task;
137 Answer answer = answerTask.getAnswer();
140 if (answer.getDestinationId() == this.id) {
141 //Ping everyone in the list
142 for (Contact c : answer.getNodes()) {
143 table.update(c.getId());
155 catch (Exception ex) {
157 Msg.info("FIND_NODE failed");
159 } while (!answerGot && trials < Common.MAX_JOIN_TRIALS);
160 /* Second step: Send a FIND_NODE in a node in each bucket */
161 int bucketId = table.findBucket(idKnown).getId();
162 for (int i = 0; ((bucketId - i) > 0 ||
163 (bucketId + i) <= Common.IDENTIFIER_SIZE) &&
164 i < Common.JOIN_BUCKETS_QUERIES; i++) {
165 if (bucketId - i > 0) {
166 int idInBucket = table.getIdInPrefix(this.id,bucketId - i);
167 this.findNode(idInBucket,false);
169 if (bucketId + i <= Common.IDENTIFIER_SIZE) {
170 int idInBucket = table.getIdInPrefix(this.id,bucketId + i);
171 findNode(idInBucket,false);
174 Msg.debug("Time spent:" + (Msg.getClock() - timeBegin));
178 * Send a request to find a node in the node's routing table.
180 public boolean findNode(int destination, boolean counts) {
181 int queries, answers;
183 boolean destinationFound = false;
185 double timeBeginReceive;
186 double timeout, globalTimeout = Msg.getClock() + Common.FIND_NODE_GLOBAL_TIMEOUT;
187 //Build a list of the closest nodes we already know.
188 Answer nodeList = table.findClosest(destination);
189 Msg.verb("Doing a FIND_NODE on " + destination);
191 timeBeginReceive = Msg.getClock();
193 queries = this.sendFindNodeToBest(nodeList);
195 timeout = Msg.getClock() + Common.FIND_NODE_TIMEOUT;
200 comm = Task.irecv(Integer.toString(id));
206 Task task = comm.getTask();
207 if (task instanceof FindNodeAnswerTask) {
208 FindNodeAnswerTask answerTask = (FindNodeAnswerTask)task;
209 //Check if we received what we are looking for.
210 if (answerTask.getDestinationId() == destination) {
211 table.update(answerTask.getSenderId());
212 //Add the answer to our routing table
213 for (Contact c: answerTask.getAnswer().getNodes()) {
214 table.update(c.getId());
218 nodesAdded = nodeList.merge(answerTask.getAnswer());
220 /* If it's not our answer, we answer to the node that
221 * has queried us anyway
225 //Update the timeout if it's not our answer.
226 timeout += Msg.getClock() - timeBeginReceive;
227 timeBeginReceive = Msg.getClock();
232 timeout += Msg.getClock() - timeBeginReceive;
233 timeBeginReceive = Msg.getClock();
238 catch (Exception e) {
241 } while (answers < queries && Msg.getClock() < timeout);
242 destinationFound = nodeList.destinationFound();
243 } while (!destinationFound && (nodesAdded > 0 || answers == 0) && Msg.getClock() < globalTimeout && steps < Common.MAX_STEPS);
245 if (destinationFound) {
249 Msg.debug("Find node on " + destination + " succedded");
252 Msg.debug("Find node on " + destination + " failed");
253 Msg.debug("Queried " + queries + " nodes to find " + destination);
254 Msg.debug(nodeList.toString());
259 return destinationFound;
262 * Sends a "PING" request to a node
263 * @param destination Ping destination id.
265 public void ping(int destination) {
266 boolean destinationFound = false;
267 double timeout = Msg.getClock() + Common.PING_TIMEOUT;
268 PingTask pingTask = new PingTask(this.id);
269 /* Sending the ping task */
270 pingTask.dsend(Integer.toString(destination));
274 Task task = Task.receive(Integer.toString(this.id),Common.PING_TIMEOUT);
275 if (task instanceof PingAnswerTask) {
276 PingAnswerTask answerTask = (PingAnswerTask)task;
277 if (answerTask.getSenderId() == destination) {
278 this.table.update(destination);
279 destinationFound = true;
290 catch (Exception ex) {
292 } while (Msg.getClock() < timeout && !destinationFound);
295 * Sends a "FIND_NODE" request (task) to a node we know.
296 * @brief id Id of the node we are querying
297 * @brief destination id of the node we are trying to find.
299 public void sendFindNode(int id, int destination) {
300 Msg.debug("Sending a FIND_NODE to " + Integer.toString(id) + " to find " + destination );
301 FindNodeTask task = new FindNodeTask(this.id,destination);
302 task.dsend(Integer.toString(id));
305 * Sends a "FIND_NODE" request to the best "alpha" nodes in a node
308 public int sendFindNodeToBest(Answer nodeList) {
309 int destination = nodeList.getDestinationId();
311 for (i = 0; i < Common.alpha && i < nodeList.size(); i++) {
312 Contact node = nodeList.getNodes().get(i);
313 if (node.getId() != this.id) {
314 this.sendFindNode(node.getId(),destination);
320 * Does a random lookup
322 public void randomLookup() {
326 * Handles an incomming task
327 * @param task The task we need to handle
329 public void handleTask(Task task) {
330 if (task instanceof KademliaTask) {
331 table.update(((KademliaTask) task).getSenderId());
332 if (task instanceof FindNodeTask) {
333 handleFindNode((FindNodeTask)task);
335 else if (task instanceof PingTask) {
336 handlePing((PingTask)task);
340 public void handleFindNode(FindNodeTask task) {
341 Msg.debug("Received a FIND_NODE from " + task.getSenderId());
342 Answer answer = table.findClosest(task.getDestination());
343 FindNodeAnswerTask taskToSend = new FindNodeAnswerTask(this.id,task.getDestination(),answer);
344 taskToSend.dsend(Integer.toString(task.getSenderId()));
346 public void handlePing(PingTask task) {
347 Msg.debug("Received a PING from " + task.getSenderId());
348 PingAnswerTask taskToSend = new PingAnswerTask(this.id);
349 taskToSend.dsend(Integer.toString(task.getSenderId()));