core/actor: message class, improvements
This commit is contained in:
parent
32913677fd
commit
1575040324
2 changed files with 23 additions and 10 deletions
|
@ -8,34 +8,47 @@
|
||||||
|
|
||||||
(in-package :scopes/core/actor)
|
(in-package :scopes/core/actor)
|
||||||
|
|
||||||
;;;; basic actor and message implementation
|
;;;; basic message and actor implementations
|
||||||
|
|
||||||
|
(defclass message ()
|
||||||
|
((content :reader content :initarg :content :initform nil)
|
||||||
|
(customer :reader customer :initarg :customer :initform nil)))
|
||||||
|
|
||||||
|
(defun message (content customer)
|
||||||
|
(make-instance 'message :content content :customer customer))
|
||||||
|
|
||||||
(defclass actor ()
|
(defclass actor ()
|
||||||
((behavior :accessor behavior :initarg :behavior :initform #'no-op)))
|
((behavior :accessor behavior :initarg :behavior :initform #'no-op)))
|
||||||
|
|
||||||
|
(defgeneric deliver (ac msg)
|
||||||
|
(:method ((ac actor) msg)
|
||||||
|
(funcall (behavior ac) ac msg)))
|
||||||
|
|
||||||
|
;;;; the core (Hewitt) actor API
|
||||||
|
|
||||||
(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))
|
||||||
(make-instance cls :behavior bhv))
|
(make-instance cls :behavior bhv))
|
||||||
|
|
||||||
(defun send (addr msg &key customer)
|
(defun send (addr content &key customer)
|
||||||
(let ((ac addr))
|
(let ((ac addr) (msg (message content customer)))
|
||||||
;(setf (customer msg) customer)
|
(deliver ac msg)))
|
||||||
(funcall (behavior ac) ac msg)))
|
|
||||||
|
|
||||||
;;;; predefined behaviors
|
;;;; predefined behaviors
|
||||||
|
|
||||||
(defun no-op (ac msg))
|
(defun no-op (ac msg))
|
||||||
|
|
||||||
(defun lgi (ac msg)
|
(defun lgi (ac msg)
|
||||||
(util:lgi msg))
|
(util:lgi (content msg)))
|
||||||
|
|
||||||
(defun echo (ac msg)
|
(defun echo (ac msg)
|
||||||
(send (customer msg) msg))
|
(send (customer msg) msg))
|
||||||
|
|
||||||
(defun inc (&optional (val 0))
|
(defun inc (&optional (val 0))
|
||||||
#'(lambda (ac msg)
|
#'(lambda (ac msg)
|
||||||
(if msg ; (payload msg)
|
(let ((c (content msg)))
|
||||||
(become ac (inc (+ msg val)))
|
(if (eq c :show)
|
||||||
(send (create #'lgi) val))))
|
(send (create #'lgi) val)
|
||||||
|
(become ac (inc (+ c val)))))))
|
||||||
|
|
|
@ -131,7 +131,7 @@
|
||||||
(setf a1 (actor:create (actor:inc)))
|
(setf a1 (actor:create (actor:inc)))
|
||||||
(actor:send a1 2)
|
(actor:send a1 2)
|
||||||
(actor:send a1 3)
|
(actor:send a1 3)
|
||||||
(actor:send a1 nil)
|
(actor:send a1 :show)
|
||||||
))
|
))
|
||||||
|
|
||||||
(deftest test-send ()
|
(deftest test-send ()
|
||||||
|
|
Loading…
Add table
Reference in a new issue