Use compressed respond data

This commit is contained in:
Julian Kornberger 2016-03-19 01:50:23 +01:00
parent 30b04d9043
commit 8d471d532a

View File

@ -1,6 +1,8 @@
package respond package respond
import ( import (
"bytes"
"compress/flate"
"encoding/json" "encoding/json"
"log" "log"
"net" "net"
@ -82,7 +84,7 @@ func (coll *Collector) sendPacket(address string) {
log.Panic(err) log.Panic(err)
} }
if _, err := coll.connection.WriteToUDP([]byte(coll.CollectType), addr); err != nil { if _, err := coll.connection.WriteToUDP([]byte("GET "+coll.CollectType), addr); err != nil {
log.Println("WriteToUDP failed:", err) log.Println("WriteToUDP failed:", err)
} }
} }
@ -101,17 +103,28 @@ func (coll *Collector) sender() {
func (coll *Collector) parser() { func (coll *Collector) parser() {
for obj := range coll.queue { for obj := range coll.queue {
// create new struct instance if err := coll.parse(obj); err != nil {
data := reflect.New(coll.msgType).Interface()
if err := json.Unmarshal(obj.Raw, data); err == nil {
coll.onReceive(obj.Address, data)
} else {
log.Println("unable to decode response from", obj.Address.String(), err, "\n", string(obj.Raw)) log.Println("unable to decode response from", obj.Address.String(), err, "\n", string(obj.Raw))
} }
} }
} }
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
}
func (coll *Collector) receiver() { func (coll *Collector) receiver() {
buf := make([]byte, maxDataGramSize) buf := make([]byte, maxDataGramSize)
for { for {