Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add kademlia example
[simgrid.git] / examples / async / Slave.java
index 3d2270b..ca06965 100644 (file)
@@ -5,7 +5,10 @@
  * under the terms of the license (GNU LGPL) which comes with this package. 
  */
 package async;
+import org.simgrid.msg.Comm;
+import org.simgrid.msg.Host;
 import org.simgrid.msg.HostFailureException;
+import org.simgrid.msg.HostNotFoundException;
 import org.simgrid.msg.Msg;
 import org.simgrid.msg.Task;
 import org.simgrid.msg.TaskCancelledException;
@@ -14,6 +17,9 @@ import org.simgrid.msg.TransferFailureException;
 import org.simgrid.msg.Process;
 
 public class Slave extends Process {
+       public Slave(Host host, String name, String[]args) {
+               super(host,name,args);
+       }
        public void main(String[] args) throws TransferFailureException, HostFailureException, TimeoutException {
                if (args.length < 1) {
                        Msg.info("Slave needs 1 argument (its number)");
@@ -21,23 +27,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 a task");
+                                               Msg.info("Received \"" + task.getName() +  "\". Processing it.");
+                                               try {
+                                                       task.execute();
+                                               } catch (TaskCancelledException e) {
+                                               
+                                               }
+                                               comm = null;                                                    
+                                       }
+                                       else {
+                                               waitFor(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!");
+               waitFor(20);
        }
 }
\ No newline at end of file