From 5237b518d4883844ec2d3d5bb744678f4e4cce97 Mon Sep 17 00:00:00 2001 From: David Botton Date: Sun, 31 Jan 2021 19:58:19 -0500 Subject: [PATCH] Make easier to use demos, change to boot.js in further prep for long polling as an option --- README.md | 12 ++++---- clog-connection.lisp | 26 ++++++++++------- clog-helpers.lisp | 33 ++++++++++++++++++++-- clog.lisp | 3 ++ demos/{01-snake-game.lisp => 01-demo.lisp} | 0 demos/{02-chat.lisp => 02-demo.lisp} | 0 demos/README.md | 8 +++--- static-files/js/boot.js | 4 +-- 8 files changed, 62 insertions(+), 24 deletions(-) rename demos/{01-snake-game.lisp => 01-demo.lisp} (100%) rename demos/{02-chat.lisp => 02-demo.lisp} (100%) diff --git a/README.md b/README.md index af59e4f..0794112 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,7 @@ To load this package and work through tutorials (assuming you have Quicklisp configured): 1. Start emacs then M-x slime -2. In the REPL, run: +2. In the REPL, run (tutorials current 1 - 18): ``` CL-USER> (ql:quickload :clog) @@ -70,11 +70,11 @@ To see where the source files are: CL-USER> (clog:clog-install-dir) ``` -You can the load the demos with: +You can the run the demos with (currently 1 or 2): ``` -CL-USER> (load "path to clog/demos/01-snake.lisp") -CL-USER> (clog-user:start-demo) +CL-USER> (ql:quickload :clog) +CL-USER> (clog-user:run-demo 1) ``` To open a browser with the CLOG manual: @@ -164,8 +164,8 @@ Tutorial Summary Demo Summary -- 01-snake-game.lisp - Sparkey the Snake Game -- 02-chat.lisp - Chat - Private instant messenger +- 01-demo.lisp - Sparkey the Snake Game +- 02-demo.lisp - Chat - Private instant messenger Enhancements underway: diff --git a/clog-connection.lisp b/clog-connection.lisp index c200945..f555031 100644 --- a/clog-connection.lisp +++ b/clog-connection.lisp @@ -58,25 +58,25 @@ script." (defvar *verbose-output* nil "Verbose server output (default false)") -(defvar *app* nil "Clack 'app' middle-ware") -(defvar *client-handler* nil "Clack 'handler' for socket traffic") -(defvar *on-connect-handler* nil "New connection event handler.") +(defvar *app* nil "Clack 'app' middle-ware") +(defvar *client-handler* nil "Clack 'handler' for socket traffic") -(defvar *new-id* 0 "Last issued connection or script IDs") +(defvar *on-connect-handler* nil "New connection event handler.") (defvar *connections* (make-hash-table) "Connections to IDs") (defvar *connection-ids* (make-hash-table) "IDs to connections") (defvar *connection-data* (make-hash-table) "Connection based data") - (defvar *connection-lock* (bordeaux-threads:make-lock) "Protect the connection hash tables") -(defvar *queries-lock* (bordeaux-threads:make-lock) - "Protect query hash tables") -(defvar *id-lock* (bordeaux-threads:make-lock) + +(defvar *new-id* 0 "Last issued connection or script IDs") +(defvar *id-lock* (bordeaux-threads:make-lock) "Protect new-id variable.") (defvar *queries* (make-hash-table) "Query ID to Answers") (defvar *queries-sems* (make-hash-table) "Query ID to semiphores") +(defvar *queries-lock* (bordeaux-threads:make-lock) + "Protect query hash tables") (defvar *query-time-out* 3 "Number of seconds to timeout waiting for a query") (defvar *url-to-boot-file* (make-hash-table :test 'equalp) "URL to boot-file") @@ -87,7 +87,6 @@ script." (defun generate-id () "Generate unique ids for use in connections and sripts." - ;; needs mutex or atomic (bordeaux-threads:with-lock-held (*id-lock*) (incf *new-id*))) ;;;;;;;;;;;;;;;;;;;; @@ -207,7 +206,12 @@ the default answer. (Private)" (let ((ws (websocket-driver:make-server env))) (websocket-driver:on :open ws (lambda () - (let ((id (getf env :query-string))) + (let* ((query (getf env :query-string)) + (items (when query + (quri:url-decode-params query))) + (id (when items + (cdr (assoc "r" items + :test #'equalp))))) (when (typep id 'string) (setf id (parse-integer id :junk-allowed t))) (handle-new-connection ws id)))) @@ -321,6 +325,8 @@ path by querying the browser. See PATH-NAME (CLOG-LOCATION)." (defun set-clog-path (path boot-file) (if boot-file (setf (gethash path *url-to-boot-file*) + ;; Make clog-path into a relative path of + ;; of site-root. (if (eql (char boot-file 0) #\/) (concatenate 'string "." boot-file) boot-file)) diff --git a/clog-helpers.lisp b/clog-helpers.lisp index 1fb3c80..726aa75 100644 --- a/clog-helpers.lisp +++ b/clog-helpers.lisp @@ -10,7 +10,7 @@ (defpackage #:clog-user (:use #:cl #:clog) - (:export start-tutorial)) + (:export start-tutorial start-demo)) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Implementation - CLOG Utilities @@ -41,12 +41,41 @@ (defun run-tutorial (num) "Run tutorial NUM" + (load-tutorial num) + (clog-user:start-tutorial)) + +;;;;;;;;;;;;;;;;;;; +;; load-tutorial ;; +;;;;;;;;;;;;;;;;;;; + +(defun load-tutorial (num) + "Load tutorial NUM - use (clog-user:start-tutorial)" (let ((p (merge-pathnames (format nil "./tutorial/~2,'0d-tutorial.lisp" num) (asdf:system-source-directory :clog)))) (load p) - (clog-user:start-tutorial) (format t "~%~% ---- The tutorial src is located at: ~A~%" p))) +;;;;;;;;;;;;;; +;; run-demo ;; +;;;;;;;;;;;;;; + +(defun run-demo (num) + "Run demo NUM" + (load-demo num) + (clog-user:start-demo)) + +;;;;;;;;;;;;;;; +;; load-demo ;; +;;;;;;;;;;;;;;; + +(defun load-demo (num) + "Load demo NUM - use (clog-user:start-demo)" + (let ((p (merge-pathnames (format nil "./demos/~2,'0d-demo.lisp" num) + (asdf:system-source-directory :clog)))) + (load p) + (format t "~%~% ---- The demo src is located at: ~A~%" p))) + + ;;;;;;;;;;;;;;;; ;; load-world ;; ;;;;;;;;;;;;;;;; diff --git a/clog.lisp b/clog.lisp index 7c79526..b368241 100644 --- a/clog.lisp +++ b/clog.lisp @@ -742,6 +742,9 @@ embedded in a native template application.)" (clog-install-dir function) (open-manual function) (run-tutorial function) + (load-tutorial function) + (run-demo function) + (load-demo function) "Functions for Compilation and Documentation" (load-world function) diff --git a/demos/01-snake-game.lisp b/demos/01-demo.lisp similarity index 100% rename from demos/01-snake-game.lisp rename to demos/01-demo.lisp diff --git a/demos/02-chat.lisp b/demos/02-demo.lisp similarity index 100% rename from demos/02-chat.lisp rename to demos/02-demo.lisp diff --git a/demos/README.md b/demos/README.md index c82230e..cd4381e 100644 --- a/demos/README.md +++ b/demos/README.md @@ -15,8 +15,8 @@ To load "clog": Load the demo: ``` -CL-USER> (load "/Users/dbotton/common-lisp/clog/demos/01-hello.lisp") -#P"/Users/dbotton/common-lisp/clog/demos/01-hello.lisp" +CL-USER> (load "/Users/dbotton/common-lisp/clog/demos/01-demo.lisp") +#P"/Users/dbotton/common-lisp/clog/demos/01-demo.lisp" ``` Start the demo: @@ -34,5 +34,5 @@ Most demos startup a browser, if not use http://127.0.0.1:8080 Demo Summary -- 01-snake-game.lisp - Sparkey the Snake Game -- 02-chat.lisp - Chat - Private instant messenger +- 01-demo.lisp - Sparkey the Snake Game +- 02-demo.lisp - Chat - Private instant messenger diff --git a/static-files/js/boot.js b/static-files/js/boot.js index ec8a0b1..bf636dd 100644 --- a/static-files/js/boot.js +++ b/static-files/js/boot.js @@ -42,7 +42,7 @@ function Setup_ws() { ws.onerror = function (event) { console.log ("onerror: reconnect"); ws = null; - ws = new WebSocket (adr + "?" + clog['connection_id']); + ws = new WebSocket (adr + "?r=" + clog['connection_id']); ws.onopen = function (event) { console.log ("onerror: reconnect successful"); Setup_ws(); @@ -56,7 +56,7 @@ function Setup_ws() { ws.onclose = function (event) { console.log ("onclose: reconnect"); ws = null; - ws = new WebSocket (adr + "?" + clog['connection_id']); + ws = new WebSocket (adr + "?r=" + clog['connection_id']); ws.onopen = function (event) { console.log ("onclose: reconnect successful"); Setup_ws();