summaryrefslogtreecommitdiffstats
path: root/crates/core/tedge_api/examples/heartbeat.rs
diff options
context:
space:
mode:
authorMarcel Müller <m.mueller@ifm.com>2022-02-04 14:49:40 +0100
committerMatthias Beyer <matthias.beyer@ifm.com>2022-03-04 15:03:53 +0100
commit8bb2a0d919636a4f5d61af4c2feefc8535f17971 (patch)
treee1fa2b5808a478558b16481e9ad7d9edc5a425bc /crates/core/tedge_api/examples/heartbeat.rs
parent32d66840213fd983b40c5000b1fe22963becd936 (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>
Diffstat (limited to 'crates/core/tedge_api/examples/heartbeat.rs')
-rw-r--r--crates/core/tedge_api/examples/heartbeat.rs105
1 files changed, 105 insertions, 0 deletions
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();
+}