view src/dispatch.rs @ 3:b049309fccb0 draft

Implement prototype dispatch loop
author Lewin Bormann <lbo@spheniscida.de>
date Sat, 26 Nov 2016 11:56:27 +0100
parents
children d680d7f1bec8
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 unix_datagram_stream::{UnixDatagramStream, new_unix_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.bind_path, &handle));
    let log_incoming_stream = new_unix_dgram_stream(cfg.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)
}