]> git.pond.sub.org Git - eow/blob - web.lisp
fix ajax update handler, break lines
[eow] / web.lisp
1 (in-package :empire-web)
2
3 (declaim (optimize (space 0) (speed 0) (safety 3) (debug 3)))
4
5
6 (defvar *this-file* (load-time-value
7                      (or #.*compile-file-pathname* *load-pathname*)))
8
9 (defvar *this-dir* (make-pathname :host (pathname-host *this-file*)
10                                   :device (pathname-device *this-file*)
11                                   :directory (pathname-directory *this-file*)))
12
13 (defparameter +templates-root+ (namestring *this-dir*))
14 (defparameter +root-url+ "/eow/")
15 (defparameter +web-root-base+ "/eow")
16 (defparameter +web-root+ (concatenate 'string +web-root-base+ "/"))
17 (defparameter +static-web-root+ (concatenate 'string +web-root+ "static/"))
18 (defparameter +static-files-root+ (concatenate 'string +templates-root+ "static/"))
19 (defparameter +login-page+ (concatenate 'string +static-web-root+ "login.html"))
20 (defparameter +root-page-file+ "root.html")
21
22 (defun string-starts-with (string prefix)
23   ;; (from Hunchentoot)
24   (let ((mismatch (mismatch string prefix :test #'char=)))
25     (or (null mismatch)
26         (>= mismatch (length prefix)))))
27
28 (defun serve-static ()
29   "Handle a request for a file under static/ directory"
30   (let* ((script-name (hunchentoot:script-name hunchentoot:*request*))
31          (fname (subseq script-name (length +static-web-root+)))
32          (fullname (concatenate 'string +static-files-root+ fname)))
33     (hunchentoot:handle-static-file fullname)))
34
35 (defclass session ()
36   ((update-queue
37     :accessor update-queue
38     :initform (locked-queue:create)
39     :documentation "Updates to be sent to the browser")
40    (update-thread
41     :accessor update-thread
42     :initform nil
43     :documentation "COMET thread waiting for updates of non-NIL")
44    (connection
45    :accessor connection
46    :documentation "Connection to the empire game server")))
47
48 (defun make-session (username password)
49   (let* ((session (make-instance 'session))
50          (connection (empire:connect :user username
51                                      :password password
52                                      :session session)))
53     (setf (slot-value session 'connection) connection)
54     session))
55
56 (defgeneric send (session string))
57 (defgeneric prompt (session string))
58 (defgeneric data (session message))
59
60 (defvar *empire-session*)
61
62 (defmacro with-session (&body body)
63   `(let ((*empire-session* (hunchentoot:session-value 'session)))
64      (if *empire-session*
65          (progn ,@body)
66          (hunchentoot:redirect +login-page+))))
67
68 (defun update ()
69   "Send stream of updates to client"
70   (with-session
71     ; kill previous update thread
72     (when (update-thread *empire-session*)
73       (empire-log:info "~a: Killing update thread ~a."
74                        *empire-session* (update-thread *empire-session*))
75       (sb-thread:terminate-thread (update-thread *empire-session*)))
76
77     ; make current thread the update thread
78     (setf (update-thread *empire-session*) sb-thread:*current-thread*)
79
80     (let ((next-updates (locked-queue:dequeue-all (update-queue *empire-session*))))
81       ; There's a race here. The next update thread might kill this one before
82       ; sending the reply.
83       (setf (update-thread *empire-session*) nil)
84       (nconc next-updates (list (parenscript:ps* '(next))))
85       (apply #'concatenate 'string next-updates))))
86
87 (defmethod send ((s session) string)
88   "Push a javascript update fragment to the client."
89   (locked-queue:enqueue (update-queue s) string))
90
91 (defmethod prompt ((s session) p)
92   (send s (parenscript:ps* `(prompt ,p))))
93
94 (defmethod data ((s session) message)
95   (send s (parenscript:ps* `(msg ,message))))
96
97 ;; destination of login-form
98 (defun login-action ()
99   (handler-case
100       (let* ((user (hunchentoot:post-parameter "username"))
101              (pass (hunchentoot:post-parameter "password"))
102              (session (make-session user pass)))
103         (setf (hunchentoot:session-value 'session) session)
104         (empire-log:info "~a: User ~a logging in." session user)
105         (hunchentoot:redirect +root-url+))
106     (usocket:connection-refused-error (e)
107       (format nil "Connection error: ~a~%" e))))
108
109 (defun command-action ()
110   (with-session
111       (empire:command (connection *empire-session*) (hunchentoot:get-parameter "q"))
112       "ok"))
113
114 (defun root-page ()
115   (with-session
116     (hunchentoot:handle-static-file (concatenate 'string +static-files-root+ +root-page-file+))))
117
118 (defun my-request-p (script-name)
119   (string-starts-with script-name +web-root+))
120
121 (defun dispatch (request)
122   (let ((script-name (hunchentoot:script-name request)))
123     (cond
124       ((not (my-request-p script-name)) nil) ; do not handle this request
125       ((string= script-name "/eow/update") 'update)
126       ((string= script-name "/eow/login") 'login-action)
127       ((string= script-name "/eow/command") 'command-action)
128                                         ; go to the start page
129       ((or (string-equal script-name +web-root-base+)
130            (string-equal script-name +web-root+)) 'root-page)
131                                         ; serve static file
132       ((string-starts-with script-name +static-web-root+) 'serve-static))))
133
134
135 (defun start ()
136   (empire-log:info "Startup")
137   (pushnew 'dispatch hunchentoot:*dispatch-table* :test #'eq))