Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Acknoledge the fact that JniException is a runtime exception now: kill all its occure...
[simgrid.git] / examples / java / basic / Forwarder.java
index 4e856bc..46d1186 100644 (file)
@@ -1,8 +1,5 @@
 /*
- * $Id$
- *
- * Copyright 2006,2007 Martin Quinson, Malek Cherier         
- * All rights reserved. 
+ * Copyright 2006,2007,2010. The SimGrid Team. All rights reserved. 
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. 
@@ -12,40 +9,35 @@ import simgrid.msg.*;
 
 public class Forwarder extends simgrid.msg.Process {
     
-   public void main(String[] args) throws JniException, NativeException {
-      Msg.info("hello!");
-      
-      int slavesCount = args.length;
-      Host[] slaves = new Host[slavesCount];
-      
-      for (int i = 0; i < args.length; i++) {
-        try {        
-           slaves[i] = Host.getByName(args[i]);
-        } catch (HostNotFoundException e) {
-           Msg.info("Buggy deployment file");
-           e.printStackTrace();
-           System.exit(1);
-        }
+   public void main(String[] args) throws NativeException {
+      if (args.length < 3) {    
+        Msg.info("Forwarder needs 3 arguments (input mailbox, first output mailbox, last one)");
+        Msg.info("Got "+args.length+" instead");
+        System.exit(1);
       }
+      int input = Integer.valueOf(args[0]).intValue();         
+      int firstOutput = Integer.valueOf(args[1]).intValue();           
+      int lastOutput = Integer.valueOf(args[2]).intValue();            
       
       int taskCount = 0;
+      int slavesCount = lastOutput - firstOutput + 1;
+      Msg.info("Receiving on 'slave_"+input+"'");
       while(true) {
-        Task t = Task.get(0);  
+        Task task = Task.receive("slave_"+input);      
         
-        if (t instanceof FinalizeTask) {
-           Msg.info("All tasks have been dispatched. Let's tell everybody the computation is over.");
+        if (task instanceof FinalizeTask) {
+           Msg.info("Got a finalize task. Let's forward that we're done.");
            
-           for (int cpt = 0; cpt<slavesCount; cpt++) {
-              slaves[cpt].put(0,new FinalizeTask());
+           for (int cpt = firstOutput; cpt<=lastOutput; cpt++) {
+              Task tf = new FinalizeTask();
+              tf.send("slave_"+cpt);
            }
            break;
         }
-        BasicTask task = (BasicTask)t;
+        int dest = firstOutput + (taskCount % slavesCount);
         
-        Msg.info("Received \"" + task.getName() + "\" ");
-                   
-        Msg.info("Sending \"" + task.getName() + "\" to \"" + slaves[taskCount % slavesCount].getName() + "\"");
-        slaves[taskCount % slavesCount].put(0, task);
+        Msg.info("Sending \"" + task.getName() + "\" to \"slave_" + dest + "\"");
+        task.send("slave_"+dest);
            
         taskCount++;
       }