Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'actor-yield' of github.com:Takishipp/simgrid into actor-yield
[simgrid.git] / examples / java / dht / kademlia / Node.java
index 25124b6..a690a01 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2012-2014, 2016. The SimGrid Team.
+/* Copyright (c) 2012-2014, 2016-2017. The SimGrid Team.
  * All rights reserved.                                                     */
 
 /* This program is free software; you can redistribute it and/or modify it
@@ -33,13 +33,13 @@ public class Node extends Process {
       Msg.info("Wrong argument count.");
       return;
     }
-    this.id = Integer.valueOf(args[0]);
+    this.id = Integer.parseInt(args[0]);
     this.table = new RoutingTable(this.id);
 
     if (args.length == 3) {
-      this.deadline = Integer.valueOf(args[2]).intValue();
+      this.deadline = Integer.parseInt(args[2]);
       Msg.info("Hi, I'm going to join the network with the id " + id + "!");
-      if (joinNetwork(Integer.valueOf(args[1]))) {
+      if (joinNetwork(Integer.parseInt(args[1]))) {
         this.mainLoop();
       }
       else {
@@ -47,7 +47,7 @@ public class Node extends Process {
       }
     }
     else {
-      this.deadline = Integer.valueOf(args[1]).intValue();
+      this.deadline = Integer.parseInt(args[1]);
       Msg.info("Hi, I'm going to create the network with the id " + id + "!");
       table.update(this.id);
       this.mainLoop();
@@ -57,16 +57,16 @@ public class Node extends Process {
   }
 
   public void mainLoop() {
-    double next_lookup_time = Msg.getClock() + Common.RANDOM_LOOKUP_INTERVAL;
+    double nextLookupTime = Msg.getClock() + Common.RANDOM_LOOKUP_INTERVAL;
     while (Msg.getClock() < this.deadline) {
       try {
         if (comm == null) {
           comm = Task.irecv(Integer.toString(id));
         }
         if (!comm.test()) {
-          if (Msg.getClock() >= next_lookup_time) {
+          if (Msg.getClock() >= nextLookupTime) {
             randomLookup();
-            next_lookup_time += Common.RANDOM_LOOKUP_INTERVAL;
+            nextLookupTime += Common.RANDOM_LOOKUP_INTERVAL;
           } else {
             waitFor(1);
           }
@@ -77,6 +77,7 @@ public class Node extends Process {
         }
       }
       catch (Exception e) {
+        Msg.debug("Caught exception: " + e);
       }
     }
     Msg.info(findNodeSuccedded + "/"  + (findNodeSuccedded + findNodeFailed) + " FIND_NODE have succedded.");
@@ -109,12 +110,10 @@ public class Node extends Process {
           } else {
             Task task = comm.getTask();
             if (task instanceof FindNodeAnswerTask) {
-              answerGot = true;
               //Retrieve the node list and ping them
               FindNodeAnswerTask answerTask = (FindNodeAnswerTask)task;
               Answer answer = answerTask.getAnswer();
               answerGot = true;
-              //answersGotten++;
               if (answer.getDestinationId() == this.id) {
                 //Ping everyone in the list
                 for (Contact c : answer.getNodes()) {
@@ -155,8 +154,8 @@ public class Node extends Process {
   public boolean findNode(int destination, boolean counts) {
     int queries;
     int answers;
-    int nodesAdded = 0;
-    boolean destinationFound = false;
+    int nodesAdded;
+    boolean destinationFound;
     int steps = 0;
     double timeBeginReceive;
     double timeout;
@@ -258,6 +257,7 @@ public class Node extends Process {
         waitFor(1);
       }
       catch (Exception ex) {
+        Msg.debug("Caught exception: " + ex);
       }
     } while (Msg.getClock() < timeout && !destinationFound);
   }
@@ -277,7 +277,7 @@ public class Node extends Process {
   public int sendFindNodeToBest(Answer nodeList) {
     int destination = nodeList.getDestinationId();
     int i;
-    for (i = 0; i < Common.alpha && i < nodeList.size(); i++) {
+    for (i = 0; i < Common.ALPHA && i < nodeList.size(); i++) {
       Contact node = nodeList.getNodes().get(i);
       if (node.getId() != this.id) {
         this.sendFindNode(node.getId(),destination);