forked from xiaojian-hong/cli
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
70 lines (59 loc) · 1.82 KB
/
main.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
package main
import (
"encoding/json"
"log"
"math/rand"
"os"
"time"
"github.com/yomorun/yomo"
)
type noiseData struct {
Noise float32 `json:"noise"` // Noise value
Time int64 `json:"time"` // Timestamp (ms)
From string `json:"from"` // Source IP
}
func main() {
// connect to YoMo-Zipper.
opts := []yomo.Option{yomo.WithZipperAddr("localhost:9000")}
if credential := os.Getenv("YOMO_CREDENTIAL"); credential != "" {
opts = append(opts, yomo.WithCredential(credential))
}
source := yomo.NewSource("yomo-source", opts...)
defer source.Close()
err := source.Connect()
if err != nil {
log.Printf("[source] ❌ Emit the data to YoMo-Zipper failure with err: %v", err)
return
}
source.SetDataTag(0x33)
// set the error handler function when server error occurs
source.SetErrorHandler(func(err error) {
log.Printf("[source] error handler: %v", err)
})
// generate mock data and send it to YoMo-Zipper in every 100 ms.
generateAndSendData(source)
}
func generateAndSendData(stream yomo.Source) {
for {
// generate random data.
data := noiseData{
Noise: rand.New(rand.NewSource(time.Now().UnixNano())).Float32() * 200,
Time: time.Now().UnixNano() / int64(time.Millisecond),
From: "localhost",
}
// encode data via JSON codec.
sendingBuf, _ := json.Marshal(data)
// send data via QUIC stream.
_, err := stream.Write(sendingBuf)
// using the following code, zipper will broadcast this message to cascading zippers.
// make sure to configure the downstream zippers using mesh-config flag,
// see the mesh example for more details.
// err := stream.Broadcast(sendingBuf)
if err != nil {
log.Printf("[source] ❌ Emit %v to YoMo-Zipper failure with err: %v", data, err)
} else {
log.Printf("[source] ✅ Emit %v to YoMo-Zipper", data)
}
time.Sleep(500 * time.Millisecond)
}
}