2016-02-25 20:06:37 +00:00
|
|
|
package responed
|
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
|
2016-02-25 20:06:37 +00:00
|
|
|
multiCastGroup string = "ff02:0:0:0:0:0:2:1001"
|
2016-01-04 01:07:09 +00:00
|
|
|
|
|
|
|
// default udp port used by announced
|
2016-02-25 20:06:37 +00:00
|
|
|
port string = "1001"
|
2016-01-04 01:07:09 +00:00
|
|
|
|
|
|
|
// maximum receivable size
|
2016-02-25 20:06:37 +00:00
|
|
|
maxDataGramSize int = 8192
|
2015-12-29 03:08:03 +00:00
|
|
|
)
|
|
|
|
|
2016-02-25 20:06:37 +00:00
|
|
|
//Response of the
|
2016-01-04 01:07:09 +00:00
|
|
|
type Response struct {
|
|
|
|
Address net.UDPAddr
|
|
|
|
Raw []byte
|
|
|
|
}
|
|
|
|
|
2016-02-25 20:06:37 +00:00
|
|
|
//Collector for a specificle responed messages
|
2015-12-29 03:08:03 +00:00
|
|
|
type Collector struct {
|
2016-02-25 20:06:37 +00:00
|
|
|
CollectType string
|
2016-01-04 01:07:09 +00:00
|
|
|
connection *net.UDPConn // UDP socket
|
|
|
|
queue chan *Response // received responses
|
2016-02-25 20:06:37 +00:00
|
|
|
parse func(coll *Collector, res *Response)
|
2015-12-29 03:08:03 +00:00
|
|
|
}
|
|
|
|
|
2016-02-25 20:06:37 +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
|
2016-02-25 20:06:37 +00:00
|
|
|
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)
|
|
|
|
}
|
2016-02-25 20:06:37 +00:00
|
|
|
conn.SetReadBuffer(maxDataGramSize)
|
2015-12-29 03:08:03 +00:00
|
|
|
|
|
|
|
collector := &Collector{
|
2016-02-25 20:06:37 +00:00
|
|
|
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-02-25 20:06:37 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-02-25 20:06:37 +00:00
|
|
|
//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() {
|
2016-02-25 20:06:37 +00:00
|
|
|
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-02-25 20:06:37 +00:00
|
|
|
coll.connection.WriteToUDP([]byte(coll.CollectType), addr)
|
2015-12-29 03:08:03 +00:00
|
|
|
}
|
|
|
|
|
2016-02-25 20:06:37 +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 {
|
2016-02-25 20:06:37 +00:00
|
|
|
coll.parse(coll, obj)
|
2015-12-29 03:08:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (coll *Collector) receiver() {
|
2016-02-25 20:06:37 +00:00
|
|
|
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,
|
|
|
|
}
|
2016-02-25 20:06:37 +00:00
|
|
|
log.Println("received", coll.CollectType, "from", src)
|
2015-12-29 03:08:03 +00:00
|
|
|
}
|
|
|
|
}
|