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.
|
2022-10-12 11:57:42 +08:00
|
|
|
ap := hpds_node.NewAccessPoint(
|
2022-10-12 11:55:36 +08:00
|
|
|
"hpds-ap",
|
|
|
|
hpds_node.WithMqAddr("localhost:27188"),
|
|
|
|
hpds_node.WithCredential("token:z1"),
|
|
|
|
)
|
2022-10-12 11:57:42 +08:00
|
|
|
err := ap.Connect()
|
2022-10-12 11:55:36 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("[AccessPoint] Emit the data to HPDS-MQ failure with err: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2022-10-12 11:57:42 +08:00
|
|
|
defer ap.Close()
|
2022-10-12 11:55:36 +08:00
|
|
|
|
2022-10-12 11:57:42 +08:00
|
|
|
ap.SetDataTag(0x33)
|
2022-10-12 11:55:36 +08:00
|
|
|
|
|
|
|
// generate mock data and send it to HPDS-MQ.
|
2022-10-12 11:57:42 +08:00
|
|
|
err = generateAndSendData(ap)
|
2022-10-12 11:55:36 +08:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|