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
use std::thread::JoinHandle;

use massa_channel::sender::MassaSender;
use massa_protocol_exports::ProtocolManager;
use tracing::info;

use crate::connectivity::ConnectivityCommand;

/// protocol manager used to stop the protocol
pub struct ProtocolManagerImpl {
    connectivity_thread: Option<(MassaSender<ConnectivityCommand>, JoinHandle<()>)>,
}

impl ProtocolManagerImpl {
    pub fn new(connectivity_thread: (MassaSender<ConnectivityCommand>, JoinHandle<()>)) -> Self {
        Self {
            connectivity_thread: Some(connectivity_thread),
        }
    }
}

impl ProtocolManager for ProtocolManagerImpl {
    /// Stop the protocol module
    fn stop(&mut self) {
        info!("stopping protocol module...");
        if let Some((tx, join_handle)) = self.connectivity_thread.take() {
            tx.send(ConnectivityCommand::Stop)
                .expect("Failed to send stop command of protocol");
            drop(tx);
            join_handle
                .join()
                .expect("connectivity thread panicked on try to join");
        }
    }
}