Skip to content

Commit de11e57

Browse files
feat(libwaku): ping peer (#3144)
1 parent 69d9524 commit de11e57

File tree

4 files changed

+82
-1
lines changed

4 files changed

+82
-1
lines changed

library/libwaku.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,12 @@ int waku_peer_exchange_request(void* ctx,
181181
WakuCallBack callback,
182182
void* userData);
183183

184+
int waku_ping_peer(void* ctx,
185+
const char* peerAddr,
186+
int timeoutMs,
187+
WakuCallBack callback,
188+
void* userData);
189+
184190
#ifdef __cplusplus
185191
}
186192
#endif

library/libwaku.nim

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import
2222
./waku_thread/inter_thread_communication/requests/protocols/lightpush_request,
2323
./waku_thread/inter_thread_communication/requests/debug_node_request,
2424
./waku_thread/inter_thread_communication/requests/discovery_request,
25+
./waku_thread/inter_thread_communication/requests/ping_request,
2526
./waku_thread/inter_thread_communication/waku_thread_request,
2627
./alloc,
2728
./callback
@@ -672,5 +673,22 @@ proc waku_peer_exchange_request(
672673
)
673674
.handleRes(callback, userData)
674675

676+
proc waku_ping_peer(
677+
ctx: ptr WakuContext,
678+
peerID: cstring,
679+
timeoutMs: cuint,
680+
callback: WakuCallBack,
681+
userData: pointer,
682+
): cint {.dynlib, exportc.} =
683+
checkLibwakuParams(ctx, callback, userData)
684+
685+
waku_thread
686+
.sendRequestToWakuThread(
687+
ctx,
688+
RequestType.PING,
689+
PingRequest.createShared(peerID, chronos.milliseconds(timeoutMs)),
690+
)
691+
.handleRes(callback, userData)
692+
675693
### End of exported procs
676694
################################################################################
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import std/json
2+
import chronos, results
3+
import libp2p/[protocols/ping, switch, multiaddress, multicodec]
4+
import ../../../../waku/[factory/waku, waku_core/peers, node/waku_node], ../../../alloc
5+
6+
type PingRequest* = object
7+
peerAddr: cstring
8+
timeout: Duration
9+
10+
proc createShared*(
11+
T: type PingRequest, peerAddr: cstring, timeout: Duration
12+
): ptr type T =
13+
var ret = createShared(T)
14+
ret[].peerAddr = peerAddr.alloc()
15+
ret[].timeout = timeout
16+
return ret
17+
18+
proc destroyShared(self: ptr PingRequest) =
19+
deallocShared(self[].peerAddr)
20+
deallocShared(self)
21+
22+
proc process*(
23+
self: ptr PingRequest, waku: ptr Waku
24+
): Future[Result[string, string]] {.async.} =
25+
defer:
26+
destroyShared(self)
27+
28+
let peerInfo = peers.parsePeerInfo($self[].peerAddr).valueOr:
29+
return err("PingRequest failed to parse peer addr: " & $error)
30+
31+
proc ping(): Future[Result[Duration, string]] {.async, gcsafe.} =
32+
try:
33+
let conn = await waku.node.switch.dial(peerInfo.peerId, peerInfo.addrs, PingCodec)
34+
let pingRTT = await waku.node.libp2pPing.ping(conn)
35+
if pingRTT == 0.nanos:
36+
return err("could not ping peer: rtt-0")
37+
return ok(pingRTT)
38+
except CatchableError:
39+
return err("could not ping peer: " & getCurrentExceptionMsg())
40+
41+
let pingFuture = ping()
42+
let pingRTT: Duration =
43+
if self[].timeout == chronos.milliseconds(0): # No timeout expected
44+
(await pingFuture).valueOr:
45+
return err(error)
46+
else:
47+
let timedOut = not (await pingFuture.withTimeout(self[].timeout))
48+
if timedOut:
49+
return err("ping timed out")
50+
pingFuture.read().valueOr:
51+
return err(error)
52+
53+
ok($(pingRTT.nanos))

library/waku_thread/inter_thread_communication/waku_thread_request.nim

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ import
1212
./requests/protocols/store_request,
1313
./requests/protocols/lightpush_request,
1414
./requests/debug_node_request,
15-
./requests/discovery_request
15+
./requests/discovery_request,
16+
./requests/ping_request
1617

1718
type RequestType* {.pure.} = enum
1819
LIFECYCLE
1920
PEER_MANAGER
21+
PING
2022
RELAY
2123
STORE
2224
DEBUG
@@ -50,6 +52,8 @@ proc process*(
5052
cast[ptr NodeLifecycleRequest](request[].reqContent).process(waku)
5153
of PEER_MANAGER:
5254
cast[ptr PeerManagementRequest](request[].reqContent).process(waku[])
55+
of PING:
56+
cast[ptr PingRequest](request[].reqContent).process(waku)
5357
of RELAY:
5458
cast[ptr RelayRequest](request[].reqContent).process(waku)
5559
of STORE:

0 commit comments

Comments
 (0)