Skip to content

Commit 25da810

Browse files
authored
chore: adding to libwaku dial and disconnect by peerIds (#3111)
1 parent 30c072a commit 25da810

File tree

4 files changed

+80
-11
lines changed

4 files changed

+80
-11
lines changed

library/libwaku.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,18 @@ int waku_connect(void* ctx,
115115
WakuCallBack callback,
116116
void* userData);
117117

118+
int waku_disconnect_peer_by_id(void* ctx,
119+
const char* peerId,
120+
WakuCallBack callback,
121+
void* userData);
122+
123+
int waku_dial_peer_by_id(void* ctx,
124+
const char* peerId,
125+
const char* protocol,
126+
int timeoutMs,
127+
WakuCallBack callback,
128+
void* userData);
129+
118130
int waku_get_peerids_from_peerstore(void* ctx,
119131
WakuCallBack callback,
120132
void* userData);

library/libwaku.nim

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,41 @@ proc waku_connect(
482482
)
483483
.handleRes(callback, userData)
484484

485+
proc waku_disconnect_peer_by_id(
486+
ctx: ptr WakuContext, peerId: cstring, callback: WakuCallBack, userData: pointer
487+
): cint {.dynlib, exportc.} =
488+
checkLibwakuParams(ctx, callback, userData)
489+
490+
waku_thread
491+
.sendRequestToWakuThread(
492+
ctx,
493+
RequestType.PEER_MANAGER,
494+
PeerManagementRequest.createShared(
495+
op = PeerManagementMsgType.DISCONNECT_PEER_BY_ID, peerId = $peerId
496+
),
497+
)
498+
.handleRes(callback, userData)
499+
500+
proc waku_dial_peer_by_id(
501+
ctx: ptr WakuContext,
502+
peerId: cstring,
503+
protocol: cstring,
504+
timeoutMs: cuint,
505+
callback: WakuCallBack,
506+
userData: pointer,
507+
): cint {.dynlib, exportc.} =
508+
checkLibwakuParams(ctx, callback, userData)
509+
510+
waku_thread
511+
.sendRequestToWakuThread(
512+
ctx,
513+
RequestType.PEER_MANAGER,
514+
PeerManagementRequest.createShared(
515+
op = PeerManagementMsgType.DIAL_PEER_BY_ID, peerId = $peerId
516+
),
517+
)
518+
.handleRes(callback, userData)
519+
485520
proc waku_get_peerids_from_peerstore(
486521
ctx: ptr WakuContext, callback: WakuCallBack, userData: pointer
487522
): cint {.dynlib, exportc.} =
@@ -504,7 +539,9 @@ proc waku_get_peerids_by_protocol(
504539
.sendRequestToWakuThread(
505540
ctx,
506541
RequestType.PEER_MANAGER,
507-
PeerManagementRequest.createGetPeerIdsByProtocolRequest($protocol),
542+
PeerManagementRequest.createShared(
543+
op = PeerManagementMsgType.GET_PEER_IDS_BY_PROTOCOL, protocol = $protocol
544+
),
508545
)
509546
.handleRes(callback, userData)
510547

library/waku_thread/inter_thread_communication/requests/peer_manager_request.nim

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import std/[sequtils, strutils]
2-
import chronicles, chronos, results
2+
import chronicles, chronos, results, options
33
import
44
../../../../waku/factory/waku,
55
../../../../waku/node/waku_node,
@@ -10,37 +10,39 @@ type PeerManagementMsgType* {.pure.} = enum
1010
CONNECT_TO
1111
GET_ALL_PEER_IDS
1212
GET_PEER_IDS_BY_PROTOCOL
13+
DISCONNECT_PEER_BY_ID
14+
DIAL_PEER_BY_ID
1315

1416
type PeerManagementRequest* = object
1517
operation: PeerManagementMsgType
1618
peerMultiAddr: cstring
1719
dialTimeout: Duration
1820
protocol: cstring
21+
peerId: cstring
1922

2023
proc createShared*(
2124
T: type PeerManagementRequest,
2225
op: PeerManagementMsgType,
2326
peerMultiAddr = "",
2427
dialTimeout = chronos.milliseconds(0), ## arbitrary Duration as not all ops needs dialTimeout
28+
peerId = "",
29+
protocol = "",
2530
): ptr type T =
2631
var ret = createShared(T)
2732
ret[].operation = op
2833
ret[].peerMultiAddr = peerMultiAddr.alloc()
29-
ret[].dialTimeout = dialTimeout
30-
return ret
31-
32-
proc createGetPeerIdsByProtocolRequest*(
33-
T: type PeerManagementRequest, protocol = ""
34-
): ptr type T =
35-
var ret = createShared(T)
36-
ret[].operation = PeerManagementMsgType.GET_PEER_IDS_BY_PROTOCOL
34+
ret[].peerId = peerId.alloc()
3735
ret[].protocol = protocol.alloc()
36+
ret[].dialTimeout = dialTimeout
3837
return ret
3938

4039
proc destroyShared(self: ptr PeerManagementRequest) =
4140
if not isNil(self[].peerMultiAddr):
4241
deallocShared(self[].peerMultiAddr)
4342

43+
if not isNil(self[].peerId):
44+
deallocShared(self[].peerId)
45+
4446
if not isNil(self[].protocol):
4547
deallocShared(self[].protocol)
4648

@@ -87,5 +89,20 @@ proc process*(
8789
.mapIt($it.peerId)
8890
.join(",")
8991
return ok(connectedPeers)
92+
of DISCONNECT_PEER_BY_ID:
93+
let peerId = PeerId.init($self[].peerId).valueOr:
94+
error "DISCONNECT_PEER_BY_ID failed", error = $error
95+
return err($error)
96+
await waku.node.peerManager.disconnectNode(peerId)
97+
return ok("")
98+
of DIAL_PEER_BY_ID:
99+
let peerId = PeerId.init($self[].peerId).valueOr:
100+
error "DIAL_PEER_BY_ID failed", error = $error
101+
return err($error)
102+
let conn = await waku.node.peerManager.dialPeer(peerId, $self[].protocol)
103+
if conn.isNone():
104+
let msg = "failed dialing peer"
105+
error "DIAL_PEER_BY_ID failed", error = msg
106+
return err(msg)
90107

91108
return ok("")

waku/node/peer_manager/peer_manager.nim

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,9 +381,12 @@ proc connectToNodes*(
381381
# later.
382382
await sleepAsync(chronos.seconds(5))
383383

384+
proc disconnectNode*(pm: PeerManager, peerId: PeerId) {.async.} =
385+
await pm.switch.disconnect(peerId)
386+
384387
proc disconnectNode*(pm: PeerManager, peer: RemotePeerInfo) {.async.} =
385388
let peerId = peer.peerId
386-
await pm.switch.disconnect(peerId)
389+
await pm.disconnectNode(peerId)
387390

388391
# Dialing should be used for just protocols that require a stream to write and read
389392
# This shall not be used to dial Relay protocols, since that would create

0 commit comments

Comments
 (0)