-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
69 lines (65 loc) · 1.58 KB
/
example_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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package lcm_test
import (
"context"
"log"
"net"
"time"
"go.einride.tech/lcm"
"go.einride.tech/lcm/compression/lcmlz4"
"golang.org/x/net/nettest"
"google.golang.org/protobuf/types/known/timestamppb"
)
func ExampleReceiver() {
ctx := context.Background()
ifi, err := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagMulticast)
if err != nil {
panic(err) // TODO: Handle error.
}
rx, err := lcm.ListenMulticastUDP(
ctx,
lcm.WithReceiveInterface(ifi.Name),
lcm.WithReceiveProtos(×tamppb.Timestamp{}),
)
if err != nil {
panic(err) // TODO: Handle error.
}
defer func() {
if err := rx.Close(); err != nil {
panic(err) // TODO: Handle error.
}
}()
log.Printf("listening on: %s\n", ifi.Name)
for {
if err := rx.ReceiveProto(ctx); err != nil {
panic(err) // TODO: Handle error.
}
log.Printf("received: %v", rx.ProtoMessage())
}
}
func ExampleTransmitter() {
ctx := context.Background()
ifi, err := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagMulticast)
if err != nil {
panic(err) // TODO: Handle error.
}
tx, err := lcm.DialMulticastUDP(
ctx,
lcm.WithTransmitInterface(ifi.Name),
lcm.WithTransmitCompressionProto(lcmlz4.NewCompressor(), ×tamppb.Timestamp{}),
)
if err != nil {
panic(err) // TODO: Handle error.
}
defer func() {
if err := tx.Close(); err != nil {
panic(err) // TODO: Handle error.
}
}()
ticker := time.NewTicker(1000 * time.Millisecond)
log.Printf("transmitting on: %s\n", ifi.Name)
for range ticker.C {
if err := tx.TransmitProto(ctx, timestamppb.Now()); err != nil {
panic(err) // TODO: Handle error.
}
}
}