run_node_with_instance

Macro run_node_with_instance 

Source
macro_rules! run_node_with_instance {
    ($node_type:ty, $node_name:expr, $instance_env:expr, $env_var:expr, $config_file:expr, $env_prefix:expr) => { ... };
}
Expand description

Run a node with instance support for multi-instance deployments

This macro extends run_node_simple! with support for instance names, allowing the same node implementation to run multiple instances with different names and configurations.

§Arguments

  • $node_type - The NodeImpl type (e.g., CameraFakeNode)
  • $node_name - The base node name string (e.g., “camera_fake”)
  • $instance_env - Environment variable for instance name (e.g., “CAMERA_INSTANCE”)
  • $env_var - Environment variable for config file path (e.g., “CAMERA_CONFIG”)
  • $config_file - Default config file path (e.g., “config/drivers/camera/default.json”)
  • $env_prefix - Environment variable prefix for config overrides (e.g., “CAMERA”)

§Example

use mecha10::prelude::*;

#[derive(Debug)]
struct CameraFakeNode {
    config: CameraConfig,
}

#[derive(Debug, Clone, Deserialize, Serialize, Default)]
struct CameraConfig {
    fps: f32,
}

#[async_trait]
impl NodeImpl for CameraFakeNode {
    type Config = CameraConfig;

    async fn init(config: Self::Config) -> Result<Self> {
        Ok(Self { config })
    }

    async fn run(&mut self, ctx: &Context) -> Result<()> {
        // Node logic here - publishes to instance-scoped topics
        Ok(())
    }
}

run_node_with_instance!(
    CameraFakeNode,
    "camera_fake",
    "CAMERA_INSTANCE",  // Set to "left" or "right" etc.
    "CAMERA_CONFIG",
    "config/drivers/camera/default.json",
    "CAMERA"
);

§Environment Variables

  • $instance_env: Instance name (e.g., “left”, “right”, “front_left”)
  • $env_var: Config file path
  • ${env_prefix}_*: Config overrides

§Instance Naming

If $instance_env is set to “left”:

  • Node ID: “camera_fake/left”
  • Topics published via publish_to_scoped(): “/sensor/camera/rgb/left”