-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
82 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
library/waku_thread/inter_thread_communication/requests/ping_request.nim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters