pub struct Context { /* private fields */ }
Expand description

Context contains Node state and references to the runtime.

Implementations

Return runtime clone

Return the primary address of the current worker

Return all addresses of the current worker

Return a reference to the mailboxes of this context

Create a new detached Context that will apply the given AccessControl to any incoming messages it receives

Create a new detached Context without spawning a full worker

Note: this function is very low-level. For most users start_worker() is the recommended to way to create a new worker context.

Start a new worker instance at the given address set

A worker is an asynchronous piece of code that can send and receive messages of a specific type. This type is encoded via the Worker trait. If your code relies on a manual run-loop you may want to use start_processor() instead!

Each address in the set must be unique and unused on the current node. Workers must implement the Worker trait and be thread-safe. Workers run asynchronously and will be scheduled independently of each other. To wait for the initialisation of your worker to complete you can use wait_for().

The worker will inherit its AccessControl from this context. Use WorkerBuilder to start a worker with custom access control.

use ockam_core::{Result, Worker, worker};
use ockam_node::Context;

struct MyWorker;

#[worker]
impl Worker for MyWorker {
    type Context = Context;
    type Message = String;
}

async fn start_my_worker(ctx: &mut Context) -> Result<()> {
    ctx.start_worker("my-worker-address", MyWorker).await
}

Start a new processor instance at the given address set

A processor is an asynchronous piece of code that runs a custom run loop, with access to a worker context to send and receive messages. If your code is built around responding to message events, consider using start_worker() instead!

Shut down a local worker by its primary address

Shut down a local processor by its address

Signal to the local runtime to shut down immediately

WARNING: calling this function may result in data loss. It is recommended to use the much safer Context::stop function instead!

Signal to the local runtime to shut down

This call will hang until a safe shutdown has been completed. The default timeout for a safe shutdown is 1 second. You can change this behaviour by calling Context::stop_timeout directly.

Signal to the local runtime to shut down

This call will hang until a safe shutdown has been completed or the desired timeout has been reached.

Using a temporary new context, send a message and then receive a message

This helper function uses new_detached, send, and receive internally. See their documentation for more details.

Send a message to another address associated with this worker

This function is a simple wrapper around Self::send() which validates the address given to it and will reject invalid addresses.

Send a message to an address or via a fully-qualified route

Routes can be constructed from a set of Addresses, or via the RouteBuilder type. Routes can contain middleware router addresses, which will re-address messages that need to be handled by specific domain workers.

use ockam_core::Message;
use serde::{Serialize, Deserialize};

#[derive(Message, Serialize, Deserialize)]
struct MyMessage(String);

impl MyMessage {
    fn new(s: &str) -> Self {
        Self(s.into())
    }
}

ctx.send("my-test-worker", MyMessage::new("Hello you there :)")).await?;
Ok(())

Send a message to an address or via a fully-qualified route after attaching the given LocalInfo to the message.

Send a message to an address or via a fully-qualified route

Routes can be constructed from a set of Addresses, or via the RouteBuilder type. Routes can contain middleware router addresses, which will re-address messages that need to be handled by specific domain workers.

This function additionally takes the sending address parameter, to specify which of a worker’s (or processor’s) addresses should be used.

Forward a transport message to its next routing destination

Similar to Context::send, but taking a TransportMessage, which contains the full destination route, and calculated return route for this hop.

Note: you most likely want to use Context::send instead, unless you are writing an external router implementation for ockam node.

Block the current worker to wait for a typed message

Warning this function will wait until its running ockam node is shut down. A safer variant of this function is receive and receive_timeout.

Block the current worker to wait for a typed message

This function may return a Err(FailedLoadData) if the underlying worker was shut down, or Err(Timeout) if the call was waiting for longer than the default timeout. Use receive_timeout to adjust the timeout period.

Will return None if the corresponding worker has been stopped, or the underlying Node has shut down.

Wait to receive a message up to a specified timeout

See receive for more details.

Wait to receive a message up to a specified timeout

See receive for more details.

Block the current worker to wait for a message satisfying a conditional

Will return Err if the corresponding worker has been stopped, or the underlying node has shut down. This operation has a default timeout.

Internally this function uses receive, so is subject to the same timeout.

Assign the current worker to a cluster

A cluster is a set of workers that should be stopped together when the node is stopped or parts of the system are reloaded. This is not to be confused with supervisors!

By adding your worker to a cluster you signal to the runtime that your worker may be depended on by other workers that should be stopped first.

Your cluster name MUST NOT start with _internals. or ockam.!

Clusters are de-allocated in reverse order of their initialisation when the node is stopped.

Return a list of all available worker addresses on a node

Register a router for a specific address type

Wait for a particular address to become “ready”

Trait Implementations

Try cloning a object and return an Err in case of failure.

Executes the destructor for this type. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more