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
use std::io;
use std::default::Default;

use mio::util::Slab;
use mio::{EventLoop, EventLoopConfig};

use handler::Handler;
use {Machine};


/// Event loop configuration
///
/// The structure currently embeds mio configuration too
#[derive(Debug, Clone)]
pub struct Config {
    mio: EventLoopConfig,
    slab_capacity: usize,
}

impl Default for Config {
    fn default() -> Config {
        Config {
            mio: Default::default(),
            slab_capacity: 4096,
        }
    }
}

impl Config {
    /// Create new configuration with default options
    pub fn new() -> Config {
        Config {
            mio: EventLoopConfig::new(),
            slab_capacity: 4096,
        }
    }
    /// A mutable reference for ``mio::EventLoopConfig``
    pub fn mio(&mut self) -> &mut EventLoopConfig {
        &mut self.mio
    }
    /// A capacity of state machine slab
    ///
    /// This limits the number of state machines that application is able
    /// to create. Consequently this limits the number of connections that
    /// server is able to establish.
    pub fn slab_capacity(&mut self, capacity: usize) {
        self.slab_capacity = capacity;
    }
}


// The functions are probably don't belong here, but they accept otherwise
// private parts of the config structure

pub fn create_slab<M:Sized>(cfg: &Config) -> Slab<M> {
    Slab::new(cfg.slab_capacity)
}

pub fn create_loop<M: Machine>(cfg: &Config)
    -> Result<EventLoop<Handler<M>>, io::Error>
{
    EventLoop::configured(cfg.mio.clone())
}