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 (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)
79 (defmethod send ((s session) string)
80 "Push a javascript update fragment to the client."
81 (locked-queue:enqueue (update-queue s) string))
83 (defmethod prompt ((s session) p)
84 (send s (parenscript:ps* `(prompt ,p))))
86 (defmethod data ((s session) message)
87 (send s (parenscript:ps* `(msg ,message))))
89 ;; destination of login-form
90 (defun login-action ()
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))))
101 (defun command-action ()
103 (empire:command (connection *empire-session*) (hunchentoot:get-parameter "q"))
108 (hunchentoot:handle-static-file (concatenate 'string +static-files-root+ +root-page-file+))))
110 (defun dispatch (request)
111 (let ((script-name (hunchentoot:script-name request)))
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
123 (empire-log:info "Startup")
124 (pushnew 'dispatch hunchentoot:*dispatch-table* :test #'eq))