Permalink
Browse files

initial work on the http-handler for swarmiji

  • Loading branch information...
1 parent 94fa680 commit 4c80fb979cf5a72ae100fe57998969b073481ae4 @amitrathore committed Mar 26, 2009
View
@@ -0,0 +1,6 @@
+*~
+*#*
+.classpath
+.project
+config/config.clj
+scripts/furtive
View
3 README
@@ -0,0 +1,3 @@
+A system that helps implement and run programs that want to use multiple cores/processors for parallel processing.
+
+The programming language is Clojure.
Binary file not shown.
Binary file not shown.
Binary file not shown.
View
@@ -0,0 +1,3 @@
+find . -name '*~' | xargs rm
+find . -name '*#*' | xargs rm
+find . -name '*DS_Store' | xargs rm
@@ -0,0 +1,6 @@
+(require '(org.runa.swarmiji.master [web-server :as web-server]))
+(require '(org.runa.swarmiji.master [grizzly-utils :as grizzly-utils]))
+
+(let [handler (web-server/http-request-handler)]
+ (grizzly-utils/start-web-server handler 8020))
+
@@ -0,0 +1,10 @@
+(ns org.runa.swarmiji.master.grizzly-utils)
+
+(import '(com.sun.grizzly.http SelectorThread))
+
+(defn start-web-server [http-request-handler port]
+ (let [st (SelectorThread.)]
+ (.setAdapter st http-request-handler)
+ (.setPort st port)
+ (.initEndpoint st)
+ (.startEndpoint st)))
@@ -0,0 +1,41 @@
+(ns org.runa.swarmiji.master.web-server)
+
+(import '(com.sun.grizzly.http SelectorThread))
+(import '(com.sun.grizzly.tcp Adapter OutputBuffer Request Response))
+(import '(com.sun.grizzly.util.buf ByteChunk))
+(import '(java.net HttpURLConnection))
+
+(defn is-get? [request]
+ (= (.toUpperCase (.method request)) "GET"))
+
+(defn response-as-chunk [grizzly-response response-text]
+ (let [response-bytes (.getBytes response-text)
+ response-length (count response-bytes)
+ out-chunk (ByteChunk.)]
+ (.setStatus grizzly-response HttpURLConnection/HTTP_OK)
+ (.setContentLength grizzly-response response-length)
+ (.setContentType grizzly-response "text/plain")
+ (.append out-chunk response-bytes 0 response-length)
+ out-chunk))
+
+(defn service-http-request [request response]
+ (do
+ (println "Recieved request from" (.requestURI request))
+ (if (is-get? request)
+ (let [out-buffer (.getOutputBuffer response)
+ response-chunk (response-as-chunk response "Hello World!")]
+ (.doWrite out-buffer response-chunk response)
+ (.finish response)))))
+
+(defn after-service [request response]
+ (do
+ (.recycle request)
+ (.recycle response)))
+
+(defn http-request-handler []
+ (proxy [Adapter] []
+ (service [req res]
+ (service-http-request req res))
+ (afterService [req res]
+ (after-service req res))))
+

0 comments on commit 4c80fb9

Please sign in to comment.