Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Deleted simulatedSleep, use waitFor instead. Moved everything from MsgNative to their...
[simgrid.git] / examples / async / Slave.java
1 /*
2  * Copyright 2006,2007,2010. The SimGrid Team. All rights reserved. 
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. 
6  */
7 package async;
8 import org.simgrid.msg.Comm;
9 import org.simgrid.msg.HostFailureException;
10 import org.simgrid.msg.Msg;
11 import org.simgrid.msg.Task;
12 import org.simgrid.msg.TaskCancelledException;
13 import org.simgrid.msg.TimeoutException;
14 import org.simgrid.msg.TransferFailureException;
15 import org.simgrid.msg.Process;
16
17 public class Slave extends Process {
18         public void main(String[] args) throws TransferFailureException, HostFailureException, TimeoutException {
19                 if (args.length < 1) {
20                         Msg.info("Slave needs 1 argument (its number)");
21                         System.exit(1);
22                 }
23
24                 int num = Integer.valueOf(args[0]).intValue();
25                 
26                 Comm comm = null;
27                 boolean slaveFinished = false;
28                 while(!slaveFinished) {  
29                         try
30                         {
31                                 if (comm == null) {
32                                         Msg.info("Receiving on 'slave_" + num + "'");
33                                         comm = Task.irecv("slave_" + num);
34                                 }
35                                 else {
36                                         if (comm.test()) {
37                                                 Task task = comm.getTask();
38         
39                                                 if (task instanceof FinalizeTask) {
40                                                         comm = null;
41                                                         break;
42                                                 }
43                                                 Msg.info("Received a task");
44                                                 Msg.info("Received \"" + task.getName() +  "\". Processing it.");
45                                                 try {
46                                                         task.execute();
47                                                 } catch (TaskCancelledException e) {
48                                                 
49                                                 }
50                                                 comm = null;                                                    
51                                         }
52                                         else {
53                                                 waitFor(1);
54                                         }
55                                 }
56                         }
57                         catch (Exception e) {
58                                 e.printStackTrace();
59                         }
60                 }
61                 Msg.info("Received Finalize. I'm done. See you!");
62                 waitFor(20);
63         }
64 }