wascc_host/bus/
mod.rs

1#[cfg(feature = "lattice")]
2use crossbeam::Sender;
3
4pub const URL_SCHEME: &str = "wasmbus";
5
6#[cfg(feature = "lattice")]
7use crate::{BindingsList, RouteKey};
8#[cfg(feature = "lattice")]
9use std::collections::HashMap;
10#[cfg(feature = "lattice")]
11use std::sync::{Arc, RwLock};
12#[cfg(feature = "lattice")]
13use wascap::jwt::{Actor, Claims};
14#[cfg(feature = "lattice")]
15use wascc_codec::capabilities::CapabilityDescriptor;
16
17#[cfg(not(feature = "lattice"))]
18pub(crate) mod inproc;
19#[cfg(feature = "lattice")]
20pub(crate) mod lattice;
21
22#[cfg(not(feature = "lattice"))]
23pub(crate) use inproc::InprocBus as MessageBus;
24
25#[cfg(feature = "lattice")]
26pub(crate) use lattice::DistributedBus as MessageBus;
27
28#[cfg(not(feature = "lattice"))]
29pub(crate) fn new() -> MessageBus {
30    inproc::InprocBus::new()
31}
32
33#[cfg(feature = "lattice")]
34pub(crate) fn new(
35    host_id: String,
36    claims: Arc<RwLock<HashMap<String, Claims<Actor>>>>,
37    caps: Arc<RwLock<HashMap<RouteKey, CapabilityDescriptor>>>,
38    bindings: Arc<RwLock<BindingsList>>,
39    labels: Arc<RwLock<HashMap<String, String>>>,
40    terminators: Arc<RwLock<HashMap<String, Sender<bool>>>>,
41    ns: Option<String>,
42    cplane_s: Sender<lattice::ControlCommand>,
43    authz: Arc<RwLock<Box<dyn crate::authz::Authorizer>>>,
44    image_map: Arc<RwLock<HashMap<String, String>>>,
45) -> MessageBus {
46    lattice::DistributedBus::new(
47        host_id,
48        claims,
49        caps,
50        bindings,
51        labels,
52        terminators,
53        ns,
54        cplane_s,
55        authz,
56        image_map,
57    )
58}
59
60const LATTICE_NAMESPACE_ENV: &str = "LATTICE_NAMESPACE";
61
62pub(crate) fn get_namespace_prefix() -> Option<String> {
63    ::std::env::var(LATTICE_NAMESPACE_ENV).ok()
64}
65
66pub(crate) fn actor_subject(ns: Option<&str>, actor: &str) -> String {
67    format!("{}.actor.{}", nsprefix(ns), actor)
68}
69
70pub(crate) fn provider_subject(ns: Option<&str>, capid: &str, binding: &str) -> String {
71    format!(
72        "{}.provider.{}.{}",
73        nsprefix(ns),
74        normalize_capid(capid),
75        binding
76    )
77}
78
79pub(crate) fn inventory_wildcard_subject(ns: Option<&str>) -> String {
80    format!("{}.inventory.*", nsprefix(ns))
81}
82
83pub(crate) fn event_subject(ns: Option<&str>) -> String {
84    format!("{}.events", nsprefix(ns))
85}
86
87// By convention most of the waSCC ecosystem uses a "group:item" string
88// for the capability IDs, e.g. "wascc:messaging" or "gpio:relay". To
89// accommodate message broker subjects that might not work with the ":"
90// character, we normalize the segments to dot-separated.
91pub(crate) fn normalize_capid(capid: &str) -> String {
92    capid.to_lowercase().replace(":", ".").replace(" ", "_")
93}
94
95pub(crate) fn provider_subject_bound_actor(
96    ns: Option<&str>,
97    capid: &str,
98    binding: &str,
99    calling_actor: &str,
100) -> String {
101    format!(
102        "{}.provider.{}.{}.{}",
103        nsprefix(ns),
104        normalize_capid(capid),
105        binding,
106        calling_actor
107    )
108}
109
110pub(crate) fn nsprefix(ns: Option<&str>) -> String {
111    match ns {
112        Some(s) => format!("{}.wasmbus", s),
113        None => "wasmbus".to_string(),
114    }
115}