mirror of
https://github.com/logos-messaging/logos-delivery.git
synced 2026-04-23 08:33:09 +00:00
feat: add waku_relay_get_connected_peers to libwaku (#3353)
This commit is contained in:
parent
00808c9495
commit
8b443edd98
@ -117,6 +117,11 @@ int waku_relay_get_num_connected_peers(void* ctx,
|
|||||||
WakuCallBack callback,
|
WakuCallBack callback,
|
||||||
void* userData);
|
void* userData);
|
||||||
|
|
||||||
|
int waku_relay_get_connected_peers(void* ctx,
|
||||||
|
const char* pubSubTopic,
|
||||||
|
WakuCallBack callback,
|
||||||
|
void* userData);
|
||||||
|
|
||||||
int waku_relay_get_num_peers_in_mesh(void* ctx,
|
int waku_relay_get_num_peers_in_mesh(void* ctx,
|
||||||
const char* pubSubTopic,
|
const char* pubSubTopic,
|
||||||
WakuCallBack callback,
|
WakuCallBack callback,
|
||||||
|
|||||||
@ -429,6 +429,27 @@ proc waku_relay_get_num_connected_peers(
|
|||||||
initializeLibrary()
|
initializeLibrary()
|
||||||
checkLibwakuParams(ctx, callback, userData)
|
checkLibwakuParams(ctx, callback, userData)
|
||||||
|
|
||||||
|
let pst = pubSubTopic.alloc()
|
||||||
|
defer:
|
||||||
|
deallocShared(pst)
|
||||||
|
|
||||||
|
handleRequest(
|
||||||
|
ctx,
|
||||||
|
RequestType.RELAY,
|
||||||
|
RelayRequest.createShared(RelayMsgType.NUM_CONNECTED_PEERS, pst),
|
||||||
|
callback,
|
||||||
|
userData,
|
||||||
|
)
|
||||||
|
|
||||||
|
proc waku_relay_get_connected_peers(
|
||||||
|
ctx: ptr WakuContext,
|
||||||
|
pubSubTopic: cstring,
|
||||||
|
callback: WakuCallBack,
|
||||||
|
userData: pointer,
|
||||||
|
): cint {.dynlib, exportc.} =
|
||||||
|
initializeLibrary()
|
||||||
|
checkLibwakuParams(ctx, callback, userData)
|
||||||
|
|
||||||
let pst = pubSubTopic.alloc()
|
let pst = pubSubTopic.alloc()
|
||||||
defer:
|
defer:
|
||||||
deallocShared(pst)
|
deallocShared(pst)
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
import std/net
|
import std/[net, sequtils, strutils]
|
||||||
import chronicles, chronos, stew/byteutils, results
|
import chronicles, chronos, stew/byteutils, results
|
||||||
import
|
import
|
||||||
../../../../../waku/waku_core/message/message,
|
../../../../../waku/waku_core/message/message,
|
||||||
@ -8,12 +8,14 @@ import
|
|||||||
../../../../../waku/waku_core/time, # Timestamp
|
../../../../../waku/waku_core/time, # Timestamp
|
||||||
../../../../../waku/waku_core/topics/pubsub_topic,
|
../../../../../waku/waku_core/topics/pubsub_topic,
|
||||||
../../../../../waku/waku_relay/protocol,
|
../../../../../waku/waku_relay/protocol,
|
||||||
|
../../../../../waku/node/peer_manager,
|
||||||
../../../../alloc
|
../../../../alloc
|
||||||
|
|
||||||
type RelayMsgType* = enum
|
type RelayMsgType* = enum
|
||||||
SUBSCRIBE
|
SUBSCRIBE
|
||||||
UNSUBSCRIBE
|
UNSUBSCRIBE
|
||||||
PUBLISH
|
PUBLISH
|
||||||
|
NUM_CONNECTED_PEERS
|
||||||
LIST_CONNECTED_PEERS
|
LIST_CONNECTED_PEERS
|
||||||
## to return the list of all connected peers to an specific pubsub topic
|
## to return the list of all connected peers to an specific pubsub topic
|
||||||
LIST_MESH_PEERS
|
LIST_MESH_PEERS
|
||||||
@ -122,11 +124,17 @@ proc process*(
|
|||||||
|
|
||||||
let msgHash = computeMessageHash(pubSubTopic, msg).to0xHex
|
let msgHash = computeMessageHash(pubSubTopic, msg).to0xHex
|
||||||
return ok(msgHash)
|
return ok(msgHash)
|
||||||
of LIST_CONNECTED_PEERS:
|
of NUM_CONNECTED_PEERS:
|
||||||
let numConnPeers = waku.node.wakuRelay.getNumConnectedPeers($self.pubsubTopic).valueOr:
|
let numConnPeers = waku.node.wakuRelay.getNumConnectedPeers($self.pubsubTopic).valueOr:
|
||||||
error "LIST_CONNECTED_PEERS failed", error = error
|
error "NUM_CONNECTED_PEERS failed", error = error
|
||||||
return err($error)
|
return err($error)
|
||||||
return ok($numConnPeers)
|
return ok($numConnPeers)
|
||||||
|
of LIST_CONNECTED_PEERS:
|
||||||
|
let connPeers = waku.node.wakuRelay.getConnectedPeers($self.pubsubTopic).valueOr:
|
||||||
|
error "LIST_CONNECTED_PEERS failed", error = error
|
||||||
|
return err($error)
|
||||||
|
## returns a comma-separated string of peerIDs
|
||||||
|
return ok(connPeers.mapIt($it).join(","))
|
||||||
of LIST_MESH_PEERS:
|
of LIST_MESH_PEERS:
|
||||||
let numPeersInMesh = waku.node.wakuRelay.getNumPeersInMesh($self.pubsubTopic).valueOr:
|
let numPeersInMesh = waku.node.wakuRelay.getNumPeersInMesh($self.pubsubTopic).valueOr:
|
||||||
error "LIST_MESH_PEERS failed", error = error
|
error "LIST_MESH_PEERS failed", error = error
|
||||||
|
|||||||
@ -539,22 +539,23 @@ proc publish*(
|
|||||||
|
|
||||||
return ok(relayedPeerCount)
|
return ok(relayedPeerCount)
|
||||||
|
|
||||||
proc getNumConnectedPeers*(
|
proc getConnectedPeers*(
|
||||||
w: WakuRelay, pubsubTopic: PubsubTopic
|
w: WakuRelay, pubsubTopic: PubsubTopic
|
||||||
): Result[int, string] =
|
): Result[seq[PeerId], string] =
|
||||||
## Returns the number of connected peers and subscribed to the passed pubsub topic.
|
## Returns the list of peerIds of connected peers and subscribed to the passed pubsub topic.
|
||||||
## The 'gossipsub' atribute is defined in the GossipSub ref object.
|
## The 'gossipsub' atribute is defined in the GossipSub ref object.
|
||||||
|
|
||||||
if pubsubTopic == "":
|
if pubsubTopic == "":
|
||||||
## Return all the connected peers
|
## Return all the connected peers
|
||||||
var numConnPeers = 0
|
var peerIds = newSeq[PeerId]()
|
||||||
for k, v in w.gossipsub:
|
for k, v in w.gossipsub:
|
||||||
numConnPeers.inc(v.len)
|
peerIds.add(toSeq(v).mapIt(it.peerId))
|
||||||
return ok(numConnPeers)
|
# alternatively: peerIds &= toSeq(v).mapIt(it.peerId)
|
||||||
|
return ok(peerIds)
|
||||||
|
|
||||||
if not w.gossipsub.hasKey(pubsubTopic):
|
if not w.gossipsub.hasKey(pubsubTopic):
|
||||||
return err(
|
return err(
|
||||||
"getNumConnectedPeers - there is no gossipsub peer for the given pubsub topic: " &
|
"getConnectedPeers - there is no gossipsub peer for the given pubsub topic: " &
|
||||||
pubsubTopic
|
pubsubTopic
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -562,8 +563,22 @@ proc getNumConnectedPeers*(
|
|||||||
w.gossipsub[pubsubTopic]
|
w.gossipsub[pubsubTopic]
|
||||||
|
|
||||||
let peers: HashSet[PubSubPeer] = peersRes.valueOr:
|
let peers: HashSet[PubSubPeer] = peersRes.valueOr:
|
||||||
|
return
|
||||||
|
err("getConnectedPeers - exception accessing " & pubsubTopic & ": " & error.msg)
|
||||||
|
|
||||||
|
let peerIds = toSeq(peers).mapIt(it.peerId)
|
||||||
|
return ok(peerIds)
|
||||||
|
|
||||||
|
proc getNumConnectedPeers*(
|
||||||
|
w: WakuRelay, pubsubTopic: PubsubTopic
|
||||||
|
): Result[int, string] =
|
||||||
|
## Returns the number of connected peers and subscribed to the passed pubsub topic.
|
||||||
|
|
||||||
|
## Return all the connected peers
|
||||||
|
let peers = w.getConnectedPeers(pubsubTopic).valueOr:
|
||||||
return err(
|
return err(
|
||||||
"getNumConnectedPeers - exception accessing " & pubsubTopic & ": " & error.msg
|
"getNumConnectedPeers - failed retrieving peers in mesh: " & pubsubTopic & ": " &
|
||||||
|
error
|
||||||
)
|
)
|
||||||
|
|
||||||
return ok(peers.len)
|
return ok(peers.len)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user