Crate time

Source
Expand description

§⏳ time-lib

Rust library to manange time.

The core concept is the [Timer], which contains information about the cycle and the state. The [Server] runs the timer and accepts connection from [Client]s using [ServerBind]ers. The clients communicate with the server using [Request]s and [Response]s, which allow them to control the timer. The timer can be customized using [TimerConfig] and [TimerCycle].

┌────────────────────────┐
│Server                  │
│             ┌────────┐ │ Request ┌────────┐
│             │        │◄├─────────┤        │
│    ┌────────┤Binder A│ │         │Client A│
│    │        │        ├─┼────────►│        │
│    │        └────────┘ │Response └────────┘
│    │                   │
│    ▼        ┌────────┐ │         ┌────────┐
│ ┌─────┐     │        │◄├─────────┤        │
│ │Timer│◄────┤Binder B│ │         │Client B│
│ └─────┘     │        ├─┼────────►│        │
│    ▲        └────────┘ │         └────────┘
│    │                   │
│    │        ┌────────┐ │         ┌────────┐
│    │        │        │◄├─────────┤        │
│    └────────┤Binder C│ │         │Client C│
│             │        ├─┼────────►│        │
│             └────────┘ │         └────────┘
│                        │
└────────────────────────┘

Example using TCP and the Pomodoro technique:

$ cargo run --example pomodoro-tcp
use std::time::Duration;
use time::{
    client::tcp::TcpClient,
    server::{tcp::TcpBind, ServerBuilder, ServerEvent},
    timer::TimerEvent,
};

static HOST: &str = "127.0.0.1";
static PORT: u16 = 3000;

#[tokio::main]
async fn main() {
    let server = ServerBuilder::new()
        .with_server_handler(|event: ServerEvent| async move {
            println!("server event: {event:?}");
            Ok(())
        })
        .with_timer_handler(|event: TimerEvent| async move {
            println!("timer event: {event:?}");
            Ok(())
        })
        .with_binder(TcpBind::new(HOST, PORT))
        .with_pomodoro_config()
        .build()
        .unwrap();

    server
        .bind_with(|| async {
            // wait for the binder to be ready
            tokio::time::sleep(Duration::from_secs(1)).await;

            let client = TcpClient::new(HOST, PORT);

            client.start().await.unwrap();
            tokio::time::sleep(Duration::from_secs(1)).await;

            client.pause().await.unwrap();
            tokio::time::sleep(Duration::from_secs(1)).await;

            let timer = client.get().await.unwrap();
            println!("current timer: {timer:?}");

            Ok(())
        })
        .await
        .unwrap();
}

See more examples.

Modules§

clientclient
Client
request
Request
response
Response
serverserver
Server
tcptcp-any
TCP
timer
Timer