Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge 'master' into mc
[simgrid.git] / examples / java / async / Forwarder.java
1 /* Copyright (c) 2006-2014. 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.Host;
9 import org.simgrid.msg.Msg;
10 import org.simgrid.msg.MsgException;
11 import org.simgrid.msg.Task;
12 import org.simgrid.msg.Process;
13
14
15 public class Forwarder extends Process {
16         public Forwarder(Host host, String name, String[]args) {
17                 super(host,name,args);
18         }
19         public void main(String[] args) throws MsgException {
20                 if (args.length < 3) {   
21                         Msg.info("Forwarder needs 3 arguments (input mailbox, first output mailbox, last one)");
22                         Msg.info("Got "+args.length+" instead");
23                         System.exit(1);
24                 }
25                 int input = Integer.valueOf(args[0]).intValue();                
26                 int firstOutput = Integer.valueOf(args[1]).intValue();          
27                 int lastOutput = Integer.valueOf(args[2]).intValue();           
28
29                 int taskCount = 0;
30                 int slavesCount = lastOutput - firstOutput + 1;
31                 Msg.info("Receiving on 'slave_"+input+"'");
32                 while(true) {
33                         Task task = Task.receive("slave_"+input);       
34
35                         if (task instanceof FinalizeTask) {
36                                 Msg.info("Got a finalize task. Let's forward (asynchronously) that we're done, and then sleep 20 seconds so that nobody gets a message from a terminated process.");
37
38                                 for (int cpt = firstOutput; cpt<=lastOutput; cpt++) {
39                                         Task tf = new FinalizeTask();
40                                         tf.dsend("slave_"+cpt);
41                                 }
42                                 waitFor(20);
43                                 break;
44                         }
45                         int dest = firstOutput + (taskCount % slavesCount);
46
47                         Msg.info("Sending \"" + task.getName() + "\" to \"slave_" + dest + "\"");
48                         task.send("slave_"+dest);
49
50                         taskCount++;
51                 }
52
53
54                 Msg.info("I'm done. See you!");
55         }
56 }
57