simple_client/
simple_client.rs1use tracing::{debug, error, info, warn};
2use pass_it_on::notifications::{ClientReadyMessage, Message};
3use pass_it_on::ClientConfiguration;
4use pass_it_on::{start_client, Error};
5use std::time::{Duration, SystemTime, UNIX_EPOCH};
6use tokio::sync::{mpsc, watch};
7use tracing::level_filters::LevelFilter;
8
9const NOTIFICATION_NAME: &str = "test1";
10const SAMPLE_MESSAGE_COUNT: usize = 1;
11
12const CLIENT_TOML_CONFIG: &str = r#"
13 [client]
14 key = "UVXu7wtbXHWNgAr6rWyPnaZbZK9aYin8"
15
16 [[client.interface]]
17 type = "http"
18 host = "127.0.0.1"
19 port = 8080
20
21"#;
22
23#[tokio::main]
24async fn main() -> Result<(), Error> {
25 tracing_subscriber::fmt().with_max_level(LevelFilter::DEBUG).init();
26
27 let config = ClientConfiguration::try_from(CLIENT_TOML_CONFIG)?;
28 let messages = get_test_messages(SAMPLE_MESSAGE_COUNT, NOTIFICATION_NAME);
29 let (interface_tx, interface_rx) = mpsc::channel(100);
30 let (shutdown_tx, shutdown_rx) = watch::channel(false);
31
32 tokio::spawn(async move {
33 info!("Sending test messages");
34 for message in messages {
35 match interface_tx.send(message).await {
36 Ok(_) => debug!("Sent test message to client"),
37 Err(error) => warn!("Unable to send test message to client: {}", error),
38 }
39 }
40 tokio::time::sleep(Duration::from_secs(1)).await;
41
42 if let Err(error) = shutdown_tx.send(true) {
44 error!("Unable to send shutdown signal: {}", error)
45 }
46 tokio::time::sleep(Duration::from_secs(1)).await;
47 });
48
49 start_client(config, interface_rx, Some(shutdown_rx), None).await?;
50
51 Ok(())
52}
53
54fn get_test_messages(msg_count: usize, notification_name: &str) -> Vec<ClientReadyMessage> {
55 let mut messages = Vec::with_capacity(msg_count);
56
57 for n in 1..=msg_count {
58 let time = SystemTime::now().duration_since(UNIX_EPOCH).unwrap_or_default().as_nanos();
59 let msg = format!("Simple Client test message test message #{}: {}", n, time);
60 messages.push(Message::new(msg).to_client_ready_message(notification_name))
61 }
62 messages
63}