core: use actor API as base for context and message handling: basically working

This commit is contained in:
Helmut Merz 2025-05-03 17:36:27 +02:00
parent 80d971bb59
commit 9389edfc57
5 changed files with 32 additions and 15 deletions

View file

@ -4,7 +4,7 @@
(:use :common-lisp) (:use :common-lisp)
(:local-nicknames (:async :scopes/util/async) (:local-nicknames (:async :scopes/util/async)
(:util :scopes/util)) (:util :scopes/util))
(:export #:actor #:bg-actor #:fg-actor (:export #:actor #:bg-actor #:fg-actor #:start #:stop
#:become #:create #:send #:become #:create #:send
#:message #:content #:customer #:message #:content #:customer
#:*logger* #:*root* #:*logger* #:*root*
@ -34,8 +34,16 @@
(:method ((ac actor))) (:method ((ac actor)))
(:method ((ac bg-actor)) (:method ((ac bg-actor))
(setf (task ac) (make-task ac)) (setf (task ac) (make-task ac))
(async:start (task ac)))
(:method ((ac fg-actor))
(setf (task ac) (make-task ac 'async:fg-task))
(async:start (task ac)))) (async:start (task ac))))
(defgeneric stop (ac)
(:method ((ac actor)))
(:method ((ac bg-actor))
(async:stop (task ac))))
(defun make-task (ac &optional (cls 'async:bg-task)) (defun make-task (ac &optional (cls 'async:bg-task))
(async:make-task :cls cls (async:make-task :cls cls
:handle-message :handle-message
@ -46,9 +54,9 @@
(defun become (ac bhv) (defun become (ac bhv)
(setf (behavior ac) bhv)) (setf (behavior ac) bhv))
(defun create (bhv &optional (cls 'actor)) (defun create (bhv &optional (cls 'actor) &rest args &key &allow-other-keys)
(let ((ac (make-instance cls :behavior bhv))) (let ((ac (apply #'make-instance cls :behavior bhv args)))
(start ac) ;(start ac)
ac)) ac))
(defgeneric send (addr content &key &allow-other-keys) (defgeneric send (addr content &key &allow-other-keys)

View file

@ -60,7 +60,7 @@
(defvar *root* nil) (defvar *root* nil)
(defclass base-context () (defclass base-context (actor:actor)
((actions :accessor actions :initform nil))) ((actions :accessor actions :initform nil)))
(defclass context (base-context) (defclass context (base-context)
@ -71,9 +71,12 @@
(defclass service (context actor:bg-actor) (defclass service (context actor:bg-actor)
((task :accessor task :initform nil))) ((task :accessor task :initform nil)))
(defclass root-service (service actor:actor) ()) (defclass root-service (service actor:fg-actor) ())
(defgeneric do-start (ctx) (defun do-start (ctx)
(actor:start ctx))
(defgeneric x-do-start (ctx)
(:method ((ctx context))) (:method ((ctx context)))
(:method ((ctx service)) (:method ((ctx service))
(setf (task ctx) (async:make-task :handle-message #'handle-message)) (setf (task ctx) (async:make-task :handle-message #'handle-message))
@ -83,14 +86,17 @@
(async:make-task :cls 'async:fg-task :handle-message #'handle-message)) (async:make-task :cls 'async:fg-task :handle-message #'handle-message))
(async:start (task ctx)))) (async:start (task ctx))))
(defgeneric send (rcvr msg) (defun send (rcvr msg)
(actor:send rcvr msg))
(defgeneric x-send (rcvr msg)
(:method ((rcvr base-context) msg) (:method ((rcvr base-context) msg)
(handle-message rcvr msg)) (handle-message rcvr msg))
(:method ((rcvr service) msg) (:method ((rcvr service) msg)
(async:send (task rcvr) msg))) (async:send (task rcvr) msg)))
(defun default-setup (cfg &optional (cls 'context) &rest args &key &allow-other-keys) (defun default-setup (cfg &optional (cls 'context) &rest args &key &allow-other-keys)
(apply #'make-instance cls :config cfg :name (config:name cfg) args)) (apply #'actor:create #'handle-message cls :config cfg :name (config:name cfg) args))
(defun find-service (name &optional (parent *root*)) (defun find-service (name &optional (parent *root*))
(with-slots (services) parent (with-slots (services) parent
@ -99,15 +105,15 @@
(defun setup-services (&optional (cfg config:*root*)) (defun setup-services (&optional (cfg config:*root*))
(async:init) (async:init)
(setf *root* (make-instance 'root-service :config cfg)) (setf *root* (actor:create #'handle-message 'root-service :config cfg))
(dolist (c (reverse (config:children cfg))) (dolist (c (reverse (config:children cfg)))
(add-service *root* c))) (add-service *root* c)))
(defun shutdown () (defun shutdown ()
(dolist (ctx (alx:hash-table-values (services *root*))) (dolist (ctx (alx:hash-table-values (services *root*)))
(funcall (config:shutdown (config ctx)) ctx)) (funcall (config:shutdown (config ctx)) ctx))
(if (and (task *root*)) (if (task *root*)
(async:stop (task *root*))) (actor:stop *root*))
(async:finish)) (async:finish))
(defun run-services (&optional (cfg config:*root*)) (defun run-services (&optional (cfg config:*root*))

View file

@ -132,6 +132,7 @@
(collector (collector
(actor:create (actor:create
#'(lambda (ac msg) (setf val (actor:content msg)))))) #'(lambda (ac msg) (setf val (actor:content msg))))))
(actor:start calc)
(actor:send calc '(actor:plus 2)) (actor:send calc '(actor:plus 2))
(actor:send calc '(actor:minus 3)) (actor:send calc '(actor:minus 3))
(actor:send calc '(actor:show)) (actor:send calc '(actor:show))

View file

@ -37,7 +37,7 @@
(test-fileserver client) (test-fileserver client)
(test-message client) (test-message client)
(sleep 0.1))) (sleep 0.1)))
(core:shutdown) ;(core:shutdown)
(t:show-result)))) (t:show-result))))
;;;; the tests ;;;; the tests

View file

@ -2,7 +2,8 @@
(defpackage :scopes/web/server (defpackage :scopes/web/server
(:use :common-lisp) (:use :common-lisp)
(:local-nicknames (:config :scopes/config) (:local-nicknames (:actor :scopes/core/actor)
(:config :scopes/config)
(:core :scopes/core) (:core :scopes/core)
(:message :scopes/core/message) (:message :scopes/core/message)
(:response :scopes/web/response) (:response :scopes/web/response)
@ -89,7 +90,8 @@
(defun message-handler (ctx env &key html-responder) (defun message-handler (ctx env &key html-responder)
(let* ((resp (response:setup ctx env :html-responder html-responder)) (let* ((resp (response:setup ctx env :html-responder html-responder))
(iact (make-instance 'response:interaction :response resp)) (iact (actor:create #'core:handle-message 'response:interaction :response resp))
;(iact (make-instance 'response:interaction :response resp))
(msg (message:create (msg (message:create
(head env) :data (plist (post-data env)) :customer iact))) (head env) :data (plist (post-data env)) :customer iact)))
(util:lgd msg) (util:lgd msg)