comparison clock.ur @ 11:ccd0a169e827

Clock corrects for skew, so that it matches server time (if an RPC round-trips fast enough)
author Adam Chlipala <adam@chlipala.net>
date Sun, 24 Jul 2011 14:51:16 -0400
parents 0337f88f2efc
children 2947170fcfd6
comparison
equal deleted inserted replaced
10:0337f88f2efc 11:ccd0a169e827
1 (** Reactive view of the current time *) 1 (** Reactive view of the current time *)
2 2
3 type t = { Source : source time, 3 type t = {
4 Period : int } 4 Source : source time, (* Server time as of last tick event *)
5 Skew : source int, (* How many seconds do we add to the local clock to match the server clock? *)
6 Period : int (* How many milliseconds between ticks? *)
7 }
5 8
6 fun create ms = 9 fun create ms =
7 tm <- now; 10 tm <- now;
8 t <- source tm; 11 t <- source tm;
9 return {Source = t, Period = ms} 12 sk <- source 0;
13 return {Source = t, Skew = sk, Period = ms}
10 14
11 fun start t = 15 fun start t =
12 let 16 let
13 fun loop () = 17 fun loop () =
14 sleep t.Period; 18 sleep t.Period;
15 tm <- now; 19 tm <- now;
16 set t.Source tm; 20 sk <- get t.Skew;
21 set t.Source (addSeconds tm sk);
17 loop () 22 loop ()
23
24 fun serverTime () = now
18 in 25 in
26 spawn (server <- rpc (serverTime ());
27 local <- now;
28 set t.Skew (diffInSeconds server local));
19 spawn (loop ()) 29 spawn (loop ())
20 end 30 end
21 31
22 fun signal t = Basis.signal t.Source 32 fun signal t = Basis.signal t.Source