mirror of
https://github.com/logos-messaging/logos-messaging-nim.git
synced 2026-01-03 06:23:10 +00:00
* 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.
57 lines
1.5 KiB
Nim
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 |