I'm new to scala and I'm trying to write a program in scala which creates multiple (say, 30) actors and passes messages between them.
Below is what i have managed till now:
import scala.actors.Actor
import scala.util.Random
class MyActor(val id:Int, val N:Int) extends Actor {
def act() {
println ("Starting actor: " + id)
/**
react{
case str : String =>
println("Received Msg: " + str)
val randNo : Int = Random.nextInt(N)
println("Actor " + id + " Picking a random actor: " + randNo)
// Here, I should forward the message received to the ALREADY created and started actors
// val objActor = new MyActor(randNo : Int, N : Int)
// objActor.start
// objActor ! str
}
*/
}
}
object Main {
def main(args:Array[String]) {
if(args.length == 0)
{
println("Usage scala Main <numNodes>")
sys.exit()
}
val N : Int = (args(0)).toInt
// Starting all actors
for (i: Int <- 0 to N-1) {
val a = new MyActor(i : Int, N : Int)
println ("About to start actor " + a.id)
a.start
// a!"Broadcast this msg to all actors"
}
}
}
The objective of the program is to create multiple actors and forward a string from one actor to another.
The above code creates 'N' number of actors given as command line argument. These actors are created and started by the object Main. Main should send a message to ONLY ONE of the above created actors. The actor which receives the message from the Main should forward the same message to another ALREADY created/started actor.
Is this possible? If so, can you please guide me on the right direction?
Thanks in advance, MS