Skip to content

Commit

Permalink
adding ping utility
Browse files Browse the repository at this point in the history
Former-commit-id: 1867cbb
  • Loading branch information
juligasa committed Sep 19, 2022
1 parent 4d5fb0d commit 005c14f
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 0 deletions.
10 changes: 10 additions & 0 deletions backend/BUILD.plz
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,13 @@ go_binary(
"//third_party:sqlite",
],
)

go_binary(
name = "pingp2p",
srcs = glob(["./cmd/pingp2p/*.go"]),
out = "pingp2p-" + target_platform_triple(),
cgo = True,
gomod = "//:gomod",
package = "./cmd/pingp2p",
visibility = ["PUBLIC"],
)
98 changes: 98 additions & 0 deletions backend/cmd/pingp2p/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package main

import (
"context"
"flag"
"fmt"
"os"
"os/signal"
"syscall"

"github.com/libp2p/go-libp2p"
peerstore "github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p/p2p/protocol/ping"
multiaddr "github.com/multiformats/go-multiaddr"
)

func main() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Pings a provided p2p node address to check visibility\n")
flag.PrintDefaults()
}
listen := flag.Bool("listen", false, "Launch a node and then listen for incoming pings until signal is received")
flag.Parse()

// start a libp2p node that listens on a random local TCP port,
// but without running the built-in ping protocol
node, err := libp2p.New(
libp2p.ListenAddrStrings("/ip4/127.0.0.1/tcp/0"),
libp2p.Ping(false),
)
if err != nil {
fmt.Printf("%s", err.Error())
os.Exit(1)
}

// configure our own ping protocol
pingService := &ping.PingService{Host: node}
node.SetStreamHandler(ping.ID, pingService.PingHandler)

// print the node's PeerInfo in multiaddr format
peerInfo := peerstore.AddrInfo{
ID: node.ID(),
Addrs: node.Addrs(),
}
addrs, err := peerstore.AddrInfoToP2pAddrs(&peerInfo)
if err != nil {
fmt.Printf("%s", err.Error())
os.Exit(1)
}

// if a remote peer has been passed on the command line, connect to it
// and send it 5 ping messages, otherwise wait for a signal to stop
if flag.NArg() == 1 {
if *listen {
fmt.Println("listen flag not allowed with positional arguments. ignoring listen")
}
fmt.Println("libp2p node address:", addrs[0])
addr, err := multiaddr.NewMultiaddr(flag.Arg(0))
if err != nil {
fmt.Printf("%s", err.Error())
os.Exit(1)
}
peer, err := peerstore.AddrInfoFromP2pAddr(addr)
if err != nil {
fmt.Printf("%s", err.Error())
os.Exit(1)
}
if err := node.Connect(context.Background(), *peer); err != nil {
fmt.Printf("%s", err.Error())
os.Exit(1)
}
fmt.Println("sending 5 ping messages to", addr)
ch := pingService.Ping(context.Background(), peer.ID)
for i := 0; i < 5; i++ {
res := <-ch
fmt.Println("pinged", addr, "in", res.RTT)
}
} else if *listen {
// wait for a SIGINT or SIGTERM signal
fmt.Println("libp2p node address:", addrs[0])
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
<-ch
fmt.Println("Received signal, shutting down...")
} else if flag.NArg() == 0 {
flag.Usage()
os.Exit(1)
} else {
fmt.Println("Some arguments were misspelled, please check the doc")
os.Exit(1)
}

// shut the node down
if err := node.Close(); err != nil {
fmt.Printf("%s", err.Error())
os.Exit(1)
}
}
4 changes: 4 additions & 0 deletions dev
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ def main():
run("plz build //frontend:app //backend:mintterd")
run("cargo tauri build --debug")

@cmd(cmds, "ping-p2p", "Execute ping utility to check visibility.")
def ping_p2p(args):
return run("plz run //backend:pingp2p", args=args)

@cmd(cmds, "run-backend", "Build and run mintterd binary for the current platform.")
def run_backend(args):
return run("plz run //backend:mintterd", args=args)
Expand Down

0 comments on commit 005c14f

Please sign in to comment.