Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge pull request #2 from mquinson/master
[simgrid.git] / examples / java / async / Slave.java
1 /* Copyright (c) 2006-2007, 2010, 2013-2014, 2016. The SimGrid Team.
2  * 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.Msg;
9 import org.simgrid.msg.Comm;
10 import org.simgrid.msg.Host;
11 import org.simgrid.msg.Task;
12 import org.simgrid.msg.Process;
13 import org.simgrid.msg.HostFailureException;
14 import org.simgrid.msg.TaskCancelledException;
15 import org.simgrid.msg.TimeoutException;
16 import org.simgrid.msg.TransferFailureException;
17
18 public class Slave extends Process {
19   public Slave(Host host, String name, String[]args) {
20     super(host,name,args);
21   }
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     int num = Integer.valueOf(args[0]).intValue();
29     Comm comm = null;
30     boolean slaveFinished = false;
31     while(!slaveFinished) {  
32       try {
33         if (comm == null) {
34           Msg.info("Receiving on 'slave_" + num + "'");
35           comm = Task.irecv("slave_" + num);
36         } else {
37           if (comm.test()) {
38             Task task = comm.getTask();
39
40             if (task instanceof FinalizeTask) {
41               comm = null;
42               break;
43             }
44             Msg.info("Received a task");
45             Msg.info("Received \"" + task.getName() +  "\". Processing it.");
46             try {
47               task.execute();
48             } catch (TaskCancelledException e) {
49             
50             }
51             comm = null;
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 }