Skip to content

Commit

Permalink
feat: adding waku_dial_peer and get_connected_peers to libwaku (#3149)
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielmer authored Oct 30, 2024
1 parent 400d7a5 commit 507b1fc
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
11 changes: 11 additions & 0 deletions library/libwaku.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,13 @@ int waku_disconnect_peer_by_id(void* ctx,
WakuCallBack callback,
void* userData);

int waku_dial_peer(void* ctx,
const char* peerMultiAddr,
const char* protocol,
int timeoutMs,
WakuCallBack callback,
void* userData);

int waku_dial_peer_by_id(void* ctx,
const char* peerId,
const char* protocol,
Expand All @@ -140,6 +147,10 @@ int waku_listen_addresses(void* ctx,
WakuCallBack callback,
void* userData);

int waku_get_connected_peers(void* ctx,
WakuCallBack callback,
void* userData);

// Returns a list of multiaddress given a url to a DNS discoverable ENR tree
// Parameters
// char* entTreeUrl: URL containing a discoverable ENR tree
Expand Down
37 changes: 36 additions & 1 deletion library/libwaku.nim
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,28 @@ proc waku_disconnect_peer_by_id(
)
.handleRes(callback, userData)

proc waku_dial_peer(
ctx: ptr WakuContext,
peerMultiAddr: cstring,
protocol: cstring,
timeoutMs: cuint,
callback: WakuCallBack,
userData: pointer,
): cint {.dynlib, exportc.} =
checkLibwakuParams(ctx, callback, userData)

waku_thread
.sendRequestToWakuThread(
ctx,
RequestType.PEER_MANAGER,
PeerManagementRequest.createShared(
op = PeerManagementMsgType.DIAL_PEER,
peerMultiAddr = $peerMultiAddr,
protocol = $protocol,
),
)
.handleRes(callback, userData)

proc waku_dial_peer_by_id(
ctx: ptr WakuContext,
peerId: cstring,
Expand All @@ -513,7 +535,7 @@ proc waku_dial_peer_by_id(
ctx,
RequestType.PEER_MANAGER,
PeerManagementRequest.createShared(
op = PeerManagementMsgType.DIAL_PEER_BY_ID, peerId = $peerId
op = PeerManagementMsgType.DIAL_PEER_BY_ID, peerId = $peerId, protocol = $protocol
),
)
.handleRes(callback, userData)
Expand All @@ -531,6 +553,19 @@ proc waku_get_peerids_from_peerstore(
)
.handleRes(callback, userData)

proc waku_get_connected_peers(
ctx: ptr WakuContext, callback: WakuCallBack, userData: pointer
): cint {.dynlib, exportc.} =
checkLibwakuParams(ctx, callback, userData)

waku_thread
.sendRequestToWakuThread(
ctx,
RequestType.PEER_MANAGER,
PeerManagementRequest.createShared(PeerManagementMsgType.GET_CONNECTED_PEERS),
)
.handleRes(callback, userData)

proc waku_get_peerids_by_protocol(
ctx: ptr WakuContext, protocol: cstring, callback: WakuCallBack, userData: pointer
): cint {.dynlib, exportc.} =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ type PeerManagementMsgType* {.pure.} = enum
GET_ALL_PEER_IDS
GET_PEER_IDS_BY_PROTOCOL
DISCONNECT_PEER_BY_ID
DIAL_PEER
DIAL_PEER_BY_ID
GET_CONNECTED_PEERS

type PeerManagementRequest* = object
operation: PeerManagementMsgType
Expand Down Expand Up @@ -95,14 +97,29 @@ proc process*(
return err($error)
await waku.node.peerManager.disconnectNode(peerId)
return ok("")
of DIAL_PEER:
let remotePeerInfo = parsePeerInfo($self[].peerMultiAddr).valueOr:
error "DIAL_PEER failed", error = $error
return err($error)
let conn = await waku.node.peerManager.dialPeer(remotePeerInfo, $self[].protocol)
if conn.isNone():
let msg = "failed dialing peer"
error "DIAL_PEER failed", error = msg, peerId = $remotePeerInfo.peerId
return err(msg)
of DIAL_PEER_BY_ID:
let peerId = PeerId.init($self[].peerId).valueOr:
error "DIAL_PEER_BY_ID failed", error = $error
return err($error)
let conn = await waku.node.peerManager.dialPeer(peerId, $self[].protocol)
if conn.isNone():
let msg = "failed dialing peer"
error "DIAL_PEER_BY_ID failed", error = msg
error "DIAL_PEER_BY_ID failed", error = msg, peerId = $peerId
return err(msg)
of GET_CONNECTED_PEERS:
## returns a comma-separated string of peerIDs
let
(inPeerIds, outPeerIds) = waku.node.peerManager.connectedPeers()
connectedPeerids = concat(inPeerIds, outPeerIds)
return ok(connectedPeerids.mapIt($it).join(","))

return ok("")

0 comments on commit 507b1fc

Please sign in to comment.