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
use tokio::sync::broadcast::{self, error::RecvError};

use rsiot_messages_core::{
    msg_meta::{ComponentId, ExecutorId},
    IMessage,
};
use tracing::warn;

#[derive(Debug)]
pub struct CmpInput<TMsg>
where
    TMsg: IMessage,
{
    channel: broadcast::Receiver<TMsg>,
    executor_id: ExecutorId,
    component_id: Option<ComponentId>,
}

impl<TMsg> CmpInput<TMsg>
where
    TMsg: IMessage,
{
    pub fn new(channel: broadcast::Receiver<TMsg>, service_id: ExecutorId) -> Self {
        Self {
            channel,
            executor_id: service_id,
            component_id: None,
        }
    }

    pub(crate) fn set_component_name(&mut self, component_name: &str) -> ComponentId {
        let component_id = ComponentId::new(&self.executor_id, component_name);
        self.component_id = Some(component_id.clone());
        component_id
    }

    pub async fn recv(&mut self) -> Result<Option<TMsg>, RecvError> {
        let msg = self.channel.recv().await?;
        if msg.cmp_process() == self.component_id {
            warn!("Message reject: {:?}", msg);
            return Ok(None);
        }
        Ok(Some(msg))
    }
}

impl<TMsg> Clone for CmpInput<TMsg>
where
    TMsg: IMessage,
{
    fn clone(&self) -> Self {
        Self {
            channel: self.channel.resubscribe(),
            executor_id: self.executor_id.clone(),
            component_id: self.component_id.clone(),
        }
    }
}