From Swing to Jwt
The other day, I read the blog post Agents of Swing by Stuart Sierra describing the use of agents in combination with a Swing UI. As soon as I read it I wanted to implement it with Jwt in the hope it would be easy. And I wasn't disappointed!
As I had expected, I was able to reuse the agents code from Stuart and integrate it very easily in my web app. Actually, the development op the web interface was similar to the development of the swing interface. It's so similar I can only paste it here without further explanations:
; Create the Timer that will update results on the page ; by calling the function passed (defn create-timer [wapp fn-update-results] (let [timer (WTimer.)] (.setInterval timer 1000) (.. timer timeout (addListener wapp (create-listener [event] (fn-update-results)))) timer)) (defn make-flipper-app [env] (let [wapp (new WApplication env) root (.getRoot wapp) result-text (WText. "") flipper (new-flipper) fn-update-results (fn  (.setText result-text ( str "error: " (error @flipper) " after " (:total @flipper) " flips, of which " (:heads @flipper) " where heads" ))) timer (create-timer wapp fn-update-results) ] (.setTitle wapp "Agents of Jwt") (.. (WPushButton. "Start counter" root) clicked (addListener wapp ( create-listener [mouse-event] (send flipper start) (fn-update-results) (.start timer) ))) (.. (WPushButton. "Stop counter" root) clicked (addListener wapp ( create-listener [mouse-event] (send flipper stop) (.stop timer) (fn-update-results) ))) (.addWidget root (WBreak.)) (.addWidget root result-text) (.addWidget root (WBreak.)) wapp))
The only thing needed in addition to this is importing the necessary jwt classes, the definition of the servlet, and starting the server, the last two being done with this code:
(def my-servlet (proxy [WtServlet]  (createApplication [env] (make-flipper-app env)))) (def my-server (jetty-server)) (add-servlet my-server my-servlet "/*") ;(org.bitumenframework.jettify.clj_jettify/start-server jwt-toss/my-server)
Here is the file with the complete code that I was able to run:
If someone's interested, I can possibly bundle a complete environment to be able to run it immediately. Just give me a sign.