]> git.pond.sub.org Git - eow/blob - web.lisp
c7ead803bf9f3a808610b80a10d0a286ed2447db
[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       (when (update-thread *empire-session*)
72         (empire-log:info "~a: Killing update thread ~a." *empire-session* (update-thread *empire-session*))
73         (sb-thread:terminate-thread (update-thread *empire-session*)))
74     (setf (update-thread *empire-session*) sb-thread:*current-thread*)
75     (let ((next-updates (locked-queue:dequeue-all (update-queue *empire-session*))))
76       ; There's a race here. The next update thread might kill this one before
77       ; sending the reply.
78       (setf (update-thread *empire-session*) nil)
79       (nconc next-updates (list (parenscript:ps* '(next))))
80       (apply #'concatenate 'string next-updates))))
81
82 (defmethod send ((s session) string)
83   "Push a javascript update fragment to the client."
84   (locked-queue:enqueue (update-queue s) string))
85
86 (defmethod prompt ((s session) p)
87   (send s (parenscript:ps* `(prompt ,p))))
88
89 (defmethod data ((s session) message)
90   (send s (parenscript:ps* `(msg ,message))))
91
92 ;; destination of login-form
93 (defun login-action ()
94   (handler-case
95       (let* ((user (hunchentoot:post-parameter "username"))
96              (pass (hunchentoot:post-parameter "password"))
97              (session (make-session user pass)))
98         (setf (hunchentoot:session-value 'session) session)
99         (empire-log:info "~a: User ~a logging in." session user)
100         (hunchentoot:redirect +root-url+))
101     (usocket:connection-refused-error (e)
102       (format nil "Connection error: ~a~%" e))))
103
104 (defun command-action ()
105   (with-session
106       (empire:command (connection *empire-session*) (hunchentoot:get-parameter "q"))
107       "ok"))
108
109 (defun root-page ()
110   (with-session
111     (hunchentoot:handle-static-file (concatenate 'string +static-files-root+ +root-page-file+))))
112
113 (defun dispatch (request)
114   (let ((script-name (hunchentoot:script-name request)))
115     (cond
116       ((not (string-starts-with script-name +web-root+)) nil) ; do not handle this request
117       ((string= script-name "/eow/update") 'update)
118       ((string= script-name "/eow/login") 'login-action)
119       ((string= script-name "/eow/command") 'command-action)
120       ((or (string-equal script-name +web-root-base+)
121            (string-equal script-name +web-root+)) 'root-page) ; go to the start page
122       ((string-starts-with script-name +static-web-root+) 'serve-static)))) ; serve static file
123
124
125 (defun start ()
126   (empire-log:info "Startup")
127   (pushnew 'dispatch hunchentoot:*dispatch-table* :test #'eq))