Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
d17bb9343e4cf4058448ff9bf949b787b76fd05e
[simgrid.git] / examples / async / Forwarder.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
8 package async;
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
17         public void main(String[] args) throws MsgException {
18                 if (args.length < 3) {   
19                         Msg.info("Forwarder needs 3 arguments (input mailbox, first output mailbox, last one)");
20                         Msg.info("Got "+args.length+" instead");
21                         System.exit(1);
22                 }
23                 int input = Integer.valueOf(args[0]).intValue();                
24                 int firstOutput = Integer.valueOf(args[1]).intValue();          
25                 int lastOutput = Integer.valueOf(args[2]).intValue();           
26
27                 int taskCount = 0;
28                 int slavesCount = lastOutput - firstOutput + 1;
29                 Msg.info("Receiving on 'slave_"+input+"'");
30                 while(true) {
31                         Task task = Task.receive("slave_"+input);       
32
33                         if (task instanceof FinalizeTask) {
34                                 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.");
35
36                                 for (int cpt = firstOutput; cpt<=lastOutput; cpt++) {
37                                         Task tf = new FinalizeTask();
38                                         tf.dsend("slave_"+cpt);
39                                 }
40                                 simulatedSleep(20);
41                                 break;
42                         }
43                         int dest = firstOutput + (taskCount % slavesCount);
44
45                         Msg.info("Sending \"" + task.getName() + "\" to \"slave_" + dest + "\"");
46                         task.send("slave_"+dest);
47
48                         taskCount++;
49                 }
50
51
52                 Msg.info("I'm done. See you!");
53         }
54 }
55