Logo AND Algorithmique Numérique Distribuée

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