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