Logo AND Algorithmique Numérique Distribuée

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