Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
6d383c9fa991d8c09b75ca92212dd283ffa8959b
[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 t = Task.receive("slave_"+input); 
30          
31          if (t 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          BasicTask task = (BasicTask)t;
41          int dest = firstOutput + (taskCount % slavesCount);
42          
43          Msg.info("Sending \"" + task.getName() + "\" to \"slave_" + dest + "\"");
44          task.send("slave_"+dest);
45             
46          taskCount++;
47       }
48       
49          
50       Msg.info("I'm done. See you!");
51    }
52 }
53