2024-10-10 08:40:09 -04:00
|
|
|
import std/[options, json, strutils, net]
|
2024-07-09 13:14:28 +02:00
|
|
|
import chronos, chronicles, results, confutils, confutils/std/net
|
2024-05-10 10:56:17 +02:00
|
|
|
|
2023-07-31 09:52:04 +02:00
|
|
|
import
|
2025-06-26 11:27:39 +02:00
|
|
|
../../../waku/node/peer_manager/peer_manager,
|
|
|
|
|
../../../waku/factory/external_config,
|
|
|
|
|
../../../waku/factory/waku,
|
|
|
|
|
../../../waku/factory/node_factory,
|
|
|
|
|
../../../waku/factory/networks_config,
|
|
|
|
|
../../../waku/factory/app_callbacks,
|
|
|
|
|
../../../waku/waku_api/rest/builder,
|
|
|
|
|
../../alloc
|
2023-07-31 09:52:04 +02:00
|
|
|
|
2024-03-16 00:08:47 +01:00
|
|
|
type NodeLifecycleMsgType* = enum
|
|
|
|
|
CREATE_NODE
|
|
|
|
|
START_NODE
|
|
|
|
|
STOP_NODE
|
2023-07-31 09:52:04 +02:00
|
|
|
|
2024-03-16 00:08:47 +01:00
|
|
|
type NodeLifecycleRequest* = object
|
|
|
|
|
operation: NodeLifecycleMsgType
|
|
|
|
|
configJson: cstring ## Only used in 'CREATE_NODE' operation
|
2024-12-13 17:38:16 +01:00
|
|
|
appCallbacks: AppCallbacks
|
2023-07-31 09:52:04 +02:00
|
|
|
|
2024-03-16 00:08:47 +01:00
|
|
|
proc createShared*(
|
2024-12-13 17:38:16 +01:00
|
|
|
T: type NodeLifecycleRequest,
|
|
|
|
|
op: NodeLifecycleMsgType,
|
|
|
|
|
configJson: cstring = "",
|
|
|
|
|
appCallbacks: AppCallbacks = nil,
|
2024-03-16 00:08:47 +01:00
|
|
|
): ptr type T =
|
2023-09-18 09:21:50 +02:00
|
|
|
var ret = createShared(T)
|
|
|
|
|
ret[].operation = op
|
2024-12-13 17:38:16 +01:00
|
|
|
ret[].appCallbacks = appCallbacks
|
2023-09-18 09:21:50 +02:00
|
|
|
ret[].configJson = configJson.alloc()
|
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
|
proc destroyShared(self: ptr NodeLifecycleRequest) =
|
|
|
|
|
deallocShared(self[].configJson)
|
|
|
|
|
deallocShared(self)
|
2023-09-01 08:37:02 +02:00
|
|
|
|
2024-12-13 17:38:16 +01:00
|
|
|
proc createWaku(
|
|
|
|
|
configJson: cstring, appCallbacks: AppCallbacks = nil
|
|
|
|
|
): Future[Result[Waku, string]] {.async.} =
|
2024-05-10 10:56:17 +02:00
|
|
|
var conf = defaultWakuNodeConf().valueOr:
|
|
|
|
|
return err("Failed creating node: " & error)
|
|
|
|
|
|
2024-02-20 16:00:03 -04:00
|
|
|
var errorResp: string
|
2023-09-01 08:37:02 +02:00
|
|
|
|
2024-05-21 18:37:50 +02:00
|
|
|
var jsonNode: JsonNode
|
2023-12-14 07:16:39 +01:00
|
|
|
try:
|
2024-05-21 18:37:50 +02:00
|
|
|
jsonNode = parseJson($configJson)
|
|
|
|
|
except Exception:
|
|
|
|
|
return err(
|
|
|
|
|
"exception in createWaku when calling parseJson: " & getCurrentExceptionMsg() &
|
|
|
|
|
" configJson string: " & $configJson
|
|
|
|
|
)
|
2023-09-01 08:37:02 +02:00
|
|
|
|
2024-05-21 18:37:50 +02:00
|
|
|
for confField, confValue in fieldPairs(conf):
|
|
|
|
|
if jsonNode.contains(confField):
|
|
|
|
|
# Make sure string doesn't contain the leading or trailing " character
|
|
|
|
|
let formattedString = ($jsonNode[confField]).strip(chars = {'\"'})
|
|
|
|
|
# Override conf field with the value set in the json-string
|
|
|
|
|
try:
|
2024-05-10 10:56:17 +02:00
|
|
|
confValue = parseCmdArg(typeof(confValue), formattedString)
|
2024-05-21 18:37:50 +02:00
|
|
|
except Exception:
|
|
|
|
|
return err(
|
|
|
|
|
"exception in createWaku when parsing configuration. exc: " &
|
|
|
|
|
getCurrentExceptionMsg() & ". string that could not be parsed: " &
|
|
|
|
|
formattedString & ". expected type: " & $typeof(confValue)
|
|
|
|
|
)
|
2023-09-01 08:37:02 +02:00
|
|
|
|
2025-02-05 18:16:37 +02:00
|
|
|
# Don't send relay app callbacks if relay is disabled
|
|
|
|
|
if not conf.relay and not appCallbacks.isNil():
|
|
|
|
|
appCallbacks.relayHandler = nil
|
|
|
|
|
appCallbacks.topicHealthChangeHandler = nil
|
|
|
|
|
|
2025-05-08 07:05:35 +10:00
|
|
|
# TODO: Convert `confJson` directly to `WakuConf`
|
2025-06-16 18:44:21 +02:00
|
|
|
var wakuConf = conf.toWakuConf().valueOr:
|
2025-05-08 07:05:35 +10:00
|
|
|
return err("Configuration error: " & $error)
|
|
|
|
|
|
2025-06-16 18:44:21 +02:00
|
|
|
wakuConf.restServerConf = none(RestServerConf) ## don't want REST in libwaku
|
|
|
|
|
|
2025-05-08 07:05:35 +10:00
|
|
|
let wakuRes = Waku.new(wakuConf, appCallbacks).valueOr:
|
2024-05-03 14:07:15 +02:00
|
|
|
error "waku initialization failed", error = error
|
|
|
|
|
return err("Failed setting up Waku: " & $error)
|
2024-03-27 10:08:53 -04:00
|
|
|
|
2024-05-03 14:07:15 +02:00
|
|
|
return ok(wakuRes)
|
2023-07-31 09:52:04 +02:00
|
|
|
|
2024-03-16 00:08:47 +01:00
|
|
|
proc process*(
|
2024-05-03 14:07:15 +02:00
|
|
|
self: ptr NodeLifecycleRequest, waku: ptr Waku
|
2024-03-16 00:08:47 +01:00
|
|
|
): Future[Result[string, string]] {.async.} =
|
|
|
|
|
defer:
|
|
|
|
|
destroyShared(self)
|
|
|
|
|
|
|
|
|
|
case self.operation
|
|
|
|
|
of CREATE_NODE:
|
2024-12-13 17:38:16 +01:00
|
|
|
waku[] = (await createWaku(self.configJson, self.appCallbacks)).valueOr:
|
2024-10-01 12:23:04 +03:00
|
|
|
error "CREATE_NODE failed", error = error
|
2025-05-14 11:05:02 +02:00
|
|
|
return err($error)
|
2024-03-16 00:08:47 +01:00
|
|
|
of START_NODE:
|
2024-05-03 14:07:15 +02:00
|
|
|
(await waku.startWaku()).isOkOr:
|
2024-10-01 12:23:04 +03:00
|
|
|
error "START_NODE failed", error = error
|
2025-05-14 11:05:02 +02:00
|
|
|
return err($error)
|
2024-03-16 00:08:47 +01:00
|
|
|
of STOP_NODE:
|
|
|
|
|
try:
|
2024-05-03 14:07:15 +02:00
|
|
|
await waku[].stop()
|
2024-03-16 00:08:47 +01:00
|
|
|
except Exception:
|
2024-10-01 12:23:04 +03:00
|
|
|
error "STOP_NODE failed", error = getCurrentExceptionMsg()
|
2025-05-14 11:05:02 +02:00
|
|
|
return err(getCurrentExceptionMsg())
|
2023-07-31 09:52:04 +02:00
|
|
|
|
|
|
|
|
return ok("")
|