-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync.go
More file actions
197 lines (166 loc) · 4.88 KB
/
sync.go
File metadata and controls
197 lines (166 loc) · 4.88 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package main
import (
"sort"
"sync"
"time"
)
// SyncHandler constantly checks and attempts to sync
// client timestamps.
func (p *Pool) SyncHandler() {
for {
// Only sync when the global state is Playing
if p.State != Playing {
// Wait until a new state is set
<-p.StateInformer
continue
}
p.sortClients()
p.syncClients()
time.Sleep(CheckInterval)
}
}
// PauseHandler monitors the global notification channel
// and pauses/resumes all clients when someone pauses or resumes.
func (p *Pool) PauseHandler() {
for {
notif := <-p.Notification
var play bool
switch notif {
case "Player.OnPause":
LogInfo("Global pause triggered")
play = false
p.State = Paused
case "Player.OnResume":
LogInfo("Global play triggered")
play = true
p.State = Playing
default:
LogInfo("Unknown notification", notif)
}
var wg sync.WaitGroup
for _, c := range p.Clients {
c.ignoreStateNotification(play)
wg.Add(1)
go c.RequestWorker(c.playPayload(play), &wg)
}
// Wait until all clients are done and
// inform every listener (if any) of a pause/resume
wg.Wait()
select {
case p.StateInformer <- p.State:
default:
}
}
}
// Play plays and pauses a client depending on
// boolean play.
func (c *Client) Play(play bool) {
c.SendChannel <- c.playPayload(play)
}
// sortClients fetches all timestamps from each client,
// sets them and sorts clients by timestamp low->high
// (most behind->most ahead player).
func (p *Pool) sortClients() {
var wg sync.WaitGroup
for _, c := range p.Clients {
params := map[string]interface{}{
"playerid": DefaultPlayerID,
"properties": []string{"time"},
}
payload := NewBaseSend("Player.GetProperties", params, PlayerGetPropertiesTime)
wg.Add(1)
go c.RequestWorker(payload, &wg)
}
// Wait until all responses are received and sort the clients
wg.Wait()
// LogInfo("All workers finished, sorting")
sort.Sort(ByTimestamp(p.Clients))
}
// ignoreStateNotification is used to ignore a play/pause notification based on the
// current state of the client. The purpose of this is to avoid
// ignoring too many notifications since the client only sends an notification
// if the action was successful, e.g. if the client is playing and a
// pause request was sent.
// Play = true means ignore a play notification and false means ignore a pause
// notification.
func (c *Client) ignoreStateNotification(play bool) {
params := map[string]interface{}{
"playerid": DefaultPlayerID,
"properties": []string{"speed"},
}
payload := NewBaseSend("Player.GetProperties", params, PlayerGetPropertiesSpeed)
var wg sync.WaitGroup
wg.Add(1)
go c.RequestWorker(payload, &wg)
wg.Wait()
// Paused and ignoring play
if c.State == 0 && play {
c.IgnoreCount++
}
// Playing and ignoring pause
if c.State == 1 && !play {
c.IgnoreCount++
}
}
// playPayload creates a payload used for playing/pausing
// the client.
func (c *Client) playPayload(play bool) BaseSend {
params := map[string]interface{}{
"playerid": DefaultPlayerID,
"play": play,
}
return NewBaseSend("Player.PlayPause", params, 0)
}
// syncClients pauses the clients in a Pool (most behind to most ahead)
// in a way that they all sync up. Returns when all clients are synced up.
func (p *Pool) syncClients() {
// Only sync when most ahead client - most behind > MaxDiff
if p.Clients[len(p.Clients)-1].TimeDifference(p.Clients[0]) < MaxDiff {
LogInfo("Not enough desync, do nothing")
return
}
// Used to determine when all clients (except the one most behind)
// have been sent a pause signal
var wgDone sync.WaitGroup
// Play client behind (in case it is paused)
// (This will trigger a global play in PauseHandler which is fine since we're
// pausing them anyway)
p.Clients[0].Play(true)
// Pause all clients ahead
for i := 1; i < len(p.Clients); i++ {
ahead := p.Clients[i]
timeDiff := ahead.TimeDifference(p.Clients[0])
LogInfof("%s desync between %s and %s\n", timeDiff.String(),
p.Clients[0].Description(), ahead.Description())
wgDone.Add(1)
go ahead.pauseClient(timeDiff, &wgDone)
}
// Wait until all clients are done syncing
wgDone.Wait()
LogInfo("Syncing completed")
}
// pauseClient pauses a client for duration amount of time.
func (c *Client) pauseClient(duration time.Duration, wgDone *sync.WaitGroup) {
defer wgDone.Done()
// Pause
c.ignoreStateNotification(false) // Ignore the pause notification
LogInfof("Pausing %s for %s\n", c.Description(), duration.String())
var wg sync.WaitGroup
wg.Add(1)
go c.RequestWorker(c.playPayload(false), &wg)
wg.Wait()
// Wait until the pause duration has passed
// or until the global state has been changed
select {
case <-time.After(duration):
break
case <-c.Pool.StateInformer:
return
}
// Play
c.ignoreStateNotification(true) // Ignore the play notification
wg.Add(1)
go c.RequestWorker(c.playPayload(true), &wg)
wg.Wait()
LogInfof("Unpausing %s\n", c.Description())
}