Skip to content
This repository has been archived by the owner on Dec 26, 2024. It is now read-only.

Commit

Permalink
gui stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
prasincs committed May 4, 2011
1 parent 65070e9 commit 5b5f34f
Show file tree
Hide file tree
Showing 8 changed files with 174 additions and 36 deletions.
Binary file modified data/ExperimentsData.xlsx
Binary file not shown.
28 changes: 6 additions & 22 deletions library/experiments/hammer-lang.clj
Original file line number Diff line number Diff line change
@@ -1,32 +1,16 @@

(use 'thor.lang)
(use '(incanter core stats charts))

(defduration 100)

(simulation-init)

(def mt (atom ()))

(at 10
(println "test " (get-current-time)))

(at 20
(println "test2 " (get-current-time)))

(every 5
(println "clock " (get-current-time) ))

(at 15
(every 10
(println "clock2 " (get-current-time))
))
(every 1 (do
(println "adding")
(swap! mt concat (list (* (get-current-time) 2)))))

(swap! mt conj
(* (get-current-time)
2))))
(at 100
(let [x (range 1 101)
x_2 @mt]
(view (bar-chart x x_2))))

x_2 (reverse @mt)]
(view (bar-chart x x_2 :y-label "2*x"))))
(simulation-run)
4 changes: 2 additions & 2 deletions library/experiments/simple-dht.clj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
{
:title "Simple DHT"
:devices {:types ["phone-1"]
:number 1e4
:number 10
:sample 'random
}
:area {:width 100 :height 100}
Expand Down Expand Up @@ -47,7 +47,7 @@
(println "Hop Count Average"
(/ (sum @hop-count-list) (get-total-samples)))
) )

(show-nodes)
(at 10
(do
(println "Storing datas")
Expand Down
6 changes: 4 additions & 2 deletions src/thor/dht/simple.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(ns thor.dht.simple
(:use thor.node thor.data-store thor.messages)
(:require thor.queue)
(:require thor.queue thor.ui.window)
)
(use '[clojure.contrib.math :only (expt)])

Expand Down Expand Up @@ -153,4 +153,6 @@
))
)


(defn show-nodes []
(thor.ui.window/init-window @*nodelist*)
)
22 changes: 17 additions & 5 deletions src/thor/lang.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
(ns #^{:doc "A description parser for thor AKA Hammer"}
thor.lang
(:use clojure.contrib.logging thor.utils)
(:require thor.queue thor.node thor.net.wireless)
(:require
thor.queue
thor.node
thor.net.wireless
)
)

(use '[clojure.contrib.math :only (expt) ])
Expand All @@ -14,13 +18,14 @@
(def *current-time* (atom 1))
(def *keyspace* (atom (expt 2 32)))
(def *simulation-running* (atom false))

(def *nodelist* (atom ()))

; medium types anything but 0
(def wired 2) ; magic number alert! ;)
(def wireless 3)
(def *network-type* (atom 0))


(def *wireless-network*
(atom {:bandwidth 10000 ; wonky magic numbers
:frequency 10000
Expand Down Expand Up @@ -255,6 +260,7 @@

(defn end-simulation []
(thor.queue/empty!)
(reset! *nodelist* () )
(reset! *simulation-running* false)
)

Expand All @@ -271,11 +277,17 @@
(defn new-node [attrs]

(if (contains? attrs :device)
(atom (thor.node/create-node
(let [n (atom (thor.node/create-node
(assoc attrs :device-attrs
(get-device
(attrs :device)))
))))
(attrs :device)))))]
(swap! *nodelist* conj n)
n
)))

(defn draw-node []

)

(defn move-node [n op pos]
(reset! n (thor.node/node-move @n op pos)))
Expand Down
6 changes: 4 additions & 2 deletions src/thor/node.clj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
(defn create-memory [size]
(struct-map mem :total size :free size))


(defrecord Node
[id
memory
Expand Down Expand Up @@ -127,7 +128,7 @@
{:max-width 100
:max-height 100}}
& [ { :keys [id] :or
{id (->
{:id (->
(* max-width
max-height)
rand int)}}
Expand Down Expand Up @@ -230,13 +231,14 @@
[num width height &[id-range] ]
; if an ID range is given, create nodes with ID in the range
(debug "random node list")
(println "width->" width "height->" height)
(let [nlist (atom ())]
(if (nil? id-range)
(dotimes [n num]
(swap! nlist
conj
(create-random-node
width height { :id n }
{:max-width width :max-height height} { :id n }
)))

; else
Expand Down
136 changes: 136 additions & 0 deletions src/thor/ui/node_viewer.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
(ns
thor.ui.node-viewer
(:import [javax.swing JFrame JList JButton BoxLayout JPanel DefaultListModel BoxLayout ListSelectionModel JScrollPane ScrollPaneConstants ]
[java.awt Dimension GridBagLayout BorderLayout]

[javax.swing.event ListSelectionListener]
))

(use '[incanter core processing io])
(def frame
(doto (JFrame. "Project Thor")
(.setDefaultCloseOperation (JFrame/EXIT_ON_CLOSE))
(.setSize (Dimension. 500 500))
))

(def radius 100)
(def textValue (atom 0))
(def nodelist (atom ()))

; keep a record of states to replay later
(def states (atom {}))

(def statesListModel (DefaultListModel.))
(def statesList (let [stateList (doto (JList.
statesListModel)
(.setSelectionMode ListSelectionModel/SINGLE_SELECTION)
(.setFixedCellWidth 200)

)
]
(doto stateList

(.addListSelectionListener
(proxy [ListSelectionListener] []

(valueChanged [evt]
(let [selected (.getSelectedValue stateList)]
(println "selected " selected )
(apply-state selected)
)
)
)
)

)

(JScrollPane. stateList ScrollPaneConstants/VERTICAL_SCROLLBAR_ALWAYS ScrollPaneConstants/HORIZONTAL_SCROLLBAR_AS_NEEDED
)))

(defn add-states-to-list [model elements]
(doseq [elem elements]
(.addElement model elem)
)
)

(defn get-sketch []


(sketch

(setup []
(doto this

(framerate 15)
(size 500 500)
(stroke-weight 1)
)
)
(draw []
(doto this
(background 255)
(smooth)
(fill 0)

(text (format "Test:%d" @textValue) 0 16 ))

(doseq [n @nodelist]
(let [loc (-> n :location)]
(doto this
(fill 0)
(ellipse (:x loc) (:y loc) 20 20)
(fill 64 187 128 100)
;(println loc)
(ellipse (:x loc) (:y loc) radius radius)
(fill 0)
(text (format "Node %d" (:id n)) (:x loc) (:y loc))
))

)))

)


(def sktch (get-sketch))

(defn init-window []

(doto frame

(.setLayout (BoxLayout. (.getContentPane frame)
BoxLayout/X_AXIS
))
(.add sktch)
(.add statesList)
(.setVisible true)
)
(.init sktch)
)

(defn apply-state [time]
(let [state (get @states time)]
(reset! textValue (:textValue state))
(reset! nodelist (:nodelist state))
))

(defn set-state [time {:keys [t nodes]}]
(println "set-state")
(swap! states assoc time
{:textValue t
:nodelist nodes
}
)
(add-states-to-list
statesListModel
(list time))
(apply-state time)
)

(defn add-nodes [time {:keys [text nodes]}]
(println nodes)
(set-state time
{:t text
:nodes nodes})

;(reset! t tval)
)
8 changes: 5 additions & 3 deletions src/thor/ui/window.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(ns thor.ui.window
(:require clojure.core thor.network))
(:require clojure.core))
(import '(javax.swing JFrame JButton JPanel JOptionPane)
'(java.awt.event ActionListener)
'(java.awt Color Dimension GridLayout))
Expand All @@ -11,7 +11,8 @@
(.setColor g (Color. 0 0 0))
(.fillOval g (get location :x) (get location :y) 15 15)
))



(defn create-canvas [node-list]
(proxy [JPanel] []
(paintComponent [g]
Expand All @@ -22,7 +23,8 @@
(getPreferredSize [] (Dimension. 500 500))))


(def window (JFrame. "Project Thor"))
(def window
(JFrame. "Project Thor"))


(defn init-window [ node-list ]
Expand Down

0 comments on commit 5b5f34f

Please sign in to comment.