Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid
[simgrid.git] / examples / java / dht / chord / Node.java
index 2cb52d8..2ef023c 100644 (file)
@@ -1,5 +1,4 @@
-/* Copyright (c) 2006-2014. The SimGrid Team.
- * All rights reserved.                                                     */
+/* Copyright (c) 2006-2017. The SimGrid Team. All rights reserved.          */
 
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
@@ -22,7 +21,7 @@ public class Node extends Process {
   protected Comm commReceive;
   ///Last time I changed a finger or my predecessor
   protected double lastChangeDate;
-  int[] fingers;
+  private int[] fingers;
 
   public Node(Host host, String name, String[] args) {
     super(host,name,args);
@@ -44,8 +43,8 @@ public class Node extends Process {
     double nextCheckPredecessorDate = initTime + Common.PERIODIC_CHECK_PREDECESSOR_DELAY;
     double nextLookupDate = initTime + Common.PERIODIC_LOOKUP_DELAY;
 
-    id = Integer.valueOf(args[0]);
-    mailbox = Integer.toString(id);
+    mailbox = args[0];
+    id = Integer.parseInt(args[0]);
 
     fingers = new int[Common.NB_BITS];
     for (i = 0; i < Common.NB_BITS; i++) {
@@ -59,57 +58,59 @@ public class Node extends Process {
       create();
       joinSuccess = true;
     } else {
-      int knownId = Integer.valueOf(args[1]);
-      deadline = Integer.valueOf(args[3]);
-      //Msg.info("Hey! Let's join the system with the id " + id + ".");
+      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;
     }
   }
 
-  void handleTask(Task task) {
+  private void handleTask(Task task) {
     if (task instanceof FindSuccessorTask) {
       FindSuccessorTask fTask = (FindSuccessorTask)task;
       Msg.debug("Receiving a 'Find Successor' request from " + fTask.getIssuerHostName() + " for id " + 
@@ -138,30 +139,19 @@ public class Node extends Process {
     }
   }
 
-  void leave() {
+  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.
+    // TODO: Notify my successor and 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 void quitNotify( int to) {
-    //TODO
-  }
-
-  /**
-   * @brief Initializes the current node as the first one of the system.
-   */
-  void create() {
+  /** @brief Initializes the current node as the first one of the system  */
+  private void create() {
     Msg.debug("Create a new Chord ring...");
     setPredecessor(-1);
   }
 
   // Makes the current node join the ring, knowing the id of a node already in the ring 
-  boolean join(int knownId) {
+  private boolean join(int knownId) {
     Msg.info("Joining the ring with id " + this.id + " knowing node " + knownId);
     setPredecessor(-1);
     int successorId = remoteFindSuccessor(knownId, this.id);
@@ -173,7 +163,7 @@ public class Node extends Process {
     return successorId != -1;
   }
 
-  void setPredecessor(int predecessorId) {
+  private void setPredecessor(int predecessorId) {
     if (predecessorId != predId) {
       predId = predecessorId;
       if (predecessorId != -1) {
@@ -188,7 +178,7 @@ public class Node extends Process {
    * @param askTo the node to ask to
    * @return the id of its predecessor node, or -1 if the request failed(or if the node does not know its predecessor)
    */
-  int remoteGetPredecessor(int askTo) {
+  private int remoteGetPredecessor(int askTo) {
     int predecessorId = -1;
     boolean stop = false;
     Msg.debug("Sending a 'Get Predecessor' request to " + askTo);
@@ -214,7 +204,6 @@ public class Node extends Process {
       }
       catch (MsgException e) {
         commReceive = null;
-        stop = true;
       }
     }
     catch (MsgException e) {
@@ -229,7 +218,7 @@ public class Node extends Process {
    * @param id the id to find
    * @return the id of the successor node, or -1 if the request failed
    */
-  int findSuccessor(int id) {
+  private int findSuccessor(int id) {
     if (isInInterval(id, this.id + 1, fingers[0])) {
       return fingers[0];
     }
@@ -239,7 +228,7 @@ public class Node extends Process {
   }
 
   // Asks another node the successor node of an id.
-  int remoteFindSuccessor(int askTo, int id) {
+  private int remoteFindSuccessor(int askTo, int id) {
     int successor = -1;
     boolean stop = false;
     String askToMailbox = Integer.toString(askTo);
@@ -281,7 +270,7 @@ public class Node extends Process {
   }
 
   // This function is called periodically. It checks the immediate successor of the current node.
-  void stabilize() {
+  private void stabilize() {
     Msg.debug("Stabilizing node");
     int candidateId;
     int successorId = fingers[0];
@@ -303,7 +292,7 @@ public class Node extends Process {
    * @brief Notifies the current node that its predecessor may have changed.
    * @param candidate_id the possible new predecessor
    */
-  void notify(int predecessorCandidateId) {
+  private void notify(int predecessorCandidateId) {
     if (predId == -1 || isInInterval(predecessorCandidateId, predId + 1, this.id - 1 )) {
       setPredecessor(predecessorCandidateId);
     }
@@ -314,7 +303,7 @@ public class Node extends Process {
    * @param notify_id id of the node to notify
    * @param candidate_id the possible new predecessor
    */
-  void remoteNotify(int notifyId, int predecessorCandidateId) {
+  private void remoteNotify(int notifyId, int predecessorCandidateId) {
     Msg.debug("Sending a 'Notify' request to " + notifyId);
     Task sentTask = new NotifyTask(getHost().getName(), this.mailbox, predecessorCandidateId);
     sentTask.dsend(Integer.toString(notifyId));
@@ -322,7 +311,7 @@ public class Node extends Process {
 
   // This function is called periodically.
   // It refreshes the finger table of the current node.
-  void fixFingers() {
+  private void fixFingers() {
     Msg.debug("Fixing fingers");
     int i = this.nextFingerToFix;
     int successorId = this.findSuccessor(this.id + (int)Math.pow(2,i)); //FIXME: SLOW
@@ -336,15 +325,15 @@ public class Node extends Process {
 
   // This function is called periodically.
   // It checks whether the predecessor has failed
-  void checkPredecessor() {
+  private void checkPredecessor() {
     //TODO
   }
 
   // Performs a find successor request to a random id.
-  void randomLookup() {
-    int id = 1337;
-    //Msg.info("Making a lookup request for id " + id);
-    findSuccessor(id);
+  private void randomLookup() {
+    int dest = 1337;
+    //Msg.info("Making a lookup request for id " + dest);
+    findSuccessor(dest);
   }
 
   /**
@@ -352,7 +341,7 @@ public class Node extends Process {
    * @param id the id to find
    * @return the closest preceding finger of that id
    */
-  int closestPrecedingNode(int id) {
+  private int closestPrecedingNode(int id) {
     for (int i = Common.NB_BITS - 1; i >= 0; i--) {
       if (isInInterval(fingers[i], this.id + 1, id - 1)) {
         return fingers[i];
@@ -377,7 +366,7 @@ public class Node extends Process {
    * @param end upper bound
    * @return a non-zero value if id in in [start, end]
    */
-  static 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);
@@ -397,7 +386,7 @@ public class Node extends Process {
    * @param id an id
    * @return the corresponding normalized id
    */
-  static int normalize(int id) {
+  private static int normalize(int id) {
     return id & (Common.NB_KEYS - 1);
   }
 
@@ -406,7 +395,7 @@ public class Node extends Process {
    * @param finger_index index of the finger to set (0 to nb_bits - 1)
    * @param id the id to set for this finger
    */
-  void setFinger(int fingerIndex, int id) {
+  private void setFinger(int fingerIndex, int id) {
     if (id != fingers[fingerIndex]) {
       fingers[fingerIndex] = id;
       lastChangeDate = Msg.getClock();