Skip to main content

ComponentContext

Struct ComponentContext 

Source
pub struct ComponentContext<CD>
where CD: ComponentTraits,
{ /* private fields */ }
Expand description

The contextual object for a Kompact component

Gives access compact internal features like timers, logging, confguration, an the self reference.

Implementations§

Source§

impl<CD> ComponentContext<CD>

Source

pub fn uninitialised() -> ComponentContext<CD>

Create a new, uninitialised component context

§Note

Nothing in this context may be used before the parent component is actually initialised!

Source

pub fn initialise(&mut self, c: Arc<Component<CD>>)

Initialise the component context with the actual component instance

This must be invoked from setup.

Source

pub fn log(&self) -> &Logger<Arc<Fuse<Async>>>

The components logger instance

This instance will already be preloaded with component specific information in the MDC.

§Example
use kompact::prelude::*;

#[derive(ComponentDefinition, Actor)]
struct HelloLogging {
   ctx: ComponentContext<Self>,
}
impl HelloLogging {
    fn new() -> HelloLogging {
        HelloLogging {
            ctx: ComponentContext::uninitialised(),
        }
    }    
}
impl ComponentLifecycle for HelloLogging {
    fn on_start(&mut self) -> HandlerResult {
        info!(self.ctx().log(), "Hello Start event");
        self.ctx().system().shutdown_async();
        Handled::OK
    }    
}

let system = KompactConfig::default().build().wait().expect("system");
let c = system.create(HelloLogging::new);
system.start(&c);
system.terminated().wait();
Source

pub fn config(&self) -> &Config

Get a reference to the system configuration

Use load_config_str or or load_config_file to load values into the config object.

§Example
use kompact::prelude::*;

#[derive(ComponentDefinition, Actor)]
struct ConfigComponent {
   ctx: ComponentContext<Self>,
}
impl ConfigComponent {
    fn new() -> ConfigComponent {
        ConfigComponent {
            ctx: ComponentContext::uninitialised(),
        }
    }    
}
impl ComponentLifecycle for ConfigComponent {
    fn on_start(&mut self) -> HandlerResult {
        assert_eq!(Some(7i64), self.ctx().config()["a"].as_i64());
        self.ctx().system().shutdown_async();
        Handled::OK
    }    
}
let default_values = r#"a = 7"#;
let mut conf = KompactConfig::default();
conf.load_config_str(default_values);
let system = conf.build().wait().expect("system");
let c = system.create(ConfigComponent::new);
system.start(&c);
system.terminated().wait();
Source

pub fn set_blocking(&mut self, future: BlockingFuture)

Sets the component to block on the provided blocking future

This should only be used when implementing custom execute logic! Otherwise the correct way to block is to return the Handled::BlockOn variant from a handler.

If this is used for custom execute logic, then the next call should be a return ExecuteResult::new(true, count, skip), as continuing to execute handlers violates blocking semantics.

Source

pub fn is_blocking(&self) -> bool

Return true if the component is set up for blocking

If this does return true, port handling must be immediately aborted and the returned result must have the form ExecuteResult::new(true, count, skip).

Source

pub fn typed_component(&self) -> Arc<Component<CD>>

Returns the component instance wrapping this component definition

This is mostly meant to be passed along for scheduling or registrations. Don’t try to lock anything on the thread already executing the component!

Source

pub fn component(&self) -> Arc<dyn CoreContainer>

Returns the component instance wrapping this component definition

This is mostly meant to be passed along for scheduling and the like. Don’t try to lock anything on the thread already executing the component!

Source

pub fn system(&self) -> ContextSystemHandle

Returns a handle to the Kompact system this component is a part of

Source

pub fn dispatcher_ref(&self) -> ActorRefStrong<DispatchEnvelope>

Returns a reference to the system dispatcher

Source

pub fn deadletter_ref(&self) -> ActorRef<!>

Returns a reference to the system’s deadletter box

Source

pub fn id(&self) -> &Uuid

Returns a reference to this components unique id

Source

pub fn suicide(&self)

Destroys this component lazily

This simply sends a Kill event to itself, which means that other events may still be handled before the component is actually killed.

For a more immediate alternative see Handled::Shutdown.

Source

pub fn preserialise<B>(&self, content: &B) -> Result<ChunkRef, SerError>
where B: Serialisable,

Attempts to create a ChunkRef of the data using the local EncodeBuffer, to be used with tell_preserialised

Source

pub fn init_buffers( &self, buffer_config: Option<BufferConfig>, custom_allocator: Option<Arc<dyn ChunkAllocator>>, )

May be used for manual initialization of a components local EncodeBuffer. If this method is never called explicitly the actor will implicitly call it when it tries to serialise its first message.

If buffers have already been initialized, explicitly or implicitly, the method does nothing.

A custom BufferConfig may be specified, if None is given the actor will first try to parse the configuration from the system wide configuration, if that fails, the default config will be used.

A custom ChunkAllocator may also be specified. if None is given the actor will use the default allocator.

Note: The BufferConfig within the systems NetworkConfig never affects the Actors Buffers (i.e. this method).

Source

pub fn set_recovery_function<F>(&self, f: F)
where F: FnOnce(FaultContext) -> RecoveryHandler + Send + 'static,

Set the recovery function for this component

See RecoveryHandler for more information.

You can perform action repeatedly in order to store state to use while recovering from a failure. Do note, however, that this call causes an additional mutex lock which is not necessarily cheap and can theoretically deadlock, if you call this from more than one place.

Trait Implementations§

Source§

impl<CD> ActorPathFactory for ComponentContext<CD>

Available on crate feature distributed only.
Source§

fn actor_path(&self) -> ActorPath

Returns the associated actor path.
Source§

impl<CD> ActorRefFactory for ComponentContext<CD>

Source§

type Message = <CD as ActorRaw>::Message

The type of messages carried by references produced by this factory
Source§

fn actor_ref(&self) -> ActorRef<<CD as ActorRaw>::Message>

Returns the associated actor reference

Auto Trait Implementations§

§

impl<CD> !Freeze for ComponentContext<CD>

§

impl<CD> !RefUnwindSafe for ComponentContext<CD>

§

impl<CD> Send for ComponentContext<CD>

§

impl<CD> !Sync for ComponentContext<CD>

§

impl<CD> Unpin for ComponentContext<CD>

§

impl<CD> UnsafeUnpin for ComponentContext<CD>

§

impl<CD> !UnwindSafe for ComponentContext<CD>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<M, T, A> Receiver<T> for A
where T: Debug + 'static, M: From<T> + MessageBounds, A: ActorRefFactory<Message = M>,

Source§

fn recipient(&self) -> Recipient<T>

Produce a recipient for T
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> Erased for T