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,
|
WakuCallBack callback,
|
||||||
void* userData);
|
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,
|
int waku_get_peerids_from_peerstore(void* ctx,
|
||||||
WakuCallBack callback,
|
WakuCallBack callback,
|
||||||
void* userData);
|
void* userData);
|
||||||
|
|
|
@ -482,6 +482,41 @@ proc waku_connect(
|
||||||
)
|
)
|
||||||
.handleRes(callback, userData)
|
.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(
|
proc waku_get_peerids_from_peerstore(
|
||||||
ctx: ptr WakuContext, callback: WakuCallBack, userData: pointer
|
ctx: ptr WakuContext, callback: WakuCallBack, userData: pointer
|
||||||
): cint {.dynlib, exportc.} =
|
): cint {.dynlib, exportc.} =
|
||||||
|
@ -504,7 +539,9 @@ proc waku_get_peerids_by_protocol(
|
||||||
.sendRequestToWakuThread(
|
.sendRequestToWakuThread(
|
||||||
ctx,
|
ctx,
|
||||||
RequestType.PEER_MANAGER,
|
RequestType.PEER_MANAGER,
|
||||||
PeerManagementRequest.createGetPeerIdsByProtocolRequest($protocol),
|
PeerManagementRequest.createShared(
|
||||||
|
op = PeerManagementMsgType.GET_PEER_IDS_BY_PROTOCOL, protocol = $protocol
|
||||||
|
),
|
||||||
)
|
)
|
||||||
.handleRes(callback, userData)
|
.handleRes(callback, userData)
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import std/[sequtils, strutils]
|
import std/[sequtils, strutils]
|
||||||
import chronicles, chronos, results
|
import chronicles, chronos, results, options
|
||||||
import
|
import
|
||||||
../../../../waku/factory/waku,
|
../../../../waku/factory/waku,
|
||||||
../../../../waku/node/waku_node,
|
../../../../waku/node/waku_node,
|
||||||
|
@ -10,37 +10,39 @@ type PeerManagementMsgType* {.pure.} = enum
|
||||||
CONNECT_TO
|
CONNECT_TO
|
||||||
GET_ALL_PEER_IDS
|
GET_ALL_PEER_IDS
|
||||||
GET_PEER_IDS_BY_PROTOCOL
|
GET_PEER_IDS_BY_PROTOCOL
|
||||||
|
DISCONNECT_PEER_BY_ID
|
||||||
|
DIAL_PEER_BY_ID
|
||||||
|
|
||||||
type PeerManagementRequest* = object
|
type PeerManagementRequest* = object
|
||||||
operation: PeerManagementMsgType
|
operation: PeerManagementMsgType
|
||||||
peerMultiAddr: cstring
|
peerMultiAddr: cstring
|
||||||
dialTimeout: Duration
|
dialTimeout: Duration
|
||||||
protocol: cstring
|
protocol: cstring
|
||||||
|
peerId: cstring
|
||||||
|
|
||||||
proc createShared*(
|
proc createShared*(
|
||||||
T: type PeerManagementRequest,
|
T: type PeerManagementRequest,
|
||||||
op: PeerManagementMsgType,
|
op: PeerManagementMsgType,
|
||||||
peerMultiAddr = "",
|
peerMultiAddr = "",
|
||||||
dialTimeout = chronos.milliseconds(0), ## arbitrary Duration as not all ops needs dialTimeout
|
dialTimeout = chronos.milliseconds(0), ## arbitrary Duration as not all ops needs dialTimeout
|
||||||
|
peerId = "",
|
||||||
|
protocol = "",
|
||||||
): ptr type T =
|
): ptr type T =
|
||||||
var ret = createShared(T)
|
var ret = createShared(T)
|
||||||
ret[].operation = op
|
ret[].operation = op
|
||||||
ret[].peerMultiAddr = peerMultiAddr.alloc()
|
ret[].peerMultiAddr = peerMultiAddr.alloc()
|
||||||
ret[].dialTimeout = dialTimeout
|
ret[].peerId = peerId.alloc()
|
||||||
return ret
|
|
||||||
|
|
||||||
proc createGetPeerIdsByProtocolRequest*(
|
|
||||||
T: type PeerManagementRequest, protocol = ""
|
|
||||||
): ptr type T =
|
|
||||||
var ret = createShared(T)
|
|
||||||
ret[].operation = PeerManagementMsgType.GET_PEER_IDS_BY_PROTOCOL
|
|
||||||
ret[].protocol = protocol.alloc()
|
ret[].protocol = protocol.alloc()
|
||||||
|
ret[].dialTimeout = dialTimeout
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
proc destroyShared(self: ptr PeerManagementRequest) =
|
proc destroyShared(self: ptr PeerManagementRequest) =
|
||||||
if not isNil(self[].peerMultiAddr):
|
if not isNil(self[].peerMultiAddr):
|
||||||
deallocShared(self[].peerMultiAddr)
|
deallocShared(self[].peerMultiAddr)
|
||||||
|
|
||||||
|
if not isNil(self[].peerId):
|
||||||
|
deallocShared(self[].peerId)
|
||||||
|
|
||||||
if not isNil(self[].protocol):
|
if not isNil(self[].protocol):
|
||||||
deallocShared(self[].protocol)
|
deallocShared(self[].protocol)
|
||||||
|
|
||||||
|
@ -87,5 +89,20 @@ proc process*(
|
||||||
.mapIt($it.peerId)
|
.mapIt($it.peerId)
|
||||||
.join(",")
|
.join(",")
|
||||||
return ok(connectedPeers)
|
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("")
|
return ok("")
|
||||||
|
|
|
@ -381,9 +381,12 @@ proc connectToNodes*(
|
||||||
# later.
|
# later.
|
||||||
await sleepAsync(chronos.seconds(5))
|
await sleepAsync(chronos.seconds(5))
|
||||||
|
|
||||||
|
proc disconnectNode*(pm: PeerManager, peerId: PeerId) {.async.} =
|
||||||
|
await pm.switch.disconnect(peerId)
|
||||||
|
|
||||||
proc disconnectNode*(pm: PeerManager, peer: RemotePeerInfo) {.async.} =
|
proc disconnectNode*(pm: PeerManager, peer: RemotePeerInfo) {.async.} =
|
||||||
let peerId = peer.peerId
|
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
|
# 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
|
# This shall not be used to dial Relay protocols, since that would create
|
||||||
|
|
Loading…
Reference in New Issue