@@ -7,64 +7,72 @@ use moq_transport::serve::{
77use chrono:: prelude:: * ;
88use tokio:: task;
99
10+ /// Publishes the current time every second in the format "YYYY-MM-DD HH:MM:SS"
1011pub struct Publisher {
11- track : SubgroupsWriter ,
12+ track_subgroups_writer : SubgroupsWriter ,
1213}
1314
1415impl Publisher {
15- pub fn new ( track : SubgroupsWriter ) -> Self {
16- Self { track }
16+ pub fn new ( track_subgroups_writer : SubgroupsWriter ) -> Self {
17+ Self {
18+ track_subgroups_writer,
19+ }
1720 }
1821
22+ /// Runs the publisher, sending the current time every second. Creates a new group for each minute.
1923 pub async fn run ( mut self ) -> anyhow:: Result < ( ) > {
2024 let start = Utc :: now ( ) ;
2125 let mut now = start;
2226
2327 // Just for fun, don't start at zero.
24- let mut sequence = start. minute ( ) ;
28+ let mut next_group_id = start. minute ( ) ;
2529
30+ // Create a new group for each minute.
2631 loop {
27- let segment = self
28- . track
32+ let subgroup_writer = self
33+ . track_subgroups_writer
2934 . create ( Subgroup {
30- group_id : sequence as u64 ,
35+ group_id : next_group_id as u64 ,
3136 subgroup_id : 0 ,
3237 priority : 0 ,
3338 } )
3439 . context ( "failed to create minute segment" ) ?;
3540
36- sequence += 1 ;
41+ next_group_id += 1 ;
3742
43+ // Spawn a new task to handle sending the object every second
3844 tokio:: spawn ( async move {
39- if let Err ( err) = Self :: send_segment ( segment , now) . await {
45+ if let Err ( err) = Self :: send_subgroup_objects ( subgroup_writer , now) . await {
4046 log:: warn!( "failed to send minute: {:?}" , err) ;
4147 }
4248 } ) ;
4349
4450 let next = now + chrono:: Duration :: try_minutes ( 1 ) . unwrap ( ) ;
4551 let next = next. with_second ( 0 ) . unwrap ( ) . with_nanosecond ( 0 ) . unwrap ( ) ;
4652
53+ // Sleep until the start of the next minute
4754 let delay = ( next - now) . to_std ( ) . unwrap ( ) ;
4855 tokio:: time:: sleep ( delay) . await ;
4956
5057 now = next; // just assume we didn't undersleep
5158 }
5259 }
5360
54- async fn send_segment (
55- mut segment : SubgroupWriter ,
61+ /// Sends the current time every second within a minute group.
62+ async fn send_subgroup_objects (
63+ mut subgroup_writer : SubgroupWriter ,
5664 mut now : DateTime < Utc > ,
5765 ) -> anyhow:: Result < ( ) > {
5866 // Everything but the second.
5967 let base = now. format ( "%Y-%m-%d %H:%M:" ) . to_string ( ) ;
6068
61- segment
69+ subgroup_writer
6270 . write ( base. clone ( ) . into ( ) )
6371 . context ( "failed to write base" ) ?;
6472
6573 loop {
6674 let delta = now. format ( "%S" ) . to_string ( ) ;
67- segment
75+ subgroup_writer
6876 . write ( delta. clone ( ) . into ( ) )
6977 . context ( "failed to write delta" ) ?;
7078
@@ -73,6 +81,7 @@ impl Publisher {
7381 let next = now + chrono:: Duration :: try_seconds ( 1 ) . unwrap ( ) ;
7482 let next = next. with_nanosecond ( 0 ) . unwrap ( ) ;
7583
84+ // Sleep until the next second
7685 let delay = ( next - now) . to_std ( ) . unwrap ( ) ;
7786 tokio:: time:: sleep ( delay) . await ;
7887
@@ -86,26 +95,35 @@ impl Publisher {
8695 }
8796 }
8897}
98+
99+ /// Subscribes to the clock and prints received time updates to stdout.
89100pub struct Subscriber {
90- track : TrackReader ,
101+ track_reader : TrackReader ,
91102}
92103
93104impl Subscriber {
94- pub fn new ( track : TrackReader ) -> Self {
95- Self { track }
105+ pub fn new ( track_reader : TrackReader ) -> Self {
106+ Self { track_reader }
96107 }
97108
109+ /// Runs the subscriber, receiving time updates and printing them to stdout.
98110 pub async fn run ( self ) -> anyhow:: Result < ( ) > {
99- match self . track . mode ( ) . await . context ( "failed to get mode" ) ? {
111+ match self
112+ . track_reader
113+ . mode ( )
114+ . await
115+ . context ( "failed to get mode" ) ?
116+ {
100117 TrackReaderMode :: Stream ( stream) => Self :: recv_stream ( stream) . await ,
101118 TrackReaderMode :: Subgroups ( subgroups) => Self :: recv_subgroups ( subgroups) . await ,
102119 TrackReaderMode :: Datagrams ( datagrams) => Self :: recv_datagrams ( datagrams) . await ,
103120 }
104121 }
105122
106- async fn recv_stream ( mut track : StreamReader ) -> anyhow:: Result < ( ) > {
107- while let Some ( mut subgroup) = track. next ( ) . await ? {
108- while let Some ( object) = subgroup. read_next ( ) . await ? {
123+ /// Receives time updates from a stream and prints them to stdout.
124+ async fn recv_stream ( mut stream_reader : StreamReader ) -> anyhow:: Result < ( ) > {
125+ while let Some ( mut stream_group_reader) = stream_reader. next ( ) . await ? {
126+ while let Some ( object) = stream_group_reader. read_next ( ) . await ? {
109127 let str = String :: from_utf8_lossy ( & object) ;
110128 println ! ( "{str}" ) ;
111129 }
@@ -114,20 +132,22 @@ impl Subscriber {
114132 Ok ( ( ) )
115133 }
116134
117- async fn recv_subgroups ( mut subgroups : SubgroupsReader ) -> anyhow:: Result < ( ) > {
118- while let Some ( mut subgroup) = subgroups. next ( ) . await ? {
119- // Spawn a new task to handle the subgroup concurrently
135+ /// Receives time updates from subgroups and prints them to stdout.
136+ async fn recv_subgroups ( mut subgroups_reader : SubgroupsReader ) -> anyhow:: Result < ( ) > {
137+ while let Some ( mut subgroup_reader) = subgroups_reader. next ( ) . await ? {
138+ // Spawn a new task to handle the subgroup concurrently, so we
139+ // don't rely on the publisher ending the previous stream before starting a new one.
120140 task:: spawn ( async move {
121141 if let Err ( e) = async {
122- let base = subgroup
142+ let base = subgroup_reader
123143 . read_next ( )
124144 . await
125145 . context ( "failed to get first object" ) ?
126146 . context ( "empty subgroup" ) ?;
127147
128148 let base = String :: from_utf8_lossy ( & base) ;
129149
130- while let Some ( object) = subgroup . read_next ( ) . await ? {
150+ while let Some ( object) = subgroup_reader . read_next ( ) . await ? {
131151 let str = String :: from_utf8_lossy ( & object) ;
132152 println ! ( "{base}{str}" ) ;
133153 }
@@ -144,8 +164,9 @@ impl Subscriber {
144164 Ok ( ( ) )
145165 }
146166
147- async fn recv_datagrams ( mut datagrams : DatagramsReader ) -> anyhow:: Result < ( ) > {
148- while let Some ( datagram) = datagrams. read ( ) . await ? {
167+ /// Receives time updates from datagrams and prints them to stdout.
168+ async fn recv_datagrams ( mut datagrams_reader : DatagramsReader ) -> anyhow:: Result < ( ) > {
169+ while let Some ( datagram) = datagrams_reader. read ( ) . await ? {
149170 let str = String :: from_utf8_lossy ( & datagram. payload ) ;
150171 println ! ( "{str}" ) ;
151172 }
0 commit comments