Ivan Folgueira Bande 2f32e733aa refactor(cbindings): Thread-safe communication between the main thread and the Waku Thread (#1978)
* Thread-safe comms between main thread & Waku Thread - ChannelSPSCSingle.
* Renaming procs from 'new' to 'createShared'. They use the shared allocator.
* peer_manager_request: no need to use ptr WakuNode.
* waku_thread: moving the 'waitFor' to upper layer.
* waku_thread: `poll()` -> `waitFor sleepAsync(1)` to avoid risk of blocking.
* libwaku: thread-safe "sub-objects" in an inter-thread requests.
  When two threads send data each other, that data cannot contain any
  GC'ed type (string, seq, ref, closures) at any level.
* Allocating the 'configJson' in main thread and deallocating in Waku Thread.
2023-09-18 09:21:50 +02:00

57 lines
1.5 KiB
Nim

## This file contains the base message request type that will be handled.
## The requests are created by the main thread and processed by
## the Waku Thread.
import
std/json,
stew/results
import
chronos
import
../../../waku/node/waku_node,
./requests/node_lifecycle_request,
./requests/peer_manager_request,
./requests/protocols/relay_request
type
RequestType* {.pure.} = enum
LIFECYCLE,
PEER_MANAGER,
RELAY,
type
InterThreadRequest* = object
reqType: RequestType
reqContent: pointer
proc createShared*(T: type InterThreadRequest,
reqType: RequestType,
reqContent: pointer): ptr type T =
var ret = createShared(T)
ret[].reqType = reqType
ret[].reqContent = reqContent
return ret
proc process*(T: type InterThreadRequest,
request: ptr InterThreadRequest,
node: ptr WakuNode):
Future[Result[string, string]] {.async.} =
## Processes the request and deallocates its memory
defer: deallocShared(request)
echo "Request received: " & $request[].reqType
let retFut =
case request[].reqType
of LIFECYCLE:
cast[ptr NodeLifecycleRequest](request[].reqContent).process(node)
of PEER_MANAGER:
cast[ptr PeerManagementRequest](request[].reqContent).process(node[])
of RELAY:
cast[ptr RelayRequest](request[].reqContent).process(node)
return await retFut
proc `$`*(self: InterThreadRequest): string =
return $self.reqType