cig 0.1.2

Simplify TCP/IP applications with a transparential, persistent-mode and data-driven protocol
Documentation
use std::time::Instant;

use cig::{conn::Conn, pkg::Pkg, smoke::Smoke};

pub struct SmokeTest {
    pub counter: usize,
}

pub fn handler(c: &mut Conn<usize>, s: &mut SmokeTest, pkg: Pkg<usize>) {
    if c.is_host() {
        panic!("should'nt receive data from slaves");
    } else {
        s.counter = *pkg.as_data();
    }
}

fn main() {
    let mut last_print = Instant::now();
    let mut sm =
        Smoke::connect_or_host("127.0.0.1:8080", SmokeTest { counter: 0 }, handler).unwrap();
    loop {
        sm.poll();
        let now = Instant::now();
        if (now - last_print).as_secs() >= 1 {
            if sm.conn.is_host() {
                sm.state.counter += 1;
                sm.conn.send_to_all(sm.state.counter).unwrap();
            }
            println!("counter: {}", sm.state.counter);
            last_print = now;
        }
    }
}