ump-server 0.4.1

Server message dispatch loop for ump.
Documentation
use std::ops::ControlFlow;

#[derive(Debug)]
pub enum Request {
  Delay(u64)
}

#[derive(Debug, PartialEq, Eq)]
pub enum Reply {
  DelayDone
}

pub struct ThreadedServer {}

impl ump_server::ThreadedHandler<Request, Reply, (), u32> for ThreadedServer {
  fn proc_req(
    &mut self,
    msg: Request,
    rctx: ump_server::ReplyContext<Reply, ()>
  ) -> ControlFlow<u32, ()> {
    match msg {
      Request::Delay(ms) => {
        std::thread::sleep(std::time::Duration::from_millis(ms));
        rctx.reply(Reply::DelayDone).unwrap();
        ControlFlow::Continue(())
      }
    }
  }
}

// Terminate the dispatcher loop by dropping the only client.
fn main() {
  #[cfg(not(feature = "watchdog"))]
  eprintln!("Warning: Example not built with watchdog feature");

  let (clnt, jh) =
    ump_server::spawn_thread(|_clnt| Ok(ThreadedServer {})).unwrap();

  println!("==> Issue request which should not timeout ..");
  clnt.req(Request::Delay(190)).unwrap();

  println!("==> Issue request which should timeout ..");
  clnt.req(Request::Delay(200)).unwrap();

  // Drop the (only) client, which should cause dispatch loop to terminate.
  drop(clnt);

  // Termination by clients disappearing should return None
  assert_eq!(jh.join().unwrap(), None);
}

// vim: set ft=rust et sw=2 ts=2 sts=2 cinoptions=2 tw=79 :