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
extern crate syscall;

use std::collections::BTreeMap;
use std::fs::File;
use std::io::{Read, Error, Result};
use std::os::unix::io::RawFd;

pub struct EventQueue<R> {
    /// The file to read events from
    file: File,
    /// A map of registered file descriptors to their handler callbacks
    callbacks: BTreeMap<RawFd, Box<FnMut(usize) -> Result<Option<R>>>>
}

impl<R> EventQueue<R> {
    /// Create a new event queue
    pub fn new() -> Result<EventQueue<R>> {
        Ok(EventQueue {
            file: File::open("event:")?,
            callbacks: BTreeMap::new()
        })
    }

    /// Add a file to the event queue, calling a callback when an event occurs
    ///
    /// The callback is given a mutable reference to the file and the event data
    /// (typically the length of data available for read)
    ///
    /// The callback returns Ok(None) if it wishes to continue the event loop,
    /// or Ok(Some(R)) to break the event loop and return the value.
    /// Err can be used to allow the callback to return an I/O error, and break the
    /// event loop
    pub fn add<F: FnMut(usize) -> Result<Option<R>> + 'static>(&mut self, fd: RawFd, callback: F) -> Result<()> {
        syscall::fevent(fd as usize, syscall::EVENT_READ).map_err(|x| Error::from_raw_os_error(x.errno))?;

        self.callbacks.insert(fd, Box::new(callback));

        Ok(())
    }

    /// Remove a file from the event queue, returning its callback if found
    pub fn remove(&mut self, fd: RawFd) -> Result<Option<Box<FnMut(usize) -> Result<Option<R>>>>> {
        if let Some(callback) = self.callbacks.remove(&fd) {
            syscall::fevent(fd as usize, 0).map_err(|x| Error::from_raw_os_error(x.errno))?;

            Ok(Some(callback))
        } else {
            Ok(None)
        }
    }

    /// Send an event to a descriptor callback
    pub fn trigger(&mut self, fd: RawFd, count: usize) -> Result<Option<R>> {
        if let Some(callback) = self.callbacks.get_mut(&fd) {
            callback(count)
        } else {
            Ok(None)
        }
    }

    /// Send an event to all descriptor callbacks, useful for cleaning out buffers after init
    pub fn trigger_all(&mut self, count: usize) -> Result<Vec<R>> {
        let mut rets = Vec::new();
        for (_fd, callback) in self.callbacks.iter_mut() {
            if let Some(ret) = callback(count)? {
                rets.push(ret);
            }
        }
        Ok(rets)
    }

    /// Process the event queue until a callback returns Some(R)
    pub fn run(&mut self) -> Result<R> {
        loop {
            let mut event = syscall::Event::default();
            if self.file.read(&mut event)? > 0 {
                if let Some(ret) = self.trigger(event.id as RawFd, event.data)? {
                    return Ok(ret);
                }
            }
        }
    }
}