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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
use std::error::Error;

use mio::{self, EventLoop, Token, EventSet, Sender};
use mio::util::Slab;

use scope::scope;
use {SpawnError, Scope, Response, Machine};
use SpawnError::{NoSlabSpace, UserError};


pub enum Timeo {
    Fsm(Token),
}

pub enum Notify {
    Fsm(Token),
}


/// Standard mio loop handler
///
///
/// # Examples
///
/// ```ignore
/// extern crate mio;
/// extern crate rotor;
///
/// let mut event_loop = mio::EventLoop::new().unwrap();
/// let mut handler = rotor::Handler::new(Context, &mut event_loop);
/// let conn = handler.add_machine_with(&mut event_loop, |scope| {
///     Ok(StateMachineConstuctor(..))
/// });
/// assert!(conn.is_ok());
/// event_loop.run(&mut handler).unwrap();
/// ```
pub struct Handler<Ctx, M>
    where M: Machine<Context=Ctx>
{
    slab: Slab<M>,
    context: Ctx,
    channel: Sender<Notify>,
}


impl<C, M> Handler<C, M>
    where M: Machine<Context=C>,
{
    pub fn new_with_capacity(context: C, eloop: &mut EventLoop<Handler<C, M>>,
        capacity: usize)
        -> Handler<C, M>
    {
        Handler {
            slab: Slab::new(capacity),
            context: context,
            channel: eloop.channel(),
        }
    }
    pub fn new(context: C, eloop: &mut EventLoop<Handler<C, M>>)
        -> Handler<C, M>
    {
        // TODO(tailhook) create default config from the ulimit data instead
        // of using real defaults
        Handler {
            slab: Slab::new(4096),
            context: context,
            channel: eloop.channel(),
        }
    }
}

pub fn create_handler<C, M>(slab: Slab<M>, context: C, channel: Sender<Notify>)
    -> Handler<C, M>
    where M: Machine<Context=C>
{
    Handler {
        slab: slab,
        context: context,
        channel: channel,
    }
}

impl<C, M> Handler<C, M>
    where M: Machine<Context=C>
{
    pub fn add_machine_with<F>(&mut self,
        eloop: &mut EventLoop<Self>, fun: F) -> Result<(), SpawnError<()>>
        where F: FnOnce(&mut Scope<C>) -> Result<M, Box<Error>>
    {
        let ref mut ctx = self.context;
        let ref mut chan = self.channel;
        let res = self.slab.insert_with(|token| {
            let ref mut scope = scope(token, ctx, chan, eloop);
            match fun(scope) {
                Ok(x) => x,
                Err(_) => {
                // TODO(tailhook) when Slab::insert_with_opt() lands, fix it
                    panic!("Unimplemented: Slab::insert_with_opt");
                }
            }
        });
        if res.is_some() {
            Ok(())
        } else {
            Err(NoSlabSpace(()))
        }
    }

}

fn machine_loop<C, M, F>(handler: &mut Handler<C, M>,
    eloop: &mut EventLoop<Handler<C, M>>, token: Token, fun: F)
    where M: Machine<Context=C>,
          F: FnOnce(M, &mut Scope<C>) -> Response<M, M::Seed>
{
    let mut creator = None;
    {
        let ref mut scope = scope(token, &mut handler.context,
            &mut handler.channel, eloop);
        handler.slab.replace_with(token, |m| {
            let res = fun(m, scope);
            creator = res.1;
            res.0
        }).ok();  // Spurious events are ok in mio
    }
    while let Some(new) = creator.take() {
        let mut new = Some(new);
        let res = handler.add_machine_with(eloop, |scope| {
            M::create(new.take().unwrap(), scope)
        });
        if let Err(err) = res {
            let err = if let Some(new) = new.take() {
                NoSlabSpace(new)
            } else if let UserError(e) = err {
                UserError(e)
            } else {
                unreachable!();
            };
            let ref mut scope = scope(token, &mut handler.context,
                &mut handler.channel, eloop);
            handler.slab.replace_with(token, |m| {
                m.spawn_error(scope, err)
            }).ok();
            break;
        } else {
            let ref mut scope = scope(token, &mut handler.context,
                &mut handler.channel, eloop);
            handler.slab.replace_with(token, |m| {
                let res = m.spawned(scope);
                creator = res.1;
                res.0
            }).ok();
        }
    }
}

impl<Ctx, M> mio::Handler for Handler<Ctx, M>
    where M: Machine<Context=Ctx>
{
    type Message = Notify;
    type Timeout = Timeo;
    fn ready<'x>(&mut self, eloop: &'x mut EventLoop<Self>,
        token: Token, events: EventSet)
    {
        machine_loop(self, eloop, token, |m, scope| { m.ready(events, scope) })
    }

    fn notify(&mut self, eloop: &mut EventLoop<Self>, msg: Notify) {
        match msg {
            Notify::Fsm(token) => {
                machine_loop(self, eloop, token,
                    |m, scope| { m.wakeup(scope) })
            }
        }
    }

    fn timeout(&mut self, eloop: &mut EventLoop<Self>, timeo: Timeo) {
        match timeo {
            Timeo::Fsm(token) => {
                machine_loop(self, eloop, token,
                    |m, scope| { m.timeout(scope) })
            }
        }
    }
}