krossbar_bus_lib/lib.rs
1//! ## Krossbar bus lib
2//!
3//! A library to register and connect Krossbar services
4//!
5//! Krossbar services utilize UDS to communicate with each other.
6//! Krossbar hub acts as a point of rendezvous for the services, checking permissions and connecting counterparties.
7//!
8//! The library uses [krossbar_rpc::rpc::Rpc] connections to comunicate.
9//!
10//! To register a service call [Service::new]. This makes a call to the hub trying to register a service with a given name.
11//!
12//! [Service] exposes two subsets of methods: to act as a message source, or a message consumer:
13//!
14//! ### Service
15//! To act as a message source you have a bunch of method to register service endpoints:
16//! - [Service::register_method] to register a method, which can be called by an arbitrary service. Can be used as a one-way message receiver (see [Client::message]);
17//! - [Service::register_signal] to register a signal, to which peers are able to subscribe. Calling [Signal::emit] will broadcast a value to the signal subscribers;
18//! - [Service::register_state] to register a state, which holds a value and can be can be subscribed to, or called to retrieve the value. Calling [State::set] will broadcast a value to the state subscribers.
19//!
20//! ### Client
21//! To act as as message consumer, you have to connect to a client using [Service::connect]. As expected, hub needs to check if you are allowed to connect to the client, but after you've connected, there a couple of methods you can use:
22//! - [Client::call] to call a method or retrieve state value;
23//! - [Client::get] get a remote state value;
24//! - [Client::subscribe] to subscribe to a signal, or state changes;
25//! - [Client::message] to send-one-way message.
26//!
27//! ### Polling
28//! In order to receive incoming connection and messages you need to poll the [Service]. There are two methods to do this:
29//!
30//! 1. Using [Service::run] if you don't need a service handle anymore. This is much more convenient way. You can spawn a task to just poll the service in a loop:
31//!
32//! ```rust
33//!
34//! use std::path::PathBuf;
35//!
36//! use tokio;
37//!
38//! use krossbar_bus_lib::service::Service;
39//!
40//! async fn example() {
41//! let mut service = Service::new("com.echo.service", &PathBuf::from("/var/run/krossbar.hub.socket"))
42//! .await
43//! .expect("Failed to register service");
44//!
45//! service
46//! .register_method("method", |client_name, value: i32| {
47//! println!("Client name: {client_name}");
48//!
49//! return format!("Hello, {}", value);
50//! })
51//! .expect("Failed to register method");
52//!
53//! tokio::spawn(service.run());
54//! }
55//! ```
56//!
57//! 2. Using [Service::poll] if you still need a service handle to make new connections, or register new endpoints.
58//! You can use future combinators to poll both: service handle and endpoint handles.
59//!
60//! ```rust
61//!
62//! use std::path::PathBuf;
63//!
64//! use futures::StreamExt;
65//! use tokio;
66//!
67//! use krossbar_bus_lib::service::Service;
68//!
69//! async fn example() {
70//! let mut service = Service::new("com.signal.subscriber", &PathBuf::from("/var/run/krossbar.hub.socket"))
71//! .await
72//! .expect("Failed to register service");
73//!
74//! let mut client = service.connect("com.signalling.service")
75//! .await
76//! .expect("Failed to register to a service");
77//!
78//! let mut subscription = client.subscribe::<String>("signal")
79//! .await
80//! .expect("Failed ot subscribe ot a signal");
81//!
82//! tokio::select! {
83//! value = subscription.next() => {
84//! println!("Signal data: {value:?}");
85//! },
86//! _ = service.poll() => {}
87//! }
88//! }
89//! ```
90//!
91//! ### Service files
92//! To be able to register as a service and receive connections, you need to maintain a service file for each of the services.
93//! The file filename is a service name. File content is a JSON, which contains a path to a binary, which is allowed to register the service name, and a list of allowed connections. Both support globs. See [examples](https://github.com/krossbar-platform/krossbar-bus/tree/main/krossbar-bus-lib/examples) for a bunch of examples.
94//!
95//! ### Monitor
96//! Having `monitor` feature allows you to use [Krossbar Monitor](https://github.com/krossbar-platform/krossbar-bus/tree/main/krossbar-bus-monitor) to monitor service communication. Refer to the corresponding crate for usage.
97//!
98//! ### Inspection
99//! Having `inspection` feature allows you to use [Krossbar Connect tool](https://github.com/krossbar-platform/krossbar-bus/tree/main/krossbar-bus-connect) to inspect service endpoints. Refer to the corresponding crate for usage.
100//!
101//! Also, the tool allows you to call or subscribe to a service using CLI.
102//!
103//! ## Examples
104//! See [examples dir](https://github.com/krossbar-platform/krossbar-bus/tree/main/krossbar-bus-lib/examples) for usage examples
105//!
106
107pub mod client;
108pub mod endpoints;
109pub mod service;
110mod signal;
111
112pub use krossbar_rpc::{Error, Result};
113
114pub use client::Client;
115pub use endpoints::{Signal, State};
116pub use service::Service;