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
use super::{link, PathNotification};
use anyhow::Error;
use async_trait::async_trait;
use meio::{ActionHandler, Actor, Context, Distributor, InterruptedBy, StartedBy};
use rill_protocol::provider::{Description, EntryId, Path};
use std::collections::{hash_map::Entry, HashMap};
use thiserror::Error;

#[derive(Debug, Error)]
pub enum Reason {
    #[error("Path already declared {0}")]
    AlreadyDeclaredPath(Path),
}

#[derive(Debug)]
struct Record {
    description: Description,
    declared: bool,
}

// TODO! RENAME TO `StructureWatcher`.

/// The `Actor` that informs about appeared providers and paths.
pub struct Broadcaster {
    name: Option<EntryId>,
    paths_trackers: Distributor<PathNotification>,
    recipients: HashMap<Path, Record>,
}

impl Broadcaster {
    pub fn new() -> Self {
        Self {
            name: None,
            paths_trackers: Distributor::new(),
            recipients: HashMap::new(),
        }
    }

    async fn graceful_shutdown(&mut self, ctx: &mut Context<Self>) {
        ctx.shutdown();
    }
}

impl Actor for Broadcaster {
    type GroupBy = ();
}

#[async_trait]
impl<T: Actor> StartedBy<T> for Broadcaster {
    async fn handle(&mut self, _ctx: &mut Context<Self>) -> Result<(), Error> {
        Ok(())
    }
}

#[async_trait]
impl<T: Actor> InterruptedBy<T> for Broadcaster {
    async fn handle(&mut self, ctx: &mut Context<Self>) -> Result<(), Error> {
        self.graceful_shutdown(ctx).await;
        Ok(())
    }
}

#[async_trait]
impl ActionHandler<link::SessionLifetime> for Broadcaster {
    async fn handle(
        &mut self,
        msg: link::SessionLifetime,
        _ctx: &mut Context<Self>,
    ) -> Result<(), Error> {
        use link::SessionLifetime::*;
        match msg {
            Attached { name, .. } => {
                let msg = PathNotification::Name { name: name.clone() };
                // Don't subscribe here till the stream (path) will be declared.
                self.name = Some(name);
                self.paths_trackers.act_all(msg).await?;
            }
            Detached => {
                self.name.take();
                for record in self.recipients.values_mut() {
                    record.declared = false;
                }
            }
        }
        Ok(())
    }
}

impl Broadcaster {
    fn declared_paths(&self) -> Vec<Description> {
        self.recipients
            .iter()
            .filter_map(|(_, record)| {
                if record.declared {
                    Some(record.description.clone())
                } else {
                    None
                }
            })
            .collect()
    }
}

#[async_trait]
impl ActionHandler<link::PathDeclared> for Broadcaster {
    async fn handle(
        &mut self,
        msg: link::PathDeclared,
        _ctx: &mut Context<Self>,
    ) -> Result<(), Error> {
        let path = msg.description.path.clone();
        log::info!("Declare path: {}", path);
        let entry = self.recipients.entry(path);
        match entry {
            Entry::Vacant(entry) => {
                let record = Record {
                    description: msg.description.clone(),
                    declared: true,
                };
                entry.insert(record);
                let msg = PathNotification::Paths {
                    descriptions: vec![msg.description],
                };
                self.paths_trackers.act_all(msg).await?;
                Ok(())
            }
            Entry::Occupied(entry) => {
                let path = entry.get().description.path.clone();
                Err(Reason::AlreadyDeclaredPath(path).into())
            }
        }
    }
}

#[async_trait]
impl ActionHandler<link::SubscribeToStructChanges> for Broadcaster {
    async fn handle(
        &mut self,
        mut msg: link::SubscribeToStructChanges,
        ctx: &mut Context<Self>,
    ) -> Result<(), Error> {
        ctx.not_terminating()?;

        if let Some(name) = self.name.clone() {
            let event = PathNotification::Name { name };
            msg.recipient.act(event).await?;
        }

        let descriptions = self.declared_paths();
        let event = PathNotification::Paths { descriptions };
        msg.recipient.act(event).await?;
        self.paths_trackers.insert(msg.recipient);
        Ok(())
    }
}