Logo AND Algorithmique Numérique Distribuée

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