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

use super::Run;
use primitives::input::{RunInput, NoOp};


/// `Topology<T>` describes a data flow and controls its execution
///
pub struct Topology {
    inputs: Vec<Box<RunInput>>,
    runners: Vec<Box<Run>>,
}

impl Topology {
    /// Create a new topology
    ///
    pub fn new(inputs: Vec<Box<RunInput>>, runners: Vec<Box<Run>>) -> Self {
        Topology { inputs: inputs, runners: runners }
    }

    /// Run the topology
    ///
    pub fn run(self) -> TopologyHandle {
        info!("----> TOPOLOGY STARTING");
        let Topology {inputs, runners} = self;

        for runner in runners.into_iter() {
            thread::spawn(move || {
                runner.run();
            });
        }

        let no_ops = Arc::new(Mutex::new(inputs.iter().map(|i| i.boxed_no_op()).collect::<Vec<Box<NoOp>>>()));
        let term_txs = inputs.iter().map(|i| i.boxed_no_op()).collect::<Vec<Box<NoOp>>>();
        for (idx, input) in inputs.into_iter().enumerate() {
            let no_ops_i = no_ops.clone();
            thread::spawn(move || {
                input.run(idx, no_ops_i);
            });
        }

        info!("----> TOPOLOGY RUNNING...");

        TopologyHandle {
            term_txs: term_txs,
        }
    }
}

/// For explicitly terminating a running topology
///
pub struct TopologyHandle {
    term_txs: Vec<Box<NoOp>>,
}

// NOTE: Drop?  seems to kill tests for some reason, maybe because not capturing
impl TopologyHandle {
    pub fn stop(&mut self) {
        for tx in self.term_txs.iter() {
            tx.send_exit();
        }
        debug!("----> TOPOLOGY DROPPED");
    }
}