-
Notifications
You must be signed in to change notification settings - Fork 48
/
example_monitor_test.go
38 lines (31 loc) · 785 Bytes
/
example_monitor_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
package tc_test
import (
"context"
"fmt"
"time"
"github.com/florianl/go-tc"
)
// This example demonstrates how Monitor() can be used
func ExampleTc_Monitor() {
tcSocket, err := tc.Open(&tc.Config{})
if err != nil {
fmt.Printf("could not open socket for TC: %v", err)
return
}
defer func() {
if err := tcSocket.Close(); err != nil {
fmt.Printf("coult not close TC socket: %v", err)
return
}
}()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Hook function mon, which is called every time,
// something is received by the kernel on this socket
mon := func(action uint16, m tc.Object) int {
fmt.Printf("Action:\t%d\nObject: \t%#v\n", action, m)
return 0
}
tcSocket.Monitor(ctx, 10*time.Millisecond, mon)
<-ctx.Done()
}