mirror of
https://github.com/logos-messaging/logos-messaging-nim.git
synced 2026-01-03 06:23:10 +00:00
feat: add waku_get_connected_peers_info to libwaku (#3356)
This commit is contained in:
parent
ef9074443a
commit
0eb9c6200f
@ -168,6 +168,10 @@ int waku_get_peerids_from_peerstore(void* ctx,
|
||||
WakuCallBack callback,
|
||||
void* userData);
|
||||
|
||||
int waku_get_connected_peers_info(void* ctx,
|
||||
WakuCallBack callback,
|
||||
void* userData);
|
||||
|
||||
int waku_get_peerids_by_protocol(void* ctx,
|
||||
const char* protocol,
|
||||
WakuCallBack callback,
|
||||
|
||||
@ -692,6 +692,20 @@ proc waku_get_peerids_from_peerstore(
|
||||
userData,
|
||||
)
|
||||
|
||||
proc waku_get_connected_peers_info(
|
||||
ctx: ptr WakuContext, callback: WakuCallBack, userData: pointer
|
||||
): cint {.dynlib, exportc.} =
|
||||
initializeLibrary()
|
||||
checkLibwakuParams(ctx, callback, userData)
|
||||
|
||||
handleRequest(
|
||||
ctx,
|
||||
RequestType.PEER_MANAGER,
|
||||
PeerManagementRequest.createShared(PeerManagementMsgType.GET_CONNECTED_PEERS_INFO),
|
||||
callback,
|
||||
userData,
|
||||
)
|
||||
|
||||
proc waku_get_connected_peers(
|
||||
ctx: ptr WakuContext, callback: WakuCallBack, userData: pointer
|
||||
): cint {.dynlib, exportc.} =
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
import std/[sequtils, strutils]
|
||||
import chronicles, chronos, results, options
|
||||
import chronicles, chronos, results, options, json
|
||||
import
|
||||
../../../../waku/factory/waku,
|
||||
../../../../waku/node/waku_node,
|
||||
@ -9,6 +9,7 @@ import
|
||||
type PeerManagementMsgType* {.pure.} = enum
|
||||
CONNECT_TO
|
||||
GET_ALL_PEER_IDS
|
||||
GET_CONNECTED_PEERS_INFO
|
||||
GET_PEER_IDS_BY_PROTOCOL
|
||||
DISCONNECT_PEER_BY_ID
|
||||
DIAL_PEER
|
||||
@ -22,6 +23,10 @@ type PeerManagementRequest* = object
|
||||
protocol: cstring
|
||||
peerId: cstring
|
||||
|
||||
type PeerInfo = object
|
||||
protocols: seq[string]
|
||||
addresses: seq[string]
|
||||
|
||||
proc createShared*(
|
||||
T: type PeerManagementRequest,
|
||||
op: PeerManagementMsgType,
|
||||
@ -83,6 +88,24 @@ proc process*(
|
||||
let peerIDs =
|
||||
waku.node.peerManager.wakuPeerStore.peers().mapIt($it.peerId).join(",")
|
||||
return ok(peerIDs)
|
||||
of GET_CONNECTED_PEERS_INFO:
|
||||
## returns a JSON string mapping peerIDs to objects with protocols and addresses
|
||||
|
||||
var peersMap = initTable[string, PeerInfo]()
|
||||
let peers = waku.node.peerManager.wakuPeerStore.peers().filterIt(
|
||||
it.connectedness == Connected
|
||||
)
|
||||
|
||||
# Build a map of peer IDs to peer info objects
|
||||
for peer in peers:
|
||||
let peerIdStr = $peer.peerId
|
||||
peersMap[peerIdStr] =
|
||||
PeerInfo(protocols: peer.protocols, addresses: peer.addrs.mapIt($it))
|
||||
|
||||
# Convert the map to JSON string
|
||||
let jsonObj = %*peersMap
|
||||
let jsonStr = $jsonObj
|
||||
return ok(jsonStr)
|
||||
of GET_PEER_IDS_BY_PROTOCOL:
|
||||
## returns a comma-separated string of peerIDs that mount the given protocol
|
||||
let connectedPeers = waku.node.peerManager.wakuPeerStore
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user