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