X-Git-Url: http://git.pond.sub.org/?p=eow;a=blobdiff_plain;f=web.lisp;h=c7ead803bf9f3a808610b80a10d0a286ed2447db;hp=40b74066ca506a21ff2c3e9fe1617c6bdf8e9ae8;hb=effa86fe2559437f6265a1495c669ac576648ca1;hpb=bf4c8fd00a49cef4ce907a341722347e640ff83b diff --git a/web.lisp b/web.lisp index 40b7406..c7ead80 100644 --- a/web.lisp +++ b/web.lisp @@ -11,11 +11,13 @@ :directory (pathname-directory *this-file*))) (defparameter +templates-root+ (namestring *this-dir*)) -(defparameter +root-url+ "/eow/static/test.html") +(defparameter +root-url+ "/eow/") (defparameter +web-root-base+ "/eow") (defparameter +web-root+ (concatenate 'string +web-root-base+ "/")) (defparameter +static-web-root+ (concatenate 'string +web-root+ "static/")) (defparameter +static-files-root+ (concatenate 'string +templates-root+ "static/")) +(defparameter +login-page+ (concatenate 'string +static-web-root+ "login.html")) +(defparameter +root-page-file+ "root.html") (defun string-starts-with (string prefix) ;; (from Hunchentoot) @@ -25,27 +27,101 @@ (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))) -(defvar *update-queue* (locked-queue:create)) +(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"))) + +(defun make-session (username password) + (let* ((session (make-instance 'session)) + (connection (empire:connect :user username + :password password + :session session))) + (setf (slot-value session 'connection) connection) + session)) + +(defgeneric send (session string)) +(defgeneric prompt (session string)) +(defgeneric data (session message)) + +(defvar *empire-session*) + +(defmacro with-session (&body body) + `(let ((*empire-session* (hunchentoot:session-value 'session))) + (if *empire-session* + (progn ,@body) + (hunchentoot:redirect +login-page+)))) (defun update () "Send stream of updates to client" - (locked-queue:dequeue *update-queue*)) + (with-session + (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*))) + (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." + (locked-queue:enqueue (update-queue s) string)) + +(defmethod prompt ((s session) p) + (send s (parenscript:ps* `(prompt ,p)))) + +(defmethod data ((s session) message) + (send s (parenscript:ps* `(msg ,message)))) + +;; destination of login-form +(defun login-action () + (handler-case + (let* ((user (hunchentoot:post-parameter "username")) + (pass (hunchentoot:post-parameter "password")) + (session (make-session user pass))) + (setf (hunchentoot:session-value 'session) session) + (empire-log:info "~a: User ~a logging in." session user) + (hunchentoot:redirect +root-url+)) + (usocket:connection-refused-error (e) + (format nil "Connection error: ~a~%" e)))) + +(defun command-action () + (with-session + (empire:command (connection *empire-session*) (hunchentoot:get-parameter "q")) + "ok")) + +(defun root-page () + (with-session + (hunchentoot:handle-static-file (concatenate 'string +static-files-root+ +root-page-file+)))) (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 ((string= script-name "/eow/update") 'update) + ((string= script-name "/eow/login") 'login-action) + ((string= script-name "/eow/command") 'command-action) ((or (string-equal script-name +web-root-base+) - (string-equal script-name +web-root+)) (redirect +root-url+)) ; go to the start page + (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 (defun start () - (pushnew 'dispatch *dispatch-table* :test #'eq)) - + (empire-log:info "Startup") + (pushnew 'dispatch hunchentoot:*dispatch-table* :test #'eq))