mirror of https://github.com/waku-org/nwaku.git
chore: adding to libwaku dial and disconnect by peerIds (#3111)
This commit is contained in:
parent
30c072a420
commit
25da810257
|
@ -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);
|
||||
|
|
|
@ -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.} =
|
||||
|
@ -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)
|
||||
|
||||
|
|
|
@ -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,
|
||||
|
@ -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)
|
||||
|
||||
|
@ -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("")
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue