Created
July 12, 2010 19:28
-
-
Save abhin4v/472946 to your computer and use it in GitHub Desktop.
Simulation of Star network topology using Actors in Scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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