Skip to content

Instantly share code, notes, and snippets.

@abhin4v
Created July 12, 2010 19:27
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abhin4v/472942 to your computer and use it in GitHub Desktop.
Save abhin4v/472942 to your computer and use it in GitHub Desktop.
Simulation of Ring network topology using Actors in Scala
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