Logo AND Algorithmique Numérique Distribuée

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