diff --git a/core/actor.lisp b/core/actor.lisp index 4f89af1..63d2f4a 100644 --- a/core/actor.lisp +++ b/core/actor.lisp @@ -8,34 +8,47 @@ (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 () ((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) (setf (behavior ac) bhv)) (defun create (bhv &optional (cls 'actor)) (make-instance cls :behavior bhv)) -(defun send (addr msg &key customer) - (let ((ac addr)) - ;(setf (customer msg) customer) - (funcall (behavior ac) ac msg))) +(defun send (addr content &key customer) + (let ((ac addr) (msg (message content customer))) + (deliver ac msg))) ;;;; predefined behaviors (defun no-op (ac msg)) (defun lgi (ac msg) - (util:lgi msg)) + (util:lgi (content msg))) (defun echo (ac msg) (send (customer msg) msg)) (defun inc (&optional (val 0)) #'(lambda (ac msg) - (if msg ; (payload msg) - (become ac (inc (+ msg val))) - (send (create #'lgi) val)))) + (let ((c (content msg))) + (if (eq c :show) + (send (create #'lgi) val) + (become ac (inc (+ c val))))))) diff --git a/test/test-core.lisp b/test/test-core.lisp index e4fad1f..04fde98 100644 --- a/test/test-core.lisp +++ b/test/test-core.lisp @@ -131,7 +131,7 @@ (setf a1 (actor:create (actor:inc))) (actor:send a1 2) (actor:send a1 3) - (actor:send a1 nil) + (actor:send a1 :show) )) (deftest test-send ()