-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclock.go
More file actions
58 lines (50 loc) · 1.09 KB
/
clock.go
File metadata and controls
58 lines (50 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package ooo
import (
"fmt"
"net/http"
"strconv"
"time"
"github.com/benitogf/ooo/monotonic"
)
// Time returns a string timestamp using the monotonic clock
func Time() string {
now := monotonic.Now()
return strconv.FormatInt(now, 10)
}
func (server *Server) sendTime() {
server.Stream.BroadcastClock(Time())
}
func (server *Server) startClock() {
defer server.clockWg.Done()
ticker := time.NewTicker(server.Tick)
defer ticker.Stop()
for {
select {
case <-server.clockStop:
return
case <-ticker.C:
if server.Active() {
server.sendTime()
} else {
return
}
}
}
}
func (server *Server) clock(w http.ResponseWriter, r *http.Request) {
if !server.Audit(r) {
w.WriteHeader(http.StatusUnauthorized)
fmt.Fprintf(w, "%s", ErrNotAuthorized)
server.Console.Err("socketConnectionUnauthorized time")
return
}
server.handlerWg.Add(1)
defer server.handlerWg.Done()
client, err := server.Stream.New("", w, r, nil, 0)
if err != nil {
return
}
// Clock uses raw text format, not the JSON envelope
server.Stream.WriteClock(client, Time())
server.Stream.Read("", client)
}