yanic/respond/collector.go

147 lines
2.9 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 (
2016-03-19 00:50:23 +00:00
"bytes"
"compress/flate"
2016-03-11 23:59:36 +00:00
"encoding/json"
2015-12-29 03:08:03 +00:00
"log"
"net"
2016-03-11 23:59:36 +00:00
"reflect"
2015-12-29 03:08:03 +00:00
"time"
)
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
2016-03-11 23:59:36 +00:00
onReceive OnReceive
msgType reflect.Type
2016-03-11 22:56:23 +00:00
// Ticker and stopper
ticker *time.Ticker
stop chan interface{}
2015-12-29 03:08:03 +00:00
}
2016-03-11 23:59:36 +00:00
type OnReceive func(net.UDPAddr, interface{})
2016-03-11 22:56:23 +00:00
//NewCollector creates a Collector struct
2016-03-12 17:26:51 +00:00
func NewCollector(CollectType string, initialDelay time.Duration, interval time.Duration, msgStruct interface{}, onReceive OnReceive) *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),
2016-03-11 22:56:23 +00:00
ticker: time.NewTicker(interval),
stop: make(chan interface{}, 1),
2016-03-11 23:59:36 +00:00
msgType: reflect.TypeOf(msgStruct),
onReceive: onReceive,
2015-12-29 03:08:03 +00:00
}
go collector.receiver()
go collector.parser()
2016-03-12 00:04:22 +00:00
// Run senders
2016-03-12 17:26:51 +00:00
go func() {
time.Sleep(initialDelay)
collector.sendOnce() // immediately
collector.sender() // periodically
}()
2016-02-19 16:14:14 +00:00
2015-12-29 03:08:03 +00:00
return collector
}
2016-03-11 22:56:23 +00:00
// Close Collector
2015-12-29 03:08:03 +00:00
func (coll *Collector) Close() {
2016-03-11 22:56:23 +00:00
// stop ticker
coll.ticker.Stop()
coll.stop <- nil
2015-12-29 03:08:03 +00:00
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-03-12 02:35:35 +00:00
log.Println("request", coll.CollectType)
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-19 00:50:23 +00:00
if _, err := coll.connection.WriteToUDP([]byte("GET "+coll.CollectType), addr); err != nil {
2016-03-07 00:37:07 +00:00
log.Println("WriteToUDP failed:", err)
}
2015-12-29 03:08:03 +00:00
}
2016-03-11 22:56:23 +00:00
// send packets continously
func (coll *Collector) sender() {
for {
select {
case <-coll.stop:
return
case <-coll.ticker.C:
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 {
2016-03-19 00:50:23 +00:00
if err := coll.parse(obj); err != nil {
log.Println("unable to decode response from", obj.Address.String(), err, "\n", string(obj.Raw))
2016-03-11 23:59:36 +00:00
}
2015-12-29 03:08:03 +00:00
}
}
2016-03-19 00:50:23 +00:00
func (coll *Collector) parse(response *Response) (err error) {
// create new struct instance
data := reflect.New(coll.msgType).Interface()
// deflater
reader := flate.NewReader(bytes.NewReader(response.Raw))
defer reader.Close()
decoder := json.NewDecoder(reader)
if err = decoder.Decode(data); err == nil {
coll.onReceive(response.Address, data)
}
return
}
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,
}
2015-12-29 03:08:03 +00:00
}
}