changeset 3:b049309fccb0 draft

Implement prototype dispatch loop
author Lewin Bormann <lbo@spheniscida.de>
date Sat, 26 Nov 2016 11:56:27 +0100
parents 58aaac3848cb
children ed83b57d6d59
files src/dispatch.rs
diffstat 1 files changed, 29 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/dispatch.rs	Sat Nov 26 11:56:27 2016 +0100
@@ -0,0 +1,29 @@
+//! 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)
+}