1 (in-package :empire-web)
3 (declaim (optimize (space 0) (speed 0) (safety 3) (debug 3)))
6 (defvar *this-file* (load-time-value
7 (or #.*compile-file-pathname* *load-pathname*)))
9 (defvar *this-dir* (make-pathname :host (pathname-host *this-file*)
10 :device (pathname-device *this-file*)
11 :directory (pathname-directory *this-file*)))
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")
22 (defun string-starts-with (string prefix)
24 (let ((mismatch (mismatch string prefix :test #'char=)))
26 (>= mismatch (length prefix)))))
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)))
37 :accessor update-queue
38 :initform (locked-queue:create)
39 :documentation "Updates to be sent to the browser")
41 :accessor update-thread
43 :documentation "COMET thread waiting for updates of non-NIL")
46 :documentation "Connection to the empire game server")))
48 (defun make-session (username password)
49 (let* ((session (make-instance 'session))
50 (connection (empire:connect :user username
53 (setf (slot-value session 'connection) connection)
56 (defgeneric send (session string))
57 (defgeneric prompt (session string))
58 (defgeneric data (session message))
60 (defvar *empire-session*)
62 (defmacro with-session (&body body)
63 `(let ((*empire-session* (hunchentoot:session-value 'session)))
66 (hunchentoot:redirect +login-page+))))
69 "Send stream of updates to client"
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*)))
77 ; make current thread the update thread
78 (setf (update-thread *empire-session*) sb-thread:*current-thread*)
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
83 (setf (update-thread *empire-session*) nil)
84 (nconc next-updates (list (parenscript:ps* '(next))))
85 (apply #'concatenate 'string next-updates))))
87 (defmethod send ((s session) string)
88 "Push a javascript update fragment to the client."
89 (locked-queue:enqueue (update-queue s) string))
91 (defmethod prompt ((s session) p)
92 (send s (parenscript:ps* `(prompt ,p))))
94 (defmethod data ((s session) message)
95 (send s (parenscript:ps* `(msg ,message))))
97 ;; destination of login-form
98 (defun login-action ()
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))))
109 (defun command-action ()
111 (empire:command (connection *empire-session*) (hunchentoot:get-parameter "q"))
116 (hunchentoot:handle-static-file (concatenate 'string +static-files-root+ +root-page-file+))))
118 (defun my-request-p (script-name)
119 (string-starts-with script-name +web-root+))
121 (defun dispatch (request)
122 (let ((script-name (hunchentoot:script-name request)))
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)
132 ((string-starts-with script-name +static-web-root+) 'serve-static))))
136 (empire-log:info "Startup")
137 (pushnew 'dispatch hunchentoot:*dispatch-table* :test #'eq))