<commit_before>// THIS IS THE UGLIEST THIGN EVER
use std::fs;
use std::fs::OpenOptions;
use std::io::prelude::*;

/// TODO: Result when error and this documentation
pub fn write_html_from_json(directory: &str, subdirectory: &str) {
    // The full path: dir\\subdir
    let mut full_path = directory.to_string();
    full_path.push_str("\\");
    full_path.push_str(subdirectory);
    // Create index file
    let mut file = OpenOptions::new()
        .read(true)
        .write(true)
        .create(true)
        .open("index.html")
        .unwrap();
    // Start of indexfile html
    let _ = writeln!(file, "");
    // Get all json files
    // dir\\subdir\\<file>.json
    for entry in fs::read_dir(full_path.clone()).unwrap() {
        // Path to the file
        let dir = entry.unwrap();
        // Removes the subdirectory
        // dir\\<file>.json
        let path_parent = dir
            .path()
            .into_os_string()
            .into_string()
            .unwrap()
            .replace(&format!("\\{}", subdirectory), "");
        // dir\\<file>.html
        let path_parent_html = path_parent.clone().replace("json", "html");
        // Create html file <dir>\\<file>.html
        let mut file_parent_html = OpenOptions::new()
        .read(true)
        .write(true)
        .create(true)
        .open(path_parent_html.clone())
        .unwrap();
        // Write to dir\\<file>.html
        let _ = file_parent_html.write_all(

// LARGE HTML AHEAD
"<!DOCTYPE html>

        <meta charset=\"utf-8\"/>
        <link rel=\"stylesheet\" href=\"css/jquery.dynatable.css\">

        back

            Artist
            Title
            Count

        back

"
// END OF MORE HTML

        .as_bytes());
        // link to dir\\<file>.html
        let _ = writeln!(file, "{0}
", path_parent_html);
    }
    let _ = writeln!(file, "");
}
<commit_msg>Presort tables<commit_after>// THIS IS THE UGLIEST THIGN EVER
use std::fs;
use std::fs::OpenOptions;
use std::io::prelude::*;

/// TODO: Result when error and this documentation
pub fn write_html_from_json(directory: &str, subdirectory: &str) {
    // The full path: dir\\subdir
    let mut full_path = directory.to_string();
    full_path.push_str("\\");
    full_path.push_str(subdirectory);
    // Create index file
    let mut file = OpenOptions::new()
        .read(true)
        .write(true)
        .create(true)
        .open("index.html")
        .unwrap();
    // Start of indexfile html
    let _ = writeln!(file, "");
    // Get all json files
    // dir\\subdir\\<file>.json
    for entry in fs::read_dir(full_path.clone()).unwrap() {
        // Path to the file
        let dir = entry.unwrap();
        // Removes the subdirectory
        // dir\\<file>.json
        let path_parent = dir
            .path()
            .into_os_string()
            .into_string()
            .unwrap()
            .replace(&format!("\\{}", subdirectory), "");
        // dir\\<file>.html
        let path_parent_html = path_parent.clone().replace("json", "html");
        // Create html file <dir>\\<file>.html
        let mut file_parent_html = OpenOptions::new()
        .read(true)
        .write(true)
        .create(true)
        .open(path_parent_html.clone())
        .unwrap();
        // Write to dir\\<file>.html
        let _ = file_parent_html.write_all(

// LARGE HTML AHEAD
"<!DOCTYPE html>

        <meta charset=\"utf-8\"/>
        <link rel=\"stylesheet\" href=\"css/jquery.dynatable.css\">

        back

            Artist
            Title
            Count

        back

"
// END OF MORE HTML

        .as_bytes());
        // link to dir\\<file>.html
        let _ = writeln!(file, "{0}
", path_parent_html);
    }
    let _ = writeln!(file, "");
}
<|endoftext|>


<commit_before>use crate::os::unix::prelude::*;

use crate::ffi::{OsString, OsStr, CString, CStr};
use crate::fmt;
use crate::io;
use crate::ptr;
use crate::sys::fd::FileDesc;
use crate::sys::fs::{File, OpenOptions};
use crate::sys::pipe::{self, AnonPipe};
use crate::sys_common::process::{CommandEnv, DefaultEnvKey};
use crate::collections::BTreeMap;

use libc::{c_int, gid_t, uid_t, c_char, EXIT_SUCCESS, EXIT_FAILURE};

////////////////////////////////////////////////////////////////////////////////
// Command
////////////////////////////////////////////////////////////////////////////////

pub struct Command {
    // Currently we try hard to ensure that the call to `.exec()` doesn't
    // actually allocate any memory. While many platforms try to ensure that
    // memory allocation works after a fork in a multithreaded process, it's
    // been observed to be buggy and somewhat unreliable, so we do our best to
    // just not do it at all!
    //
    // Along those lines, the `argv` and `envp` raw pointers here are exactly
    // what's gonna get passed to `execvp`. The `argv` array starts with the
    // `program` and ends with a NULL, and the `envp` pointer, if present, is
    // also null-terminated.
    //
    // Right now we don't support removing arguments, so there's no much fancy
    // support there, but we support adding and removing environment variables,
    // so a side table is used to track where in the `envp` array each key is
    // located. Whenever we add a key we update it in place if it's already
    // present, and whenever we remove a key we update the locations of all
    // other keys.
    program: CString,
    args: Vec<CString>,
    argv: Argv,
    env: CommandEnv<DefaultEnvKey>,

    cwd: Option<CString>,
    uid: Option<uid_t>,
    gid: Option<gid_t>,
    saw_nul: bool,
    closures: Vec<Box<dyn FnMut() -> io::Result<()> + Send + Sync>>,
    stdin: Option<Stdio>,
    stdout: Option<Stdio>,
    stderr: Option<Stdio>,
}

// Create a new type for argv, so that we can make it `Send`
struct Argv(Vec<*const c_char>);

// It is safe to make Argv Send, because it contains pointers to memory owned by `Command.args`
unsafe impl Send for Argv {}

// passed back to std::process with the pipes connected to the child, if any
// were requested
pub struct StdioPipes {
    pub stdin: Option<AnonPipe>,
    pub stdout: Option<AnonPipe>,
    pub stderr: Option<AnonPipe>,
}

// passed to do_exec() with configuration of what the child stdio should look
// like
pub struct ChildPipes {
    pub stdin: ChildStdio,
    pub stdout: ChildStdio,
    pub stderr: ChildStdio,
}

pub enum ChildStdio {
    Inherit,
    Explicit(c_int),
    Owned(FileDesc),
}

pub enum Stdio {
    Inherit,
    Null,
    MakePipe,
    Fd(FileDesc),
}

impl Command {
    pub fn new(program: &OsStr) -> Command {
        let mut saw_nul = false;
        let program = os2c(program, &mut saw_nul);
        Command {
            argv: Argv(vec![program.as_ptr(), ptr::null()]),
            program,
            args: Vec::new(),
            env: Default::default(),
            cwd: None,
            uid: None,
            gid: None,
            saw_nul,
            closures: Vec::new(),
            stdin: None,
            stdout: None,
            stderr: None,
        }
    }

    pub fn arg(&mut self, arg: &OsStr) {
        // Overwrite the trailing NULL pointer in `argv` and then add a new null
        // pointer.
        let arg = os2c(arg, &mut self.saw_nul);
        self.argv.0[self.args.len() + 1] = arg.as_ptr();
        self.argv.0.push(ptr::null());

        // Also make sure we keep track of the owned value to schedule a
        // destructor for this memory.
        self.args.push(arg);
    }

    pub fn cwd(&mut self, dir: &OsStr) {
        self.cwd = Some(os2c(dir, &mut self.saw_nul));
    }
    pub fn uid(&mut self, id: uid_t) {
        self.uid = Some(id);
    }
    pub fn gid(&mut self, id: gid_t) {
        self.gid = Some(id);
    }

    pub fn saw_nul(&self) -> bool {
        self.saw_nul
    }
    pub fn get_argv(&self) -> &Vec<*const c_char> {
        &self.argv.0
    }

    #[allow(dead_code)]
    pub fn get_cwd(&self) -> &Option<CString> {
        &self.cwd
    }
    #[allow(dead_code)]
    pub fn get_uid(&self) -> Option<uid_t> {
        self.uid
    }
    #[allow(dead_code)]
    pub fn get_gid(&self) -> Option<gid_t> {
        self.gid
    }

    pub fn get_closures(&mut self) -> &mut Vec<Box<dyn FnMut() -> io::Result<()> + Send + Sync>> {
        &mut self.closures
    }

    pub unsafe fn pre_exec(
        &mut self,
        _f: Box<dyn FnMut() -> io::Result<()> + Send + Sync>,
    ) {
        // Fork() is not supported in vxWorks so no way to run the closure in the new procecss.
        unimplemented!();;
    }

    pub fn stdin(&mut self, stdin: Stdio) {
        self.stdin = Some(stdin);
    }

    pub fn stdout(&mut self, stdout: Stdio) {
        self.stdout = Some(stdout);
    }

    pub fn stderr(&mut self, stderr: Stdio) {
        self.stderr = Some(stderr);
    }

    pub fn env_mut(&mut self) -> &mut CommandEnv<DefaultEnvKey> {
        &mut self.env
    }

    pub fn capture_env(&mut self) -> Option<CStringArray> {
        let maybe_env = self.env.capture_if_changed();
        maybe_env.map(|env| construct_envp(env, &mut self.saw_nul))
    }
    #[allow(dead_code)]
    pub fn env_saw_path(&self) -> bool {
        self.env.have_changed_path()
    }

    pub fn setup_io(&self, default: Stdio, needs_stdin: bool)
                -> io::Result<(StdioPipes, ChildPipes)> {
        let null = Stdio::Null;
        let default_stdin = if needs_stdin {&default} else {&null};
        let stdin = self.stdin.as_ref().unwrap_or(default_stdin);
        let stdout = self.stdout.as_ref().unwrap_or(&default);
        let stderr = self.stderr.as_ref().unwrap_or(&default);
        let (their_stdin, our_stdin) = stdin.to_child_stdio(true)?;
        let (their_stdout, our_stdout) = stdout.to_child_stdio(false)?;
        let (their_stderr, our_stderr) = stderr.to_child_stdio(false)?;
        let ours = StdioPipes {
            stdin: our_stdin,
            stdout: our_stdout,
            stderr: our_stderr,
        };
        let theirs = ChildPipes {
            stdin: their_stdin,
            stdout: their_stdout,
            stderr: their_stderr,
        };
        Ok((ours, theirs))
    }
}

fn os2c(s: &OsStr, saw_nul: &mut bool) -> CString {
    CString::new(s.as_bytes()).unwrap_or_else(|_e| {
        *saw_nul = true;
        CString::new("<string-with-nul>").unwrap()
    })
}

// Helper type to manage ownership of the strings within a C-style array.
pub struct CStringArray {
    items: Vec<CString>,
    ptrs: Vec<*const c_char>
}

impl CStringArray {
    pub fn with_capacity(capacity: usize) -> Self {
        let mut result = CStringArray {
            items: Vec::with_capacity(capacity),
            ptrs: Vec::with_capacity(capacity+1)
        };
        result.ptrs.push(ptr::null());
        result
    }
    pub fn push(&mut self, item: CString) {
        let l = self.ptrs.len();
        self.ptrs[l-1] = item.as_ptr();
        self.ptrs.push(ptr::null());
        self.items.push(item);
    }
    pub fn as_ptr(&self) -> *const *const c_char {
        self.ptrs.as_ptr()
    }
}

fn construct_envp(env: BTreeMap<DefaultEnvKey, OsString>, saw_nul: &mut bool) -> CStringArray {
    let mut result = CStringArray::with_capacity(env.len());
    for (k, v) in env {
        let mut k: OsString = k.into();

        // Reserve additional space for '=' and null terminator
        k.reserve_exact(v.len() + 2);
        k.push("=");
        k.push(&v);

        // Add the new entry into the array
        if let Ok(item) = CString::new(k.into_vec()) {
            result.push(item);
        } else {
            *saw_nul = true;
        }
    }

    result
}

impl Stdio {
    pub fn to_child_stdio(&self, readable: bool)
                      -> io::Result<(ChildStdio, Option<AnonPipe>)> {
        match *self {
            Stdio::Inherit => {
                Ok((ChildStdio::Inherit, None))
            },

            // Make sure that the source descriptors are not an stdio
            // descriptor, otherwise the order which we set the child's
            // descriptors may blow away a descriptor which we are hoping to
            // save. For example, suppose we want the child's stderr to be the
            // parent's stdout, and the child's stdout to be the parent's
            // stderr. No matter which we dup first, the second will get
            // overwritten prematurely.
            Stdio::Fd(ref fd) => {
                if fd.raw() >= 0 && fd.raw() <= libc::STDERR_FILENO {
                    Ok((ChildStdio::Owned(fd.duplicate()?), None))
                } else {
                    Ok((ChildStdio::Explicit(fd.raw()), None))
                }
            }

            Stdio::MakePipe => {
                let (reader, writer) = pipe::anon_pipe()?;
                let (ours, theirs) = if readable {
                    (writer, reader)
                } else {
                    (reader, writer)
                };
                Ok((ChildStdio::Owned(theirs.into_fd()), Some(ours)))
            }

            Stdio::Null => {
                let mut opts = OpenOptions::new();
                opts.read(readable);
                opts.write(!readable);
                let path = unsafe {
                    CStr::from_ptr("/null\0".as_ptr() as *const _)
                };
                let fd = File::open_c(&path, &opts)?;
                Ok((ChildStdio::Owned(fd.into_fd()), None))
            }
        }
    }
}

impl From<AnonPipe> for Stdio {
    fn from(pipe: AnonPipe) -> Stdio {
        Stdio::Fd(pipe.into_fd())
    }
}

impl From<File> for Stdio {
    fn from(file: File) -> Stdio {
        Stdio::Fd(file.into_fd())
    }
}

impl ChildStdio {
    pub fn fd(&self) -> Option<c_int> {
        match *self {
            ChildStdio::Inherit => None,
            ChildStdio::Explicit(fd) => Some(fd),
            ChildStdio::Owned(ref fd) => Some(fd.raw()),
        }
    }
}

impl fmt::Debug for Command {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{:?}", self.program)?;
        for arg in &self.args {
            write!(f, " {:?}", arg)?;
        }
        Ok(())
    }
}

/// Unix exit statuses
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
pub struct ExitStatus(c_int);

impl ExitStatus {
    pub fn new(status: c_int) -> ExitStatus {
        ExitStatus(status)
    }

    fn exited(&self) -> bool {
        /*unsafe*/ { libc::WIFEXITED(self.0) }
    }

    pub fn success(&self) -> bool {
        self.code() == Some(0)
    }

    pub fn code(&self) -> Option<i32> {
        if self.exited() {
            Some(/*unsafe*/ { libc::WEXITSTATUS(self.0) })
        } else {
            None
        }
    }

    pub fn signal(&self) -> Option<i32> {
        if !self.exited() {
            Some(/*unsafe*/ { libc::WTERMSIG(self.0) })
        } else {
            None
        }
    }
}

impl From<c_int> for ExitStatus {
    fn from(a: c_int) -> ExitStatus {
        ExitStatus(a)
    }
}

impl fmt::Display for ExitStatus {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if let Some(code) = self.code() {
            write!(f, "exit code: {}", code)
        } else {
            let signal = self.signal().unwrap();
            write!(f, "signal: {}", signal)
        }
    }
}

#[derive(PartialEq, Eq, Clone, Copy, Debug)]
pub struct ExitCode(u8);

impl ExitCode {
    pub const SUCCESS: ExitCode = ExitCode(EXIT_SUCCESS as _);
    pub const FAILURE: ExitCode = ExitCode(EXIT_FAILURE as _);

    #[inline]
    pub fn as_i32(&self) -> i32 {
        self.0 as i32
    }
}

#[cfg(all(test, not(target_os = "emscripten")))]
mod tests {
    use super::*;

    use crate::ffi::OsStr;
    use crate::mem;
    use crate::ptr;
    use crate::sys::cvt;

    macro_rules! t {
        ($e:expr) => {
            match $e {
                Ok(t) => t,
                Err(e) => panic!("received error for `{}`: {}", stringify!($e), e),
            }
        }
    }

    extern {
        fn sigemptyset(set: *mut libc::sigset_t) -> libc::c_int;
        fn sigaddset(set: *mut libc::sigset_t, signum: libc::c_int) -> libc::c_int;
    }

    #[test]
    fn test_process_mask() {
        unsafe {
            // Test to make sure that a signal mask does not get inherited.
            let mut cmd = Command::new(OsStr::new("cat"));

            let mut set: libc::sigset_t = mem::uninitialized();
            let mut old_set: libc::sigset_t = mem::uninitialized();
            t!(cvt(sigemptyset(&mut set)));
            t!(cvt(sigaddset(&mut set, libc::SIGINT)));
            t!(cvt(libc::pthread_sigmask(libc::SIG_SETMASK, &set, &mut old_set)));

            cmd.stdin(Stdio::MakePipe);
            cmd.stdout(Stdio::MakePipe);

            let (mut cat, mut pipes) = t!(cmd.spawn(Stdio::Null, true));
            let stdin_write = pipes.stdin.take().unwrap();
            let stdout_read = pipes.stdout.take().unwrap();

            t!(cvt(libc::pthread_sigmask(libc::SIG_SETMASK, &old_set,
                                         ptr::null_mut())));

            t!(cvt(libc::kill(cat.id() as libc::pid_t, libc::SIGINT)));
            // We need to wait until SIGINT is definitely delivered. The
            // easiest way is to write something to cat, and try to read it
            // back: if SIGINT is unmasked, it'll get delivered when cat is
            // next scheduled.
            let _ = stdin_write.write(b"Hello");
            drop(stdin_write);

            // Either EOF or failure (EPIPE) is okay.
            let mut buf = [0; 5];
            if let Ok(ret) = stdout_read.read(&mut buf) {
                assert_eq!(ret, 0);
            }

            t!(cat.wait());
        }
    }
}
<commit_msg>Rollup merge of #63206 - BaoshanPang:master, r=alexcrichton<commit_after>use crate::os::unix::prelude::*;

use crate::ffi::{OsString, OsStr, CString, CStr};
use crate::fmt;
use crate::io;
use crate::ptr;
use crate::sys::fd::FileDesc;
use crate::sys::fs::{File, OpenOptions};
use crate::sys::pipe::{self, AnonPipe};
use crate::sys_common::process::{CommandEnv, DefaultEnvKey};
use crate::collections::BTreeMap;

use libc::{c_int, gid_t, uid_t, c_char, EXIT_SUCCESS, EXIT_FAILURE};

////////////////////////////////////////////////////////////////////////////////
// Command
////////////////////////////////////////////////////////////////////////////////

pub struct Command {
    // Currently we try hard to ensure that the call to `.exec()` doesn't
    // actually allocate any memory. While many platforms try to ensure that
    // memory allocation works after a fork in a multithreaded process, it's
    // been observed to be buggy and somewhat unreliable, so we do our best to
    // just not do it at all!
    //
    // Along those lines, the `argv` and `envp` raw pointers here are exactly
    // what's gonna get passed to `execvp`. The `argv` array starts with the
    // `program` and ends with a NULL, and the `envp` pointer, if present, is
    // also null-terminated.
    //
    // Right now we don't support removing arguments, so there's no much fancy
    // support there, but we support adding and removing environment variables,
    // so a side table is used to track where in the `envp` array each key is
    // located. Whenever we add a key we update it in place if it's already
    // present, and whenever we remove a key we update the locations of all
    // other keys.
    program: CString,
    args: Vec<CString>,
    argv: Argv,
    env: CommandEnv<DefaultEnvKey>,

    cwd: Option<CString>,
    uid: Option<uid_t>,
    gid: Option<gid_t>,
    saw_nul: bool,
    closures: Vec<Box<dyn FnMut() -> io::Result<()> + Send + Sync>>,
    stdin: Option<Stdio>,
    stdout: Option<Stdio>,
    stderr: Option<Stdio>,
}

// Create a new type for argv, so that we can make it `Send`
struct Argv(Vec<*const c_char>);

// It is safe to make Argv Send, because it contains pointers to memory owned by `Command.args`
unsafe impl Send for Argv {}

// passed back to std::process with the pipes connected to the child, if any
// were requested
pub struct StdioPipes {
    pub stdin: Option<AnonPipe>,
    pub stdout: Option<AnonPipe>,
    pub stderr: Option<AnonPipe>,
}

// passed to do_exec() with configuration of what the child stdio should look
// like
pub struct ChildPipes {
    pub stdin: ChildStdio,
    pub stdout: ChildStdio,
    pub stderr: ChildStdio,
}

pub enum ChildStdio {
    Inherit,
    Explicit(c_int),
    Owned(FileDesc),
}

pub enum Stdio {
    Inherit,
    Null,
    MakePipe,
    Fd(FileDesc),
}

impl Command {
    pub fn new(program: &OsStr) -> Command {
        let mut saw_nul = false;
        let program = os2c(program, &mut saw_nul);
        Command {
            argv: Argv(vec![program.as_ptr(), ptr::null()]),
            program,
            args: Vec::new(),
            env: Default::default(),
            cwd: None,
            uid: None,
            gid: None,
            saw_nul,
            closures: Vec::new(),
            stdin: None,
            stdout: None,
            stderr: None,
        }
    }

    pub fn arg(&mut self, arg: &OsStr) {
        // Overwrite the trailing NULL pointer in `argv` and then add a new null
        // pointer.
        let arg = os2c(arg, &mut self.saw_nul);
        self.argv.0[self.args.len() + 1] = arg.as_ptr();
        self.argv.0.push(ptr::null());

        // Also make sure we keep track of the owned value to schedule a
        // destructor for this memory.
        self.args.push(arg);
    }

    pub fn cwd(&mut self, dir: &OsStr) {
        self.cwd = Some(os2c(dir, &mut self.saw_nul));
    }
    pub fn uid(&mut self, id: uid_t) {
        self.uid = Some(id);
    }
    pub fn gid(&mut self, id: gid_t) {
        self.gid = Some(id);
    }

    pub fn saw_nul(&self) -> bool {
        self.saw_nul
    }
    pub fn get_argv(&self) -> &Vec<*const c_char> {
        &self.argv.0
    }

    #[allow(dead_code)]
    pub fn get_cwd(&self) -> &Option<CString> {
        &self.cwd
    }
    #[allow(dead_code)]
    pub fn get_uid(&self) -> Option<uid_t> {
        self.uid
    }
    #[allow(dead_code)]
    pub fn get_gid(&self) -> Option<gid_t> {
        self.gid
    }

    pub fn get_closures(&mut self) -> &mut Vec<Box<dyn FnMut() -> io::Result<()> + Send + Sync>> {
        &mut self.closures
    }

    pub unsafe fn pre_exec(
        &mut self,
        _f: Box<dyn FnMut() -> io::Result<()> + Send + Sync>,
    ) {
        // Fork() is not supported in vxWorks so no way to run the closure in the new procecss.
        unimplemented!();;
    }

    pub fn stdin(&mut self, stdin: Stdio) {
        self.stdin = Some(stdin);
    }

    pub fn stdout(&mut self, stdout: Stdio) {
        self.stdout = Some(stdout);
    }

    pub fn stderr(&mut self, stderr: Stdio) {
        self.stderr = Some(stderr);
    }

    pub fn env_mut(&mut self) -> &mut CommandEnv<DefaultEnvKey> {
        &mut self.env
    }

    pub fn capture_env(&mut self) -> Option<CStringArray> {
        let maybe_env = self.env.capture_if_changed();
        maybe_env.map(|env| construct_envp(env, &mut self.saw_nul))
    }
    #[allow(dead_code)]
    pub fn env_saw_path(&self) -> bool {
        self.env.have_changed_path()
    }

    pub fn setup_io(&self, default: Stdio, needs_stdin: bool)
                -> io::Result<(StdioPipes, ChildPipes)> {
        let null = Stdio::Null;
        let default_stdin = if needs_stdin {&default} else {&null};
        let stdin = self.stdin.as_ref().unwrap_or(default_stdin);
        let stdout = self.stdout.as_ref().unwrap_or(&default);
        let stderr = self.stderr.as_ref().unwrap_or(&default);
        let (their_stdin, our_stdin) = stdin.to_child_stdio(true)?;
        let (their_stdout, our_stdout) = stdout.to_child_stdio(false)?;
        let (their_stderr, our_stderr) = stderr.to_child_stdio(false)?;
        let ours = StdioPipes {
            stdin: our_stdin,
            stdout: our_stdout,
            stderr: our_stderr,
        };
        let theirs = ChildPipes {
            stdin: their_stdin,
            stdout: their_stdout,
            stderr: their_stderr,
        };
        Ok((ours, theirs))
    }
}

fn os2c(s: &OsStr, saw_nul: &mut bool) -> CString {
    CString::new(s.as_bytes()).unwrap_or_else(|_e| {
        *saw_nul = true;
        CString::new("<string-with-nul>").unwrap()
    })
}

// Helper type to manage ownership of the strings within a C-style array.
pub struct CStringArray {
    items: Vec<CString>,
    ptrs: Vec<*const c_char>
}

impl CStringArray {
    pub fn with_capacity(capacity: usize) -> Self {
        let mut result = CStringArray {
            items: Vec::with_capacity(capacity),
            ptrs: Vec::with_capacity(capacity+1)
        };
        result.ptrs.push(ptr::null());
        result
    }
    pub fn push(&mut self, item: CString) {
        let l = self.ptrs.len();
        self.ptrs[l-1] = item.as_ptr();
        self.ptrs.push(ptr::null());
        self.items.push(item);
    }
    pub fn as_ptr(&self) -> *const *const c_char {
        self.ptrs.as_ptr()
    }
}

fn construct_envp(env: BTreeMap<DefaultEnvKey, OsString>, saw_nul: &mut bool) -> CStringArray {
    let mut result = CStringArray::with_capacity(env.len());
    for (k, v) in env {
        let mut k: OsString = k.into();

        // Reserve additional space for '=' and null terminator
        k.reserve_exact(v.len() + 2);
        k.push("=");
        k.push(&v);

        // Add the new entry into the array
        if let Ok(item) = CString::new(k.into_vec()) {
            result.push(item);
        } else {
            *saw_nul = true;
        }
    }

    result
}

impl Stdio {
    pub fn to_child_stdio(&self, readable: bool)
                      -> io::Result<(ChildStdio, Option<AnonPipe>)> {
        match *self {
            Stdio::Inherit => {
                Ok((ChildStdio::Inherit, None))
            },

            // Make sure that the source descriptors are not an stdio
            // descriptor, otherwise the order which we set the child's
            // descriptors may blow away a descriptor which we are hoping to
            // save. For example, suppose we want the child's stderr to be the
            // parent's stdout, and the child's stdout to be the parent's
            // stderr. No matter which we dup first, the second will get
            // overwritten prematurely.
            Stdio::Fd(ref fd) => {
                if fd.raw() >= 0 && fd.raw() <= libc::STDERR_FILENO {
                    Ok((ChildStdio::Owned(fd.duplicate()?), None))
                } else {
                    Ok((ChildStdio::Explicit(fd.raw()), None))
                }
            }

            Stdio::MakePipe => {
                let (reader, writer) = pipe::anon_pipe()?;
                let (ours, theirs) = if readable {
                    (writer, reader)
                } else {
                    (reader, writer)
                };
                Ok((ChildStdio::Owned(theirs.into_fd()), Some(ours)))
            }

            Stdio::Null => {
                let mut opts = OpenOptions::new();
                opts.read(readable);
                opts.write(!readable);
                let path = unsafe {
                    CStr::from_ptr("/null\0".as_ptr() as *const _)
                };
                let fd = File::open_c(&path, &opts)?;
                Ok((ChildStdio::Owned(fd.into_fd()), None))
            }
        }
    }
}

impl From<AnonPipe> for Stdio {
    fn from(pipe: AnonPipe) -> Stdio {
        Stdio::Fd(pipe.into_fd())
    }
}

impl From<File> for Stdio {
    fn from(file: File) -> Stdio {
        Stdio::Fd(file.into_fd())
    }
}

impl ChildStdio {
    pub fn fd(&self) -> Option<c_int> {
        match *self {
            ChildStdio::Inherit => None,
            ChildStdio::Explicit(fd) => Some(fd),
            ChildStdio::Owned(ref fd) => Some(fd.raw()),
        }
    }
}

impl fmt::Debug for Command {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{:?}", self.program)?;
        for arg in &self.args {
            write!(f, " {:?}", arg)?;
        }
        Ok(())
    }
}

/// Unix exit statuses
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
pub struct ExitStatus(c_int);

impl ExitStatus {
    pub fn new(status: c_int) -> ExitStatus {
        ExitStatus(status)
    }

    fn exited(&self) -> bool {
        /*unsafe*/ { libc::WIFEXITED(self.0) }
    }

    pub fn success(&self) -> bool {
        self.code() == Some(0)
    }

    pub fn code(&self) -> Option<i32> {
        if self.exited() {
            Some(/*unsafe*/ { libc::WEXITSTATUS(self.0) })
        } else {
            None
        }
    }

    pub fn signal(&self) -> Option<i32> {
        if !self.exited() {
            Some(/*unsafe*/ { libc::WTERMSIG(self.0) })
        } else {
            None
        }
    }
}

impl From<c_int> for ExitStatus {
    fn from(a: c_int) -> ExitStatus {
        ExitStatus(a)
    }
}

impl fmt::Display for ExitStatus {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if let Some(code) = self.code() {
            write!(f, "exit code: {}", code)
        } else {
            let signal = self.signal().unwrap();
            write!(f, "signal: {}", signal)
        }
    }
}

#[derive(PartialEq, Eq, Clone, Copy, Debug)]
pub struct ExitCode(u8);

impl ExitCode {
    pub const SUCCESS: ExitCode = ExitCode(EXIT_SUCCESS as _);
    pub const FAILURE: ExitCode = ExitCode(EXIT_FAILURE as _);

    #[inline]
    pub fn as_i32(&self) -> i32 {
        self.0 as i32
    }
}
<|endoftext|>


<commit_before>use beacon_chain::BeaconChain;
use iron::{status::Status, Handler, IronResult, Request, Response};
use prometheus::{IntCounter, Encoder, Opts, Registry, TextEncoder};
use std::sync::Arc;
use types::EthSpec;

pub struct PrometheusHandler<T, U, F, E: EthSpec> {
    pub beacon_chain: Arc<BeaconChain<T, U, F, E>>,
}

impl<T, U, F, E> PrometheusHandler<T, U, F, E> where E: EthSpec {}

impl<T, U, F, E> Handler for PrometheusHandler<T, U, F, E>
where
    E: EthSpec + 'static,
    U: slot_clock::SlotClock + Send + Sync + 'static,
    T: Send + Sync + 'static,
    F: Send + Sync + 'static,
{
    fn handle(&self, _: &mut Request) -> IronResult<Response> {
        // Create a Counter.
        let counter_opts = Opts::new("present_slot", "direct_slot_clock_reading");
        let counter = IntCounter::with_opts(counter_opts).unwrap();

        // Create a Registry and register Counter.
        let r = Registry::new();
        r.register(Box::new(counter.clone())).unwrap();

        if let Ok(Some(slot)) = self.beacon_chain.slot_clock.present_slot() {
            counter.inc_by(slot.as_u64() as i64);
        }

        // Gather the metrics.
        let mut buffer = vec![];
        let encoder = TextEncoder::new();
        let metric_families = r.gather();
        encoder.encode(&metric_families, &mut buffer).unwrap();

        let prom_string = String::from_utf8(buffer).unwrap();

        Ok(Response::with((Status::Ok, prom_string)))
    }
}
<commit_msg>Add slot to prometheus endpoint<commit_after>use beacon_chain::BeaconChain;
use iron::{status::Status, Handler, IronResult, Request, Response};
use prometheus::{Encoder, IntCounter, Opts, Registry, TextEncoder};
use std::sync::Arc;
use types::{EthSpec, Slot};

pub struct PrometheusHandler<T, U, F, E: EthSpec> {
    pub beacon_chain: Arc<BeaconChain<T, U, F, E>>,
}

impl<T, U, F, E> PrometheusHandler<T, U, F, E> where E: EthSpec {}

impl<T, U, F, E> Handler for PrometheusHandler<T, U, F, E>
where
    E: EthSpec + 'static,
    U: slot_clock::SlotClock + Send + Sync + 'static,
    T: Send + Sync + 'static,
    F: Send + Sync + 'static,
{
    fn handle(&self, _: &mut Request) -> IronResult<Response> {
        let r = Registry::new();

        let present_slot = if let Ok(Some(slot)) = self.beacon_chain.slot_clock.present_slot() {
            slot
        } else {
            Slot::new(0)
        };
        register_and_set_slot(
            &r,
            "present_slot",
            "direct_slock_clock_reading",
            present_slot,
        );

        // Gather the metrics.
        let mut buffer = vec![];
        let encoder = TextEncoder::new();
        let metric_families = r.gather();
        encoder.encode(&metric_families, &mut buffer).unwrap();

        let prom_string = String::from_utf8(buffer).unwrap();

        Ok(Response::with((Status::Ok, prom_string)))
    }
}

fn register_and_set_slot(registry: &Registry, name: &str, help: &str, slot: Slot) {
    let counter_opts = Opts::new(name, help);
    let counter = IntCounter::with_opts(counter_opts).unwrap();
    registry.register(Box::new(counter.clone())).unwrap();
    counter.inc_by(slot.as_u64() as i64);
}
<|endoftext|>


<commit_before>use libc::{c_int, c_char, c_long, time_t, size_t};
use std::fmt::Debug;

#[derive(Debug)]
#[repr(C)]
pub struct JTM {
  pub tm_sec: c_int,       /* Seconds. (0-59) */
  pub tm_min: c_int,       /* Minutes. (0-59) */
  pub tm_hour: c_int,      /* Hours. (0-59) */
  pub tm_mday: c_int,      /* Day of the month. (1-31) */
  pub tm_mon: c_int,       /* Month. (0-11) */
  pub tm_year: c_int,      /* Year. */
  pub tm_wday: c_int,      /* Day of the week. (0-6) */
  pub tm_yday: c_int,      /* Day in the year. (0-365) */
  pub tm_isdst: c_int,     /* Daylight saving time is in effect. */
  pub tm_gmtoff: c_long,   /* Seconds east of UTC. */
  tm_zone: *const c_char,  /* Timezone abbreviation.  */
}

#[derive(Debug)]
#[repr(C)]
pub struct AB_JTM {
  pub ab_sec: c_int,
  pub ab_min: c_int,
  pub ab_hour: c_int,
  pub ab_days: c_int,
}

#[derive(Debug)]
#[repr(C)]
pub struct JYInfo {
  pub lf: c_int,          /* leap indicator flag */
  pub y: c_int,           /* year */
  pub r: c_int,           /* reamining years in grand cycle */
  pub p: c_int,           /* passed years from grand cycle*/
  pub rl: c_int,          /* remaining leap years in grand cycle */
  pub pl: c_int,          /* passed leap years in grand cycle */
  pub apl: c_int,         /* absolute passed leaps */
}
<commit_msg>make tm_zone field public<commit_after>use libc::{c_int, c_char, c_long, time_t, size_t};
use std::fmt::Debug;

#[derive(Debug)]
#[repr(C)]
pub struct JTM {
  pub tm_sec: c_int,       /* Seconds. (0-59) */
  pub tm_min: c_int,       /* Minutes. (0-59) */
  pub tm_hour: c_int,      /* Hours. (0-59) */
  pub tm_mday: c_int,      /* Day of the month. (1-31) */
  pub tm_mon: c_int,       /* Month. (0-11) */
  pub tm_year: c_int,      /* Year. */
  pub tm_wday: c_int,      /* Day of the week. (0-6) */
  pub tm_yday: c_int,      /* Day in the year. (0-365) */
  pub tm_isdst: c_int,     /* Daylight saving time is in effect. */
  pub tm_gmtoff: c_long,   /* Seconds east of UTC. */
  pub tm_zone: *const c_char,  /* Timezone abbreviation.  */
}

#[derive(Debug)]
#[repr(C)]
pub struct AB_JTM {
  pub ab_sec: c_int,
  pub ab_min: c_int,
  pub ab_hour: c_int,
  pub ab_days: c_int,
}

#[derive(Debug)]
#[repr(C)]
pub struct JYInfo {
  pub lf: c_int,          /* leap indicator flag */
  pub y: c_int,           /* year */
  pub r: c_int,           /* reamining years in grand cycle */
  pub p: c_int,           /* passed years from grand cycle*/
  pub rl: c_int,          /* remaining leap years in grand cycle */
  pub pl: c_int,          /* passed leap years in grand cycle */
  pub apl: c_int,         /* absolute passed leaps */
}
<|endoftext|>


<commit_before>use std::collections::HashMap;

use semver::{Version, VersionReq};
use rustc_serialize::json;

use super::Dependency;

#[derive(RustcDecodable, Debug)]
#[allow(non_snake_case)]
struct BowerJson {
    dependencies: Option<HashMap<String, String>>,
    devDependencies: Option<HashMap<String, String>>
}

#[derive(Debug, Clone)]
pub struct BowerDependency {
    name: String,
    version_req: VersionReq,
}

impl Dependency for BowerDependency {
    fn to_check(bower_json_contents: &str, _path: &Path) -> Vec<BowerDependency> {
        let bower_json = match json::decode::<BowerJson>(bower_json_contents) {
            Ok(json) => json,
            Err(err) => panic!("Failed to parse bower.json: {:?}", err)
        };

        let requires = bower_json.dependencies.unwrap_or(HashMap::new());
        let require_devs = bower_json.devDependencies.unwrap_or(HashMap::new());

        requires.iter().chain(require_devs.iter()).filter_map(|(name, version)|
            match VersionReq::parse(version.trim_left_matches('v')) {
                Ok(vr) => Some(BowerDependency { name: name.clone(), version_req: vr }),
                Err(err) => {
                    println!("{} ignored (could not parse {}: {:?})", name, version, err);
                    None
                }
            }
        ).collect()
    }

    fn name(&self) -> &String {
        &self.name
    }

    fn version_req(&self) -> &VersionReq {
        &self.version_req
    }

    fn registry_version(&self) -> Option<Version> {
        None // this needs git handling
    }
}
<commit_msg>remove obsolete file<commit_after><|endoftext|>


<commit_before>pub mod connection;
pub mod stream;
pub mod timer;
pub mod udp_packet;
mod stream_buffer;
#[cfg(test)]
mod tests;

use std::collections::{VecDeque, HashMap};
use std::io;
use std::net;
use std::time;

use rand;
use rand::Rng;

use quic::endpoint_role::EndpointRole;
use quic::errors::{Result};
use quic::packets;
use quic::packets::frames::Frame;
use self::connection::Connection;
use self::udp_packet::{IncomingUdpPacket, OutgoingUdpPacket};

#[derive(Clone, Debug, Default, PartialEq)]
pub struct QuicEngine<T: timer::Timer> {
    timer: T,

    accept_connections: bool,
    connections: HashMap<u64, Connection>,
    new_connection_ids: VecDeque<u64>,

    pending_packets: Vec<OutgoingUdpPacket>,
}

impl <T: timer::Timer> QuicEngine<T> {
    pub fn new(timer: T, accept_connections: bool) -> QuicEngine<T> {
        QuicEngine {
            timer: timer,

            accept_connections: accept_connections,
            connections: HashMap::new(),
            new_connection_ids: VecDeque::new(),

            pending_packets: Vec::new(),
        }
    }

    pub fn initiate_connection(&mut self, addr: net::SocketAddr) -> u64 {
        let mut rng = rand::thread_rng();
        let connection_id = rng.gen();

        let connection = Connection::new(connection_id, EndpointRole::Client, addr);
        self.connections.insert(connection_id, connection);

        debug!("Initiating connection (id: {})", connection_id);
        connection_id
    }

    fn accept_connection(&mut self, connection_id: u64, addr: net::SocketAddr) {
        let connection = Connection::new(connection_id, EndpointRole::Server, addr);
        self.connections.insert(connection_id, connection);
        self.new_connection_ids.push_back(connection_id);
    }

    pub fn have_connections(&self) -> bool {
        !self.new_connection_ids.is_empty()
    }

    pub fn pop_new_connection(&mut self) -> u64 {
        self.new_connection_ids.pop_front().unwrap()
    }

    pub fn handle_incoming_packet(&mut self, packet: IncomingUdpPacket) {
        let endpoint_role = if self.accept_connections {
            EndpointRole::Server
        } else {
            EndpointRole::Client
        };

        let source_address = packet.source_address;
        let packet = match packets::Packet::decode(&mut io::Cursor::new(packet.payload), endpoint_role) {
            Ok(packet) => packet,
            Err(e) => {
                error!("Error while decoding incoming packet: {}", e);
                return;
            }
        };

        match packet {
            packets::Packet::PublicReset(..) => {
                unimplemented!()
            },
            packets::Packet::Regular(ref regular_packet) => {
                match regular_packet.header.connection_id {
                    Some(connection_id) => {
                        if !self.connections.contains_key(&connection_id) {
                            if self.accept_connections {
                                debug!("Registering connection (id: {})", connection_id);
                                self.accept_connection(connection_id, source_address);
                            } else {
                                warn!("Dropping a packet with unknown connection id, can't accept");
                                return;
                            }
                        }

                        let connection = self.connections.get_mut(&connection_id).unwrap();
                        connection.handle_regular_packet(regular_packet);
                    },
                    None => unimplemented!(),
                }
            },
            packets::Packet::VersionNegotiation(..) => {
                unimplemented!()
            },
        }

        self.flush_buffered_data();
    }

    pub fn handle_due_events(&mut self) {
        for event in self.timer.pop_due_events() {
            trace!("Handling event: {:?}", event);

            match event {
                timer::ScheduledEvent::ResendUnackedPacket(packet) => {
                    let connection_id = packet.connection_id().unwrap();
                    let connection = self.connections.get_mut(&connection_id).unwrap();
                    connection.check_unacked_packet(packet);
                }
            }
        }

        self.flush_buffered_data();
    }

    pub fn write(&mut self, connection_id: u64, stream_id: u32, buf: &[u8]) -> Result<()> {
        {
            let connection =
                self.connections.get_mut(&connection_id)
                .expect("Invalid connection id");
            connection.write(stream_id, buf)?;
        }

        self.flush_buffered_data();

        Ok(())
    }

    pub fn finalize_outgoing_stream(&mut self, connection_id: u64, stream_id: u32) -> Result<()> {
        {
            let connection =
                self.connections.get_mut(&connection_id)
                .expect("Invalid connection id");
            connection.finalize_outgoing_stream(stream_id)?;
        }

        self.flush_buffered_data();

        Ok(())
    }

    pub fn pop_pending_packets(&mut self) -> Vec<OutgoingUdpPacket> {
        self.pending_packets.drain(..).collect()
    }

    pub fn timer_ref(&self) -> &T {
        &self.timer
    }

    pub fn data_available(&self, connection_id: u64, stream_id: u32) -> bool {
        let connection =
            self.connections.get(&connection_id)
            .expect("Invalid connection id");

        connection.data_available(stream_id)
    }

    pub fn any_data_available(&self, connection_id: u64) -> bool {
        let connection =
            self.connections.get(&connection_id)
            .expect("Invalid connection id");

        connection.any_data_available()
    }

    pub fn read(&mut self, connection_id: u64, stream_id: u32, buf: &mut [u8]) -> Result<usize> {
        let read_size = {
            let connection =
                self.connections.get_mut(&connection_id)
                .expect("Invalid connection id");

            connection.read(stream_id, buf)
        };

        self.flush_buffered_data();

        read_size
    }

    fn flush_buffered_data(&mut self) {
        for connection in self.connections.values_mut() {
            let peer_address = connection.peer_address();
            for packet in connection.drain_outgoing_packets() {
                let ack_only_packet = match packet {
                    packets::Packet::Regular(ref regular_packet) => {
                        let mut ack_only_packet = true;
                        for frame in &regular_packet.payload.frames {
                            match *frame {
                                Frame::Ack(..) => {},
                                _ => {
                                    ack_only_packet = false;
                                    break;
                                }
                            }
                        }

                        ack_only_packet
                    },
                    _ => false,
                };

                if !ack_only_packet {
                    connection.unacked_packet_numbers.insert(packet.packet_number().unwrap());

                    self.timer.schedule(
                        time::Duration::from_millis(5000),
                        timer::ScheduledEvent::ResendUnackedPacket(packet.clone()),
                    );
                }

                let mut buffer = vec![];
                packet.encode(&mut buffer).unwrap();

                self.pending_packets.push(OutgoingUdpPacket {
                    destination_address: peer_address,
                    payload: buffer,
                });
            }
        }
    }
}
<commit_msg>lower the timeout<commit_after>pub mod connection;
pub mod stream;
pub mod timer;
pub mod udp_packet;
mod stream_buffer;
#[cfg(test)]
mod tests;

use std::collections::{VecDeque, HashMap};
use std::io;
use std::net;
use std::time;

use rand;
use rand::Rng;

use quic::endpoint_role::EndpointRole;
use quic::errors::{Result};
use quic::packets;
use quic::packets::frames::Frame;
use self::connection::Connection;
use self::udp_packet::{IncomingUdpPacket, OutgoingUdpPacket};

#[derive(Clone, Debug, Default, PartialEq)]
pub struct QuicEngine<T: timer::Timer> {
    timer: T,

    accept_connections: bool,
    connections: HashMap<u64, Connection>,
    new_connection_ids: VecDeque<u64>,

    pending_packets: Vec<OutgoingUdpPacket>,
}

impl <T: timer::Timer> QuicEngine<T> {
    pub fn new(timer: T, accept_connections: bool) -> QuicEngine<T> {
        QuicEngine {
            timer: timer,

            accept_connections: accept_connections,
            connections: HashMap::new(),
            new_connection_ids: VecDeque::new(),

            pending_packets: Vec::new(),
        }
    }

    pub fn initiate_connection(&mut self, addr: net::SocketAddr) -> u64 {
        let mut rng = rand::thread_rng();
        let connection_id = rng.gen();

        let connection = Connection::new(connection_id, EndpointRole::Client, addr);
        self.connections.insert(connection_id, connection);

        debug!("Initiating connection (id: {})", connection_id);
        connection_id
    }

    fn accept_connection(&mut self, connection_id: u64, addr: net::SocketAddr) {
        let connection = Connection::new(connection_id, EndpointRole::Server, addr);
        self.connections.insert(connection_id, connection);
        self.new_connection_ids.push_back(connection_id);
    }

    pub fn have_connections(&self) -> bool {
        !self.new_connection_ids.is_empty()
    }

    pub fn pop_new_connection(&mut self) -> u64 {
        self.new_connection_ids.pop_front().unwrap()
    }

    pub fn handle_incoming_packet(&mut self, packet: IncomingUdpPacket) {
        let endpoint_role = if self.accept_connections {
            EndpointRole::Server
        } else {
            EndpointRole::Client
        };

        let source_address = packet.source_address;
        let packet = match packets::Packet::decode(&mut io::Cursor::new(packet.payload), endpoint_role) {
            Ok(packet) => packet,
            Err(e) => {
                error!("Error while decoding incoming packet: {}", e);
                return;
            }
        };

        match packet {
            packets::Packet::PublicReset(..) => {
                unimplemented!()
            },
            packets::Packet::Regular(ref regular_packet) => {
                match regular_packet.header.connection_id {
                    Some(connection_id) => {
                        if !self.connections.contains_key(&connection_id) {
                            if self.accept_connections {
                                debug!("Registering connection (id: {})", connection_id);
                                self.accept_connection(connection_id, source_address);
                            } else {
                                warn!("Dropping a packet with unknown connection id, can't accept");
                                return;
                            }
                        }

                        let connection = self.connections.get_mut(&connection_id).unwrap();
                        connection.handle_regular_packet(regular_packet);
                    },
                    None => unimplemented!(),
                }
            },
            packets::Packet::VersionNegotiation(..) => {
                unimplemented!()
            },
        }

        self.flush_buffered_data();
    }

    pub fn handle_due_events(&mut self) {
        for event in self.timer.pop_due_events() {
            trace!("Handling event: {:?}", event);

            match event {
                timer::ScheduledEvent::ResendUnackedPacket(packet) => {
                    let connection_id = packet.connection_id().unwrap();
                    let connection = self.connections.get_mut(&connection_id).unwrap();
                    connection.check_unacked_packet(packet);
                }
            }
        }

        self.flush_buffered_data();
    }

    pub fn write(&mut self, connection_id: u64, stream_id: u32, buf: &[u8]) -> Result<()> {
        {
            let connection =
                self.connections.get_mut(&connection_id)
                .expect("Invalid connection id");
            connection.write(stream_id, buf)?;
        }

        self.flush_buffered_data();

        Ok(())
    }

    pub fn finalize_outgoing_stream(&mut self, connection_id: u64, stream_id: u32) -> Result<()> {
        {
            let connection =
                self.connections.get_mut(&connection_id)
                .expect("Invalid connection id");
            connection.finalize_outgoing_stream(stream_id)?;
        }

        self.flush_buffered_data();

        Ok(())
    }

    pub fn pop_pending_packets(&mut self) -> Vec<OutgoingUdpPacket> {
        self.pending_packets.drain(..).collect()
    }

    pub fn timer_ref(&self) -> &T {
        &self.timer
    }

    pub fn data_available(&self, connection_id: u64, stream_id: u32) -> bool {
        let connection =
            self.connections.get(&connection_id)
            .expect("Invalid connection id");

        connection.data_available(stream_id)
    }

    pub fn any_data_available(&self, connection_id: u64) -> bool {
        let connection =
            self.connections.get(&connection_id)
            .expect("Invalid connection id");

        connection.any_data_available()
    }

    pub fn read(&mut self, connection_id: u64, stream_id: u32, buf: &mut [u8]) -> Result<usize> {
        let read_size = {
            let connection =
                self.connections.get_mut(&connection_id)
                .expect("Invalid connection id");

            connection.read(stream_id, buf)
        };

        self.flush_buffered_data();

        read_size
    }

    fn flush_buffered_data(&mut self) {
        for connection in self.connections.values_mut() {
            let peer_address = connection.peer_address();
            for packet in connection.drain_outgoing_packets() {
                let ack_only_packet = match packet {
                    packets::Packet::Regular(ref regular_packet) => {
                        let mut ack_only_packet = true;
                        for frame in &regular_packet.payload.frames {
                            match *frame {
                                Frame::Ack(..) => {},
                                _ => {
                                    ack_only_packet = false;
                                    break;
                                }
                            }
                        }

                        ack_only_packet
                    },
                    _ => false,
                };

                if !ack_only_packet {
                    connection.unacked_packet_numbers.insert(packet.packet_number().unwrap());

                    self.timer.schedule(
                        time::Duration::from_millis(100),
                        timer::ScheduledEvent::ResendUnackedPacket(packet.clone()),
                    );
                }

                let mut buffer = vec![];
                packet.encode(&mut buffer).unwrap();

                self.pending_packets.push(OutgoingUdpPacket {
                    destination_address: peer_address,
                    payload: buffer,
                });
            }
        }
    }
}
<|endoftext|>


<commit_before>use std::collections::HashSet;
use std::hash::Hash;
use std::net::{Ipv4Addr, Ipv6Addr};
use std::slice::Iter;
use std::sync::Arc;
use std::time::{Duration, Instant};

use chrono::{DateTime, Utc};
use futures::stream::{self, StreamExt};
use log::{debug, trace};
use serde::Serialize;
use trust_dns_resolver::error::{ResolveError, ResolveErrorKind};
use trust_dns_resolver::proto::xfer::DnsRequestOptions;

use crate::error::Error;
use crate::nameserver::NameServerConfig;
use crate::resolver::{MultiQuery, Resolver, UniQuery};
use crate::resources::rdata::{Name, MX, NULL, SOA, SRV, TXT, UNKNOWN};
use crate::resources::{RData, Record};
use crate::serialize::ser_arc_nameserver_config;

#[derive(Debug, Clone, Serialize)]
pub struct Lookups {
    inner: Vec<Lookup>,
}

macro_rules! accessor {
    ($method:ident, $out_type:ty) => {
        pub fn $method(&self) -> Vec<$out_type> {
            self.filter_record_type(|rdata| rdata.$method())
        }
    };
}

impl Lookups {
    #[allow(dead_code)]
    #[doc(hidden)]
    pub(crate) fn new(inner: Vec<Lookup>) -> Lookups {
        Lookups { inner }
    }

    pub fn len(&self) -> usize {
        self.inner.len()
    }

    pub fn is_empty(&self) -> bool {
        self.inner.is_empty()
    }

    pub fn iter(&self) -> Iter<Lookup> {
        self.inner.iter()
    }

    accessor!(a, Ipv4Addr);
    accessor!(aaaa, Ipv6Addr);
    accessor!(aname, Name);
    accessor!(cname, Name);
    accessor!(mx, MX);
    accessor!(null, NULL);
    accessor!(ns, Name);
    accessor!(ptr, Name);
    accessor!(soa, SOA);
    accessor!(srv, SRV);
    accessor!(txt, TXT);
    accessor!(unknown, UNKNOWN);

    fn filter_record_type<T: Clone, F: Fn(&RData) -> Option<&T>>(&self, filter: F) -> Vec<T> {
        self.inner
            .iter()
            .map(|x| x.result().response())
            .flatten()
            .map(|x| x.records())
            .flatten()
            .map(|x| filter(x.rdata()))
            .flatten()
            .cloned()
            .collect()
    }
}

pub trait Uniquify<'a, T: Clone + Eq + Hash> {
    fn unique(self) -> Uniquified<'a, T>;
}

// It's impossible to impl ToOwned fpr HashSet<&Name>, because of Borrow<Self>, thus I use the
// new-type pattern to add a to_owned method by myself. Not perfect, but it is what it is.
#[derive(Debug)]
pub struct Uniquified<'a, T: Clone + Eq + Hash> {
    inner: HashSet<&'a T>,
}

impl<'a, T: Clone + Eq + Hash> Uniquified<'a, T> {
    pub fn set(&'a self) -> &'a HashSet<&'a T> {
        &self.inner
    }

    pub fn to_owned(&self) -> HashSet<T> {
        self.inner.iter().map(|x| (*x).clone()).collect()
    }

    pub fn len(&self) -> usize {
        self.inner.len()
    }

    pub fn is_empty(&self) -> bool {
        self.inner.is_empty()
    }
}

impl<'a, S, T> Uniquify<'a, T> for S
where
    S: std::marker::Sized + IntoIterator<Item = &'a T> + 'a,
    T: Clone + Eq + Hash + 'a,
{
    fn unique(self) -> Uniquified<'a, T> {
        Uniquified {
            inner: self.into_iter().collect(),
        }
    }
}

impl IntoIterator for Lookups {
    type Item = Lookup;
    type IntoIter = std::vec::IntoIter<Self::Item>;

    fn into_iter(self) -> Self::IntoIter {
        self.inner.into_iter()
    }
}

impl From<Vec<Lookup>> for Lookups {
    fn from(lookups: Vec<Lookup>) -> Self {
        Lookups { inner: lookups }
    }
}

#[derive(Debug, Clone, Serialize)]
pub struct Lookup {
    query: UniQuery,
    #[serde(serialize_with = "ser_arc_nameserver_config")]
    name_server: Arc<NameServerConfig>,
    result: LookupResult,
}

impl Lookup {
    pub fn query(&self) -> &UniQuery {
        &self.query
    }

    pub fn name_server(&self) -> &NameServerConfig {
        &self.name_server
    }

    pub fn result(&self) -> &LookupResult {
        &self.result
    }
}

#[derive(Debug, Clone, Serialize)]
pub enum LookupResult {
    Response(Response),
    NxDomain(NxDomain),
    Timeout,
    Error(String),
}

#[derive(Debug, Clone, Serialize)]
pub struct Response {
    records: Vec<Record>,
    response_time: Duration,
    valid_until: DateTime<Utc>,
}

impl Response {
    pub fn records(&self) -> &[Record] {
        &self.records
    }

    pub fn response_time(&self) -> &Duration {
        &self.response_time
    }

    pub fn valid_until(&self) -> &DateTime<Utc> {
        &self.valid_until
    }
}

#[derive(Debug, Clone, Serialize)]
pub struct NxDomain {
    response_time: Duration,
    valid_until: Option<DateTime<Utc>>,
}

impl LookupResult {
    pub fn is_response(&self) -> bool {
        match self {
            LookupResult::Response { .. } => true,
            _ => false,
        }
    }

    pub fn is_nxdomain(&self) -> bool {
        match self {
            LookupResult::NxDomain { .. } => true,
            _ => false,
        }
    }

    pub fn is_timeout(&self) -> bool {
        match self {
            LookupResult::Timeout { .. } => true,
            _ => false,
        }
    }

    pub fn is_err(&self) -> bool {
        match self {
            LookupResult::Error { .. } => true,
            _ => false,
        }
    }

    pub fn response(&self) -> Option<&Response> {
        match self {
            LookupResult::Response(ref response) => Some(response),
            _ => None,
        }
    }

    pub fn nxdomain(&self) -> Option<&NxDomain> {
        match self {
            LookupResult::NxDomain(ref nxdomain) => Some(nxdomain),
            _ => None,
        }
    }

    pub fn err(&self) -> Option<&String> {
        match self {
            LookupResult::Error(ref err) => Some(&err),
            _ => None,
        }
    }
}

pub async fn lookup<T: Into<MultiQuery>>(resolver: Resolver, query: T) -> Lookups {
    let query = query.into();

    let lookup_futures = query
        .into_uni_queries()
        .into_iter()
        .map(|q| single_lookup(&resolver, q));

    let lookups = stream::iter(lookup_futures)
        .buffer_unordered(resolver.opts.max_concurrent_requests)
        .inspect(|lookup| trace!("Received lookup {:?}", lookup))
        .collect::<Vec<Lookup>>()
        .await;

    lookups.into()
}

async fn single_lookup(resolver: &Resolver, query: UniQuery) -> Lookup {
    let q = query.clone();
    let start_time = Instant::now();
    let result = resolver
        .inner
        .lookup(q.name, q.record_type.into(), DnsRequestOptions::default())
        .await
        .into_lookup(start_time);
    debug!(
        "Received Lookup for '{}', record type {} from {:?}.",
        &query.name,
        &query.record_type,
        resolver.name()
    );

    Lookup {
        query,
        name_server: resolver.name_server.clone(),
        result,
    }
}

#[doc(hidden)]
trait IntoLookup {
    fn into_lookup(self, start_time: Instant) -> LookupResult;
}

#[doc(hidden)]
impl IntoLookup for std::result::Result<trust_dns_resolver::lookup::Lookup, ResolveError> {
    fn into_lookup(self, start_time: Instant) -> LookupResult {
        match self {
            Ok(lookup) => {
                let records: Vec<Record> = lookup.record_iter().map(Record::from).collect();
                LookupResult::Response(Response {
                    records,
                    response_time: Instant::now() - start_time,
                    valid_until: instant_to_utc(lookup.valid_until()),
                })
            }
            Err(err) => match err.kind() {
                ResolveErrorKind::NoRecordsFound { valid_until, .. } => LookupResult::NxDomain(NxDomain {
                    response_time: Instant::now() - start_time,
                    valid_until: valid_until.map(instant_to_utc),
                }),
                ResolveErrorKind::Timeout => LookupResult::Timeout,
                _ => LookupResult::Error(Error::from(err).to_string()),
            },
        }
    }
}

fn instant_to_utc(instant: Instant) -> DateTime<Utc> {
    let now = Instant::now();
    let duration = instant.duration_since(now);

    Utc::now() + chrono::Duration::from_std(duration).unwrap() // Safe, because I know this is a valid duration
}
<commit_msg>Use borrowing for Lookups.rr()<commit_after>use std::collections::HashSet;
use std::hash::Hash;
use std::net::{Ipv4Addr, Ipv6Addr};
use std::slice::Iter;
use std::sync::Arc;
use std::time::{Duration, Instant};

use chrono::{DateTime, Utc};
use futures::stream::{self, StreamExt};
use log::{debug, trace};
use serde::Serialize;
use trust_dns_resolver::error::{ResolveError, ResolveErrorKind};
use trust_dns_resolver::proto::xfer::DnsRequestOptions;

use crate::error::Error;
use crate::nameserver::NameServerConfig;
use crate::resolver::{MultiQuery, Resolver, UniQuery};
use crate::resources::rdata::{Name, MX, NULL, SOA, SRV, TXT, UNKNOWN};
use crate::resources::{RData, Record};
use crate::serialize::ser_arc_nameserver_config;

#[derive(Debug, Clone, Serialize)]
pub struct Lookups {
    inner: Vec<Lookup>,
}

macro_rules! accessor {
    ($method:ident, $out_type:ty) => {
        pub fn $method(&self) -> Vec<&$out_type> {
            self.filter_record_type(|rdata| rdata.$method())
        }
    };
}

impl Lookups {
    #[allow(dead_code)]
    #[doc(hidden)]
    pub(crate) fn new(inner: Vec<Lookup>) -> Lookups {
        Lookups { inner }
    }

    pub fn len(&self) -> usize {
        self.inner.len()
    }

    pub fn is_empty(&self) -> bool {
        self.inner.is_empty()
    }

    pub fn iter(&self) -> Iter<Lookup> {
        self.inner.iter()
    }

    accessor!(a, Ipv4Addr);
    accessor!(aaaa, Ipv6Addr);
    accessor!(aname, Name);
    accessor!(cname, Name);
    accessor!(mx, MX);
    accessor!(null, NULL);
    accessor!(ns, Name);
    accessor!(ptr, Name);
    accessor!(soa, SOA);
    accessor!(srv, SRV);
    accessor!(txt, TXT);
    accessor!(unknown, UNKNOWN);

    fn filter_record_type<T, F: Fn(&RData) -> Option<&T>>(&self, filter: F) -> Vec<&T> {
        self.inner
            .iter()
            .map(|x| x.result().response())
            .flatten()
            .map(|x| x.records())
            .flatten()
            .map(|x| filter(x.rdata()))
            .flatten()
            .collect()
    }
}

pub trait Uniquify<'a, T: Clone + Eq + Hash> {
    fn unique(self) -> Uniquified<'a, T>;
}

// It's impossible to impl ToOwned fpr HashSet<&Name>, because of Borrow<Self>, thus I use the
// new-type pattern to add a to_owned method by myself. Not perfect, but it is what it is.
#[derive(Debug)]
pub struct Uniquified<'a, T: Clone + Eq + Hash> {
    inner: HashSet<&'a T>,
}

impl<'a, T: Clone + Eq + Hash> Uniquified<'a, T> {
    pub fn set(&'a self) -> &'a HashSet<&'a T> {
        &self.inner
    }

    pub fn to_owned(&self) -> HashSet<T> {
        self.inner.iter().map(|x| (*x).clone()).collect()
    }

    pub fn len(&self) -> usize {
        self.inner.len()
    }

    pub fn is_empty(&self) -> bool {
        self.inner.is_empty()
    }
}

impl<'a, S, T> Uniquify<'a, T> for S
where
    S: std::marker::Sized + IntoIterator<Item = &'a T> + 'a,
    T: Clone + Eq + Hash + 'a,
{
    fn unique(self) -> Uniquified<'a, T> {
        Uniquified {
            inner: self.into_iter().collect(),
        }
    }
}

impl IntoIterator for Lookups {
    type Item = Lookup;
    type IntoIter = std::vec::IntoIter<Self::Item>;

    fn into_iter(self) -> Self::IntoIter {
        self.inner.into_iter()
    }
}

impl From<Vec<Lookup>> for Lookups {
    fn from(lookups: Vec<Lookup>) -> Self {
        Lookups { inner: lookups }
    }
}

#[derive(Debug, Clone, Serialize)]
pub struct Lookup {
    query: UniQuery,
    #[serde(serialize_with = "ser_arc_nameserver_config")]
    name_server: Arc<NameServerConfig>,
    result: LookupResult,
}

impl Lookup {
    pub fn query(&self) -> &UniQuery {
        &self.query
    }

    pub fn name_server(&self) -> &NameServerConfig {
        &self.name_server
    }

    pub fn result(&self) -> &LookupResult {
        &self.result
    }
}

#[derive(Debug, Clone, Serialize)]
pub enum LookupResult {
    Response(Response),
    NxDomain(NxDomain),
    Timeout,
    Error(String),
}

#[derive(Debug, Clone, Serialize)]
pub struct Response {
    records: Vec<Record>,
    response_time: Duration,
    valid_until: DateTime<Utc>,
}

impl Response {
    pub fn records(&self) -> &[Record] {
        &self.records
    }

    pub fn response_time(&self) -> &Duration {
        &self.response_time
    }

    pub fn valid_until(&self) -> &DateTime<Utc> {
        &self.valid_until
    }
}

#[derive(Debug, Clone, Serialize)]
pub struct NxDomain {
    response_time: Duration,
    valid_until: Option<DateTime<Utc>>,
}

impl LookupResult {
    pub fn is_response(&self) -> bool {
        match self {
            LookupResult::Response { .. } => true,
            _ => false,
        }
    }

    pub fn is_nxdomain(&self) -> bool {
        match self {
            LookupResult::NxDomain { .. } => true,
            _ => false,
        }
    }

    pub fn is_timeout(&self) -> bool {
        match self {
            LookupResult::Timeout { .. } => true,
            _ => false,
        }
    }

    pub fn is_err(&self) -> bool {
        match self {
            LookupResult::Error { .. } => true,
            _ => false,
        }
    }

    pub fn response(&self) -> Option<&Response> {
        match self {
            LookupResult::Response(ref response) => Some(response),
            _ => None,
        }
    }

    pub fn nxdomain(&self) -> Option<&NxDomain> {
        match self {
            LookupResult::NxDomain(ref nxdomain) => Some(nxdomain),
            _ => None,
        }
    }

    pub fn err(&self) -> Option<&String> {
        match self {
            LookupResult::Error(ref err) => Some(&err),
            _ => None,
        }
    }
}

pub async fn lookup<T: Into<MultiQuery>>(resolver: Resolver, query: T) -> Lookups {
    let query = query.into();

    let lookup_futures = query
        .into_uni_queries()
        .into_iter()
        .map(|q| single_lookup(&resolver, q));

    let lookups = stream::iter(lookup_futures)
        .buffer_unordered(resolver.opts.max_concurrent_requests)
        .inspect(|lookup| trace!("Received lookup {:?}", lookup))
        .collect::<Vec<Lookup>>()
        .await;

    lookups.into()
}

async fn single_lookup(resolver: &Resolver, query: UniQuery) -> Lookup {
    let q = query.clone();
    let start_time = Instant::now();
    let result = resolver
        .inner
        .lookup(q.name, q.record_type.into(), DnsRequestOptions::default())
        .await
        .into_lookup(start_time);
    debug!(
        "Received Lookup for '{}', record type {} from {:?}.",
        &query.name,
        &query.record_type,
        resolver.name()
    );

    Lookup {
        query,
        name_server: resolver.name_server.clone(),
        result,
    }
}

#[doc(hidden)]
trait IntoLookup {
    fn into_lookup(self, start_time: Instant) -> LookupResult;
}

#[doc(hidden)]
impl IntoLookup for std::result::Result<trust_dns_resolver::lookup::Lookup, ResolveError> {
    fn into_lookup(self, start_time: Instant) -> LookupResult {
        match self {
            Ok(lookup) => {
                let records: Vec<Record> = lookup.record_iter().map(Record::from).collect();
                LookupResult::Response(Response {
                    records,
                    response_time: Instant::now() - start_time,
                    valid_until: instant_to_utc(lookup.valid_until()),
                })
            }
            Err(err) => match err.kind() {
                ResolveErrorKind::NoRecordsFound { valid_until, .. } => LookupResult::NxDomain(NxDomain {
                    response_time: Instant::now() - start_time,
                    valid_until: valid_until.map(instant_to_utc),
                }),
                ResolveErrorKind::Timeout => LookupResult::Timeout,
                _ => LookupResult::Error(Error::from(err).to_string()),
            },
        }
    }
}

fn instant_to_utc(instant: Instant) -> DateTime<Utc> {
    let now = Instant::now();
    let duration = instant.duration_since(now);

    Utc::now() + chrono::Duration::from_std(duration).unwrap() // Safe, because I know this is a valid duration
}
<|endoftext|>


<commit_before>//!
//! Interpreter Compiler for Expressions
//!

use common::err::{Error, TResult, Void, void_ok};
use eval::Eval;
use expr::{Datum, Expr, Visitor};
use schema::{Column, Schema};
use std::boxed::Box;
use types::{DataTy, HasDataTy, HasTy, Ty};

// Unary Expressions
pub struct Not {child: Box<Eval>}
pub struct IsNull {child: Box<Eval>}

// Binary Comparison Expressions
pub struct And {lhs: Box<Eval>, rhs: Box<Eval>}
pub struct Or {lhs: Box<Eval>, rhs: Box<Eval>}
pub struct Equal {lhs: Box<Eval>, rhs: Box<Eval>}
pub struct NotEqual {lhs: Box<Eval>, rhs: Box<Eval>}
pub struct LessThan {lhs: Box<Eval>, rhs: Box<Eval>}
pub struct LessThanOrEqual {lhs: Box<Eval>, rhs: Box<Eval>}
pub struct GreaterThan {lhs: Box<Eval>, rhs: Box<Eval>}
pub struct GreaterThanOrEqual {lhs: Box<Eval>, rhs: Box<Eval>}

// Binary Arithmetic Evalessions
pub struct Plus {pub lhs: Box<Eval>, pub rhs: Box<Eval>}
pub struct Minus {lhs: Box<Eval>, rhs: Box<Eval>}
pub struct Multiply {lhs: Box<Eval>, rhs: Box<Eval>}
pub struct Divide {lhs: Box<Eval>, rhs: Box<Eval>}
pub struct Modular {lhs: Box<Eval>, rhs: Box<Eval>}

// String operators or pattern matching predicates
pub struct Concatenate {lhs: Box<Eval>, rhs: Box<Eval>}
pub struct Like {pattern: String, child: Box<Eval>}
pub struct SimilarTo {pattern: String, child: Box<Eval>}
pub struct RegexMatch {pattern: String, child: Box<Eval>}

pub struct Between {pred: Box<Eval>, begin: Box<Eval>, end: Box<Eval>}
pub struct In {pred: Box<Eval>, row: Box<Row>}

pub struct Row {values: Vec<Box<Eval>>}
pub struct Field {column: Column, field_id: Option<usize>}
pub struct Const {datum: Datum, res_type: DataTy}

// impl Eval for Plus {
//   fn bind(&mut self, schema: &Schema) -> Void {
//     //self.field_id = schema.get_

//     void_ok()
//   }

//   fn ty(&self) -> &DataTy {
//     self.lhs.ty()
//   }

//   fn is_const(&self) -> bool { false }
// }

impl Field {
  pub fn new(column: &Column) -> Field {
    Field {
      column: column.clone(),
      field_id: None
    }
  }
}

impl HasDataTy for Field {
  fn data_ty(&self) -> &DataTy {
    &self.column.data_ty
  }
}

impl Eval for Field {

  fn bind(&mut self, schema: &Schema) -> Void {

    match schema.column_id(&self.column.name) {

     Some(id) => {
      self.field_id = Some(id);
      void_ok()
     },

     None => Err(Error::UndefinedColumn)
    }
  }

  fn is_const(&self) -> bool { false }

  // fn eval(&self, RowBlock) -> TResult<&Vector1>;
}

impl Const {
  fn new(datum: &Datum) -> Const {
    Const {
        datum: datum.clone(),
        res_type: DataTy::new(datum.ty())
    }
  }
}

// impl Eval for Const {

//   fn bind(&mut self, schema: &Schema) -> Void {
//     self.res_type = DataTy::new(self.datum.ty());
//     void_ok()
//   }

//   fn ty(&self) -> &DataTy {
//     &self.res_type
//   }

//   fn is_const(&self) -> bool { true }
// }

pub fn compile<'a>(expr: &'a Expr) -> TResult<Box<Eval>> {
  match *expr {

    // Expr::Between {lhs, mid, rhs} => {
    //   Ok(Box::new(
    //     Between {
    //       lhs: try!(compile(lhs)),
    //       mid: try!(compile(mid)),
    //       rhs: try!(compile(rhs))
    //     })
    //   )
    // },

    // Expr::Plus (ref lhs, ref rhs) => {
    //   Ok(Box::new(
    //     Plus {
    //       lhs: try!(compile(&**lhs)),
    //       rhs: try!(compile(&**rhs)),
    //     }
    //   ))
    // },

    // Expr::Field {column} => Ok(Box::new(Field::new(column))),

    // Expr::Const {datum} => Ok(Box::new(Const::new(&datum))),

    _ => Err(Error::InvalidExpression)
  }
}

pub struct InterpreterCompiler {
  tree: Option<Box<Eval>>,
  err: Option<Error>,
  node_num: u32
}

impl<'v> Visitor<'v> for InterpreterCompiler {
  fn visit_field(&mut self, c: &'v Column) {
    println!("column: {}", c.name);
  }
}<commit_msg>Recover Plus.<commit_after>//!
//! Interpreter Compiler for Expressions
//!

use common::err::{Error, TResult, Void, void_ok};
use eval::Eval;
use expr::{Datum, Expr, Visitor};
use schema::{Column, Schema};
use std::boxed::Box;
use types::{DataTy, HasDataTy, HasTy, result_data_ty, Ty};

// Unary Expressions
pub struct Not {child: Box<Eval>}
pub struct IsNull {child: Box<Eval>}

// Binary Comparison Expressions
pub struct And {lhs: Box<Eval>, rhs: Box<Eval>}
pub struct Or {lhs: Box<Eval>, rhs: Box<Eval>}
pub struct Equal {lhs: Box<Eval>, rhs: Box<Eval>}
pub struct NotEqual {lhs: Box<Eval>, rhs: Box<Eval>}
pub struct LessThan {lhs: Box<Eval>, rhs: Box<Eval>}
pub struct LessThanOrEqual {lhs: Box<Eval>, rhs: Box<Eval>}
pub struct GreaterThan {lhs: Box<Eval>, rhs: Box<Eval>}
pub struct GreaterThanOrEqual {lhs: Box<Eval>, rhs: Box<Eval>}

// Binary Arithmetic Evalessions
pub struct Plus {data_ty: DataTy, pub lhs: Box<Eval>, pub rhs: Box<Eval>}
pub struct Minus {lhs: Box<Eval>, rhs: Box<Eval>}
pub struct Multiply {lhs: Box<Eval>, rhs: Box<Eval>}
pub struct Divide {lhs: Box<Eval>, rhs: Box<Eval>}
pub struct Modular {lhs: Box<Eval>, rhs: Box<Eval>}

// String operators or pattern matching predicates
pub struct Concatenate {lhs: Box<Eval>, rhs: Box<Eval>}
pub struct Like {pattern: String, child: Box<Eval>}
pub struct SimilarTo {pattern: String, child: Box<Eval>}
pub struct RegexMatch {pattern: String, child: Box<Eval>}

pub struct Between {pred: Box<Eval>, begin: Box<Eval>, end: Box<Eval>}
pub struct In {pred: Box<Eval>, row: Box<Row>}

pub struct Row {values: Vec<Box<Eval>>}
pub struct Field {column: Column, field_id: Option<usize>}
pub struct Const {datum: Datum, res_type: DataTy}

impl Eval for Plus {
  fn bind(&mut self, schema: &Schema) -> Void {
    self.data_ty = result_data_ty(self.lhs.data_ty(), self.rhs.data_ty());
    void_ok()
  }

  fn is_const(&self) -> bool { false }
}

impl HasDataTy for Plus {
  #[inline]
  fn data_ty(&self) -> &DataTy {
    &self.data_ty
  }
}

impl Field {
  pub fn new(column: &Column) -> Field {
    Field {
      column: column.clone(),
      field_id: None
    }
  }
}

impl HasDataTy for Field {
  fn data_ty(&self) -> &DataTy {
    &self.column.data_ty
  }
}

impl Eval for Field {

  fn bind(&mut self, schema: &Schema) -> Void {

    match schema.column_id(&self.column.name) {

     Some(id) => {
      self.field_id = Some(id);
      void_ok()
     },

     None => Err(Error::UndefinedColumn)
    }
  }

  fn is_const(&self) -> bool { false }

  // fn eval(&self, RowBlock) -> TResult<&Vector1>;
}

impl Const {
  fn new(datum: &Datum) -> Const {
    Const {
        datum: datum.clone(),
        res_type: DataTy::new(datum.ty())
    }
  }
}

// impl Eval for Const {

//   fn bind(&mut self, schema: &Schema) -> Void {
//     self.res_type = DataTy::new(self.datum.ty());
//     void_ok()
//   }

//   fn ty(&self) -> &DataTy {
//     &self.res_type
//   }

//   fn is_const(&self) -> bool { true }
// }

pub fn compile<'a>(expr: &'a Expr) -> TResult<Box<Eval>> {
  match *expr {

    // Expr::Between {lhs, mid, rhs} => {
    //   Ok(Box::new(
    //     Between {
    //       lhs: try!(compile(lhs)),
    //       mid: try!(compile(mid)),
    //       rhs: try!(compile(rhs))
    //     })
    //   )
    // },

    // Expr::Plus (ref lhs, ref rhs) => {
    //   Ok(Box::new(
    //     Plus {
    //       lhs: try!(compile(&**lhs)),
    //       rhs: try!(compile(&**rhs)),
    //     }
    //   ))
    // },

    // Expr::Field {column} => Ok(Box::new(Field::new(column))),

    // Expr::Const {datum} => Ok(Box::new(Const::new(&datum))),

    _ => Err(Error::InvalidExpression)
  }
}

pub struct InterpreterCompiler {
  tree: Option<Box<Eval>>,
  err: Option<Error>,
  node_num: u32
}

impl<'v> Visitor<'v> for InterpreterCompiler {
  fn visit_field(&mut self, c: &'v Column) {
    println!("column: {}", c.name);
  }
}<|endoftext|>


<commit_before>//! The distrib module provides methods for executing the rendering in a
//! distributed environment across multiple nodes. The worker module provides
//! the Worker which does the actual job of rendering a subsection of the image.
//! The master module provides the Master which instructs the Workers and collects
//! their results.
//!
//! **Note:** At this time I do nothing for distributed fault handling or work
//! stealing. If a node crashes during rendering it's results will be lost and the
//! master will hang forever waiting to hear back from the crashed node.

use bincode::rustc_serialize::encoded_size;

pub use self::worker::Worker;
pub use self::master::Master;

pub mod worker;
pub mod master;

/// Stores instructions sent to a worker about which blocks it should be rendering,
/// block size is assumed to be 8x8
#[derive(Debug, Clone, RustcEncodable, RustcDecodable)]
struct Instructions {
    /// Size header for binary I/O with bincode
    pub encoded_size: u64,
    /// Scene file for the worker to load
    pub scene: String,
    /// Frames to be rendered (inclusive)
    pub frames: (usize, usize),
    /// Block in the z-order queue of blocks this worker will
    /// start at
    pub block_start: usize,
    /// Number of blocks this worker will render
    pub block_count: usize,
}

impl Instructions {
    pub fn new(scene: &String, frames: (usize, usize), block_start: usize,
               block_count: usize) -> Instructions {
        let mut instr = Instructions { encoded_size: 0, scene: scene.clone(), frames: frames,
                       block_start: block_start, block_count: block_count };
        instr.encoded_size = encoded_size(&instr);
        instr
    }
}

/// Frame is used by the worker to send its results back to the master. Sends information
/// about which frame is being sent, which blocks were rendered and the data for the blocks
#[derive(RustcEncodable, RustcDecodable)]
struct Frame {
    /// Size header for binary I/O with bincode
    pub encoded_size: u64,
    /// Which frame the worker is sending its results for
    pub frame: usize,
    /// Block size of the blocks being sent
    pub block_size: (usize, usize),
    /// Starting locations of each block
    pub blocks: Vec<(usize, usize)>,
    /// Sample data for each block, RGBW_F32 (W = weight)
    pub pixels: Vec<f32>,
}

impl Frame {
    pub fn new(frame: usize, block_size: (usize, usize), blocks: Vec<(usize, usize)>,
               pixels: Vec<f32>) -> Frame {
        let mut frame = Frame { encoded_size: 0, frame: frame, block_size: block_size,
                            blocks: blocks, pixels: pixels };
        frame.encoded_size = encoded_size(&frame);
        frame
    }
}

<commit_msg>Update doc on using the distributed rendering<commit_after>//! The distrib module provides methods for executing the rendering in a
//! distributed environment across multiple machies. The worker module provides
//! the Worker which does the actual job of rendering a subregion of the image.
//! The master module provides the Master which instructs the Workers what to render
//! and collects their results to save out the final image.
//!
//! # Usage
//!
//! The worker process takes very few arguments, just a flag indicating it's a worker
//! and optionally the number of threads to use with `-n`.
//!
//! ```
//! ./tray_rust --worker
//! ```
//!
//! The worker processes will listen on a hard-coded port for the master to send them instructions
//! about what parts of the image they should render. This is `exec::distrib::worker::PORT` which
//! you can change and re-compile if the default of 63234 conflicts with other applications.
//!
//! The master process can be run on the same machine as a worker since it doesn't take
//! up too much CPU time. To run the master you'll pass it the scene file, a list of the
//! worker hostnames or IP addresses and optionally an output path and start/end frame numbers.
//! You can also run tray\_rust with the `-h` or `--help` flag to see a list of options.
//!
//! ```
//! ./tray_rust cornell_box.json --master worker1 worker2 192.168.32.129
//! ```
//!
//! The master will send the workers the location of the scene file which is assumed to
//! be on some shared filesystem or otherwise available at the same path on all the workers.
//!
//! # Running on GCE or EC2
//!
//! You can run on any network of home machines but you can also run on virtual machines from
//! Google or Amazon if you want to rent a mini cluster. On GCE or EC2 you'll want machines in the
//! same region for faster communication and will then pass the local IPs of the workers to
//! the master. For example on GCE you're given a virtual local network, you would use
//! these IP addresses instead of the public IPs of the worker nodes.
//!

use bincode::rustc_serialize::encoded_size;

pub use self::worker::Worker;
pub use self::master::Master;

pub mod worker;
pub mod master;

/// Stores instructions sent to a worker about which blocks it should be rendering,
/// block size is assumed to be 8x8
#[derive(Debug, Clone, RustcEncodable, RustcDecodable)]
struct Instructions {
    /// Size header for binary I/O with bincode
    pub encoded_size: u64,
    /// Scene file for the worker to load
    pub scene: String,
    /// Frames to be rendered (inclusive)
    pub frames: (usize, usize),
    /// Block in the z-order queue of blocks this worker will
    /// start at
    pub block_start: usize,
    /// Number of blocks this worker will render
    pub block_count: usize,
}

impl Instructions {
    pub fn new(scene: &String, frames: (usize, usize), block_start: usize,
               block_count: usize) -> Instructions {
        let mut instr = Instructions { encoded_size: 0, scene: scene.clone(), frames: frames,
                       block_start: block_start, block_count: block_count };
        instr.encoded_size = encoded_size(&instr);
        instr
    }
}

/// Frame is used by the worker to send its results back to the master. Sends information
/// about which frame is being sent, which blocks were rendered and the data for the blocks
#[derive(RustcEncodable, RustcDecodable)]
struct Frame {
    /// Size header for binary I/O with bincode
    pub encoded_size: u64,
    /// Which frame the worker is sending its results for
    pub frame: usize,
    /// Block size of the blocks being sent
    pub block_size: (usize, usize),
    /// Starting locations of each block
    pub blocks: Vec<(usize, usize)>,
    /// Sample data for each block, RGBW_F32 (W = weight)
    pub pixels: Vec<f32>,
}

impl Frame {
    pub fn new(frame: usize, block_size: (usize, usize), blocks: Vec<(usize, usize)>,
               pixels: Vec<f32>) -> Frame {
        let mut frame = Frame { encoded_size: 0, frame: frame, block_size: block_size,
                            blocks: blocks, pixels: pixels };
        frame.encoded_size = encoded_size(&frame);
        frame
    }
}

<|endoftext|>