Logo AND Algorithmique Numérique Distribuée

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