merge inotify changes from @haykinson.

pull/14/head
Christopher Young 2015-08-21 04:24:51 -04:00
rodzic 8d88e4f91c
commit f8b3f53fa0
1 zmienionych plików z 24 dodań i 4 usunięć

Wyświetl plik

@ -1,6 +1,7 @@
package main
import (
"golang.org/x/exp/inotify"
"io/ioutil"
"log"
"net"
@ -30,11 +31,12 @@ var netMutex *sync.Mutex
const (
NETWORK_GDL90 = 1
NETWORK_AHRS = 2
dhcp_lease_file = "/var/lib/dhcp/dhcpd.leases"
)
// Read the "dhcpd.leases" file and parse out IP/hostname.
func getDHCPLeases() (map[string]string, error) {
dat, err := ioutil.ReadFile("/var/lib/dhcp/dhcpd.leases")
dat, err := ioutil.ReadFile(dhcp_lease_file)
ret := make(map[string]string)
if err != nil {
return ret, err
@ -115,15 +117,12 @@ func refreshConnectedClients() {
func messageQueueSender() {
secondTimer := time.NewTicker(1 * time.Second)
dhcpRefresh := time.NewTicker(30 * time.Second)
for {
select {
case msg := <-messageQueue:
sendToAllConnectedClients(msg)
case <-secondTimer.C:
getNetworkStats()
case <-dhcpRefresh.C:
refreshConnectedClients()
}
}
}
@ -136,10 +135,31 @@ func sendGDL90(msg []byte) {
sendMsg(msg, NETWORK_GDL90)
}
func monitorDHCPLeases() {
watcher, err := inotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
err = watcher.AddWatch(dhcp_lease_file, inotify.IN_CLOSE_WRITE)
if err != nil {
log.Fatal(err)
}
for {
select {
case <-watcher.Event:
log.Println("file modified, attempting to refresh DHCP")
refreshConnectedClients()
case err := <-watcher.Error:
log.Println("error with DHCP file system watcher:", err)
}
}
}
func initNetwork() {
messageQueue = make(chan networkMessage, 1024) // Buffered channel, 1024 messages.
outSockets = make(map[string]networkConnection)
netMutex = &sync.Mutex{}
refreshConnectedClients()
go monitorDHCPLeases()
go messageQueueSender()
}