Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
New version of the java bindings by Malek, passing all [existing] tests
[simgrid.git] / examples / java / basic / Forwarder.java
1 /*
2  * $Id$
3  *
4  * Copyright 2006,2007 Martin Quinson, Malek Cherier         
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       Msg.info("hello!");
17       
18       int slavesCount = args.length;
19       Host[] slaves = new Host[slavesCount];
20       
21       for (int i = 0; i < args.length; i++) {
22          try {        
23             slaves[i] = Host.getByName(args[i]);
24          } catch (HostNotFoundException e) {
25             Msg.info("Buggy deployment file");
26             e.printStackTrace();
27             System.exit(1);
28          }
29       }
30       
31       Channel channel = new Channel(0);
32                 
33       int taskCount = 0;
34       while(true) {
35                         
36          BasicTask task = (BasicTask)channel.get();
37          
38          Msg.info("Received \"" + task.getName() + "\" ");
39          
40          if(task.getData() == 221297) {
41             Msg.info("All tasks have been dispatched. Let's tell everybody the computation is over.");
42             
43             for (int cpt = 0; cpt<slavesCount; cpt++) {
44                BasicTask forwardedtask = new BasicTask("finalize", 0, 0);
45                forwardedtask.setData(221297);
46                
47                channel.put(forwardedtask,slaves[cpt]);
48             }
49             break;
50             
51          } else {
52             
53             Msg.info("Sending \"" + task.getName() + "\" to \"" + slaves[taskCount % slavesCount].getName() + "\"");
54             channel.put(task, slaves[taskCount % slavesCount]);
55             
56             taskCount++;
57          }
58          
59          Msg.info("I'm done. See you!");
60       }
61    }
62 }
63