Skip to content

Instantly share code, notes, and snippets.

@abhin4v
Created July 15, 2010 19:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abhin4v/477392 to your computer and use it in GitHub Desktop.
Save abhin4v/477392 to your computer and use it in GitHub Desktop.
Simulation of Ring network topology using Agents in Clojure
(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