view src/dispatch.rs @ 12:d680d7f1bec8 draft

Update dispatch and main for new Stream/Sink API
author Lewin Bormann <lbo@spheniscida.de>
date Sat, 03 Dec 2016 11:18:41 +0100
parents b049309fccb0
children ed9eb9cfeb83
line wrap: on
line source

//! This module contains the central event loop that receives and dispatches log messages.
//!

use std::io::Result;

use config::Config;
use dgram_stream;

use futures::Stream;
use tokio_core::reactor::Core;
use tokio_uds::UnixDatagram;

pub fn setup_and_run(cfg: Config) -> Result<()> {
    #[allow(non_snake_case)]

    let mut LOOP = try!(Core::new());
    let handle = LOOP.handle();

    let log_socket = try!(UnixDatagram::bind(cfg.general.bind_path, &handle));
    let log_incoming_stream = dgram_stream::new_unix_dgram_stream(cfg.general.max_msg_len, log_socket);

    let finished = log_incoming_stream.for_each(|msg_buf| {
        let msg = String::from_utf8(msg_buf).unwrap_or("<UTF8 decoding error>".to_string());
        println!("{}", msg);
        Ok(())
    });

    LOOP.run(finished)
}