Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' into fix/execute_benched
[simgrid.git] / examples / java / dht / chord / Node.java
index 1e6f6b1..94e8112 100644 (file)
@@ -58,53 +58,55 @@ public class Node extends Process {
       create();
       joinSuccess = true;
     } else {
-      int knownId = Integer.valueOf(args[1]);
-      deadline = Integer.valueOf(args[3]);
+      int knownId = Integer.parseInt(args[1]);
+      deadline = Integer.parseInt(args[3]);
       Msg.debug("Hey! Let's join the system with the id " + id + ".");
 
       joinSuccess = join(knownId);
     }
-    if (joinSuccess) {
-      double currentClock = Msg.getClock();
-      while (currentClock < (initTime + deadline) && currentClock < Common.MAX_SIMULATION_TIME) {
-        if (commReceive == null) {
-          commReceive = Task.irecv(this.mailbox);
-        }
-        try {
-          if (!commReceive.test()) {
-            if (currentClock >= nextStabilizeDate) {
-              stabilize();
-              nextStabilizeDate = Msg.getClock() + Common.PERIODIC_STABILIZE_DELAY;
-            } else if (currentClock >= nextFixFingersDate) {
-              fixFingers();
-              nextFixFingersDate = Msg.getClock() + Common.PERIODIC_FIX_FINGERS_DELAY;
-            } else if (currentClock >= nextCheckPredecessorDate) {
-              this.checkPredecessor();
-              nextCheckPredecessorDate = Msg.getClock() + Common.PERIODIC_CHECK_PREDECESSOR_DELAY;
-            } else if (currentClock >= nextLookupDate) {
-              this.randomLookup();
-              nextLookupDate = Msg.getClock() + Common.PERIODIC_LOOKUP_DELAY;
-            } else {
-              waitFor(5);
-            }
-            currentClock = Msg.getClock();
+
+    if (!joinSuccess) {
+      Msg.info("I couldn't join the ring");
+      return;
+    }
+
+    double currentClock = Msg.getClock();
+    while (currentClock < (initTime + deadline) && currentClock < Common.MAX_SIMULATION_TIME) {
+      if (commReceive == null) {
+        commReceive = Task.irecv(this.mailbox);
+      }
+      try {
+        if (!commReceive.test()) {
+          if (currentClock >= nextStabilizeDate) {
+            stabilize();
+            nextStabilizeDate = Msg.getClock() + Common.PERIODIC_STABILIZE_DELAY;
+          } else if (currentClock >= nextFixFingersDate) {
+            fixFingers();
+            nextFixFingersDate = Msg.getClock() + Common.PERIODIC_FIX_FINGERS_DELAY;
+          } else if (currentClock >= nextCheckPredecessorDate) {
+            this.checkPredecessor();
+            nextCheckPredecessorDate = Msg.getClock() + Common.PERIODIC_CHECK_PREDECESSOR_DELAY;
+          } else if (currentClock >= nextLookupDate) {
+            this.randomLookup();
+            nextLookupDate = Msg.getClock() + Common.PERIODIC_LOOKUP_DELAY;
           } else {
-            handleTask(commReceive.getTask());
-            currentClock = Msg.getClock();
-            commReceive = null;
+            waitFor(5);
           }
-        }
-        catch (Exception e) {
+          currentClock = Msg.getClock();
+        } else {
+          handleTask(commReceive.getTask());
           currentClock = Msg.getClock();
           commReceive = null;
         }
       }
-      leave();
-      if (commReceive != null) {
+      catch (Exception e) {
+        currentClock = Msg.getClock();
         commReceive = null;
       }
-    } else {
-      Msg.info("I couldn't join the ring");
+    }
+    leave();
+    if (commReceive != null) {
+      commReceive = null;
     }
   }
 
@@ -139,16 +141,7 @@ public class Node extends Process {
 
   private void leave() {
     Msg.debug("Well Guys! I Think it's time for me to quit ;)");
-    quitNotify(1); //Notify my successor
-    quitNotify(-1); //Notify my predecessor.
-  }
-
-  /**
-   * @brief Notifies the successor or the predecessor of the current node of the departure
-   * @param to 1 to notify the successor, -1 to notify the predecessor
-   */
-  static private void quitNotify( int to) {
-    //TODO
+    // TODO: Notify my successor and predecessor.
   }
 
   /** @brief Initializes the current node as the first one of the system  */
@@ -211,7 +204,6 @@ public class Node extends Process {
       }
       catch (MsgException e) {
         commReceive = null;
-        stop = true;
       }
     }
     catch (MsgException e) {
@@ -339,9 +331,8 @@ public class Node extends Process {
 
   // Performs a find successor request to a random id.
   private void randomLookup() {
-    int id = 1337;
-    //Msg.info("Making a lookup request for id " + id);
-    findSuccessor(id);
+    int dest = 1337;
+    findSuccessor(dest);
   }
 
   /**
@@ -374,7 +365,7 @@ public class Node extends Process {
    * @param end upper bound
    * @return a non-zero value if id in in [start, end]
    */
-  static private boolean isInInterval(int id, int start, int end) {
+  private static boolean isInInterval(int id, int start, int end) {
     int normId = normalize(id);
     int normStart = normalize(start);
     int normEnd = normalize(end);
@@ -394,7 +385,7 @@ public class Node extends Process {
    * @param id an id
    * @return the corresponding normalized id
    */
-  static private int normalize(int id) {
+  private static int normalize(int id) {
     return id & (Common.NB_KEYS - 1);
   }