Created
July 12, 2010 19:27
-
-
Save abhin4v/472942 to your computer and use it in GitHub Desktop.
Simulation of Ring network topology using Actors in Scala
This file contains 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 RingTopologySimulation extends Application { | |
case class Message(count: Int) | |
class Node(id: Int, stops: Int) extends Actor { | |
var next: Node = null | |
def act() { | |
val startTime = System.nanoTime | |
loop { | |
react { | |
case msg: Message => { | |
if (id % stops == 0) { | |
println("%.0f messages/sec" format | |
(msg.count * 1e9/(System.nanoTime - startTime))) | |
} | |
next ! Message(msg.count + 1) | |
} | |
} | |
} | |
} | |
} | |
print("Number of nodes? ") | |
val noOfNodes = readInt | |
print("Number of probes? ") | |
val noOfProbes = readInt | |
val nodes = for { i <- 1 to noOfNodes } | |
yield new Node(i, noOfNodes/noOfProbes) | |
(nodes zip nodes.tail) foreach { z => z._1.next = z._2 } | |
nodes.last.next = nodes.head | |
nodes foreach { _.start } | |
nodes.head ! Message(0) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment