| 1 | use std::time::Duration; |
| 2 | |
| 3 | use chatsvc::{ |
| 4 | ActivityReporting, PresenceService, Realtime, RealtimeConfig, RealtimeEvent, StatusKind, |
| 5 | }; |
| 6 | use session::{DEFAULT_ENDPOINT, Session}; |
| 7 | |
| 8 | const CONNECT_TIMEOUT: Duration = Duration::from_secs(60); |
| 9 | const SETTLE: Duration = Duration::from_secs(10); |
| 10 | |
| 11 | #[tokio::main] |
| 12 | async fn main() { |
| 13 | let user_id = std::env::args() |
| 14 | .nth(1) |
| 15 | .expect("usage: presence_endpoint_probe <own user id GUID> [inactive wait seconds]"); |
| 16 | let inactive_wait = std::env::args() |
| 17 | .nth(2) |
| 18 | .and_then(|value| value.parse().ok()) |
| 19 | .map_or(SETTLE, Duration::from_secs); |
| 20 | let endpoint = std::env::var("CDP_ENDPOINT").unwrap_or_else(|_| DEFAULT_ENDPOINT.to_owned()); |
| 21 | let session = Session::connect(&endpoint).await.expect("browser"); |
| 22 | let presence = PresenceService::new(&session); |
| 23 | |
| 24 | let config = RealtimeConfig { |
| 25 | initially_active: true, |
| 26 | ..RealtimeConfig::default() |
| 27 | }; |
| 28 | let mut realtime = Realtime::start_with(&session, config) |
| 29 | .await |
| 30 | .expect("realtime start"); |
| 31 | let endpoint_id = wait_for_endpoint(&mut realtime).await; |
| 32 | println!("# trouter connected"); |
| 33 | |
| 34 | println!( |
| 35 | "register: {}", |
| 36 | outcome( |
| 37 | presence |
| 38 | .register_endpoint(&endpoint_id, ActivityReporting::Transport) |
| 39 | .await |
| 40 | ) |
| 41 | ); |
| 42 | realtime.resend_activity(); |
| 43 | println!("# sent active"); |
| 44 | tokio::time::sleep(SETTLE).await; |
| 45 | print_own_presence(&presence, &user_id).await; |
| 46 | |
| 47 | realtime.set_activity(false); |
| 48 | println!("# sent inactive"); |
| 49 | tokio::time::sleep(inactive_wait).await; |
| 50 | print_own_presence(&presence, &user_id).await; |
| 51 | |
| 52 | println!( |
| 53 | "http report inactive: {}", |
| 54 | outcome(presence.report_activity(&endpoint_id, false).await) |
| 55 | ); |
| 56 | println!( |
| 57 | "unregister: {}", |
| 58 | outcome(presence.unregister_endpoint(&endpoint_id).await) |
| 59 | ); |
| 60 | realtime.stop().await; |
| 61 | println!("# stopped"); |
| 62 | } |
| 63 | |
| 64 | async fn wait_for_endpoint(realtime: &mut Realtime) -> String { |
| 65 | let wait = async { |
| 66 | let mut endpoint_id = None; |
| 67 | let mut connected = false; |
| 68 | while let Some(event) = realtime.recv().await { |
| 69 | match event { |
| 70 | RealtimeEvent::Endpoint(endpoint) => endpoint_id = Some(endpoint.endpoint_id), |
| 71 | RealtimeEvent::Status(status) if status.kind == StatusKind::Connected => { |
| 72 | connected = true |
| 73 | } |
| 74 | _ => {} |
| 75 | } |
| 76 | if connected && let Some(endpoint_id) = endpoint_id.take() { |
| 77 | return endpoint_id; |
| 78 | } |
| 79 | } |
| 80 | panic!("realtime closed before connecting"); |
| 81 | }; |
| 82 | tokio::time::timeout(CONNECT_TIMEOUT, wait) |
| 83 | .await |
| 84 | .expect("trouter did not connect") |
| 85 | } |
| 86 | |
| 87 | async fn print_own_presence(presence: &PresenceService, user_id: &str) { |
| 88 | match presence.status(user_id).await { |
| 89 | Ok(status) => println!( |
| 90 | "own presence: availability={} activity={}", |
| 91 | status.availability, |
| 92 | status.activity.as_deref().unwrap_or("-") |
| 93 | ), |
| 94 | Err(error) => println!("own presence: failed {}", status_of(&error)), |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | fn outcome(result: chatsvc::Result<()>) -> String { |
| 99 | match result { |
| 100 | Ok(()) => "ok".to_owned(), |
| 101 | Err(error) => format!("failed {}", status_of(&error)), |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | fn status_of(error: &chatsvc::Error) -> String { |
| 106 | match error { |
| 107 | chatsvc::Error::Session(session::Error::Api { status, .. }) => format!("http {status}"), |
| 108 | _ => "no http status".to_owned(), |
| 109 | } |
| 110 | } |