-
Notifications
You must be signed in to change notification settings - Fork 2
/
device_watcher.go
238 lines (197 loc) · 6.58 KB
/
device_watcher.go
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package adb
import (
"errors"
"fmt"
"log"
"math/rand"
"runtime"
"strings"
"sync/atomic"
"time"
"github.com/prife/goadb/wire"
)
// DeviceWatcher publishes device status change events.
// If the server dies while listening for events, it restarts the server.
type DeviceWatcher struct {
*deviceWatcherImpl
}
// DeviceStateChangedEvent represents a device state transition.
// Contains the device’s old and new states, but also provides methods to query the
// type of state transition.
type DeviceStateChangedEvent struct {
Serial string
OldState DeviceState
NewState DeviceState
}
// CameOnline returns true if this event represents a device coming online.
func (s DeviceStateChangedEvent) CameOnline() bool {
return s.OldState != StateOnline && s.NewState == StateOnline
}
// WentOffline returns true if this event represents a device going offline.
func (s DeviceStateChangedEvent) WentOffline() bool {
return s.OldState == StateOnline && s.NewState != StateOnline
}
type deviceWatcherImpl struct {
server server
// If an error occurs, it is stored here and eventChan is close immediately after.
err atomic.Value
eventChan chan DeviceStateChangedEvent
}
func newDeviceWatcher(server server) *DeviceWatcher {
watcher := &DeviceWatcher{&deviceWatcherImpl{
server: server,
eventChan: make(chan DeviceStateChangedEvent),
}}
runtime.SetFinalizer(watcher, func(watcher *DeviceWatcher) {
watcher.Shutdown()
})
go publishDevices(watcher.deviceWatcherImpl)
return watcher
}
// C returns a channel than can be received on to get events.
// If an unrecoverable error occurs, or Shutdown is called, the channel will be closed.
func (w *DeviceWatcher) C() <-chan DeviceStateChangedEvent {
return w.eventChan
}
// Err returns the error that caused the channel returned by C to be closed, if C is closed.
// If C is not closed, its return value is undefined.
func (w *DeviceWatcher) Err() error {
if err, ok := w.err.Load().(error); ok {
return err
}
return nil
}
// Shutdown stops the watcher from listening for events and closes the channel returned
// from C.
func (w *DeviceWatcher) Shutdown() {
// TODO(z): Implement.
}
func (w *deviceWatcherImpl) reportErr(err error) {
w.err.Store(err)
}
// publishDevices reads device lists from scanner, calculates diffs, and publishes events on
// eventChan.
// Returns when scanner returns an error.
// Doesn't refer directly to a *DeviceWatcher so it can be GCed (which will,
// in turn, close Scanner and stop this goroutine).
//
// TODO: to support shutdown, spawn a new goroutine each time a server connection is established.
// This goroutine should read messages and send them to a message channel. Can write errors directly
// to errVal. publishDevicesUntilError should take the msg chan and the scanner and select on the msg chan and stop chan, and if the stop
// chan sends, close the scanner and return true. If the msg chan closes, just return false.
// publishDevices can look at ret val: if false and err == EOF, reconnect. If false and other error, report err
// and abort. If true, report no error and stop.
func publishDevices(watcher *deviceWatcherImpl) {
defer close(watcher.eventChan)
var lastKnownStates map[string]DeviceState
finished := false
for {
scanner, err := connectToTrackDevices(watcher.server)
if err != nil {
watcher.reportErr(err)
return
}
finished, err = publishDevicesUntilError(scanner, watcher.eventChan, &lastKnownStates)
if finished {
scanner.Close()
return
}
if errors.Is(err, wire.ErrConnectionReset) {
// The server died, restart and reconnect.
if realServer, ok := watcher.server.(*realServer); ok {
if !realServer.config.AutoStart {
watcher.reportErr(fmt.Errorf("server killed"))
return
}
}
// report all devices removed
for serial, deviceState := range lastKnownStates {
watcher.eventChan <- DeviceStateChangedEvent{serial, deviceState, StateDisconnected}
}
lastKnownStates = nil
// Delay by a random [0ms, 500ms) in case multiple DeviceWatchers are trying to
// start the same server.
delay := time.Duration(rand.Intn(500)) * time.Millisecond
log.Printf("[DeviceWatcher] server died, restarting in %s…", delay)
time.Sleep(delay)
if err := watcher.server.Start(); err != nil {
log.Println("[DeviceWatcher] error restarting server, giving up")
watcher.reportErr(err)
return
} // Else server should be running, continue listening.
} else {
// Unknown error, don't retry.
watcher.reportErr(err)
return
}
}
}
func connectToTrackDevices(server server) (wire.Scanner, error) {
conn, err := server.Dial()
if err != nil {
return nil, err
}
if err := conn.SendMessage([]byte("host:track-devices")); err != nil {
conn.Close()
return nil, err
}
if _, err := conn.ReadStatus("host:track-devices"); err != nil {
conn.Close()
return nil, err
}
return conn, nil
}
func publishDevicesUntilError(scanner wire.Scanner, eventChan chan<- DeviceStateChangedEvent, lastKnownStates *map[string]DeviceState) (finished bool, err error) {
for {
msg, err := scanner.ReadMessage()
if err != nil {
return false, err
}
deviceStates, err := parseDeviceStates(string(msg))
if err != nil {
return false, err
}
for _, event := range calculateStateDiffs(*lastKnownStates, deviceStates) {
eventChan <- event
}
*lastKnownStates = deviceStates
}
}
func parseDeviceStates(msg string) (states map[string]DeviceState, err error) {
states = make(map[string]DeviceState)
for lineNum, line := range strings.Split(msg, "\n") {
if len(line) == 0 {
continue
}
fields := strings.Split(line, "\t")
if len(fields) != 2 {
err = fmt.Errorf("%w: invalid device state line %d: %s", wire.ErrParse, lineNum, line)
return
}
serial, stateString := fields[0], fields[1]
var state DeviceState
state, err = parseDeviceState(stateString)
states[serial] = state
}
return
}
func calculateStateDiffs(oldStates, newStates map[string]DeviceState) (events []DeviceStateChangedEvent) {
for serial, newState := range newStates {
if oldState, ok := oldStates[serial]; ok {
// Device present in both lists: state changed.
if oldState != newState {
events = append(events, DeviceStateChangedEvent{serial, oldState, newState})
}
} else {
// Device only present in new list: device added.
events = append(events, DeviceStateChangedEvent{serial, StateDisconnected, newState})
}
}
for serial, oldState := range oldStates {
if _, ok := newStates[serial]; !ok {
// Device only present in old list: device removed.
events = append(events, DeviceStateChangedEvent{serial, oldState, StateDisconnected})
}
}
return events
}