aboutsummaryrefslogtreecommitdiffstats
path: root/p2p/simulations/adapters/ws.go
diff options
context:
space:
mode:
authorIvan Daniluk <ivan.daniluk@gmail.com>2018-05-08 23:05:27 +0800
committerFelix Lange <fjl@users.noreply.github.com>2018-05-08 23:05:27 +0800
commitc4a4613d9504db43a26a3c79dda8bf6be0d1237a (patch)
treea9bb552bddbb3fe3ecc795768b376f5b99104e16 /p2p/simulations/adapters/ws.go
parentfedae9501582cb64c850def557b1fc3742d892dc (diff)
downloaddexon-c4a4613d9504db43a26a3c79dda8bf6be0d1237a.tar.gz
dexon-c4a4613d9504db43a26a3c79dda8bf6be0d1237a.tar.zst
dexon-c4a4613d9504db43a26a3c79dda8bf6be0d1237a.zip
p2p/simulations/adapters: fix websocket log line parsing in exec adapter (#16667)
Diffstat (limited to 'p2p/simulations/adapters/ws.go')
-rw-r--r--p2p/simulations/adapters/ws.go51
1 files changed, 51 insertions, 0 deletions
diff --git a/p2p/simulations/adapters/ws.go b/p2p/simulations/adapters/ws.go
new file mode 100644
index 000000000..979a21709
--- /dev/null
+++ b/p2p/simulations/adapters/ws.go
@@ -0,0 +1,51 @@
+package adapters
+
+import (
+ "bufio"
+ "errors"
+ "io"
+ "regexp"
+ "strings"
+ "time"
+)
+
+// wsAddrPattern is a regex used to read the WebSocket address from the node's
+// log
+var wsAddrPattern = regexp.MustCompile(`ws://[\d.:]+`)
+
+func matchWSAddr(str string) (string, bool) {
+ if !strings.Contains(str, "WebSocket endpoint opened") {
+ return "", false
+ }
+
+ return wsAddrPattern.FindString(str), true
+}
+
+// findWSAddr scans through reader r, looking for the log entry with
+// WebSocket address information.
+func findWSAddr(r io.Reader, timeout time.Duration) (string, error) {
+ ch := make(chan string)
+
+ go func() {
+ s := bufio.NewScanner(r)
+ for s.Scan() {
+ addr, ok := matchWSAddr(s.Text())
+ if ok {
+ ch <- addr
+ }
+ }
+ close(ch)
+ }()
+
+ var wsAddr string
+ select {
+ case wsAddr = <-ch:
+ if wsAddr == "" {
+ return "", errors.New("empty result")
+ }
+ case <-time.After(timeout):
+ return "", errors.New("timed out")
+ }
+
+ return wsAddr, nil
+}