Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Reorganize java sources
[simgrid.git] / examples / basic / 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 import simgrid.msg.*;
9
10 public class Forwarder extends simgrid.msg.Process {
11     
12    public void main(String[] args) throws MsgException {
13       if (args.length < 3) {     
14          Msg.info("Forwarder needs 3 arguments (input mailbox, first output mailbox, last one)");
15          Msg.info("Got "+args.length+" instead");
16          System.exit(1);
17       }
18       int input = Integer.valueOf(args[0]).intValue();          
19       int firstOutput = Integer.valueOf(args[1]).intValue();            
20       int lastOutput = Integer.valueOf(args[2]).intValue();             
21       
22       int taskCount = 0;
23       int slavesCount = lastOutput - firstOutput + 1;
24       Msg.info("Receiving on 'slave_"+input+"'");
25       while(true) {
26          Task task = Task.receive("slave_"+input);      
27          
28          if (task instanceof FinalizeTask) {
29             Msg.info("Got a finalize task. Let's forward that we're done.");
30             
31             for (int cpt = firstOutput; cpt<=lastOutput; cpt++) {
32                Task tf = new FinalizeTask();
33                tf.send("slave_"+cpt);
34             }
35             break;
36          }
37          int dest = firstOutput + (taskCount % slavesCount);
38          
39          Msg.info("Sending \"" + task.getName() + "\" to \"slave_" + dest + "\"");
40          task.send("slave_"+dest);
41             
42          taskCount++;
43       }
44       
45          
46       Msg.info("I'm done. See you!");
47    }
48 }
49