Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adding waku_dial_peer and get_connected_peers to libwaku #3149

Merged
merged 7 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,6 +97,15 @@ 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"
gabrielmer marked this conversation as resolved.
Show resolved Hide resolved
error "DIAL_PEER failed", error = msg
return err(msg)
of DIAL_PEER_BY_ID:
let peerId = PeerId.init($self[].peerId).valueOr:
error "DIAL_PEER_BY_ID failed", error = $error
Expand All @@ -104,5 +115,11 @@ proc process*(
let msg = "failed dialing peer"
error "DIAL_PEER_BY_ID failed", error = msg
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("")
Loading