]> git.pond.sub.org Git - eow/blobdiff - web.lisp
fix ajax update handler, break lines
[eow] / web.lisp
index f97b18da710f536d792f98d3ea321b59194ec59a..127a87ecc7bb61716f5e454b96ad946b7efa1bf7 100644 (file)
--- a/web.lisp
+++ b/web.lisp
 
 (defun serve-static ()
   "Handle a request for a file under static/ directory"
-  (let* ((script-name (script-name))
+  (let* ((script-name (hunchentoot:script-name hunchentoot:*request*))
          (fname (subseq script-name (length +static-web-root+)))
          (fullname (concatenate 'string +static-files-root+ fname)))
-    (handle-static-file fullname)))
+    (hunchentoot:handle-static-file fullname)))
 
 (defclass session ()
   ((update-queue
     :accessor update-queue
     :initform (locked-queue:create)
     :documentation "Updates to be sent to the browser")
+   (update-thread
+    :accessor update-thread
+    :initform nil
+    :documentation "COMET thread waiting for updates of non-NIL")
    (connection
    :accessor connection
    :documentation "Connection to the empire game server")))
 (defvar *empire-session*)
 
 (defmacro with-session (&body body)
-  `(let ((*empire-session* (session-value 'session)))
+  `(let ((*empire-session* (hunchentoot:session-value 'session)))
      (if *empire-session*
         (progn ,@body)
-        (redirect +login-page+))))
+        (hunchentoot:redirect +login-page+))))
 
 (defun update ()
   "Send stream of updates to client"
   (with-session
-    (locked-queue:dequeue (update-queue *empire-session*))))
+    ; kill previous update thread
+    (when (update-thread *empire-session*)
+      (empire-log:info "~a: Killing update thread ~a."
+                      *empire-session* (update-thread *empire-session*))
+      (sb-thread:terminate-thread (update-thread *empire-session*)))
+
+    ; make current thread the update thread
+    (setf (update-thread *empire-session*) sb-thread:*current-thread*)
+
+    (let ((next-updates (locked-queue:dequeue-all (update-queue *empire-session*))))
+      ; There's a race here. The next update thread might kill this one before
+      ; sending the reply.
+      (setf (update-thread *empire-session*) nil)
+      (nconc next-updates (list (parenscript:ps* '(next))))
+      (apply #'concatenate 'string next-updates))))
 
 (defmethod send ((s session) string)
   "Push a javascript update fragment to the client."
 ;; destination of login-form
 (defun login-action ()
   (handler-case
-      (let* ((user (post-parameter "username"))
-            (pass (post-parameter "password"))
+      (let* ((user (hunchentoot:post-parameter "username"))
+            (pass (hunchentoot:post-parameter "password"))
             (session (make-session user pass)))
-       (setf (session-value 'session) session)
+       (setf (hunchentoot:session-value 'session) session)
        (empire-log:info "~a: User ~a logging in." session user)
-       (redirect +root-url+))
+       (hunchentoot:redirect +root-url+))
     (usocket:connection-refused-error (e)
       (format nil "Connection error: ~a~%" e))))
 
 (defun command-action ()
   (with-session
-      (empire:send-message (connection *empire-session*) (get-parameter "q"))))
+      (empire:command (connection *empire-session*) (hunchentoot:get-parameter "q"))
+      "ok"))
 
 (defun root-page ()
   (with-session
-    (handle-static-file (concatenate 'string +static-files-root+ +root-page-file+))))
+    (hunchentoot:handle-static-file (concatenate 'string +static-files-root+ +root-page-file+))))
+
+(defun my-request-p (script-name)
+  (string-starts-with script-name +web-root+))
 
 (defun dispatch (request)
-  (let ((script-name (script-name request)))
+  (let ((script-name (hunchentoot:script-name request)))
     (cond
-      ((not (string-starts-with script-name +web-root+)) nil) ; do not handle this request
+      ((not (my-request-p script-name)) nil) ; do not handle this request
       ((string= script-name "/eow/update") 'update)
       ((string= script-name "/eow/login") 'login-action)
       ((string= script-name "/eow/command") 'command-action)
+                                       ; go to the start page
       ((or (string-equal script-name +web-root-base+)
-           (string-equal script-name +web-root+)) 'root-page) ; go to the start page
-      ((string-starts-with script-name +static-web-root+) 'serve-static)))) ; serve static file
+           (string-equal script-name +web-root+)) 'root-page)
+                                       ; serve static file
+      ((string-starts-with script-name +static-web-root+) 'serve-static))))
 
 
 (defun start ()
   (empire-log:info "Startup")
-  (pushnew 'dispatch *dispatch-table* :test #'eq))
-
+  (pushnew 'dispatch hunchentoot:*dispatch-table* :test #'eq))