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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of
// the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/

//! This module provides functionality to implement main program loop by waiting for system events
//! using `epoll` mechanism.
//!
//! Source of events is represented by `EventHandler`s, while whole loop by `Dispatcher`s.
//! `LocalDispatcher` can be used for single-thread programs when `EventHandler` do not implement
//! `Send` while `Dispatcher` is meant for multi-threaded programs. `DispatcherController` and
//! `LocalDispatcherController` are used to control `Dispatcher` and `LocalDispatcher`
//! respectively.

// -------------------------------------------------------------------------------------------------

use std::sync::{Arc, Mutex};
use std::sync::atomic::{AtomicBool, Ordering};
use std::os::unix::io::RawFd;
use std::collections::HashMap;
use std::convert::From;

use nix;
use nix::sys::epoll;

use system;

// -------------------------------------------------------------------------------------------------

/// `epoll_wait` waits infinitely when passed negative number as timeout.
const WAIT_INFINITELY: isize = -1;

// -------------------------------------------------------------------------------------------------

/// This module contains flags defining kind of event.
pub mod event_kind {
    use std::convert::From;
    use nix::sys::epoll;

    bitflags!(
        /// Type defining event kind.
        pub flags EventKind: u32 {
            /// Defines read event.
            const READ = 0x1,
            /// Defines write event.
            const WRITE = 0x2,
            /// Defines error or hangup. Can be omitted when adding source. It will be subscribed
            /// even if not specified, so it has to be always handled by `EventHandler`
            /// implementation.
            const HANGUP = 0x4
        }
    );

    impl From<epoll::EpollFlags> for EventKind {
        fn from(flags: epoll::EpollFlags) -> Self {
            let mut result = EventKind::empty();
            if flags.intersects(epoll::EPOLLIN) {
                result.insert(READ);
            }
            if flags.intersects(epoll::EPOLLOUT) {
                result.insert(WRITE);
            }
            if flags.intersects(epoll::EPOLLERR | epoll::EPOLLHUP) {
                result.insert(HANGUP);
            }
            result
        }
    }

    impl Into<epoll::EpollFlags> for EventKind {
        fn into(self) -> epoll::EpollFlags {
            let mut result = epoll::EpollFlags::empty();
            if self.intersects(READ) {
                result.insert(epoll::EPOLLIN);
            }
            if self.intersects(WRITE) {
                result.insert(epoll::EPOLLOUT);
            }
            if self.intersects(HANGUP) {
                // Only for completeness. This is not necessary to pass these flags to `epoll_ctl`.
                result.insert(epoll::EPOLLERR | epoll::EPOLLHUP);
            }
            result
        }
    }
}

pub use event_kind::EventKind;

// -------------------------------------------------------------------------------------------------

/// Id of `EventHandler` (generated by `Dispatcher`).
pub type EventHandlerId = u64;

// -------------------------------------------------------------------------------------------------

/// Trait for all structures supposed to be handlers for events registered in `Dispatcher`.
/// `EventHandler` is responsible for processing events. `EventHandler::process_event` will be
/// called when handlers file descriptor becomes readable in thread where `Dispatcher::start` was
/// called.
pub trait EventHandler {
    /// Returns file descriptor.
    fn get_fd(&self) -> RawFd;

    /// Callback function executed on event received.
    fn process_event(&mut self, event_kind: EventKind);

    /// This method is called by `Dispatcher` right after adding this `EventHandler`. Passed value
    /// is newly assigned ID of `EventHandler` which can be later used to delete it from
    /// `Dispatcher`.
    fn set_id(&mut self, _id: EventHandlerId) {}
}

// -------------------------------------------------------------------------------------------------

/// Helper structure for storing part of state of `Dispatcher` and `LocalDispatcher` which must be
/// guarded by mutex.
struct InnerState<E>
    where E: EventHandler + ?Sized
{
    epfd: RawFd,
    last_id: EventHandlerId,
    handlers: HashMap<EventHandlerId, Box<E>>,
}

impl<E> InnerState<E>
    where E: EventHandler + ?Sized
{
    /// Constructs new `InnerState`.
    pub fn new() -> Self {
        InnerState {
            epfd: epoll::epoll_create().expect("Failed to create epoll!"),
            last_id: 0,
            handlers: HashMap::new(),
        }
    }

    /// Adds `EventHandler`.
    ///
    /// Returns ID assigned to the added `EventHandler` which can be used to later delete it.
    pub fn add_source(&mut self, mut source: Box<E>, event_kind: EventKind) -> EventHandlerId {
        self.last_id += 1;
        let id = self.last_id;
        source.set_id(id);
        let fd = source.get_fd();
        self.handlers.insert(id, source);

        let mut event = epoll::EpollEvent::new(event_kind.into(), id);
        epoll::epoll_ctl(self.epfd, epoll::EpollOp::EpollCtlAdd, fd, &mut event)
            .expect("Failed to perform `epoll_ctl`");

        id
    }

    /// Deletes `EventHandler`.
    pub fn delete_source(&mut self, id: EventHandlerId) -> Option<Box<E>> {
        let result = self.handlers.remove(&id);
        if let Some(ref handler) = result {
            let mut event = epoll::EpollEvent::new(epoll::EpollFlags::empty(), 0);
            epoll::epoll_ctl(self.epfd, epoll::EpollOp::EpollCtlDel, handler.get_fd(), &mut event)
                .expect("Failed to delete epoll source");
        }
        result
    }

    /// Passes execution of event to handler.
    ///
    /// If file descriptor hung up the corresponding handler is removed.
    pub fn process(&mut self, id: EventHandlerId, event_kind: EventKind) {
        if let Some(handler) = self.handlers.get_mut(&id) {
            handler.process_event(event_kind);
        }
        if event_kind == event_kind::HANGUP {
            self.delete_source(id);
        }
    }
}

// -------------------------------------------------------------------------------------------------

/// Helper structure for storing state of `Dispatcher` and `LocalDispatcher`.
struct State<E>
    where E: EventHandler + ?Sized
{
    state: Mutex<InnerState<E>>,
    run: AtomicBool,
}

impl<E> State<E>
    where E: EventHandler + ?Sized
{
    pub fn new() -> Self {
        State {
            state: Mutex::new(InnerState::new()),
            run: AtomicBool::new(false),
        }
    }
}

// -------------------------------------------------------------------------------------------------

/// Helper method for waiting for events and then processing them.
fn do_wait_and_process<E>(state: &mut Arc<State<E>>, epfd: RawFd, timeout: isize)
    where E: EventHandler + ?Sized
{
    // We will process epoll events one by one.
    let mut events = [epoll::EpollEvent::new(epoll::EpollFlags::empty(), 0)];

    let wait_result = epoll::epoll_wait(epfd, &mut events[0..1], timeout);

    match wait_result {
        Ok(ready) => {
            if ready > 0 {
                let id = &events[0].data();
                let event_kind = EventKind::from(events[0].events());
                state.state.lock().unwrap().process(*id, event_kind);
            }
        }
        Err(err) => {
            if let nix::Error::Sys(errno) = err {
                if errno != nix::Errno::EINTR {
                    panic!("Error occurred during processing epoll events! ({:?})", err);
                }
            }
        }
    }
}

/// Helper method for precessing events in infinite loop.
fn do_run<E>(state: &mut Arc<State<E>>, epfd: RawFd)
    where E: EventHandler + ?Sized
{
    // Initial setup
    system::block_signals();
    state.run.store(true, Ordering::Relaxed);

    // Main loop
    loop {
        do_wait_and_process(state, epfd, WAIT_INFINITELY);

        if !state.run.load(Ordering::Relaxed) {
            break;
        }
    }
}


// -------------------------------------------------------------------------------------------------

/// Structure representing dispatcher of system events for use in one-threaded program.
pub struct LocalDispatcher {
    state: Arc<State<EventHandler>>,
}

impl LocalDispatcher {
    /// Constructor new `LocalDispatcher`.
    pub fn new() -> Self {
        LocalDispatcher { state: Arc::new(State::new()) }
    }

    /// Return local controller.
    ///
    /// This controller does not implement `Send`.
    pub fn get_controller(&self) -> LocalDispatcherController {
        LocalDispatcherController { state: self.state.clone() }
    }

    /// Waits for events and processes first one.
    pub fn wait_and_process(&mut self, timeout: Option<usize>) {
        let timeout = if let Some(t) = timeout {
            t as isize
        } else {
            WAIT_INFINITELY
        };
        let epfd = self.state.state.lock().unwrap().epfd;
        do_wait_and_process(&mut self.state, epfd, timeout);
    }

    /// Adds `EventHandler`.
    ///
    /// Returns ID assigned to the added `EventHandler` which can be used to later delete it.
    pub fn add_source(&mut self,
                      source: Box<EventHandler>,
                      event_kind: EventKind)
                      -> EventHandlerId {
        self.state.state.lock().unwrap().add_source(source, event_kind)
    }

    /// Deletes `EventHandler`.
    pub fn delete_source(&mut self, id: EventHandlerId) -> Option<Box<EventHandler>> {
        self.state.state.lock().unwrap().delete_source(id)
    }

    /// Starts processing events in current thread.
    pub fn run(&mut self) {
        let epfd = self.state.state.lock().unwrap().epfd;
        do_run(&mut self.state, epfd);
    }

    /// Stops processing of events.
    pub fn stop(&self) {
        self.state.run.store(false, Ordering::Relaxed)
    }
}

// -------------------------------------------------------------------------------------------------

/// Structure representing dispatcher of system events for use in multi-threaded program.
///
/// This version of `Dispatcher` does not accept `EventHandler`s which are not `Send`.
pub struct Dispatcher {
    state: Arc<State<EventHandler + Send>>,
}

impl Dispatcher {
    /// Constructs new `Dispatcher`.
    pub fn new() -> Self {
        Dispatcher { state: Arc::new(State::new()) }
    }

    /// Return controller.
    pub fn get_controller(&self) -> DispatcherController {
        DispatcherController { state: self.state.clone() }
    }

    /// Starts processing events in current thread.
    pub fn run(&mut self) {
        let epfd = self.state.state.lock().unwrap().epfd;
        do_run(&mut self.state, epfd);
    }
}

// -------------------------------------------------------------------------------------------------

/// Helps controlling `LocalDispatcher`.
///
/// Does not allow to add or delete handlers. In one-threaded program this would be unsafe.
#[derive(Clone)]
pub struct LocalDispatcherController {
    state: Arc<State<EventHandler>>,
}

impl LocalDispatcherController {
    /// Stops processing events.
    pub fn stop(&self) {
        self.state.run.store(false, Ordering::Relaxed)
    }
}

// -------------------------------------------------------------------------------------------------

/// Helps controlling `Dispatcher`.
#[derive(Clone)]
pub struct DispatcherController {
    state: Arc<State<EventHandler + Send>>,
}

impl DispatcherController {
    /// Adds `EventHandler`.
    ///
    /// Returns ID assigned to the added `EventHandler` which can be used to later delete it.
    pub fn add_source(&mut self,
                      source: Box<EventHandler + Send>,
                      event_kind: EventKind)
                      -> EventHandlerId {
        self.state.state.lock().unwrap().add_source(source, event_kind)
    }

    /// Deletes `EventHandler`.
    pub fn delete_source(&mut self, id: EventHandlerId) -> Option<Box<EventHandler + Send>> {
        self.state.state.lock().unwrap().delete_source(id)
    }

    /// Stops processing events.
    pub fn stop(&self) {
        self.state.run.store(false, Ordering::Relaxed)
    }
}

// -------------------------------------------------------------------------------------------------