pub trait Service: Send + Sync {
// Required methods
fn run<'life0, 'async_trait>(
&'life0 self,
action: Value,
write: WebSocketWrite,
) -> Pin<Box<dyn Future<Output = (i32, String)> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait;
fn as_any(&self) -> &dyn Any;
fn stop_service(&self);
fn get_version(&self) -> String;
}Expand description
Common interface every business module in pispas-modules implements.
Incoming WebSocket messages are dispatched to an implementation by the
TARGET field of the JSON payload. The trait stays deliberately small
so adding a new module is cheap: implement Service::run, return the
static version via Service::get_version, hook it up in
load_services, and document it in docs/MODULES.md.
§Example skeleton
use async_trait::async_trait;
use serde_json::Value;
use pispas_modules::prelude::{Service, WebSocketWrite};
pub struct MyService;
#[async_trait]
impl Service for MyService {
async fn run(&self, action: Value, _write: WebSocketWrite) -> (i32, String) {
match action.get("ACTION").and_then(|v| v.as_str()) {
Some("PING") => (0, "pong".into()),
_ => (1, "unknown action".into()),
}
}
fn as_any(&self) -> &dyn std::any::Any { self }
fn stop_service(&self) {}
fn get_version(&self) -> String { env!("CARGO_PKG_VERSION").into() }
}Required Methods§
Sourcefn run<'life0, 'async_trait>(
&'life0 self,
action: Value,
write: WebSocketWrite,
) -> Pin<Box<dyn Future<Output = (i32, String)> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn run<'life0, 'async_trait>(
&'life0 self,
action: Value,
write: WebSocketWrite,
) -> Pin<Box<dyn Future<Output = (i32, String)> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Executes an action for the service.
§Arguments
action- AValueobject representing the action to be performed. The structure of the action depends on the service implementation.write- AWebSocketWriteobject for sending responses or notifications via a WebSocket connection.
§Returns
A tuple containing:
i32- Status code (e.g., 0 for success, 1 for error).String- A descriptive message or response related to the action.
§Notes
This method must be implemented asynchronously by the service.
Sourcefn as_any(&self) -> &dyn Any
fn as_any(&self) -> &dyn Any
Allows dynamic casting of the service to a specific type.
§Returns
A reference to dyn Any, which can be used for type-safe downcasting.
Sourcefn stop_service(&self)
fn stop_service(&self)
Stops the service and cleans up resources.
§Notes
This method should ensure that all tasks and processes related to the service are properly terminated. It can also reset any internal states.
Sourcefn get_version(&self) -> String
fn get_version(&self) -> String
Retrieves the version of the service.
§Returns
A String containing the version information of the service.
Implementors§
impl Service for BaseService
Implementation of the Service trait for BaseService.