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"
|
2022-11-02 10:39:13 +00:00
|
|
|
logging "github.com/ipfs/go-log/v2"
|
2022-11-09 19:53:01 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/node"
|
|
|
|
"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"
|
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()
|
|
|
|
|
2021-04-19 00:07:21 +00:00
|
|
|
wakuNode, err := node.New(ctx,
|
|
|
|
node.WithPrivateKey(prvKey),
|
2021-11-17 16:19:42 +00:00
|
|
|
node.WithHostAddress(hostAddr),
|
2022-12-09 03:08:04 +00:00
|
|
|
node.WithNTP(),
|
2021-04-19 00:07:21 +00:00
|
|
|
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
|
|
|
|
2021-04-28 20:28:45 +00:00
|
|
|
go writeLoop(ctx, wakuNode)
|
2021-10-10 22:44:07 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-04-28 20:28:45 +00:00
|
|
|
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
|
2022-12-09 03:08:04 +00:00
|
|
|
var timestamp int64 = utils.GetUnixEpoch(wakuNode.Timesource())
|
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
|
|
|
}
|
|
|
|
|
2021-11-19 20:01:52 +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: ", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-28 20:28:45 +00:00
|
|
|
func writeLoop(ctx context.Context, wakuNode *node.WakuNode) {
|
2021-04-04 19:33:21 +00:00
|
|
|
for {
|
|
|
|
time.Sleep(2 * time.Second)
|
2021-04-28 20:28:45 +00:00
|
|
|
write(ctx, wakuNode, "Hello world!")
|
2021-04-04 19:33:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-10 22:44:07 +00:00
|
|
|
func readLoop(ctx context.Context, wakuNode *node.WakuNode) {
|
2021-11-19 20:01:52 +00:00
|
|
|
sub, err := wakuNode.Relay().Subscribe(ctx)
|
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
|
|
|
}
|
|
|
|
}
|