-
Notifications
You must be signed in to change notification settings - Fork 6
/
doc_test.go
174 lines (154 loc) · 3.91 KB
/
doc_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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package lifxlan_test
import (
"bytes"
"context"
"encoding/binary"
"log"
"sync"
"time"
"go.yhsif.com/lifxlan"
)
// This example demonstrates how to do device discovery.
func Example_deviceDiscovery() {
// Config values that should be initialized with proper args in real code.
var (
// The target device you want to find.
target lifxlan.Target
// The discover timeout.
timeout time.Duration
)
// It's important to be able to cancel the context.
var ctx context.Context
var cancel context.CancelFunc
if timeout > 0 {
ctx, cancel = context.WithTimeout(context.Background(), timeout)
} else {
ctx, cancel = context.WithCancel(context.Background())
}
defer cancel()
deviceChan := make(chan lifxlan.Device)
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
if err := lifxlan.Discover(ctx, deviceChan, ""); err != nil {
if err != nil && err != context.Canceled && err != context.DeadlineExceeded {
log.Fatalf("Discover failed: %v", err)
}
}
}()
for device := range deviceChan {
if !device.Target().Matches(target) {
continue
}
wg.Add(1)
go func(device lifxlan.Device) {
defer wg.Done()
// TODO: handle device
// If you are satisfied with the device(s) found,
// you can cancel the context now:
cancel()
}(device)
}
wg.Wait()
}
// This example demonstrates how to send a message and wait for the ack.
//
// Please note that this example assumes that no other replies besides ack are
// expected.
// If this message will cause a response,
// you should write your own read loop instead of using WaitForAcks.
func Example_sendMessageWithAck() {
// Should actually be a proper struct according to the Protocol definition.
type payloadType struct{}
// Config values that should be initialized with proper args in real code.
var (
// Should come with a timeout, or WaitForAcks might wait forever.
ctx context.Context
// The discovered device to use.
device lifxlan.Device
// The actual message type to be sent.
message lifxlan.MessageType
// The actual payload values.
payload payloadType
)
conn, err := device.Dial()
if err != nil {
log.Fatal(err)
}
defer conn.Close()
if ctx.Err() != nil {
log.Fatal(ctx.Err())
}
seq, err := device.Send(
ctx,
conn,
lifxlan.FlagAckRequired,
message,
&payload, // could be nil if this message doesn't need payload.
)
if err != nil {
log.Fatal(err)
}
if err := lifxlan.WaitForAcks(ctx, conn, device.Source(), seq); err != nil {
log.Fatal(err)
}
}
// This example demonstrates how to send a message and read the response.
func Example_sendMessageWithResponse() {
// Should actually be proper structs according to the Protocol definition.
type (
payloadType struct{}
respPayloadType struct{}
)
// Config values that should be initialized with proper args in real code.
var (
// Should come with a timeout, or we might wait forever.
ctx context.Context
// The discovered device to use.
device lifxlan.Device
// The actual message type to be sent.
message lifxlan.MessageType
// The actual payload values.
payload payloadType
// The response message type.
respMessage lifxlan.MessageType
)
conn, err := device.Dial()
if err != nil {
log.Fatal(err)
}
defer conn.Close()
if ctx.Err() != nil {
log.Fatal(ctx.Err())
}
seq, err := device.Send(
ctx,
conn,
0, // flags, not requiring ack because this message will get a response.
message,
&payload, // could be nil if this message doesn't need payload.
)
if err != nil {
log.Fatal(err)
}
for {
resp, err := lifxlan.ReadNextResponse(ctx, conn)
if err != nil {
log.Fatal(err)
}
if resp.Sequence != seq || resp.Source != device.Source() {
continue
}
if resp.Message != respMessage {
continue
}
var raw respPayloadType
r := bytes.NewReader(resp.Payload)
if err := binary.Read(r, binary.LittleEndian, &raw); err != nil {
log.Fatal(err)
}
// TODO: handle payload value in raw
return
}
}