csys: now without dispatcher loop - send is directly submitting the selected action

This commit is contained in:
Helmut Merz 2025-06-11 20:03:42 +02:00
parent 6cef655707
commit 5ff08dcde6
3 changed files with 13 additions and 24 deletions

View file

@ -10,42 +10,32 @@
(:shape :scopes/shape)
(:util :scopes/util)
(:alx :alexandria))
(:export #:*dispatcher* #:setup #:start #:shutdown
(:export #:*dispatcher* #:setup #:send
#:printer
))
(in-package :scopes/csys)
(defun send (msg)
(handle-message msg))
(defvar *dispatcher* nil)
(defun setup (&optional (cfg config:*root*))
(setf *dispatcher* (make-instance 'core:context :config cfg))
(setf (core:mailbox *dispatcher*) (async:make-mb))
(dolist (a (config:actions cfg))
(core:add-action *dispatcher* (car a) (cadr a))))
(defun start ()
(async:init)
(actor:start (core:mailbox *dispatcher*)
(lambda (msg) (funcall #'handle-message msg))
:foreground t))
(defun handle-message (msg)
(let ((hdlrs (core:select msg (core:actions *dispatcher*))))
(if hdlrs
(mapcar #'(lambda (hdlr) (run-action hdlr msg)) hdlrs)
(util:lgw "no action selected" msg)))
nil)
(util:lgw "no action selected" msg))))
(defun run-action (job msg)
(let ((ch (async:make-ch)))
(async:submit-task ch (lambda () (funcall job msg)))))
(defun shutdown (msg &optional (delay 0.1))
(sleep delay)
(format t "~&*** shutdown ~a~%" msg)
(actor:stop (core:mailbox *dispatcher*)))
;;;; example behaviors / actions
(defun printer (msg)

View file

@ -5,7 +5,6 @@
(config:root)
(config:add-action '(:test) #'csys:printer)
(config:add-action '(:stop) #'csys:shutdown)
(config:add :logger :class 'logging:config
:loglevel (config:from-env :loglevel :info)

View file

@ -21,19 +21,19 @@
;;;; test runner
(defun run ()
(async:init)
(let* ((t:*test-suite* (make-instance 't:test-suite :name "csys")))
(load (t:test-path "config-csys" "etc"))
(unwind-protect
(progn
(test-basic))
(test-nodispatch))
(async:finish)
(t:show-result))))
(deftest test-basic ()
(deftest test-nodispatch ()
(csys:setup)
(!= csys:*dispatcher* nil)
(actor:send (core:mailbox csys:*dispatcher*) (message:create '(:test)))
(actor:send (core:mailbox csys:*dispatcher*) (message:create '(:stop)))
;(actor:stop (core:mailbox csys:*dispatcher*))
(csys:start)
)
(csys:send (message:create '(:test 1)))
(csys:send (message:create '(:test 2)))
(csys:send (message:create '(:test 3)))
(csys:send (message:create '(:test 4)))
)