Created
July 15, 2010 19:20
-
-
Save abhin4v/477392 to your computer and use it in GitHub Desktop.
Simulation of Ring network topology using Agents in Clojure
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
(ns net.abhinavsarkar | |
(:use [clojure.contrib.import-static])) | |
(import-static java.lang.Integer parseInt) | |
(def no-of-nodes | |
(do | |
(println "Number of nodes? ") | |
(parseInt (read-line)))) | |
(def no-of-probes | |
(do | |
(println "Number of probes? ") | |
(parseInt (read-line)))) | |
(def nodes (vec (map #(agent [% 0]) (range no-of-nodes)))) | |
(defn wrap-id [id] | |
(if (< id no-of-nodes) | |
id | |
(mod id no-of-nodes))) | |
(defn message-rate [start-time count id] | |
(* | |
(+ (* count no-of-nodes) id) | |
(/ 1e9 (- (System/nanoTime) start-time)))) | |
(defn message [[id count] start-time] | |
(do | |
(send (nth nodes (wrap-id (inc id))) message start-time) | |
(when (zero? (mod id (/ no-of-nodes no-of-probes))) | |
(println | |
(format "%.0f messages/sec" | |
(message-rate start-time count id)))) | |
(vector id (inc count)))) | |
(send (first nodes) message (System/nanoTime)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment