Skip to main content

NetworkActor

Trait NetworkActor 

Source
pub trait NetworkActor: ComponentLogging {
    type Message: MessageBounds;
    type Deserialiser: Deserialiser<Self::Message>;

    // Required method
    fn receive(
        &mut self,
        sender: Option<ActorPath>,
        msg: Self::Message,
    ) -> Result<Handled, HandlerError>;

    // Provided method
    fn on_error(
        &mut self,
        error: UnpackError<NetMessage>,
    ) -> Result<Handled, HandlerError> { ... }
}
Expand description

A trait for actors that handle the same set of messages locally and remotely

Implementing this trait is roughly equivalent to the default assumptions in most actor system implementations with a distributed runtime (Erlang, Akka, Orelans, etc.).

§Example

This implementation uses the trivial default Deserialiser for the unit type () from the serialisation::default_serialisiers module (which is imported by default with the prelude).

use kompact::prelude::*;

#[derive(ComponentDefinition)]
struct NetActor {
   ctx: ComponentContext<Self>
}
ignore_lifecycle!(NetActor);
impl NetworkActor for NetActor {
    type Message = ();
    type Deserialiser = ();

    fn receive(&mut self, sender: Option<ActorPath>, msg: Self::Message) -> HandlerResult {
        info!(self.log(), "Got a local or deserialised remote message: {:?}", msg);
        Handled::OK
    }
}

Required Associated Types§

Source

type Message: MessageBounds

The type of messages the actor accepts.

Source

type Deserialiser: Deserialiser<Self::Message>

The deserialiser used to unpack network messages into Self::Message.

Required Methods§

Source

fn receive( &mut self, sender: Option<ActorPath>, msg: Self::Message, ) -> Result<Handled, HandlerError>

Handles all messages after deserialisation.

The sender argument will only be supplied if the original message was a NetMessage, otherwise it’s None.

Provided Methods§

Source

fn on_error( &mut self, error: UnpackError<NetMessage>, ) -> Result<Handled, HandlerError>

Handle errors during unpacking of network messages.

The default implementation logs every error as a warning.

Implementors§