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
156
157
158
159
160
161
162
163
use std::string::ToString;
use std::collections::HashMap;
use tokio_core::reactor::{Core, Handle};
use futures::sync::oneshot::{channel, Receiver, Sender};

use actor::{Actor, Handler, ResponseType};
use address::SyncAddress;
use arbiter::Arbiter;
use context::Context;
use msgs::{SystemExit, StopArbiter};
use message::Response;

/// System is an actor which manages process.
///
/// Before starting any actix's actors, `System` actor has to be create
/// with `System::new()` call. This method creates new `Arbiter` in current thread
/// and starts `System` actor.
///
/// # Examples
///
/// ```rust
/// extern crate actix;
///
/// use std::time::Duration;
/// use actix::prelude::*;
///
/// struct Timer {dur: Duration}
///
/// impl Actor for Timer {
///    type Context = Context<Self>;
///
///    // stop system after `self.dur` seconds
///    fn started(&mut self, ctx: &mut Context<Self>) {
///        ctx.run_later(self.dur, |act, ctx| {
///            // send `SystemExit` to `System` actor.
///            Arbiter::system().send(msgs::SystemExit(0));
///        });
///    }
/// }
///
/// fn main() {
///    // initialize system
///    let sys = System::new("test");
///
///    // Start `Timer` actor
///    let _:() = Timer{dur: Duration::new(0, 1)}.start();
///
///    // Run system, this function blocks current thread
///    let code = sys.run();
///    std::process::exit(code);
/// }
/// ```
pub struct System {
    stop: Option<Sender<i32>>,
    arbiters: HashMap<String, SyncAddress<Arbiter>>,
}

impl Actor for System {
    type Context = Context<Self>;
}

impl System {

    #[cfg_attr(feature="cargo-clippy", allow(new_ret_no_self))]
    /// Create new system
    pub fn new<T: ToString>(name: T) -> SystemRunner {
        let core = Arbiter::new_system(name.to_string());
        let (stop_tx, stop_rx) = channel();

        // start system
        let sys = System {
            arbiters: HashMap::new(), stop: Some(stop_tx)}.start();
        Arbiter::set_system(sys, name.to_string());

        SystemRunner {
            core: core,
            stop: stop_rx,
        }
    }
}

/// Helper object that runs System's event loop
#[must_use="SystemRunner must be run"]
pub struct SystemRunner {
    core: Core,
    stop: Receiver<i32>,
}

impl SystemRunner {

    /// Returns handle to the current event loop.
    pub fn handle(&self) -> &Handle {
        Arbiter::handle()
    }

    /// This function will start event loop and will finish once the `SystemExit`
    /// message get received.
    pub fn run(self) -> i32 {
        let SystemRunner { mut core, stop, ..} = self;

        // run loop
        match core.run(stop) {
            Ok(code) => code,
            Err(_) => 1,
        }
    }
}

impl Handler<SystemExit> for System {

    fn handle(&mut self, msg: SystemExit, _: &mut Context<Self>) -> Response<Self, SystemExit>
    {
        // stop rbiters
        for addr in self.arbiters.values() {
            addr.send(StopArbiter(msg.0));
        }
        // stop event loop
        if let Some(stop) = self.stop.take() {
            let _ = stop.send(msg.0);
        }
        Self::empty()
    }
}

/// Register Arbiter within system
pub(crate) struct RegisterArbiter(pub String, pub SyncAddress<Arbiter>);

#[doc(hidden)]
impl ResponseType for RegisterArbiter {
    type Item = ();
    type Error = ();
}

#[doc(hidden)]
impl Handler<RegisterArbiter> for System {

    fn handle(&mut self, msg: RegisterArbiter, _: &mut Context<Self>)
              -> Response<Self, RegisterArbiter>
    {
        self.arbiters.insert(msg.0, msg.1);
        Self::empty()
    }
}

/// Unregister Arbiter
pub(crate) struct UnregisterArbiter(pub String);

#[doc(hidden)]
impl ResponseType for UnregisterArbiter {
    type Item = ();
    type Error = ();
}

#[doc(hidden)]
impl Handler<UnregisterArbiter> for System {

    fn handle(&mut self, msg: UnregisterArbiter, _: &mut Context<Self>)
              -> Response<Self, UnregisterArbiter>
    {
        self.arbiters.remove(&msg.0);
        Self::empty()
    }
}