blob: ffade53eb50f63b3ebfec65b71433ba076fe84ee [file] [log] [blame]
David Why Use Two When One Will Do Presottof6813ec2014-07-11 16:12:54 -07001// +build plan9 windows
2
3package netconfig
4
5// Code to signal a network change every 2 minutes. We use
6// this for systems where we don't yet have a good way to
7// watch for network changes.
8
9import (
10 "time"
11)
12
13type timerNetConfigWatcher struct {
14 c chan struct{} // channel to signal confg changes
15 stop chan struct{} // channel to tell the watcher to stop
16}
17
18// Stop any waiter
19func (w *timerNetConfigWatcher) Stop() {
20 w.stop <- struct{}{}
21}
22
23func (w *timerNetConfigWatcher) Channel() chan struct{} {
24 return w.c
25}
26
27func (w *timerNetConfigWatcher) watcher() {
28 for {
29 select {
30 case <-w.stop:
31 close(w.c)
32 return
33 case <-time.NewTimer(2 * time.Minute).C:
34 select {
35 case w.c <- struct{}{}:
36 default:
37 }
38 }
39 }
40}
41
42func NewNetConfigWatcher() (NetConfigWatcher, error) {
43 w.c = make(chan struct{})
44 w.stop = make(chan struct{}, 1)
45 go w.watcher()
46 return w, nil
47}