Thanks Steve. The code is called by another function that handles the
agent state so no it is not called directly. Here is the additional
code (it is a simple Newbie web server learning project):

Here is how the web server is run:
========================
(ws-run 3000 wh-handler)

The web server:
===========

(import '(java.io BufferedReader InputStreamReader PrintWriter))

;*******************************************************************;

(defn reader-make-from-socket [socket]
        (BufferedReader. (InputStreamReader. (.getInputStream socket))))

;*******************************************************************;

(def lines-read-max 100)

(defn lines-read [reader lines]
        (let [line (.readLine reader)]
                (cond
                        (= line nil)                                            
[]
                        (= line "")                                             
        lines
                        (> (count lines) lines-read-max)        lines
                        true                                                    
        (lines-read reader (concat lines [line])))))

;*******************************************************************;

(defn http-line-to-arg [cmd line]
        (let [key (first (re-seq #"\S+:" line))]
        (let [value (apply str (drop (count key) line))]
                (conj cmd {(apply str (drop-last key)) (apply str (drop 1
value))}))))

(defn http-line-to-url [line]
        (let [[cmd url http] (re-seq #"\S+" line)]
                {"Cmd" cmd "Url" url "Http" http}))

;*******************************************************************;

(defn cmd-make-from-http-lines2 [cmd lines]
        (if (= (first lines) nil)
                cmd
                (cmd-make-from-http-lines2 (http-line-to-arg cmd (first lines))
(rest lines))))

(defn cmd-make-from-http-lines [lines]
        (if (= (first lines) nil)
                {}
                (cmd-make-from-http-lines2 (http-line-to-url (first lines)) 
(rest
lines))))

(defn cmd-print [cmd]
        (let [ks (sort (keys cmd))]
                (println "Cmd:")
                (doseq [k ks]
                        (println (format "  \"%s\" => \"%s\"" k (cmd k))))))

(defn cmd-read [socket]
        (let [reader (reader-make-from-socket socket) lines (lines-read
reader []) cmd (cmd-make-from-http-lines lines)]
                (cmd-print cmd)
                (conj cmd {:socket socket})))

;*******************************************************************;

(defn ws-agent [agentCounter socket fn]
        (let [cmd (cmd-read socket)]
                (fn cmd)
                (.close socket)
                (+ agentCounter 1)))

(defn ws-accept [socket tasks fn]
        (println (format "Server accepting (clients done %d)" (deref tasks)))
        (let [client (.accept socket)]
                (send-off tasks ws-agent client fn)))

(defn ws-run [port fn]
        (let [socket (java.net.ServerSocket. port) tasks (agent 0)]
                (dotimes [i 3] (ws-accept socket tasks fn))
                (await tasks)
                (println (format "Server done (%d clients)" (deref tasks)))
                (.close socket)))

The web handler
============

(load-file "Web Server.clj")

;*******************************************************************;

(defn http-write-ok [out]
        (.write out "HTTP/1.1 200 OK\r\n\r\n"))

;*******************************************************************;

(defn html-write-head [out]
        (.write out "<html><body>"))

(defn html-write-tail [out]
        (.write out "</html></body>"))

; THIS IS WHERE IT GOES WRONG IF I CHANGE (keys cmd) to (sort (keys
cmd)):

(defn html-write-cmd [out cmd]
        (doseq [key (keys cmd)]
                (.write out
                        (format "<b>\"%s\"</b> => \"%s\"<br>" key (cmd key)))))

(defn html-write-test [out cmd]
        (http-write-ok out)
        (html-write-head out)
        (.write out "<h1>Hello from Clojure!</h1>")
        (.write out (str "<h2>" (System/currentTimeMillis) "</h2>"))
        (html-write-cmd out cmd)
        (html-write-tail out))

;*******************************************************************;

(defn wh-make-writer [socket]
        (PrintWriter. (.getOutputStream socket)))

(defn wh-write-test [cmd]
        (let [out (wh-make-writer (cmd :socket))]
                (html-write-test out cmd)
                (.flush out)))

;*******************************************************************;

(defn wh-handler [cmd]
        (wh-write-test cmd))

;*******************************************************************;

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To post to this group, send email to clojure@googlegroups.com
To unsubscribe from this group, send email to 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to