Skip to content

Commit

Permalink
allow additional initialization arguments be passed to actor.
Browse files Browse the repository at this point in the history
  • Loading branch information
mdbergmann committed Jul 29, 2023
1 parent 5be04c5 commit b7b8dcd
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 15 deletions.
13 changes: 10 additions & 3 deletions src/actor-api.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
(:use :cl)
(:nicknames :act)
(:export #:make-actor
#:other-init-args
#:actor
#:tell
#:!
Expand All @@ -27,7 +28,11 @@
(in-package :sento.actor)

(defclass actor (actor-cell)
((receive :initarg :receive
((other-init-args :initarg :other-init-args
:initform nil
:reader other-init-args
:documentation "Other init args passed to `make-actor`")
(receive :initarg :receive
:initform (error "'receive' must be specified!")
:reader receive
:documentation
Expand Down Expand Up @@ -66,7 +71,7 @@ There is asynchronous `tell`, a synchronous `ask-s` and asynchronous `ask` which
To stop an actors message processing in order to cleanup resouces you should `tell` (or `ask-s`) the `:stop` message. It will respond with `:stopped` (in case of `ask(-s)`)."))

(defgeneric make-actor (receive &key name state type init destroy)
(defgeneric make-actor (receive &key name state type init destroy other-init-args)
(:documentation
"Constructs an `actor`.
Expand All @@ -84,7 +89,9 @@ If you have additional initializations to make you can do so in `initialize-inst
- `state`: initialize an actor with a state. (default is `nil`)
- `init` and `destroy`: are functions that take one argument, the actor instance.
Those hooks are called on (after) initialization and (after) stop respectively."))
Those hooks are called on (after) initialization and (after) stop respectively.
- `other-init-args`: are additional arguments passed to `initialize-instance` of the actor class."))

(defgeneric pre-start (actor)
(:documentation
Expand Down
5 changes: 3 additions & 2 deletions src/actor-context-api.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
(in-package :sento.actor-context)

(defgeneric actor-of (context
&key receive init destroy dispatcher state type name)
&key receive init destroy dispatcher state type name other-args)
(:documentation "Interface for creating an actor.
**!!! Attention:** this factory function wraps the `act:make-actor` functionality to something more simple to use.
Expand All @@ -37,7 +37,8 @@ This function allows to unsubsribe from event-stream or such.
- `:dispatcher` key can be used to define the message dispatcher manually.
Options are `:shared` (default) and `:pinned`.
- `:type` can specify a custom actor class. See `act:make-actor` for more info.
- `:name` to set a specific name to the actor, otherwise a random name will be used."))
- `:name` to set a specific name to the actor, otherwise a random name will be used.
- `:other-args` are in the actor instance as `other-init-args` and can be acted upon in i.e. `pre-start` for additional initialization."))

(defgeneric find-actors (context path &key test key)
(:documentation "Returns actors to be found by the criteria of:
Expand Down
6 changes: 4 additions & 2 deletions src/actor-context.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ An `act:actor` contains an `actor-context`."
&key receive
(init nil) (destroy nil)
(dispatcher :shared) (state nil)
(type 'act:actor) (name nil))
(type 'act:actor) (name nil)
(other-args nil))
"See `ac:actor-of`."
(check-type receive function "a function!")
(%actor-of context
Expand All @@ -119,7 +120,8 @@ An `act:actor` contains an `actor-context`."
:name name
:type type
:init init
:destroy destroy))
:destroy destroy
:other-init-args other-args))
:dispatcher dispatcher))

;; test 2-arity function with 'path' and 'act-cell-name' (default)
Expand Down
12 changes: 8 additions & 4 deletions src/actor-system.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ See `config:config-from`."
state
(type 'act:actor)
name
(context-key :user))
(context-key :user)
(other-args nil))
"Private API to create system actors. Context-key is either `:internal` or `:user`
Users should use `actor-of`."
(ac:actor-of (actor-context-for-key context-key system)
Expand All @@ -141,7 +142,8 @@ Users should use `actor-of`."
:dispatcher dispatcher
:state state
:type type
:name name))
:name name
:other-args other-args))

(defun %find-actors (system path &key test key context-key)
"Private API to find actors in both contexts the actor-system supports.
Expand Down Expand Up @@ -192,7 +194,8 @@ Users should use `ac:find-actors`."
&key receive
(init nil) (destroy nil)
(dispatcher :shared) (state nil)
(type 'act:actor) (name nil))
(type 'act:actor) (name nil)
(other-args nil))
"See `ac:actor-of`"
(%actor-of system
:receive receive
Expand All @@ -202,7 +205,8 @@ Users should use `ac:find-actors`."
:state state
:type type
:name name
:context-key :user))
:context-key :user
:other-args other-args))

(defmethod find-actors ((self actor-system) path &key (test #'string=) (key #'act-cell:name))
"See `ac:find-actors`"
Expand Down
11 changes: 7 additions & 4 deletions src/actor.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@
(setf (symbol-function '!) #'act:tell)
(setf (symbol-function '?) #'act:ask)

(defmethod make-actor (receive &key name state (type 'actor) (init nil) (destroy nil))
(defmethod make-actor (receive &key name state (type 'actor) (init nil) (destroy nil) (other-init-args nil))
(make-instance type
:name name
:state state
:receive receive
:init init
:destroy destroy))
:destroy destroy
:other-init-args other-init-args))

(defun finalize-initialization (actor message-box actor-context)
"Private API: finalize initialization of the actor with a `mesgb:message-box` and an `ac:actor-context`."
Expand Down Expand Up @@ -252,7 +253,8 @@ Use this from within receive function to reply to a sender."
&key receive
(init nil) (destroy nil)
(dispatcher :shared) (state nil)
(type 'act:actor) (name nil))
(type 'act:actor) (name nil)
(other-args nil))
"`ac:actor-context` protocol implementation"
(ac:actor-of (context actor)
:receive receive
Expand All @@ -261,4 +263,5 @@ Use this from within receive function to reply to a sender."
:dispatcher dispatcher
:state state
:type type
:name name))
:name name
:other-args other-args))

0 comments on commit b7b8dcd

Please sign in to comment.