Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Bugfix in async example. maybe our code should be more defensive for that kind of...
[simgrid.git] / examples / async / Slave.java
index 3d2270b..f1a8fab 100644 (file)
@@ -5,6 +5,7 @@
  * under the terms of the license (GNU LGPL) which comes with this package. 
  */
 package async;
+import org.simgrid.msg.Comm;
 import org.simgrid.msg.HostFailureException;
 import org.simgrid.msg.Msg;
 import org.simgrid.msg.Task;
@@ -21,23 +22,42 @@ public class Slave extends Process {
                }
 
                int num = Integer.valueOf(args[0]).intValue();
-               //Msg.info("Receiving on 'slave_"+num+"'");
-
-               while(true) {  
-                       Task task = Task.receive("slave_"+num); 
-
-                       if (task instanceof FinalizeTask) {
-                               break;
+               
+               Comm comm = null;
+               boolean slaveFinished = false;
+               while(!slaveFinished) {  
+                       try
+                       {
+                               if (comm == null) {
+                                       Msg.info("Receiving on 'slave_" + num + "'");
+                                       comm = Task.irecv("slave_" + num);
+                               }
+                               else {
+                                       if (comm.test()) {
+                                               Task task = comm.getTask();
+       
+                                               if (task instanceof FinalizeTask) {
+                                                       comm = null;
+                                                       break;
+                                               }
+                                               Msg.info("Received \"" + task.getName() +  "\". Processing it.");
+                                               try {
+                                                       task.execute();
+                                               } catch (TaskCancelledException e) {
+                                               
+                                               }
+                                               comm = null;                                                    
+                                       }
+                                       else {
+                                               simulatedSleep(1);
+                                       }
+                               }
                        }
-                       Msg.info("Received \"" + task.getName() +  "\". Processing it.");
-                       try {
-                               task.execute();
-                       } catch (TaskCancelledException e) {
-
+                       catch (Exception e) {
+                               e.printStackTrace();
                        }
-               //      Msg.info("\"" + task.getName() + "\" done ");
                }
-
                Msg.info("Received Finalize. I'm done. See you!");
+               simulatedSleep(20);
        }
 }
\ No newline at end of file