Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add a scala masterslave example
[simgrid.git] / examples / scala / masterslave / Slave.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 package masterslave;
8
9 import Stream._
10 import org.simgrid.msg.Host
11 import org.simgrid.msg.HostFailureException
12 import org.simgrid.msg.Msg
13 import org.simgrid.msg.Task
14 import org.simgrid.msg.TaskCancelledException
15 import org.simgrid.msg.TimeoutException
16 import org.simgrid.msg.TransferFailureException
17 import org.simgrid.msg.Process
18
19 class Slave(host:Host, name:String, args:Array[String]) extends Process(host,name,args) {
20   def main(args:Array[String]){
21     if (args.length < 1) {
22       Msg.info("Slave needs 1 argument (its number)")
23       System.exit(1)
24     }
25
26     val num = args(0).toInt
27     
28     continually({Task.receive("slave_"+num)})
29       .takeWhile(!_.isInstanceOf[FinalizeTask])
30       .foreach(task => {
31         Msg.info("Received \"" + task.getName() +  "\". Processing it.");
32         try {
33           task.execute();
34         } catch {
35           case e:TaskCancelledException => {}
36         }
37       })
38
39     Msg.info("Received Finalize. I'm done. See you!")
40   }
41 }