Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
dd69021aff9a5c390c968f27e07150cf6efe469b
[simgrid.git] / examples / java / basic / Forwarder.java
1 /*
2  * $Id$
3  *
4  * Copyright 2006,2007,2010 The SimGrid Team
5  * All rights reserved. 
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the license (GNU LGPL) which comes with this package. 
9  */
10
11 import simgrid.msg.*;
12
13 public class Forwarder extends simgrid.msg.Process {
14     
15    public void main(String[] args) throws JniException, NativeException {
16       if (args.length < 3) {     
17          Msg.info("Forwarder needs 3 arguments (input mailbox, first output mailbox, last one)");
18          Msg.info("Got "+args.length+" instead");
19          System.exit(1);
20       }
21       int input = Integer.valueOf(args[0]).intValue();          
22       int firstOutput = Integer.valueOf(args[1]).intValue();            
23       int lastOutput = Integer.valueOf(args[2]).intValue();             
24       
25       int taskCount = 0;
26       int slavesCount = lastOutput - firstOutput + 1;
27       Msg.info("Receiving on 'slave_"+input+"'");
28       while(true) {
29          Task task = Task.receive("slave_"+input);      
30          
31          if (task instanceof FinalizeTask) {
32             Msg.info("Got a finalize task. Let's forward that we're done.");
33             
34             for (int cpt = firstOutput; cpt<=lastOutput; cpt++) {
35                Task tf = new FinalizeTask();
36                tf.send("slave_"+cpt);
37             }
38             break;
39          }
40          int dest = firstOutput + (taskCount % slavesCount);
41          
42          Msg.info("Sending \"" + task.getName() + "\" to \"slave_" + dest + "\"");
43          task.send("slave_"+dest);
44             
45          taskCount++;
46       }
47       
48          
49       Msg.info("I'm done. See you!");
50    }
51 }
52