yanic/respond/collector.go

121 lines
2.3 KiB
Go
Raw Normal View History

2016-02-26 08:28:31 +00:00
package respond
2015-12-29 03:08:03 +00:00
import (
"log"
"net"
"time"
)
const (
2016-01-04 01:07:09 +00:00
// default multicast group used by announced
multiCastGroup string = "ff02:0:0:0:0:0:2:1001"
2016-01-04 01:07:09 +00:00
// default udp port used by announced
port string = "1001"
2016-01-04 01:07:09 +00:00
// maximum receivable size
maxDataGramSize int = 8192
2015-12-29 03:08:03 +00:00
)
2016-02-26 08:28:31 +00:00
//Response of the respond request
2016-01-04 01:07:09 +00:00
type Response struct {
Address net.UDPAddr
Raw []byte
}
2016-02-26 08:28:31 +00:00
//Collector for a specificle respond messages
2015-12-29 03:08:03 +00:00
type Collector struct {
CollectType string
2016-01-04 01:07:09 +00:00
connection *net.UDPConn // UDP socket
queue chan *Response // received responses
parse func(coll *Collector, res *Response)
2015-12-29 03:08:03 +00:00
}
//NewCollector creates a Collector struct
func NewCollector(CollectType string, parseFunc func(coll *Collector, res *Response)) *Collector {
2015-12-29 03:08:03 +00:00
// Parse address
addr, err := net.ResolveUDPAddr("udp", "[::]:0")
2015-12-29 03:08:03 +00:00
if err != nil {
log.Panic(err)
}
// Open socket
conn, err := net.ListenUDP("udp", addr)
if err != nil {
log.Panic(err)
}
conn.SetReadBuffer(maxDataGramSize)
2015-12-29 03:08:03 +00:00
collector := &Collector{
CollectType: CollectType,
2015-12-29 13:05:47 +00:00
connection: conn,
2016-02-19 13:25:11 +00:00
queue: make(chan *Response, 400),
parse: parseFunc,
2015-12-29 03:08:03 +00:00
}
go collector.receiver()
go collector.parser()
2016-02-19 16:14:14 +00:00
collector.sendOnce()
2015-12-29 03:08:03 +00:00
return collector
}
//Close Collector
2015-12-29 03:08:03 +00:00
func (coll *Collector) Close() {
coll.connection.Close()
close(coll.queue)
}
2015-12-29 13:05:47 +00:00
func (coll *Collector) sendOnce() {
coll.sendPacket(net.JoinHostPort(multiCastGroup, port))
2016-02-19 16:14:14 +00:00
log.Println("send request")
2015-12-29 13:05:47 +00:00
}
func (coll *Collector) sendPacket(address string) {
2015-12-29 03:08:03 +00:00
addr, err := net.ResolveUDPAddr("udp", address)
2016-02-19 10:13:30 +00:00
if err != nil {
log.Panic(err)
}
2015-12-29 13:05:47 +00:00
2016-03-07 00:37:07 +00:00
if _, err := coll.connection.WriteToUDP([]byte(coll.CollectType), addr); err != nil {
log.Println("WriteToUDP failed:", err)
}
2015-12-29 03:08:03 +00:00
}
func (coll *Collector) sender(collectInterval time.Duration) {
2015-12-29 13:05:47 +00:00
c := time.Tick(collectInterval)
for range c {
2016-01-04 01:07:09 +00:00
// TODO break condition
2015-12-29 13:05:47 +00:00
coll.sendOnce()
2015-12-29 03:08:03 +00:00
}
}
func (coll *Collector) parser() {
2016-01-04 01:07:09 +00:00
for obj := range coll.queue {
coll.parse(coll, obj)
2015-12-29 03:08:03 +00:00
}
}
func (coll *Collector) receiver() {
buf := make([]byte, maxDataGramSize)
2015-12-29 03:08:03 +00:00
for {
2016-01-04 01:07:09 +00:00
n, src, err := coll.connection.ReadFromUDP(buf)
2015-12-29 03:08:03 +00:00
if err != nil {
log.Println("ReadFromUDP failed:", err)
2015-12-29 13:05:47 +00:00
return
2015-12-29 03:08:03 +00:00
}
2016-01-04 01:07:09 +00:00
raw := make([]byte, n)
copy(raw, buf)
coll.queue <- &Response{
Address: *src,
Raw: raw,
}
log.Println("received", coll.CollectType, "from", src)
2015-12-29 03:08:03 +00:00
}
}