core: context: add-action
This commit is contained in:
parent
6a88716b36
commit
46089ac27c
2 changed files with 34 additions and 22 deletions
|
@ -5,7 +5,7 @@
|
||||||
(:local-nicknames (:config :scopes/config)
|
(:local-nicknames (:config :scopes/config)
|
||||||
(:message :scopes/core/message))
|
(:message :scopes/core/message))
|
||||||
(:export #:config #:service-config
|
(:export #:config #:service-config
|
||||||
#:context #:name #:actions #:send
|
#:context #:name #:actions #:add-action #:send
|
||||||
#:action-spec
|
#:action-spec
|
||||||
#:printer))
|
#:printer))
|
||||||
|
|
||||||
|
@ -25,21 +25,6 @@
|
||||||
((pattern :reader pattern :initarg :pattern :initform nil)
|
((pattern :reader pattern :initarg :pattern :initform nil)
|
||||||
(handlers :reader handlers :initarg :handlers)))
|
(handlers :reader handlers :initarg :handlers)))
|
||||||
|
|
||||||
;;;; context
|
|
||||||
|
|
||||||
(defvar *context* nil)
|
|
||||||
|
|
||||||
(defclass context ()
|
|
||||||
((name :reader name :initarg :name)
|
|
||||||
(actions :accessor actions :initform nil)))
|
|
||||||
|
|
||||||
(defgeneric send (rcvr msg)
|
|
||||||
(:method ((rcvr context) msg)
|
|
||||||
(let* ((acts (actions rcvr))
|
|
||||||
(hdlrs (select msg acts)))
|
|
||||||
(dolist (hdlr hdlrs)
|
|
||||||
(funcall hdlr rcvr msg)))))
|
|
||||||
|
|
||||||
(defun select (msg acts)
|
(defun select (msg acts)
|
||||||
(let ((h (message:head-as-list msg))
|
(let ((h (message:head-as-list msg))
|
||||||
(hdlrs nil))
|
(hdlrs nil))
|
||||||
|
@ -48,6 +33,7 @@
|
||||||
(dolist (hdlr (handlers a))
|
(dolist (hdlr (handlers a))
|
||||||
(push hdlr hdlrs))))
|
(push hdlr hdlrs))))
|
||||||
hdlrs))
|
hdlrs))
|
||||||
|
;(nreverse hdlrs)))
|
||||||
|
|
||||||
(defun match (pat h)
|
(defun match (pat h)
|
||||||
(dolist (item pat)
|
(dolist (item pat)
|
||||||
|
@ -55,6 +41,34 @@
|
||||||
(return-from match nil)))
|
(return-from match nil)))
|
||||||
t)
|
t)
|
||||||
|
|
||||||
|
(defun find-action (pat acts)
|
||||||
|
(dolist (a acts)
|
||||||
|
(if (equal (pattern a) pat)
|
||||||
|
(return-from find-action a))))
|
||||||
|
|
||||||
|
;;;; context
|
||||||
|
|
||||||
|
(defvar *context* nil)
|
||||||
|
|
||||||
|
(defclass context ()
|
||||||
|
((name :reader name :initarg :name)
|
||||||
|
(actions :accessor actions :initform nil)))
|
||||||
|
|
||||||
|
(defun add-action (ctx pat hdlr)
|
||||||
|
(let* ((acts (actions ctx))
|
||||||
|
(act (find-action pat acts)))
|
||||||
|
(if act
|
||||||
|
(push hdlr (handlers act))
|
||||||
|
(push (make-instance 'action-spec :pattern pat :handlers (list hdlr))
|
||||||
|
(actions ctx)))))
|
||||||
|
|
||||||
|
(defgeneric send (rcvr msg)
|
||||||
|
(:method ((rcvr context) msg)
|
||||||
|
(let* ((acts (actions rcvr))
|
||||||
|
(hdlrs (select msg acts)))
|
||||||
|
(dolist (hdlr hdlrs)
|
||||||
|
(funcall hdlr rcvr msg)))))
|
||||||
|
|
||||||
;;;; simple printer service
|
;;;; simple printer service
|
||||||
|
|
||||||
(defun do-print (ctx msg)
|
(defun do-print (ctx msg)
|
||||||
|
|
|
@ -19,10 +19,6 @@
|
||||||
|
|
||||||
(defclass test-receiver (core:context)
|
(defclass test-receiver (core:context)
|
||||||
((core:name :initform :test-rcvr)
|
((core:name :initform :test-rcvr)
|
||||||
(core:actions
|
|
||||||
:initform (list
|
|
||||||
(make-instance 'core:action-spec
|
|
||||||
:handlers (list #'check-message))))
|
|
||||||
(expected :accessor expected
|
(expected :accessor expected
|
||||||
:initform (make-hash-table :test #'equalp))))
|
:initform (make-hash-table :test #'equalp))))
|
||||||
|
|
||||||
|
@ -52,9 +48,11 @@
|
||||||
:initform (make-instance 'test-receiver))))
|
:initform (make-instance 'test-receiver))))
|
||||||
|
|
||||||
(defun run ()
|
(defun run ()
|
||||||
(let ((*config* nil)
|
(let* ((*config* nil)
|
||||||
(t:*test-suite* (make-instance 'test-suite :name "core")))
|
(t:*test-suite* (make-instance 'test-suite :name "core"))
|
||||||
|
(rcvr (receiver t:*test-suite*)))
|
||||||
(load (t:test-path "config-core" "etc"))
|
(load (t:test-path "config-core" "etc"))
|
||||||
|
(core:add-action rcvr '(:test) #'check-message)
|
||||||
(test-send)
|
(test-send)
|
||||||
(check-expected)
|
(check-expected)
|
||||||
(t:show-result)))
|
(t:show-result)))
|
||||||
|
|
Loading…
Add table
Reference in a new issue