hpds_node/example/multi-mq/mock_ap/main.go

53 lines
1.1 KiB
Go
Raw Normal View History

2022-10-12 11:55:36 +08:00
package main
import (
"fmt"
"git.hpds.cc/Component/network/log"
"git.hpds.cc/pavement/hpds_node"
"math/rand"
"os"
"time"
)
func main() {
// connect to HPDS-MQ.
source := hpds_node.NewAccessPoint(
"hpds-ap",
hpds_node.WithMqAddr("localhost:27188"),
hpds_node.WithCredential("token:z1"),
)
err := source.Connect()
if err != nil {
log.Printf("[AccessPoint] Emit the data to HPDS-MQ failure with err: %v", err)
return
}
defer source.Close()
source.SetDataTag(0x33)
// generate mock data and send it to HPDS-MQ.
err = generateAndSendData(source)
if err != nil {
log.Printf("[AccessPoint] >>>> ERR >>>> %v", err)
os.Exit(1)
}
select {}
}
func generateAndSendData(stream hpds_node.AccessPoint) error {
for {
rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
data := []byte(fmt.Sprintf("%d", rnd.Uint32()))
// send data via QUIC stream.
_, err := stream.Write(data)
if err != nil {
log.Printf("[AccessPoint] Emit %v to HPDS-MQ failure with err: %v", data, err)
time.Sleep(500 * time.Millisecond)
continue
}
log.Printf("[AccessPoint] Emit %s to HPDS-MQ", data)
time.Sleep(1000 * time.Millisecond)
}
}