| 1 |
/* |
| 2 |
Copyright 2013-2015 Canonical Ltd. |
| 3 |
|
| 4 |
This program is free software: you can redistribute it and/or modify it |
| 5 |
under the terms of the GNU General Public License version 3, as published |
| 6 |
by the Free Software Foundation. |
| 7 |
|
| 8 |
This program is distributed in the hope that it will be useful, but |
| 9 |
WITHOUT ANY WARRANTY; without even the implied warranties of |
| 10 |
MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR |
| 11 |
PURPOSE. See the GNU General Public License for more details. |
| 12 |
|
| 13 |
You should have received a copy of the GNU General Public License along |
| 14 |
with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
*/ |
| 16 |
|
| 17 |
// Package connectivity implements a single, simple stream of booleans |
| 18 |
// to answer the question "are we connected?". |
| 19 |
// |
| 20 |
// It can potentially fire two falses in a row, if a disconnected |
| 21 |
// state is followed by a dbus watch error. Other than that, it's edge |
| 22 |
// triggered. |
| 23 |
package connectivity |
| 24 |
|
| 25 |
import ( |
| 26 |
"errors" |
| 27 |
"sync" |
| 28 |
"time" |
| 29 |
|
| 30 |
"gitlab.com/ubports/development/core/lomiri-push-service/bus" |
| 31 |
"gitlab.com/ubports/development/core/lomiri-push-service/bus/networkmanager" |
| 32 |
"gitlab.com/ubports/development/core/lomiri-push-service/config" |
| 33 |
"gitlab.com/ubports/development/core/lomiri-push-service/logger" |
| 34 |
"gitlab.com/ubports/development/core/lomiri-push-service/util" |
| 35 |
) |
| 36 |
|
| 37 |
// The configuration for ConnectedState, intended to be populated from a config file. |
| 38 |
type ConnectivityConfig struct { |
| 39 |
// how long to wait after a state change to make sure it's "stable" |
| 40 |
// before acting on it |
| 41 |
StabilizingTimeout config.ConfigTimeDuration `json:"stabilizing_timeout"` |
| 42 |
// How long to wait between online connectivity checks. |
| 43 |
RecheckTimeout config.ConfigTimeDuration `json:"recheck_timeout"` |
| 44 |
// The URL against which to do the connectivity check. |
| 45 |
ConnectivityCheckURL string `json:"connectivity_check_url"` |
| 46 |
// The expected MD5 of the content at the ConnectivityCheckURL |
| 47 |
ConnectivityCheckMD5 string `json:"connectivity_check_md5"` |
| 48 |
// The expected string match of the content at the ConnectivityCheckURL |
| 49 |
ConnectivityCheckString string `json:"connectivity_check_string"` |
| 50 |
} |
| 51 |
|
| 52 |
// ConnectedState helps tracking connectivity. |
| 53 |
type ConnectedState struct { |
| 54 |
networkStateCh <-chan networkmanager.State |
| 55 |
networkConCh <-chan string |
| 56 |
config ConnectivityConfig |
| 57 |
log logger.Logger |
| 58 |
endp bus.Endpoint |
| 59 |
connAttempts uint32 |
| 60 |
webchk Webchecker |
| 61 |
webgetCh chan bool |
| 62 |
currentState networkmanager.State |
| 63 |
lastSent bool |
| 64 |
timer *time.Timer |
| 65 |
doneLck sync.Mutex |
| 66 |
done chan struct{} |
| 67 |
canceled bool |
| 68 |
stateWatch bus.Cancellable |
| 69 |
conWatch bus.Cancellable |
| 70 |
} |
| 71 |
|
| 72 |
// New makes a ConnectedState for connectivity tracking. |
| 73 |
// |
| 74 |
// The endpoint need not be dialed; Track() will Dial() and |
| 75 |
// Close() it as it sees fit. |
| 76 |
func New(endp bus.Endpoint, config ConnectivityConfig, log logger.Logger) *ConnectedState { |
| 77 |
wg := NewWebchecker(config.ConnectivityCheckURL, config.ConnectivityCheckMD5, config.ConnectivityCheckString, 10*time.Second, log) |
| 78 |
return &ConnectedState{ |
| 79 |
config: config, |
| 80 |
log: log, |
| 81 |
endp: endp, |
| 82 |
webchk: wg, |
| 83 |
done: make(chan struct{}), |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
// cancel watches if any |
| 88 |
func (cs *ConnectedState) reset() { |
| 89 |
if cs.stateWatch != nil { |
| 90 |
cs.stateWatch.Cancel() |
| 91 |
cs.stateWatch = nil |
| 92 |
} |
| 93 |
if cs.conWatch != nil { |
| 94 |
cs.conWatch.Cancel() |
| 95 |
cs.conWatch = nil |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
// start connects to the bus, gets the initial NetworkManager state, and sets |
| 100 |
// up the watch. |
| 101 |
func (cs *ConnectedState) start() networkmanager.State { |
| 102 |
var initial networkmanager.State |
| 103 |
var stateCh <-chan networkmanager.State |
| 104 |
var primary string |
| 105 |
var conCh <-chan string |
| 106 |
var err error |
| 107 |
for { |
| 108 |
ar := util.NewAutoRedialer(cs.endp) |
| 109 |
cs.connAttempts += ar.Redial() |
| 110 |
nm := networkmanager.New(cs.endp, cs.log) |
| 111 |
|
| 112 |
cs.reset() |
| 113 |
// set up the watch |
| 114 |
stateCh, cs.stateWatch, err = nm.WatchState() |
| 115 |
if err != nil { |
| 116 |
cs.log.Debugf("failed to set up the state watch: %s", err) |
| 117 |
goto Continue |
| 118 |
} |
| 119 |
|
| 120 |
// Get the current state. |
| 121 |
initial = nm.GetState() |
| 122 |
if initial == networkmanager.Unknown { |
| 123 |
cs.log.Debugf("failed to get state.") |
| 124 |
goto Continue |
| 125 |
} |
| 126 |
cs.log.Debugf("got initial state of %s", initial) |
| 127 |
|
| 128 |
conCh, cs.conWatch, err = nm.WatchPrimaryConnection() |
| 129 |
if err != nil { |
| 130 |
cs.log.Debugf("failed to set up the connection watch: %s", err) |
| 131 |
goto Continue |
| 132 |
} |
| 133 |
|
| 134 |
primary = nm.GetPrimaryConnection() |
| 135 |
cs.log.Debugf("primary connection starts as %#v", primary) |
| 136 |
|
| 137 |
cs.networkStateCh = stateCh |
| 138 |
cs.networkConCh = conCh |
| 139 |
|
| 140 |
return initial |
| 141 |
|
| 142 |
Continue: |
| 143 |
cs.endp.Close() |
| 144 |
time.Sleep(10 * time.Millisecond) // that should cool things |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
var errCanceled = errors.New("canceled") |
| 149 |
|
| 150 |
// step takes one step forwards in the “am I connected?” |
| 151 |
// answering state machine. |
| 152 |
func (cs *ConnectedState) step() (bool, error) { |
| 153 |
stabilizingTimeout := cs.config.StabilizingTimeout.Duration |
| 154 |
recheckTimeout := cs.config.RecheckTimeout.Duration |
| 155 |
log := cs.log |
| 156 |
|
| 157 |
Loop: |
| 158 |
for { |
| 159 |
select { |
| 160 |
case <-cs.done: |
| 161 |
return false, errCanceled |
| 162 |
case <-cs.networkConCh: |
| 163 |
cs.webgetCh = nil |
| 164 |
cs.timer.Reset(stabilizingTimeout) |
| 165 |
if cs.lastSent == true { |
| 166 |
log.Debugf("connectivity: PrimaryConnection changed. lastSent: %v, sending 'disconnected'.", cs.lastSent) |
| 167 |
cs.lastSent = false |
| 168 |
break Loop |
| 169 |
} else { |
| 170 |
log.Debugf("connectivity: PrimaryConnection changed. lastSent: %v, Ignoring.", cs.lastSent) |
| 171 |
} |
| 172 |
|
| 173 |
case v, ok := <-cs.networkStateCh: |
| 174 |
// Handle only disconnecting here, connecting handled under the timer below |
| 175 |
if !ok { |
| 176 |
// tear it all down and start over |
| 177 |
return false, errors.New("got not-OK from StateChanged watch") |
| 178 |
} |
| 179 |
cs.webgetCh = nil |
| 180 |
lastState := cs.currentState |
| 181 |
cs.currentState = v |
| 182 |
// ignore Connecting (followed immediately by "Connected Global") and repeats |
| 183 |
if v != networkmanager.Connecting && lastState != v { |
| 184 |
cs.timer.Reset(stabilizingTimeout) |
| 185 |
log.Debugf("state changed to %s. Assuming disconnect.", v) |
| 186 |
if cs.lastSent == true { |
| 187 |
log.Debugf("connectivity: %s -> %s. lastSent: %v, sending 'disconnected'", lastState, v, cs.lastSent) |
| 188 |
cs.lastSent = false |
| 189 |
break Loop |
| 190 |
} else { |
| 191 |
log.Debugf("connectivity: %s -> %s. lastSent: %v, Ignoring.", lastState, v, cs.lastSent) |
| 192 |
} |
| 193 |
} else { |
| 194 |
log.Debugf("connectivity: %s -> %s. lastSent: %v, Ignoring.", lastState, v, cs.lastSent) |
| 195 |
} |
| 196 |
|
| 197 |
case <-cs.timer.C: |
| 198 |
if cs.currentState == networkmanager.ConnectedGlobal { |
| 199 |
log.Debugf("connectivity: timer signal, state: ConnectedGlobal, checking...") |
| 200 |
// use a buffered channel, otherwise |
| 201 |
// we may leak webcheckers that cannot |
| 202 |
// send their result because we have |
| 203 |
// cleared webgetCh and wont receive |
| 204 |
// on it |
| 205 |
cs.webgetCh = make(chan bool, 1) |
| 206 |
go cs.webchk.Webcheck(cs.webgetCh) |
| 207 |
} |
| 208 |
|
| 209 |
case connected := <-cs.webgetCh: |
| 210 |
cs.timer.Reset(recheckTimeout) |
| 211 |
log.Debugf("connectivity: connection check says: %t", connected) |
| 212 |
cs.webgetCh = nil |
| 213 |
if connected && cs.lastSent == false { |
| 214 |
log.Debugf("connectivity: connection check ok, lastSent: %v, sending 'connected'.", cs.lastSent) |
| 215 |
cs.lastSent = true |
| 216 |
break Loop |
| 217 |
} |
| 218 |
} |
| 219 |
} |
| 220 |
return cs.lastSent, nil |
| 221 |
} |
| 222 |
|
| 223 |
// Track sends the initial NetworkManager state and changes to it |
| 224 |
// over the "out" channel. Sends "false" as soon as it detects trouble, "true" |
| 225 |
// after checking actual connectivity. |
| 226 |
// |
| 227 |
func (cs *ConnectedState) Track(out chan<- bool) { |
| 228 |
|
| 229 |
Start: |
| 230 |
cs.log.Debugf("sending initial 'disconnected'.") |
| 231 |
select { |
| 232 |
case <-cs.done: |
| 233 |
return |
| 234 |
case out <- false: |
| 235 |
} |
| 236 |
cs.lastSent = false |
| 237 |
cs.currentState = cs.start() |
| 238 |
defer cs.reset() |
| 239 |
cs.timer = time.NewTimer(cs.config.StabilizingTimeout.Duration) |
| 240 |
|
| 241 |
for { |
| 242 |
v, err := cs.step() |
| 243 |
if err == errCanceled { |
| 244 |
return |
| 245 |
} |
| 246 |
if err != nil { |
| 247 |
// tear it all down and start over |
| 248 |
cs.log.Errorf("%s", err) |
| 249 |
goto Start |
| 250 |
} |
| 251 |
select { |
| 252 |
case <-cs.done: |
| 253 |
return |
| 254 |
case out <- v: |
| 255 |
} |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
// Cancel stops the ConnectedState machinary. |
| 260 |
func (cs *ConnectedState) Cancel() { |
| 261 |
cs.doneLck.Lock() |
| 262 |
defer cs.doneLck.Unlock() |
| 263 |
if !cs.canceled { |
| 264 |
cs.canceled = true |
| 265 |
close(cs.done) |
| 266 |
cs.webchk.Close() |
| 267 |
} |
| 268 |
} |