mirror of
https://github.com/logos-messaging/logos-messaging-nim.git
synced 2026-01-07 16:33:08 +00:00
feat: add waku_get_connected_peers_info to libwaku (#3356)
This commit is contained in:
parent
6d3c758540
commit
93698a0a88
@ -168,6 +168,10 @@ int waku_get_peerids_from_peerstore(void* ctx,
|
|||||||
WakuCallBack callback,
|
WakuCallBack callback,
|
||||||
void* userData);
|
void* userData);
|
||||||
|
|
||||||
|
int waku_get_connected_peers_info(void* ctx,
|
||||||
|
WakuCallBack callback,
|
||||||
|
void* userData);
|
||||||
|
|
||||||
int waku_get_peerids_by_protocol(void* ctx,
|
int waku_get_peerids_by_protocol(void* ctx,
|
||||||
const char* protocol,
|
const char* protocol,
|
||||||
WakuCallBack callback,
|
WakuCallBack callback,
|
||||||
|
|||||||
@ -692,6 +692,20 @@ proc waku_get_peerids_from_peerstore(
|
|||||||
userData,
|
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(
|
proc waku_get_connected_peers(
|
||||||
ctx: ptr WakuContext, callback: WakuCallBack, userData: pointer
|
ctx: ptr WakuContext, callback: WakuCallBack, userData: pointer
|
||||||
): cint {.dynlib, exportc.} =
|
): cint {.dynlib, exportc.} =
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
import std/[sequtils, strutils]
|
import std/[sequtils, strutils]
|
||||||
import chronicles, chronos, results, options
|
import chronicles, chronos, results, options, json
|
||||||
import
|
import
|
||||||
../../../../waku/factory/waku,
|
../../../../waku/factory/waku,
|
||||||
../../../../waku/node/waku_node,
|
../../../../waku/node/waku_node,
|
||||||
@ -9,6 +9,7 @@ import
|
|||||||
type PeerManagementMsgType* {.pure.} = enum
|
type PeerManagementMsgType* {.pure.} = enum
|
||||||
CONNECT_TO
|
CONNECT_TO
|
||||||
GET_ALL_PEER_IDS
|
GET_ALL_PEER_IDS
|
||||||
|
GET_CONNECTED_PEERS_INFO
|
||||||
GET_PEER_IDS_BY_PROTOCOL
|
GET_PEER_IDS_BY_PROTOCOL
|
||||||
DISCONNECT_PEER_BY_ID
|
DISCONNECT_PEER_BY_ID
|
||||||
DIAL_PEER
|
DIAL_PEER
|
||||||
@ -22,6 +23,10 @@ type PeerManagementRequest* = object
|
|||||||
protocol: cstring
|
protocol: cstring
|
||||||
peerId: cstring
|
peerId: cstring
|
||||||
|
|
||||||
|
type PeerInfo = object
|
||||||
|
protocols: seq[string]
|
||||||
|
addresses: seq[string]
|
||||||
|
|
||||||
proc createShared*(
|
proc createShared*(
|
||||||
T: type PeerManagementRequest,
|
T: type PeerManagementRequest,
|
||||||
op: PeerManagementMsgType,
|
op: PeerManagementMsgType,
|
||||||
@ -83,6 +88,24 @@ proc process*(
|
|||||||
let peerIDs =
|
let peerIDs =
|
||||||
waku.node.peerManager.wakuPeerStore.peers().mapIt($it.peerId).join(",")
|
waku.node.peerManager.wakuPeerStore.peers().mapIt($it.peerId).join(",")
|
||||||
return ok(peerIDs)
|
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:
|
of GET_PEER_IDS_BY_PROTOCOL:
|
||||||
## returns a comma-separated string of peerIDs that mount the given protocol
|
## returns a comma-separated string of peerIDs that mount the given protocol
|
||||||
let connectedPeers = waku.node.peerManager.wakuPeerStore
|
let connectedPeers = waku.node.peerManager.wakuPeerStore
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user