-
Notifications
You must be signed in to change notification settings - Fork 1
/
example_decoder_test.go
50 lines (42 loc) · 1.26 KB
/
example_decoder_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package ipfix_test
import (
"bytes"
"context"
"fmt"
"log"
"os"
"github.com/zoomoid/go-ipfix"
)
// A simple decoder of IPFIX messages read from a file. The example uses the
// IPFIXFileReader, which asserts the file must contain IPFIX messages according
// to RFC 5655.
// The decoder is fed with messages from the reader the same way we previously did in
// the TCP and UDP listener example, using a indefinitely running goroutine with message channels.
func Example_decoder() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
f, _ := os.Open("demo_flow_records.ipfix")
defer f.Close()
r := ipfix.NewIPFIXFileReader(f)
go r.Start(ctx)
templateCache := ipfix.NewDefaultEphemeralCache()
fieldCache := ipfix.NewEphemeralFieldCache(templateCache)
decoder := ipfix.NewDecoder(templateCache, fieldCache, ipfix.DecoderOptions{OmitRFC5610Records: false})
go func() {
for {
select {
case raw := <-r.Messages():
msg, err := decoder.Decode(ctx, bytes.NewBuffer(raw))
if err != nil {
log.Println(fmt.Errorf("failed to decode IPFIX message: %w", err))
}
log.Println(msg)
case err := <-r.Errors():
log.Println(fmt.Errorf("failed to read IPFIX message: %w", err))
case <-ctx.Done():
return
}
}
}()
<-ctx.Done()
}