go-waku/examples/basic2/main.go

136 lines
2.8 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"
logging "github.com/ipfs/go-log"
"github.com/status-im/go-waku/waku/v2/node"
2021-11-10 14:28:45 +00:00
"github.com/status-im/go-waku/waku/v2/protocol"
2021-04-22 00:12:51 +00:00
"github.com/status-im/go-waku/waku/v2/protocol/pb"
2021-10-12 13:12:54 +00:00
"github.com/status-im/go-waku/waku/v2/utils"
2021-04-04 19:33:21 +00:00
)
var log = logging.Logger("basic2")
func main() {
lvl, err := logging.LevelFromString("info")
if err != nil {
panic(err)
}
logging.SetAllLoggers(lvl)
hostAddr, _ := net.ResolveTCPAddr("tcp", fmt.Sprint("0.0.0.0:0"))
key, err := randomHex(32)
if err != nil {
log.Error("Could not generate random key")
return
}
prvKey, err := crypto.HexToECDSA(key)
2021-11-02 00:05:36 +00:00
if err != nil {
log.Error(err)
return
}
2021-04-04 19:33:21 +00:00
ctx := context.Background()
wakuNode, err := node.New(ctx,
node.WithPrivateKey(prvKey),
node.WithHostAddress([]*net.TCPAddr{hostAddr}),
node.WithWakuRelay(),
)
2021-10-05 02:13:54 +00:00
if err != nil {
log.Error(err)
return
}
if err := wakuNode.Start(); err != nil {
log.Error(err)
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-11-10 14:28:45 +00:00
contentTopic := protocol.NewContentTopic("basic2", 1, "test", "proto")
2021-04-04 19:33:21 +00:00
var version uint32 = 0
2021-10-12 13:12:54 +00:00
var timestamp float64 = utils.GetUnixEpoch()
2021-04-04 19:33:21 +00:00
2021-04-11 23:45:42 +00:00
p := new(node.Payload)
p.Data = []byte(wakuNode.ID() + ": " + msgContent)
p.Key = &node.KeyInfo{Kind: node.None}
payload, err := p.Encode(version)
2021-11-02 00:05:36 +00:00
if err != nil {
log.Error("Error encoding the payload: ", err)
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,
2021-11-10 14:28:45 +00:00
ContentTopic: contentTopic.String(),
2021-04-11 23:45:42 +00:00
Timestamp: timestamp,
2021-04-04 19:33:21 +00:00
}
_, err = wakuNode.Relay().Publish(ctx, msg, nil)
2021-04-04 19:33:21 +00:00
if err != nil {
log.Error("Error sending a message: ", err)
}
}
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, nil)
2021-04-04 19:33:21 +00:00
if err != nil {
log.Error("Could not subscribe: ", err)
return
}
for value := range sub.C {
payload, err := node.DecodePayload(value.Message(), &node.KeyInfo{Kind: node.None})
if err != nil {
fmt.Println(err)
return
}
2021-04-11 23:45:42 +00:00
log.Info("Received msg, ", string(payload.Data))
2021-04-04 19:33:21 +00:00
}
}