66//! follow follow ws://HOST:4141/vox 30 # follows for 30 s, then reports
77//! ```
88//!
9- //! The leader serves `session::clock::SessionClock` and Architect's
10- //! presence (a `DocRegistry`'s ) over vox on a WebSocket
9+ //! The leader serves Architect's presence (a `DocRegistry`'s, which also
10+ //! serves its clock: `DocPresence::now` ) over vox on a WebSocket
1111//! (`architect::host::EngineHost`), joins its own presence as peer
1212//! `lead`, and plays a loop: play from 0 at 1 s, seek to 42 s at 5 s, stop
1313//! at 8 s, every 10 s. It is the session host, so its clock is the shared
14- //! clock and its `PlayState::at_ms` is stamped in it.
14+ //! clock, and `Here::play_now` stamps its `PlayState::at_ms` in it.
1515//!
16- //! The follower dials it, pings the clock ten times a second
17- //! (`SharedClock::run `), reads `lead/play` from its presence , and draws
16+ //! The follower dials it (its presence driver pings the host's clock ten
17+ //! times a second into `PresencePeer::clock `), reads `lead/play`, and draws
1818//! the playhead with `sync::Tracker` every 16 ms. It reports the clock
1919//! estimate and how far its drawn playhead was from the leader's.
2020//!
@@ -52,15 +52,16 @@ use crdt::sync::{
5252 DocPresenceClient , DocPresenceDispatcher , PresencePeer , doc_presence_service_descriptor,
5353} ;
5454use session:: Transport ;
55- use session:: clock:: {
56- ClockHost , SessionClockClient , SharedClock , serve as serve_clock,
57- session_clock_rpc_service_descriptor,
58- } ;
5955use session:: doc:: transport_of;
6056use session:: sync:: Tracker ;
61- use session:: sync:: clock:: now_micros_f64;
6257use uuid:: Uuid ;
6358
59+ /// This process's clock, microseconds: the one the presence clock's
60+ /// offset is to (`crdt::presence::clock_ms`).
61+ fn now_micros_f64 ( ) -> f64 {
62+ crdt:: presence:: clock_ms ( ) * 1000.0
63+ }
64+
6465/// The one presence channel both join: a fixed document id.
6566const DOC : Uuid = Uuid :: from_u128 ( 0x5e55_1011_0000_4000_8000_0000_0000_0001 ) ;
6667
@@ -89,15 +90,10 @@ async fn join(url: &str, me: &str) -> PresencePeer {
8990
9091async fn lead ( bind : & str ) {
9192 let registry = DocRegistry :: new ( |_| Box :: pin ( async { Ok ( CrdtDoc :: ephemeral ( ) ) } ) ) ;
92- let router = LayerRouter :: new ( )
93- . with (
94- doc_presence_service_descriptor ( ) ,
95- DocPresenceDispatcher :: new ( registry) ,
96- )
97- . with (
98- session_clock_rpc_service_descriptor ( ) ,
99- serve_clock ( ClockHost ) ,
100- ) ;
93+ let router = LayerRouter :: new ( ) . with (
94+ doc_presence_service_descriptor ( ) ,
95+ DocPresenceDispatcher :: new ( registry) ,
96+ ) ;
10197 let bind = bind. to_owned ( ) ;
10298 tokio:: spawn ( async move {
10399 EngineHost :: ungated ( router, bind)
@@ -119,7 +115,11 @@ async fn lead(bind: &str) {
119115 "lead: serving on {url}; epoch {:.0} µs" ,
120116 epoch_wall_micros( )
121117 ) ;
122- // The host's clock is the shared clock: its stamps need no offset.
118+ // Stamped in the host's clock by the peer's own estimate of it (the
119+ // leader is the host here, so the estimate is its own clock).
120+ while !here. play_now ( false , 0.0 , 1.0 ) {
121+ tokio:: time:: sleep ( Duration :: from_millis ( 50 ) ) . await ;
122+ }
123123 let mut cycle = 0_u32 ;
124124 loop {
125125 let start = now_micros_f64 ( ) ;
@@ -132,13 +132,7 @@ async fn lead(bind: &str) {
132132 if wait > 0.0 {
133133 tokio:: time:: sleep ( Duration :: from_secs_f64 ( wait / 1e6 ) ) . await ;
134134 }
135- let now = now_micros_f64 ( ) ;
136- here. play ( & crdt:: presence:: PlayState {
137- playing,
138- position,
139- at_ms : now / 1000.0 ,
140- rate : 1.0 ,
141- } ) ;
135+ here. play_now ( playing, position, 1.0 ) ;
142136 println ! ( "lead: cycle {cycle} {what} at {position} s" ) ;
143137 }
144138 let rest = start + 10e6 - now_micros_f64 ( ) ;
@@ -149,16 +143,8 @@ async fn lead(bind: &str) {
149143
150144#[ allow( clippy:: too_many_lines) ]
151145async fn follow ( url : & str , seconds : f64 ) {
152- let clock = SharedClock :: default ( ) ;
153- let host: SessionClockClient = pool ( )
154- . client ( & Endpoint :: url ( url) )
155- . await
156- . expect ( "dial clock" ) ;
157- let pinger = clock. clone ( ) ;
158- tokio:: spawn ( async move {
159- let _ = pinger. run ( & host) . await ;
160- } ) ;
161146 let peer = join ( url, "follow" ) . await ;
147+ let clock = peer. clock ( ) . clone ( ) ;
162148 let mut tracker = Tracker :: default ( ) ;
163149 let mut errors: Vec < f64 > = Vec :: new ( ) ;
164150 let mut offset_errors: Vec < f64 > = Vec :: new ( ) ;
@@ -170,7 +156,7 @@ async fn follow(url: &str, seconds: f64) {
170156 tokio:: time:: sleep ( Duration :: from_millis ( 16 ) ) . await ;
171157 let states = peer. states ( ) ;
172158 let ( Some ( offset) , Some ( leader) , Some ( lead_epoch) ) = (
173- clock. offset_micros ( ) ,
159+ clock. offset_ms ( ) . map ( |ms| ms * 1000.0 ) ,
174160 states. get ( "lead/play" ) . and_then ( transport_of) ,
175161 states
176162 . get ( "lead/epoch" )
@@ -197,7 +183,7 @@ async fn follow(url: &str, seconds: f64) {
197183 offset_errors. push ( ( offset - true_offset) . abs ( ) ) ;
198184 }
199185 }
200- let rtt = clock. round_trip_micros ( ) . unwrap_or ( f64:: NAN ) ;
186+ let rtt = clock. round_trip_ms ( ) . map_or ( f64:: NAN , |ms| ms * 1000.0 ) ;
201187 let stat = |v : & mut Vec < f64 > | -> ( f64 , f64 , f64 ) {
202188 v. sort_by ( f64:: total_cmp) ;
203189 if v. is_empty ( ) {
0 commit comments