go-waku/examples/basic2/main.go

134 lines
3.0 KiB
Go
Raw Normal View History

2021-04-04 19:33:21 +00:00
package main
import (
"context"
"crypto/rand"
"encoding/hex"
"fmt"
"net"
"os"
"os/signal"
"syscall"
"time"
"github.com/ethereum/go-ethereum/crypto"
"github.com/waku-org/go-waku/waku/v2/node"
2022-12-21 18:47:43 +00:00
"github.com/waku-org/go-waku/waku/v2/payload"
"github.com/waku-org/go-waku/waku/v2/protocol"
"github.com/waku-org/go-waku/waku/v2/protocol/pb"
"github.com/waku-org/go-waku/waku/v2/utils"
"go.uber.org/zap"
2021-04-04 19:33:21 +00:00
)
var log = utils.Logger().Named("basic2")
2023-02-09 12:53:37 +00:00
var contentTopic = protocol.NewContentTopic("basic2", 1, "test", "proto").String()
2021-04-04 19:33:21 +00:00
func main() {
hostAddr, _ := net.ResolveTCPAddr("tcp", "0.0.0.0:0")
2021-04-04 19:33:21 +00:00
key, err := randomHex(32)
if err != nil {
log.Error("Could not generate random key", zap.Error(err))
2021-04-04 19:33:21 +00:00
return
}
prvKey, err := crypto.HexToECDSA(key)
2021-11-02 00:05:36 +00:00
if err != nil {
log.Error("Could not convert hex into ecdsa key", zap.Error(err))
2021-11-02 00:05:36 +00:00
return
}
2021-04-04 19:33:21 +00:00
ctx := context.Background()
2023-01-06 22:37:57 +00:00
wakuNode, err := node.New(
node.WithPrivateKey(prvKey),
2021-11-17 16:19:42 +00:00
node.WithHostAddress(hostAddr),
2022-12-09 03:08:04 +00:00
node.WithNTP(),
node.WithWakuRelay(),
)
2021-10-05 02:13:54 +00:00
if err != nil {
log.Error("Error creating wakunode", zap.Error(err))
2021-10-05 02:13:54 +00:00
return
}
2023-01-06 22:37:57 +00:00
if err := wakuNode.Start(ctx); err != nil {
log.Error("Error starting wakunode", zap.Error(err))
2021-10-05 02:13:54 +00:00
return
}
2021-04-04 19:33:21 +00:00
go writeLoop(ctx, wakuNode)
go readLoop(ctx, wakuNode)
2021-04-04 19:33:21 +00:00
// Wait for a SIGINT or SIGTERM signal
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
<-ch
fmt.Println("\n\n\nReceived signal, shutting down...")
// shut the node down
wakuNode.Stop()
}
func randomHex(n int) (string, error) {
bytes := make([]byte, n)
if _, err := rand.Read(bytes); err != nil {
return "", err
}
return hex.EncodeToString(bytes), nil
}
func write(ctx context.Context, wakuNode *node.WakuNode, msgContent string) {
2021-04-04 19:33:21 +00:00
var version uint32 = 0
2022-12-09 03:08:04 +00:00
var timestamp int64 = utils.GetUnixEpoch(wakuNode.Timesource())
2021-04-04 19:33:21 +00:00
2022-12-21 18:47:43 +00:00
p := new(payload.Payload)
2021-04-11 23:45:42 +00:00
p.Data = []byte(wakuNode.ID() + ": " + msgContent)
2022-12-21 18:47:43 +00:00
p.Key = &payload.KeyInfo{Kind: payload.None}
2021-04-11 23:45:42 +00:00
payload, err := p.Encode(version)
2021-11-02 00:05:36 +00:00
if err != nil {
log.Error("Error encoding the payload", zap.Error(err))
2021-11-02 00:05:36 +00:00
return
}
2021-04-11 23:45:42 +00:00
2021-04-22 00:12:51 +00:00
msg := &pb.WakuMessage{
2021-04-04 19:33:21 +00:00
Payload: payload,
2021-04-11 23:45:42 +00:00
Version: version,
2023-02-09 12:53:37 +00:00
ContentTopic: contentTopic,
2021-04-11 23:45:42 +00:00
Timestamp: timestamp,
2021-04-04 19:33:21 +00:00
}
_, err = wakuNode.Relay().Publish(ctx, msg)
2021-04-04 19:33:21 +00:00
if err != nil {
log.Error("Error sending a message", zap.Error(err))
2021-04-04 19:33:21 +00:00
}
}
func writeLoop(ctx context.Context, wakuNode *node.WakuNode) {
2021-04-04 19:33:21 +00:00
for {
time.Sleep(2 * time.Second)
write(ctx, wakuNode, "Hello world!")
2021-04-04 19:33:21 +00:00
}
}
func readLoop(ctx context.Context, wakuNode *node.WakuNode) {
sub, err := wakuNode.Relay().Subscribe(ctx)
2021-04-04 19:33:21 +00:00
if err != nil {
log.Error("Could not subscribe", zap.Error(err))
2021-04-04 19:33:21 +00:00
return
}
2023-02-09 12:53:37 +00:00
for envelope := range sub.C {
if envelope.Message().ContentTopic != contentTopic {
continue
}
payload, err := payload.DecodePayload(envelope.Message(), &payload.KeyInfo{Kind: payload.None})
2021-04-04 19:33:21 +00:00
if err != nil {
2023-02-09 12:53:37 +00:00
log.Error("Error decoding payload", zap.Error(err))
continue
2021-04-04 19:33:21 +00:00
}
log.Info("Received msg, ", zap.String("data", string(payload.Data)))
2021-04-04 19:33:21 +00:00
}
}