summaryrefslogtreecommitdiffstats
path: root/crates/core/tedge_api/examples/heartbeat.rs
blob: 0b5cc823cce6660fad984bb7eb448a43dbd9ae6d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
use async_trait::async_trait;
use tedge_api::{
    address::EndpointKind,
    plugin::{Handle, HandleTypes, Message},
    Address, CoreCommunication, MessageKind, Plugin, PluginBuilder, PluginConfiguration,
    PluginError,
};

struct Heartbeat;
impl Message for Heartbeat {}

enum HeartbeatStatusReply {
    Alive,
    Degraded,
}
impl Message for HeartbeatStatusReply {}

struct HeartbeatServiceBuilder;

#[async_trait]
impl PluginBuilder for HeartbeatServiceBuilder {
    fn kind_name(&self) -> &'static str {
        todo!()
    }

    fn kind_message_types(&self) -> tedge_api::plugin::HandleTypes {
        HandleTypes::get_handlers_for::<(HeartbeatStatusReply,), HeartbeatService>()
    }

    async fn verify_configuration(
        &self,
        _config: &PluginConfiguration,
    ) -> Result<(), tedge_api::error::PluginError> {
        Ok(())
    }

    async fn instantiate(
        &self,
        config: PluginConfiguration,
        tedge_comms: tedge_api::plugin::CoreCommunication,
    ) -> Result<Box<dyn Plugin>, PluginError> {
        let hb_config: HeartbeatConfig = toml::Value::try_into(config.into_inner())?;
        Ok(Box::new(HeartbeatService::new(tedge_comms, hb_config)))
    }
}

#[derive(serde::Deserialize, Debug)]
struct HeartbeatConfig {
    interval: u64,
}

struct HeartbeatService {
    comms: tedge_api::plugin::CoreCommunication,
    config: HeartbeatConfig,
}

impl HeartbeatService {
    fn new(comms: tedge_api::plugin::CoreCommunication, config: HeartbeatConfig) -> Self {
        Self { comms, config }
    }
}

#[async_trait]
impl Handle<HeartbeatStatusReply> for HeartbeatService {
    async fn handle_message(&self, message: HeartbeatStatusReply) -> Result<(), PluginError> {
        println!("Received Heartbeat!");
        Ok(())
    }
}

struct CriticalServiceBuilder;

#[async_trait]
impl PluginBuilder for CriticalServiceBuilder {
    fn kind_name(&self) -> &'static str {
        todo!()
    }

    fn kind_message_types(&self) -> tedge_api::plugin::HandleTypes {
        HandleTypes::get_handlers_for::<(Heartbeat,), CriticalService>()
    }

    async fn verify_configuration(
        &self,
        _config: &PluginConfiguration,
    ) -> Result<(), tedge_api::error::PluginError> {
        Ok(())
    }

    async fn instantiate(
        &self,
        config: PluginConfiguration,
        tedge_comms: tedge_api::plugin::CoreCommunication,
    ) -> Result<Box<dyn Plugin>, PluginError> {
        let hb_config: HeartbeatConfig = toml::Value::try_into(config.into_inner())?;
        Ok(Box::new(HeartbeatService::new(tedge_comms, hb_config)))
    }
}

struct CriticalService;

#[async_trait]
impl Handle<Heartbeat> for CriticalService {
    async fn handle_message(&self, message: Heartbeat) -> Result<(), PluginError> {
        println!("Received Heartbeat!");
        Ok(())
    }
}

#[async_trait]
impl Plugin for HeartbeatService {
    async fn setup(&mut self) -> Result<(), PluginError> {
        println!(
            "Setting up heartbeat service with interval: {}!",
            self.config.interval
        );
        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 plugin_name = "heartbeat-service".to_string();
    let comms = CoreCommunication::new(plugin_name.clone(), sender);

    let config = toml::from_str(
        r#"
    interval = 200
    "#,
    )
    .unwrap();

    let mut heartbeat = hsb.instantiate(config, comms.clone()).await.unwrap();

    heartbeat.setup().await.unwrap();

    let handle = tokio::task::spawn(async move {
        let hb = heartbeat;

        hb.handle_message(Message::new(
            Address::new(EndpointKind::Plugin { id: plugin_name }),
            Address::new(EndpointKind::Core),
            MessageKind::CheckReadyness,
        ))
        .await
        .unwrap();

        hb
    });

    println!(
        "Receiving message from service: {:#?}",
        receiver.recv().await
    );

    let mut heartbeat = handle.await.unwrap();

    heartbeat.shutdown().await.unwrap();
}