Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add a straightforward implementation of start_time and kill_time. This change the...
[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.Host;
10 import org.simgrid.msg.HostFailureException;
11 import org.simgrid.msg.HostNotFoundException;
12 import org.simgrid.msg.Msg;
13 import org.simgrid.msg.Task;
14 import org.simgrid.msg.TaskCancelledException;
15 import org.simgrid.msg.TimeoutException;
16 import org.simgrid.msg.TransferFailureException;
17 import org.simgrid.msg.Process;
18
19 public class Slave extends Process {
20         public Slave(Host host, String name, String[]args, double startTime, double killTime) {
21                 super(host,name,args,startTime,killTime);
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                 
31                 Comm comm = null;
32                 boolean slaveFinished = false;
33                 while(!slaveFinished) {  
34                         try
35                         {
36                                 if (comm == null) {
37                                         Msg.info("Receiving on 'slave_" + num + "'");
38                                         comm = Task.irecv("slave_" + num);
39                                 }
40                                 else {
41                                         if (comm.test()) {
42                                                 Task task = comm.getTask();
43         
44                                                 if (task instanceof FinalizeTask) {
45                                                         comm = null;
46                                                         break;
47                                                 }
48                                                 Msg.info("Received a task");
49                                                 Msg.info("Received \"" + task.getName() +  "\". Processing it.");
50                                                 try {
51                                                         task.execute();
52                                                 } catch (TaskCancelledException e) {
53                                                 
54                                                 }
55                                                 comm = null;                                                    
56                                         }
57                                         else {
58                                                 waitFor(1);
59                                         }
60                                 }
61                         }
62                         catch (Exception e) {
63                                 e.printStackTrace();
64                         }
65                 }
66                 Msg.info("Received Finalize. I'm done. See you!");
67                 waitFor(20);
68         }
69 }