Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'hypervisor' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid into hypervisor
[simgrid.git] / examples / scala / masterslave / Forwarder.scala
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 Stream._
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 class Forwarder(host:Host, name:String, args:Array[String]) extends Process(host,name,args) {
18
19    def main(args: Array[String]){
20       if (args.length < 3) {     
21          Msg.info("Forwarder needs 3 arguments (input mailbox, first output mailbox, last one)")
22          Msg.info("Got "+args.length+" instead")
23          System.exit(1)
24       }
25       val input = args(0).toInt         
26       val firstOutput = args(1).toInt           
27       val lastOutput = args(2).toInt            
28       
29       var taskCount = 0
30       val slavesCount = lastOutput - firstOutput + 1
31       Msg.info("Receiving on 'slave_"+input+"'")
32       var cont = true
33       
34       continually({Task.receive("slave_"+input)})
35         .takeWhile(!_.isInstanceOf[FinalizeTask])
36         .foreach(task => {
37           val dest = firstOutput + (taskCount % slavesCount)
38           Msg.info("Sending \"" + task.getName() + "\" to \"slave_" + dest + "\"")
39           task.send("slave_"+dest)
40           taskCount += 1
41         })
42
43       Msg.info("Got a finalize task. Let's forward that we're done.")
44       for (cpt <- firstOutput to lastOutput) {
45         val tf = new FinalizeTask()
46         tf.send("slave_"+cpt)
47       }
48          
49       Msg.info("I'm done. See you!")
50    }
51 }
52