Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fix leak of memory caused to some Events
[simgrid.git] / examples / java / dht / chord / Node.java
index 0216108..835c316 100644 (file)
@@ -22,7 +22,7 @@ public class Node extends Process {
   protected Comm commReceive;
   ///Last time I changed a finger or my predecessor
   protected double lastChangeDate;
-  int fingers[];
+  int[] fingers;
 
   public Node(Host host, String name, String[] args) {
     super(host,name,args);
@@ -36,7 +36,7 @@ public class Node extends Process {
     }
     double initTime = Msg.getClock();
     int i;
-    boolean joinSuccess = false;
+    boolean joinSuccess;
     double deadline;
 
     double nextStabilizeDate = initTime + Common.PERIODIC_STABILIZE_DELAY;
@@ -44,8 +44,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++) {
@@ -55,13 +55,13 @@ public class Node extends Process {
 
     //First node
     if (args.length == 2) {
-      deadline = Integer.valueOf(args[1]);
+      deadline = Integer.parseInt(args[1]);
       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 + ".");
+      Msg.debug("Hey! Let's join the system with the id " + id + ".");
 
       joinSuccess = join(knownId);
     }
@@ -112,26 +112,27 @@ public class Node extends Process {
   void handleTask(Task task) {
     if (task instanceof FindSuccessorTask) {
       FindSuccessorTask fTask = (FindSuccessorTask)task;
-      Msg.debug("Receiving a 'Find Successor' request from " + fTask.issuerHostName + " for id " + fTask.requestId);
+      Msg.debug("Receiving a 'Find Successor' request from " + fTask.getIssuerHostName() + " for id " + 
+                fTask.getRequestId());
       // is my successor the successor?
-      if (isInInterval(fTask.requestId, this.id + 1, fingers[0])) {
-        //Msg.info("Send the request to " + fTask.answerTo + " with answer " + fingers[0]);
+      if (isInInterval(fTask.getRequestId(), this.id + 1, fingers[0])) {
+        Msg.debug("Send the request to " + fTask.getAnswerTo() + " with answer " + fingers[0]);
         FindSuccessorAnswerTask answer = new FindSuccessorAnswerTask(getHost().getName(), mailbox, fingers[0]);
-        answer.dsend(fTask.answerTo);
+        answer.dsend(fTask.getAnswerTo());
       } else {
         // otherwise, forward the request to the closest preceding finger in my table
-        int closest = closestPrecedingNode(fTask.requestId);
-        //Msg.info("Forward the request to " + closest);
+        int closest = closestPrecedingNode(fTask.getRequestId());
+        Msg.debug("Forward the request to " + closest);
         fTask.dsend(Integer.toString(closest));
       }
     } else if (task instanceof GetPredecessorTask) {
       GetPredecessorTask gTask = (GetPredecessorTask)(task);
-      Msg.debug("Receiving a 'Get Predecessor' request from " + gTask.issuerHostName);
+      Msg.debug("Receiving a 'Get Predecessor' request from " + gTask.getIssuerHostName());
       GetPredecessorAnswerTask answer = new GetPredecessorAnswerTask(getHost().getName(), mailbox, predId);
-      answer.dsend(gTask.answerTo);
+      answer.dsend(gTask.getAnswerTo());
     } else if (task instanceof NotifyTask) {
       NotifyTask nTask = (NotifyTask)task;
-      notify(nTask.requestId);
+      notify(nTask.getRequestId());
     } else {
       Msg.debug("Ignoring unexpected task of type:" + task);
     }
@@ -203,7 +204,7 @@ public class Node extends Process {
           commReceive.waitCompletion(Common.TIMEOUT);
           Task taskReceived = commReceive.getTask();
           if (taskReceived instanceof GetPredecessorAnswerTask) {
-            predecessorId = ((GetPredecessorAnswerTask) taskReceived).answerId;
+            predecessorId = ((GetPredecessorAnswerTask) taskReceived).getAnswerId();
             stop = true;
           } else {
             handleTask(taskReceived);
@@ -241,11 +242,11 @@ public class Node extends Process {
   int remoteFindSuccessor(int askTo, int id) {
     int successor = -1;
     boolean stop = false;
-    String mailbox = Integer.toString(askTo);
+    String askToMailbox = Integer.toString(askTo);
     Task sendTask = new FindSuccessorTask(getHost().getName(), this.mailbox, id);
-    Msg.debug("Sending a 'Find Successor' request to " + mailbox + " for id " + id);
+    Msg.debug("Sending a 'Find Successor' request to " + askToMailbox + " for id " + id);
     try {
-      sendTask.send(mailbox, Common.TIMEOUT);
+      sendTask.send(askToMailbox, Common.TIMEOUT);
       do {
         if (commReceive == null) {
           commReceive = Task.irecv(this.mailbox);
@@ -257,7 +258,7 @@ public class Node extends Process {
             //TODO: Check if this this our answer.
             FindSuccessorAnswerTask fTask = (FindSuccessorAnswerTask) task;
             stop = true;
-            successor = fTask.answerId;
+            successor = fTask.getAnswerId();
           } else {
             handleTask(task);
           }
@@ -324,10 +325,10 @@ public class Node extends Process {
   void fixFingers() {
     Msg.debug("Fixing fingers");
     int i = this.nextFingerToFix;
-    int id = this.findSuccessor(this.id + (int)Math.pow(2,i)); //FIXME: SLOW
-    if (id != -1) {
-      if (id != fingers[i]) {
-        setFinger(i, id);
+    int successorId = this.findSuccessor(this.id + (int)Math.pow(2,i)); //FIXME: SLOW
+    if (successorId != -1) {
+      if (successorId != fingers[i]) {
+        setFinger(i, successorId);
       }
       nextFingerToFix = (i + 1) % Common.NB_BITS;
     }
@@ -352,8 +353,7 @@ public class Node extends Process {
    * @return the closest preceding finger of that id
    */
   int closestPrecedingNode(int id) {
-    int i;
-    for (i = Common.NB_BITS - 1; i >= 0; i--) {
+    for (int i = Common.NB_BITS - 1; i >= 0; i--) {
       if (isInInterval(fingers[i], this.id + 1, id - 1)) {
         return fingers[i];
       }
@@ -378,18 +378,18 @@ public class Node extends Process {
    * @return a non-zero value if id in in [start, end]
    */
   static boolean isInInterval(int id, int start, int end) {
-    id = normalize(id);
-    start = normalize(start);
-    end = normalize(end);
+    int normId = normalize(id);
+    int normStart = normalize(start);
+    int normEnd = normalize(end);
 
     // make sure end >= start and id >= start
-    if (end < start) {
-      end += Common.NB_KEYS;
+    if (normEnd < normStart) {
+      normEnd += Common.NB_KEYS;
     }
-    if (id < start) {
-      id += Common.NB_KEYS;
+    if (normId < normStart) {
+      normId += Common.NB_KEYS;
     }
-    return (id <= end);
+    return (normId <= normEnd);
   }
 
   /**