Skip to content

Instantly share code, notes, and snippets.

@abhin4v
Created July 12, 2010 19:28
Show Gist options
  • Save abhin4v/472946 to your computer and use it in GitHub Desktop.
Save abhin4v/472946 to your computer and use it in GitHub Desktop.
Simulation of Star network topology using Actors in Scala
import actors.Actor
import actors.Actor._
object StarTopologySimulation extends Application {
case class Message(sender: Node)
class Node(id: Int) extends Actor {
def act() {
val startTime = System.nanoTime
loop {
react {
case msg: Message => CentralNode ! Message(this)
}
}
}
}
object CentralNode extends Node(0) {
override def act() {
val startTime = System.nanoTime
var msgCount = 0
loop {
react {
case Message(sender) => {
msgCount += 1
if (msgCount % 1000 == 0) {
println("%.0f messages/sec" format
(msgCount * 1e9/(System.nanoTime - startTime)))
}
sender ! Message(CentralNode)
}
}
}
}
}
print("Number of nodes? ")
val noOfNodes = readInt
val nodes = for { i <- 1 to noOfNodes } yield new Node(i)
CentralNode start
nodes foreach { _.start }
nodes foreach { _ ! Message(CentralNode) }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment