forked from u-root/u-root
-
Notifications
You must be signed in to change notification settings - Fork 2
/
service.go
224 lines (195 loc) · 5.18 KB
/
service.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
// Copyright 2018 the u-root Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"github.com/u-root/u-root/pkg/wifi"
)
// Exported Constants
var (
DefaultBufferSize = 4
)
// Exported Types
type State struct {
CurEssid string
ConnectingEssid string
Refreshing bool
NearbyWifis []wifi.Option
}
type ConnectReqMsg struct {
// private channel that the requesting routine is listening on
c chan (error)
args []string
}
type RefreshReqMsg chan error
type StateReqMsg chan State
type WifiService struct {
// Share Resource between goroutines
wifiWorker wifi.WiFi
// Communicating Channels between internal goroutines
connectArbitratorQuit chan bool
refreshPoolerQuit chan bool
stateTrackerQuit chan bool
stateUpdateChan chan stateUpdateMsg
// Communicating Channels with Server goroutines
connectReqChan chan ConnectReqMsg
refreshReqChan chan RefreshReqMsg
stateReqChan chan StateReqMsg
}
// Internal type
type stateComponent int
const (
curEssidComp stateComponent = iota
connectingEssidComp
refreshingComp
nearbyWifisComp
)
type stateUpdateMsg struct {
key stateComponent
val interface{}
doneUpdate chan bool // Used when need to ensure happening before relationship
}
func (ws WifiService) startConnectWifiArbitrator() {
curEssid, connectingEssid := "", ""
workDone := make(chan error, 1)
var winningChan chan error
for {
select {
case req := <-ws.connectReqChan:
if connectingEssid == "" {
// The requesting routine wins
connectingEssid = req.args[0]
ws.stateUpdateChan <- stateUpdateMsg{connectingEssidComp, connectingEssid, nil}
winningChan = req.c
// Starts connection
go func(args ...string) {
workDone <- ws.wifiWorker.Connect(args...)
}(req.args...)
} else {
// The requesting routine loses
req.c <- fmt.Errorf("Service is trying to connect to %s", connectingEssid)
}
case err := <-workDone:
// Update states
if err != nil {
curEssid, _ = ws.wifiWorker.GetID()
} else {
curEssid = connectingEssid
}
doneUpdate := make(chan bool, 2)
ws.stateUpdateChan <- stateUpdateMsg{curEssidComp, curEssid, doneUpdate}
connectingEssid = ""
ws.stateUpdateChan <- stateUpdateMsg{connectingEssidComp, connectingEssid, doneUpdate}
<-doneUpdate
<-doneUpdate
winningChan <- err
case <-ws.connectArbitratorQuit:
return
}
}
}
func (ws WifiService) startRefreshPooler() {
workDone := make(chan bool, 1)
pool := make(chan RefreshReqMsg, DefaultBufferSize)
refreshing := false
// Pooler
for {
select {
case req := <-ws.refreshReqChan:
if !refreshing {
refreshing = true
ws.stateUpdateChan <- stateUpdateMsg{refreshingComp, refreshing, nil}
// Notifier
go func(p chan RefreshReqMsg) {
o, err := ws.wifiWorker.Scan()
doneUpdate := make(chan bool, 1)
ws.stateUpdateChan <- stateUpdateMsg{nearbyWifisComp, o, doneUpdate}
<-doneUpdate
workDone <- true
for ch := range p {
ch <- err
}
}(pool)
}
pool <- req
case <-workDone:
close(pool)
refreshing = false
ws.stateUpdateChan <- stateUpdateMsg{refreshingComp, refreshing, nil}
pool = make(chan RefreshReqMsg, DefaultBufferSize)
case <-ws.refreshPoolerQuit:
return
}
}
}
func (ws WifiService) startStateTracker() {
state := State{
CurEssid: "",
ConnectingEssid: "",
Refreshing: false,
NearbyWifis: nil,
}
for {
select {
case r := <-ws.stateReqChan:
r <- state
case updateMsg := <-ws.stateUpdateChan:
updateState(&state, updateMsg)
if updateMsg.doneUpdate != nil {
updateMsg.doneUpdate <- true
}
case <-ws.stateTrackerQuit:
return
}
}
}
func updateState(state *State, update stateUpdateMsg) {
switch update.key {
case curEssidComp:
state.CurEssid = update.val.(string)
case connectingEssidComp:
state.ConnectingEssid = update.val.(string)
case refreshingComp:
state.Refreshing = update.val.(bool)
case nearbyWifisComp:
state.NearbyWifis = update.val.([]wifi.Option)
}
}
func NewWifiService(w wifi.WiFi) (*WifiService, error) {
return &WifiService{
wifiWorker: w,
connectArbitratorQuit: make(chan bool, 1),
refreshPoolerQuit: make(chan bool, 1),
stateTrackerQuit: make(chan bool, 1),
stateUpdateChan: make(chan stateUpdateMsg, 4),
connectReqChan: make(chan ConnectReqMsg, DefaultBufferSize),
refreshReqChan: make(chan RefreshReqMsg, DefaultBufferSize),
stateReqChan: make(chan StateReqMsg, DefaultBufferSize),
}, nil
}
func (ws WifiService) Start() {
go ws.startConnectWifiArbitrator()
go ws.startRefreshPooler()
go ws.startStateTracker()
}
func (ws WifiService) Shutdown() {
ws.connectArbitratorQuit <- true
ws.refreshPoolerQuit <- true
ws.stateTrackerQuit <- true
}
func (ws WifiService) GetState() State {
c := make(chan State, 1)
ws.stateReqChan <- (c)
return <-c
}
func (ws WifiService) Connect(args []string) error {
c := make(chan error, 1)
ws.connectReqChan <- ConnectReqMsg{c, args}
return <-c
}
func (ws WifiService) Refresh() error {
c := make(chan error, 1)
ws.refreshReqChan <- (c)
return <-c
}