Skip to content

Commit

Permalink
chore: adding to libwaku dial and disconnect by peerIds (#3111)
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielmer authored Oct 15, 2024
1 parent 30c072a commit 25da810
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 11 deletions.
12 changes: 12 additions & 0 deletions library/libwaku.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,18 @@ int waku_connect(void* ctx,
WakuCallBack callback,
void* userData);

int waku_disconnect_peer_by_id(void* ctx,
const char* peerId,
WakuCallBack callback,
void* userData);

int waku_dial_peer_by_id(void* ctx,
const char* peerId,
const char* protocol,
int timeoutMs,
WakuCallBack callback,
void* userData);

int waku_get_peerids_from_peerstore(void* ctx,
WakuCallBack callback,
void* userData);
Expand Down
39 changes: 38 additions & 1 deletion library/libwaku.nim
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,41 @@ proc waku_connect(
)
.handleRes(callback, userData)

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

waku_thread
.sendRequestToWakuThread(
ctx,
RequestType.PEER_MANAGER,
PeerManagementRequest.createShared(
op = PeerManagementMsgType.DISCONNECT_PEER_BY_ID, peerId = $peerId
),
)
.handleRes(callback, userData)

proc waku_dial_peer_by_id(
ctx: ptr WakuContext,
peerId: 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_BY_ID, peerId = $peerId
),
)
.handleRes(callback, userData)

proc waku_get_peerids_from_peerstore(
ctx: ptr WakuContext, callback: WakuCallBack, userData: pointer
): cint {.dynlib, exportc.} =
Expand All @@ -504,7 +539,9 @@ proc waku_get_peerids_by_protocol(
.sendRequestToWakuThread(
ctx,
RequestType.PEER_MANAGER,
PeerManagementRequest.createGetPeerIdsByProtocolRequest($protocol),
PeerManagementRequest.createShared(
op = PeerManagementMsgType.GET_PEER_IDS_BY_PROTOCOL, protocol = $protocol
),
)
.handleRes(callback, userData)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import std/[sequtils, strutils]
import chronicles, chronos, results
import chronicles, chronos, results, options
import
../../../../waku/factory/waku,
../../../../waku/node/waku_node,
Expand All @@ -10,37 +10,39 @@ type PeerManagementMsgType* {.pure.} = enum
CONNECT_TO
GET_ALL_PEER_IDS
GET_PEER_IDS_BY_PROTOCOL
DISCONNECT_PEER_BY_ID
DIAL_PEER_BY_ID

type PeerManagementRequest* = object
operation: PeerManagementMsgType
peerMultiAddr: cstring
dialTimeout: Duration
protocol: cstring
peerId: cstring

proc createShared*(
T: type PeerManagementRequest,
op: PeerManagementMsgType,
peerMultiAddr = "",
dialTimeout = chronos.milliseconds(0), ## arbitrary Duration as not all ops needs dialTimeout
peerId = "",
protocol = "",
): ptr type T =
var ret = createShared(T)
ret[].operation = op
ret[].peerMultiAddr = peerMultiAddr.alloc()
ret[].dialTimeout = dialTimeout
return ret

proc createGetPeerIdsByProtocolRequest*(
T: type PeerManagementRequest, protocol = ""
): ptr type T =
var ret = createShared(T)
ret[].operation = PeerManagementMsgType.GET_PEER_IDS_BY_PROTOCOL
ret[].peerId = peerId.alloc()
ret[].protocol = protocol.alloc()
ret[].dialTimeout = dialTimeout
return ret

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

if not isNil(self[].peerId):
deallocShared(self[].peerId)

if not isNil(self[].protocol):
deallocShared(self[].protocol)

Expand Down Expand Up @@ -87,5 +89,20 @@ proc process*(
.mapIt($it.peerId)
.join(",")
return ok(connectedPeers)
of DISCONNECT_PEER_BY_ID:
let peerId = PeerId.init($self[].peerId).valueOr:
error "DISCONNECT_PEER_BY_ID failed", error = $error
return err($error)
await waku.node.peerManager.disconnectNode(peerId)
return ok("")
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
return err(msg)

return ok("")
5 changes: 4 additions & 1 deletion waku/node/peer_manager/peer_manager.nim
Original file line number Diff line number Diff line change
Expand Up @@ -381,9 +381,12 @@ proc connectToNodes*(
# later.
await sleepAsync(chronos.seconds(5))

proc disconnectNode*(pm: PeerManager, peerId: PeerId) {.async.} =
await pm.switch.disconnect(peerId)

proc disconnectNode*(pm: PeerManager, peer: RemotePeerInfo) {.async.} =
let peerId = peer.peerId
await pm.switch.disconnect(peerId)
await pm.disconnectNode(peerId)

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

0 comments on commit 25da810

Please sign in to comment.