Skip to content

Commit 507b1fc

Browse files
authored
feat: adding waku_dial_peer and get_connected_peers to libwaku (#3149)
1 parent 400d7a5 commit 507b1fc

File tree

3 files changed

+65
-2
lines changed

3 files changed

+65
-2
lines changed

library/libwaku.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,13 @@ int waku_disconnect_peer_by_id(void* ctx,
120120
WakuCallBack callback,
121121
void* userData);
122122

123+
int waku_dial_peer(void* ctx,
124+
const char* peerMultiAddr,
125+
const char* protocol,
126+
int timeoutMs,
127+
WakuCallBack callback,
128+
void* userData);
129+
123130
int waku_dial_peer_by_id(void* ctx,
124131
const char* peerId,
125132
const char* protocol,
@@ -140,6 +147,10 @@ int waku_listen_addresses(void* ctx,
140147
WakuCallBack callback,
141148
void* userData);
142149

150+
int waku_get_connected_peers(void* ctx,
151+
WakuCallBack callback,
152+
void* userData);
153+
143154
// Returns a list of multiaddress given a url to a DNS discoverable ENR tree
144155
// Parameters
145156
// char* entTreeUrl: URL containing a discoverable ENR tree

library/libwaku.nim

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,28 @@ proc waku_disconnect_peer_by_id(
498498
)
499499
.handleRes(callback, userData)
500500

501+
proc waku_dial_peer(
502+
ctx: ptr WakuContext,
503+
peerMultiAddr: cstring,
504+
protocol: cstring,
505+
timeoutMs: cuint,
506+
callback: WakuCallBack,
507+
userData: pointer,
508+
): cint {.dynlib, exportc.} =
509+
checkLibwakuParams(ctx, callback, userData)
510+
511+
waku_thread
512+
.sendRequestToWakuThread(
513+
ctx,
514+
RequestType.PEER_MANAGER,
515+
PeerManagementRequest.createShared(
516+
op = PeerManagementMsgType.DIAL_PEER,
517+
peerMultiAddr = $peerMultiAddr,
518+
protocol = $protocol,
519+
),
520+
)
521+
.handleRes(callback, userData)
522+
501523
proc waku_dial_peer_by_id(
502524
ctx: ptr WakuContext,
503525
peerId: cstring,
@@ -513,7 +535,7 @@ proc waku_dial_peer_by_id(
513535
ctx,
514536
RequestType.PEER_MANAGER,
515537
PeerManagementRequest.createShared(
516-
op = PeerManagementMsgType.DIAL_PEER_BY_ID, peerId = $peerId
538+
op = PeerManagementMsgType.DIAL_PEER_BY_ID, peerId = $peerId, protocol = $protocol
517539
),
518540
)
519541
.handleRes(callback, userData)
@@ -531,6 +553,19 @@ proc waku_get_peerids_from_peerstore(
531553
)
532554
.handleRes(callback, userData)
533555

556+
proc waku_get_connected_peers(
557+
ctx: ptr WakuContext, callback: WakuCallBack, userData: pointer
558+
): cint {.dynlib, exportc.} =
559+
checkLibwakuParams(ctx, callback, userData)
560+
561+
waku_thread
562+
.sendRequestToWakuThread(
563+
ctx,
564+
RequestType.PEER_MANAGER,
565+
PeerManagementRequest.createShared(PeerManagementMsgType.GET_CONNECTED_PEERS),
566+
)
567+
.handleRes(callback, userData)
568+
534569
proc waku_get_peerids_by_protocol(
535570
ctx: ptr WakuContext, protocol: cstring, callback: WakuCallBack, userData: pointer
536571
): cint {.dynlib, exportc.} =

library/waku_thread/inter_thread_communication/requests/peer_manager_request.nim

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ type PeerManagementMsgType* {.pure.} = enum
1111
GET_ALL_PEER_IDS
1212
GET_PEER_IDS_BY_PROTOCOL
1313
DISCONNECT_PEER_BY_ID
14+
DIAL_PEER
1415
DIAL_PEER_BY_ID
16+
GET_CONNECTED_PEERS
1517

1618
type PeerManagementRequest* = object
1719
operation: PeerManagementMsgType
@@ -95,14 +97,29 @@ proc process*(
9597
return err($error)
9698
await waku.node.peerManager.disconnectNode(peerId)
9799
return ok("")
100+
of DIAL_PEER:
101+
let remotePeerInfo = parsePeerInfo($self[].peerMultiAddr).valueOr:
102+
error "DIAL_PEER failed", error = $error
103+
return err($error)
104+
let conn = await waku.node.peerManager.dialPeer(remotePeerInfo, $self[].protocol)
105+
if conn.isNone():
106+
let msg = "failed dialing peer"
107+
error "DIAL_PEER failed", error = msg, peerId = $remotePeerInfo.peerId
108+
return err(msg)
98109
of DIAL_PEER_BY_ID:
99110
let peerId = PeerId.init($self[].peerId).valueOr:
100111
error "DIAL_PEER_BY_ID failed", error = $error
101112
return err($error)
102113
let conn = await waku.node.peerManager.dialPeer(peerId, $self[].protocol)
103114
if conn.isNone():
104115
let msg = "failed dialing peer"
105-
error "DIAL_PEER_BY_ID failed", error = msg
116+
error "DIAL_PEER_BY_ID failed", error = msg, peerId = $peerId
106117
return err(msg)
118+
of GET_CONNECTED_PEERS:
119+
## returns a comma-separated string of peerIDs
120+
let
121+
(inPeerIds, outPeerIds) = waku.node.peerManager.connectedPeers()
122+
connectedPeerids = concat(inPeerIds, outPeerIds)
123+
return ok(connectedPeerids.mapIt($it).join(","))
107124

108125
return ok("")

0 commit comments

Comments
 (0)