pub trait Actor {
type Message: MessageBounds;
// Required methods
fn receive_local(
&mut self,
msg: Self::Message,
) -> Result<Handled, HandlerError>;
fn receive_network(
&mut self,
msg: NetMessage,
) -> Result<Handled, HandlerError>;
}Expand description
A slightly higher level Actor API that handles local messages, and optionally distributed ones
This trait should generally be preferred over ActorRaw, as it abstracts away the message envelope enum used internally.
Actor can also be derived via #[derive(Actor)] for components which don’t require
this messaging mechanism.
§Example
use kompact::prelude::*;
#[derive(ComponentDefinition)]
struct NormalActor {
ctx: ComponentContext<Self>
}
ignore_lifecycle!(NormalActor);
impl Actor for NormalActor {
type Message = ();
fn receive_local(&mut self, msg: Self::Message) -> HandlerResult {
info!(self.log(), "Got a local message: {:?}", msg);
Handled::OK
}
#[cfg(feature = "distributed")]
fn receive_network(&mut self, msg: NetMessage) -> HandlerResult {
info!(self.log(), "Got a network message: {:?}", msg);
Handled::OK
}
}Required Associated Types§
Sourcetype Message: MessageBounds
type Message: MessageBounds
The type of local messages the actor accepts
Required Methods§
Sourcefn receive_local(&mut self, msg: Self::Message) -> Result<Handled, HandlerError>
fn receive_local(&mut self, msg: Self::Message) -> Result<Handled, HandlerError>
Sourcefn receive_network(&mut self, msg: NetMessage) -> Result<Handled, HandlerError>
fn receive_network(&mut self, msg: NetMessage) -> Result<Handled, HandlerError>
Handle an incoming network message
Network messages are of type NetMessage and can be either be serialised data, or a heap-allocated “reflected” message. Messages are “reflected” instead of serialised whenever possible for messages sent to an ActorPath that turned out to be in the same KompactSystem.
§Note
Remember that components usually run on a shared thread pool, so, just like for handle implementations, you shouldn’t ever block in this method unless you know what you are doing.