diff --git a/respond/collector.go b/respond/collector.go index c48103d..86eea69 100644 --- a/respond/collector.go +++ b/respond/collector.go @@ -4,6 +4,7 @@ import ( "bytes" "compress/flate" "encoding/json" + "io/ioutil" "log" "net" "reflect" @@ -117,11 +118,27 @@ func (coll *Collector) parse(response *Response) (err error) { 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) + decompressed, err := ioutil.ReadAll(reader) + if err != nil { + return } + // Remove useless wrapper element that only exists in compressed data. + // Who introduced this !? + if bytes.HasPrefix(decompressed, []byte(`{"neighbours":`)) || + bytes.HasPrefix(decompressed, []byte(`{"statistics":`)) { + decompressed = decompressed[14 : len(decompressed)-1] + } else if bytes.HasPrefix(decompressed, []byte(`{"nodeinfo":`)) { + decompressed = decompressed[12 : len(decompressed)-1] + } + + err = json.Unmarshal(decompressed, data) + if err != nil { + return + } + + coll.onReceive(response.Address, data) + return } diff --git a/respond/collector_test.go b/respond/collector_test.go new file mode 100644 index 0000000..7ae2812 --- /dev/null +++ b/respond/collector_test.go @@ -0,0 +1,42 @@ +package respond + +import ( + "io/ioutil" + "net" + "reflect" + "testing" + + "github.com/FreifunkBremen/respond-collector/data" + "github.com/stretchr/testify/assert" +) + +func TestParse(t *testing.T) { + assert := assert.New(t) + var decompressed *data.NodeInfo + + // callback function + onReceive := func(addr net.UDPAddr, msg interface{}) { + switch msg := msg.(type) { + case *data.NodeInfo: + decompressed = msg + default: + t.Error("unexpected message:", msg) + } + } + + collector := &Collector{ + msgType: reflect.TypeOf(data.NodeInfo{}), + onReceive: onReceive, + } + + // read testdata + compressed, err := ioutil.ReadFile("testdata/nodeinfo.flated") + assert.Nil(err) + + collector.parse(&Response{ + Raw: compressed, + }) + + assert.NotNil(decompressed) + assert.Equal("f81a67a5e9c1", decompressed.NodeId) +} diff --git a/respond/testdata/nodeinfo.flated b/respond/testdata/nodeinfo.flated new file mode 100644 index 0000000..6db53fe Binary files /dev/null and b/respond/testdata/nodeinfo.flated differ