diff options
author | Marcel Müller <m.mueller@ifm.com> | 2022-02-04 14:49:40 +0100 |
---|---|---|
committer | Matthias Beyer <matthias.beyer@ifm.com> | 2022-03-04 15:03:53 +0100 |
commit | 8bb2a0d919636a4f5d61af4c2feefc8535f17971 (patch) | |
tree | e1fa2b5808a478558b16481e9ad7d9edc5a425bc | |
parent | 32d66840213fd983b40c5000b1fe22963becd936 (diff) |
Add example service to tedge_api
A very simple service that is meant to showcase how plugins can be built
and their lifecycles.
Signed-off-by: Marcel Müller <m.mueller@ifm.com>
Signed-off-by: Matthias Beyer <matthias.beyer@ifm.com>
-rw-r--r-- | crates/core/tedge_api/Cargo.toml | 1 | ||||
-rw-r--r-- | crates/core/tedge_api/examples/heartbeat.rs | 105 |
2 files changed, 106 insertions, 0 deletions
diff --git a/crates/core/tedge_api/Cargo.toml b/crates/core/tedge_api/Cargo.toml index dcbb7f87..cab37c73 100644 --- a/crates/core/tedge_api/Cargo.toml +++ b/crates/core/tedge_api/Cargo.toml @@ -13,3 +13,4 @@ toml = "0.5.8" [dev-dependencies] static_assertions = "1.1.0" +tokio = { version = "1.16.1", features = ["full"] } diff --git a/crates/core/tedge_api/examples/heartbeat.rs b/crates/core/tedge_api/examples/heartbeat.rs new file mode 100644 index 00000000..83c8a672 --- /dev/null +++ b/crates/core/tedge_api/examples/heartbeat.rs @@ -0,0 +1,105 @@ +use async_trait::async_trait; +use tedge_api::{ + address::EndpointKind, + messages::{CoreMessageKind, PluginMessageKind}, + plugins::Comms, + Address, CoreMessage, Plugin, PluginBuilder, PluginConfiguration, PluginError, PluginMessage, +}; + +struct HeartbeatServiceBuilder; + +impl PluginBuilder for HeartbeatServiceBuilder { + fn name(&self) -> &'static str { + todo!() + } + + fn verify_configuration( + &self, + _config: PluginConfiguration, + ) -> Result<(), tedge_api::errors::PluginConfigurationError> { + Ok(()) + } + + fn instantiate( + &self, + _config: PluginConfiguration, + tedge_comms: tedge_api::plugins::Comms, + ) -> Box<dyn Plugin + 'static> { + Box::new(HeartbeatService::new(tedge_comms)) + } +} + +struct HeartbeatService { + comms: tedge_api::plugins::Comms, +} + +impl HeartbeatService { + fn new(comms: tedge_api::plugins::Comms) -> Self { + Self { comms } + } +} + +#[async_trait] +impl Plugin for HeartbeatService { + async fn setup(&mut self) -> Result<(), PluginError> { + println!("Setting up heartbeat service!"); + Ok(()) + } + + async fn handle_message(&self, message: PluginMessage) -> Result<(), PluginError> { + match message.kind() { + tedge_api::messages::PluginMessageKind::CheckReadyness => { + let msg = CoreMessage::new( + message.origin().clone(), + CoreMessageKind::SignalPluginState { + state: String::from("Ok"), + }, + ); + self.comms.send(msg).await?; + } + } + + Ok(()) + } + + async fn shutdown(&mut self) -> Result<(), PluginError> { + println!("Shutting down heartbeat service!"); + Ok(()) + } +} + +#[tokio::main] +async fn main() { + let hsb = HeartbeatServiceBuilder; + let (sender, mut receiver) = tokio::sync::mpsc::channel(10); + + let comms = Comms::new(sender); + + let config = toml::from_str("").unwrap(); + + let mut heartbeat = hsb.instantiate(config, comms); + + heartbeat.setup().await.unwrap(); + + let handle = tokio::task::spawn(async move { + let hb = heartbeat; + + hb.handle_message(PluginMessage::new( + Address::new(EndpointKind::Core), + PluginMessageKind::CheckReadyness, + )) + .await + .unwrap(); + + hb + }); + + println!( + "Receiving message from service: {:#?}", + receiver.recv().await + ); + + let mut heartbeat = handle.await.unwrap(); + + heartbeat.shutdown().await.unwrap(); +} |