yanic/respond/daemon/main.go

60 lines
1.1 KiB
Go
Raw Normal View History

2019-01-24 02:54:21 +01:00
package respondd
import (
"net"
"time"
"github.com/bdlm/log"
"github.com/FreifunkBremen/yanic/data"
)
func (d *Daemon) Start() {
2019-04-10 13:18:05 +02:00
d.dataByInterface = make(map[string]*data.ResponseData)
if d.AnswerByZones == nil {
d.AnswerByZones = make(map[string]*AnswerConfig)
2019-01-24 02:54:21 +01:00
}
d.updateData()
go d.updateWorker()
for _, listen := range d.Listen {
var socket *net.UDPConn
var err error
addr := net.ParseIP(listen.Address)
if addr.IsMulticast() {
var iface *net.Interface
if listen.Interface != "" {
iface, err = net.InterfaceByName(listen.Interface)
if err != nil {
log.Fatal(err)
}
}
if socket, err = net.ListenMulticastUDP("udp6", iface, &net.UDPAddr{
IP: addr,
Port: listen.Port,
}); err != nil {
log.Fatal(err)
}
} else {
if socket, err = net.ListenUDP("udp6", &net.UDPAddr{
IP: addr,
Port: listen.Port,
}); err != nil {
log.Fatal(err)
}
}
go d.handler(socket)
}
log.Debug("all listener started")
}
func (d *Daemon) updateWorker() {
c := time.Tick(d.DataInterval.Duration)
for range c {
d.updateData()
}
}