diff --git a/examples/basic2/go.mod b/examples/basic2/go.mod index 431708b0..585abc9b 100644 --- a/examples/basic2/go.mod +++ b/examples/basic2/go.mod @@ -5,5 +5,5 @@ go 1.15 require ( github.com/ethereum/go-ethereum v1.9.5 github.com/ipfs/go-log v1.0.5 - github.com/status-im/go-waku v0.0.0-20210411234359-f9e7589ffdd9 + github.com/status-im/go-waku v0.0.0-20210412175941-fa79f9a864bb ) diff --git a/examples/basic2/go.sum b/examples/basic2/go.sum index 844bfae1..2a83957f 100644 --- a/examples/basic2/go.sum +++ b/examples/basic2/go.sum @@ -436,6 +436,8 @@ github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcncea github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= github.com/mattn/go-isatty v0.0.5/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= +github.com/mattn/go-sqlite3 v1.14.6 h1:dNPt6NO46WmLVt2DLNpwczCmdV5boIZ6g/tlDrlRUbg= +github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE= github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg= @@ -597,6 +599,8 @@ github.com/status-im/go-waku v0.0.0-20210404170848-47752170e9df h1:wm5+68DGiRMvZ github.com/status-im/go-waku v0.0.0-20210404170848-47752170e9df/go.mod h1:949jqEGfl0gWv8NMWJU5xhW0hSRJicDxYWQAZkJMsqQ= github.com/status-im/go-waku v0.0.0-20210411234359-f9e7589ffdd9 h1:smFp8UIU5OYZuWNxyNfIKcCbP74NSqi6EqVmcqh6Zlo= github.com/status-im/go-waku v0.0.0-20210411234359-f9e7589ffdd9/go.mod h1:2fHIqaNF8M8W/DQL5J3R9dXbrTLpOefvD5tbKSvoxBg= +github.com/status-im/go-waku v0.0.0-20210412175941-fa79f9a864bb h1:Nw2aNm6JgjHbCvIneCBP0Cju66mhVXfEP5VyfAK2TQk= +github.com/status-im/go-waku v0.0.0-20210412175941-fa79f9a864bb/go.mod h1:J+Pvtku93TXlU/X3CzLwD4C7bXUFVpIiF4iPQksbUBY= github.com/status-im/go-wakurelay-pubsub v0.4.2 h1:F4UGcP80H0PGaeJ0mRMzA1Ux3DKYiyv/qu3bOR/efTg= github.com/status-im/go-wakurelay-pubsub v0.4.2/go.mod h1:LSCVYR7mnBBsxVJghrGpQ3yJAAATEe6XeQQqGCZhwrE= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= diff --git a/examples/chat2/chat.go b/examples/chat2/chat.go index 920121a9..505839b9 100644 --- a/examples/chat2/chat.go +++ b/examples/chat2/chat.go @@ -89,20 +89,30 @@ func (cr *Chat) Publish(message string) error { return err } +func (cr *Chat) decodeMessage(wakumsg *protocol.WakuMessage) { + payload, err := node.DecodePayload(wakumsg, &node.KeyInfo{Kind: node.None}) + if err != nil { + return + } + + msg := &pb.Chat2Message{} + if err := proto.Unmarshal(payload.Data, msg); err != nil { + return + } + + // send valid messages onto the Messages channel + cr.Messages <- msg +} + // readLoop pulls messages from the pubsub topic and pushes them onto the Messages channel. func (cr *Chat) readLoop() { for value := range cr.sub.C { - payload, err := node.DecodePayload(value.Message(), &node.KeyInfo{Kind: node.None}) - if err != nil { - continue - } - - msg := &pb.Chat2Message{} - if err := proto.Unmarshal(payload.Data, msg); err != nil { - continue - } - - // send valid messages onto the Messages channel - cr.Messages <- msg + cr.decodeMessage(value.Message()) + } +} + +func (cr *Chat) displayMessages(messages []*protocol.WakuMessage) { + for _, msg := range messages { + cr.decodeMessage(msg) } } diff --git a/examples/chat2/go.mod b/examples/chat2/go.mod index 2579778a..fd65cacc 100644 --- a/examples/chat2/go.mod +++ b/examples/chat2/go.mod @@ -10,6 +10,6 @@ require ( github.com/ipfs/go-log/v2 v2.1.1 github.com/libp2p/go-libp2p-core v0.8.5 github.com/rivo/tview v0.0.0-20210312174852-ae9464cc3598 - github.com/status-im/go-waku v0.0.0-20210411234359-f9e7589ffdd9 + github.com/status-im/go-waku v0.0.0-20210412175941-fa79f9a864bb google.golang.org/protobuf v1.25.0 ) diff --git a/examples/chat2/go.sum b/examples/chat2/go.sum index 9b9e414e..59fd92f7 100644 --- a/examples/chat2/go.sum +++ b/examples/chat2/go.sum @@ -566,6 +566,8 @@ github.com/mattn/go-runewidth v0.0.7/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m github.com/mattn/go-runewidth v0.0.10 h1:CoZ3S2P7pvtP45xOtBw+/mDL2z0RKI576gSkzRRpdGg= github.com/mattn/go-runewidth v0.0.10/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk= github.com/mattn/go-sqlite3 v1.11.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= +github.com/mattn/go-sqlite3 v1.14.6 h1:dNPt6NO46WmLVt2DLNpwczCmdV5boIZ6g/tlDrlRUbg= +github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= github.com/mattn/go-tty v0.0.0-20180907095812-13ff1204f104/go.mod h1:XPvLUNfbS4fJH25nqRHfWLMa1ONC8Amw+mIA639KxkE= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE= diff --git a/examples/chat2/main.go b/examples/chat2/main.go index c38f6b14..acdb4421 100644 --- a/examples/chat2/main.go +++ b/examples/chat2/main.go @@ -13,8 +13,6 @@ import ( "net" "net/http" "os" - "os/signal" - "syscall" "time" "github.com/ethereum/go-ethereum/crypto" @@ -113,27 +111,20 @@ func main() { ui.displayMessage("Connected to storenode: " + storenode) } - // TODO: query historic messages - /*time.Sleep(300 * time.Millisecond) + time.Sleep(300 * time.Millisecond) ui.displayMessage("Querying historic messages") response, err := wakuNode.Query(DefaultContentTopic, true, 0) if err != nil { - fmt.Println(err) - return - }*/ - + ui.displayMessage("Could not query storenode: " + err.Error()) + } else { + chat.displayMessages(response.Messages) + } }() //draw the UI if err = ui.Run(); err != nil { printErr("error running text UI: %s", err) } - - // 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...") } // Generates a random hex string with a length of n