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
use std::fmt::Debug;

use tokio::sync::mpsc;

use rsiot_messages_core::*;
use uuid::Uuid;

use crate::ComponentError;

#[derive(Clone, Debug)]
pub struct CmpOutput<TMsg> {
    channel: mpsc::Sender<Message<TMsg>>,
    id: Uuid,
    name: String,
}

impl<TMsg> CmpOutput<TMsg>
where
    TMsg: MsgDataBound,
{
    pub fn new(
        channel: mpsc::Sender<Message<TMsg>>,
        executor_name: &str,
        executor_id: Uuid,
    ) -> Self {
        Self {
            channel,
            id: executor_id,
            name: executor_name.into(),
        }
    }

    pub(crate) fn set_component_id(&mut self, name: &str, id: Uuid) {
        self.id = id;
        self.name = format!("{}::{}", self.name, name);
    }

    pub(crate) fn set_session_id(&mut self, name: &str, id: Uuid) {
        self.id = id;
        self.name = format!("{}::{}", self.name, name);
    }
    pub async fn send(&self, mut msg: Message<TMsg>) -> Result<(), ComponentError> {
        // msg.cmp_set(&self.msg_source);
        msg.add_trace_item(&self.id, &self.name);
        self.channel
            .send(msg)
            .await
            .map_err(|e| ComponentError::CmpOutput(e.to_string()))
    }
}