Skip to content

Commit

Permalink
feat(libwaku): ping peer (#3144)
Browse files Browse the repository at this point in the history
  • Loading branch information
richard-ramos authored Oct 24, 2024
1 parent 69d9524 commit de11e57
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 1 deletion.
6 changes: 6 additions & 0 deletions library/libwaku.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,12 @@ int waku_peer_exchange_request(void* ctx,
WakuCallBack callback,
void* userData);

int waku_ping_peer(void* ctx,
const char* peerAddr,
int timeoutMs,
WakuCallBack callback,
void* userData);

#ifdef __cplusplus
}
#endif
Expand Down
18 changes: 18 additions & 0 deletions library/libwaku.nim
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import
./waku_thread/inter_thread_communication/requests/protocols/lightpush_request,
./waku_thread/inter_thread_communication/requests/debug_node_request,
./waku_thread/inter_thread_communication/requests/discovery_request,
./waku_thread/inter_thread_communication/requests/ping_request,
./waku_thread/inter_thread_communication/waku_thread_request,
./alloc,
./callback
Expand Down Expand Up @@ -672,5 +673,22 @@ proc waku_peer_exchange_request(
)
.handleRes(callback, userData)

proc waku_ping_peer(
ctx: ptr WakuContext,
peerID: cstring,
timeoutMs: cuint,
callback: WakuCallBack,
userData: pointer,
): cint {.dynlib, exportc.} =
checkLibwakuParams(ctx, callback, userData)

waku_thread
.sendRequestToWakuThread(
ctx,
RequestType.PING,
PingRequest.createShared(peerID, chronos.milliseconds(timeoutMs)),
)
.handleRes(callback, userData)

### End of exported procs
################################################################################
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import std/json
import chronos, results
import libp2p/[protocols/ping, switch, multiaddress, multicodec]
import ../../../../waku/[factory/waku, waku_core/peers, node/waku_node], ../../../alloc

type PingRequest* = object
peerAddr: cstring
timeout: Duration

proc createShared*(
T: type PingRequest, peerAddr: cstring, timeout: Duration
): ptr type T =
var ret = createShared(T)
ret[].peerAddr = peerAddr.alloc()
ret[].timeout = timeout
return ret

proc destroyShared(self: ptr PingRequest) =
deallocShared(self[].peerAddr)
deallocShared(self)

proc process*(
self: ptr PingRequest, waku: ptr Waku
): Future[Result[string, string]] {.async.} =
defer:
destroyShared(self)

let peerInfo = peers.parsePeerInfo($self[].peerAddr).valueOr:
return err("PingRequest failed to parse peer addr: " & $error)

proc ping(): Future[Result[Duration, string]] {.async, gcsafe.} =
try:
let conn = await waku.node.switch.dial(peerInfo.peerId, peerInfo.addrs, PingCodec)
let pingRTT = await waku.node.libp2pPing.ping(conn)
if pingRTT == 0.nanos:
return err("could not ping peer: rtt-0")
return ok(pingRTT)
except CatchableError:
return err("could not ping peer: " & getCurrentExceptionMsg())

let pingFuture = ping()
let pingRTT: Duration =
if self[].timeout == chronos.milliseconds(0): # No timeout expected
(await pingFuture).valueOr:
return err(error)
else:
let timedOut = not (await pingFuture.withTimeout(self[].timeout))
if timedOut:
return err("ping timed out")
pingFuture.read().valueOr:
return err(error)

ok($(pingRTT.nanos))
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ import
./requests/protocols/store_request,
./requests/protocols/lightpush_request,
./requests/debug_node_request,
./requests/discovery_request
./requests/discovery_request,
./requests/ping_request

type RequestType* {.pure.} = enum
LIFECYCLE
PEER_MANAGER
PING
RELAY
STORE
DEBUG
Expand Down Expand Up @@ -50,6 +52,8 @@ proc process*(
cast[ptr NodeLifecycleRequest](request[].reqContent).process(waku)
of PEER_MANAGER:
cast[ptr PeerManagementRequest](request[].reqContent).process(waku[])
of PING:
cast[ptr PingRequest](request[].reqContent).process(waku)
of RELAY:
cast[ptr RelayRequest](request[].reqContent).process(waku)
of STORE:
Expand Down

0 comments on commit de11e57

Please sign in to comment.