mirror of
https://github.com/logos-messaging/logos-messaging-nim.git
synced 2026-07-30 03:53:28 +00:00
Each layer now separates its constructible core from its public surface:
- core module (waku.nim / messaging_client.nim /
reliable_channel_manager.nim): the type plus new/start/stop and the
private construction helpers.
- api/ folder: one module per differentiated set of operations
(waku: topics/relay/filter/lightpush/store/peer_manager/discovery/
debug/health) plus an events surface.
The waku api is reshaped to be the complete operation surface the C
bindings need, so the library no longer reaches into node internals:
relayPublish returns the message hash, relaySubscribe takes an optional
handler, filter/lightpush auto-select the service peer, connectedPeersInfo
returns structured data, pingPeer honours the timeout, plus
relayNumPeersInMesh / relayNumConnectedPeers / isOnline. library/ is now a
thin C-ABI shim: each {.ffi.} proc only marshals cstring/JSON/callbacks and
delegates to ctx.myLib[].waku.<op> (or messagingClient.<op>).
app_callbacks re-exports the modules defining its handler types, which the
included FFI files previously relied on by leakage.
Events move next to the surface that owns them, with each dependency kept
pointing the right way:
- waku/events/ relocated under waku/api/events/.
- channel events live in channels/api/events.nim.
- the four messaging-level message events move to messaging/api/events;
MessageSeenEvent stays in waku because it is emitted by waku core, so
moving it would make waku depend on the messaging layer.
- delivery_events renamed to filter_subscribe_events to match the
OnFilterSubscribe/Unsubscribe events it actually declares.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
46 lines
1.4 KiB
Nim
46 lines
1.4 KiB
Nim
## Waku layer API — ping operation.
|
|
{.push raises: [].}
|
|
|
|
import results, chronos, chronicles
|
|
import libp2p/protocols/ping
|
|
import libp2p/switch
|
|
|
|
import logos_delivery/waku/waku
|
|
import logos_delivery/waku/[waku_core, node/waku_node, node/waku_node/ping]
|
|
|
|
proc pingPeer*(
|
|
self: Waku, peerAddr: string, timeoutMs: int
|
|
): Future[Result[int64, string]] {.async.} =
|
|
## Pings the peer; `timeoutMs <= 0` means no timeout. Returns RTT in nanos.
|
|
try:
|
|
let peerInfo = parsePeerInfo(peerAddr).valueOr:
|
|
return err("pingPeer failed to parse peer addr: " & $error)
|
|
|
|
proc doPing(): Future[Result[Duration, string]] {.async.} =
|
|
try:
|
|
let conn =
|
|
await self.node.switch.dial(peerInfo.peerId, peerInfo.addrs, PingCodec)
|
|
defer:
|
|
await conn.close()
|
|
let rtt = await self.node.libp2pPing.ping(conn)
|
|
if rtt == 0.nanos:
|
|
return err("could not ping peer: rtt-0")
|
|
return ok(rtt)
|
|
except CatchableError as e:
|
|
return err("could not ping peer: " & e.msg)
|
|
|
|
let pingFut = doPing()
|
|
let rtt: Duration =
|
|
if timeoutMs <= 0:
|
|
(await pingFut).valueOr:
|
|
return err(error)
|
|
else:
|
|
if not await pingFut.withTimeout(chronos.milliseconds(timeoutMs)):
|
|
return err("ping timed out")
|
|
pingFut.read().valueOr:
|
|
return err(error)
|
|
|
|
return ok(rtt.nanos)
|
|
except CatchableError as e:
|
|
return err(e.msg)
|