Expose the is_shutdown API to all bindings

This commit is contained in:
Brandon Vandegrift 2025-04-09 00:51:57 -04:00
parent 6df2661d07
commit 10b0c9c80e
12 changed files with 126 additions and 2 deletions

View File

@ -143,6 +143,10 @@ pub enum ResponseOp {
#[serde(flatten)]
result: ApiResult<Box<VeilidState>>,
},
IsShutdown {
#[serde(flatten)]
result: ApiResult<bool>,
},
Attach {
#[serde(flatten)]
result: ApiResult<()>,

View File

@ -137,6 +137,7 @@ abstract class Veilid {
void changeLogIgnore(String layer, List<String> changes);
Future<Stream<VeilidUpdate>> startupVeilidCore(VeilidConfig config);
Future<VeilidState> getVeilidState();
Future<bool> isShutdown();
Future<void> attach();
Future<void> detach();
Future<void> shutdownVeilidCore();

View File

@ -38,6 +38,8 @@ typedef _ChangeLogIgnoreDart = void Function(Pointer<Utf8>, Pointer<Utf8>);
typedef _StartupVeilidCoreDart = void Function(int, int, Pointer<Utf8>);
// fn get_veilid_state(port: i64)
typedef _GetVeilidStateDart = void Function(int);
// fn is_shutdown(port: i64)
typedef _IsShutdownDart = void Function(int);
// fn attach(port: i64)
typedef _AttachDart = void Function(int);
// fn detach(port: i64)
@ -1251,6 +1253,8 @@ class VeilidFFI extends Veilid {
_startupVeilidCore = dylib.lookupFunction<
Void Function(Int64, Int64, Pointer<Utf8>),
_StartupVeilidCoreDart>('startup_veilid_core'),
_isShutdown = dylib.lookupFunction<Void Function(Int64), _IsShutdownDart>(
'is_shutdown'),
_getVeilidState =
dylib.lookupFunction<Void Function(Int64), _GetVeilidStateDart>(
'get_veilid_state'),
@ -1490,6 +1494,7 @@ class VeilidFFI extends Veilid {
final _ChangeLogIgnoreDart _changeLogIgnore;
final _StartupVeilidCoreDart _startupVeilidCore;
final _GetVeilidStateDart _getVeilidState;
final _IsShutdownDart _isShutdown;
final _AttachDart _attach;
final _DetachDart _detach;
final _ShutdownVeilidCoreDart _shutdownVeilidCore;
@ -1623,6 +1628,14 @@ class VeilidFFI extends Veilid {
return processFutureJson(VeilidState.fromJson, recvPort.first);
}
@override
Future<bool> isShutdown() async {
final recvPort = ReceivePort('is_shutdown');
final sendPort = recvPort.sendPort;
_isShutdown(sendPort.nativePort);
return processFuturePlain<bool>(recvPort.first);
}
@override
Future<void> attach() async {
final recvPort = ReceivePort('attach');

View File

@ -627,6 +627,11 @@ class VeilidJS extends Veilid {
VeilidState.fromJson(jsonDecode(await _wrapApiPromise<String>(
js_util.callMethod(wasm, 'get_veilid_state', []))));
@override
Future<bool> isShutdown() async =>
await _wrapApiPromise<bool>(
js_util.callMethod(wasm, 'is_shutdown', []));
@override
Future<void> attach() =>
_wrapApiPromise(js_util.callMethod(wasm, 'attach', []));

View File

@ -482,6 +482,23 @@ pub extern "C" fn get_veilid_state(port: i64) {
);
}
#[no_mangle]
#[instrument(level = "trace", target = "ffi", skip_all)]
pub extern "C" fn is_shutdown(port: i64) {
DartIsolateWrapper::new(port).spawn_result(
async move {
let veilid_api = get_veilid_api().await;
if let Err(veilid_core::VeilidAPIError::NotInitialized) = veilid_api {
return APIResult::Ok(true);
}
let veilid_api = veilid_api.unwrap();
let is_shutdown = veilid_api.is_shutdown();
APIResult::Ok(is_shutdown)
}
.in_current_span(),
);
}
#[no_mangle]
#[instrument(level = "trace", target = "ffi", skip_all)]
pub extern "C" fn attach(port: i64) {

View File

@ -374,6 +374,10 @@ class VeilidAPI(ABC):
async def get_state(self) -> VeilidState:
pass
@abstractmethod
async def is_shutdown(self) -> bool:
pass
@abstractmethod
async def attach(self):
pass

View File

@ -323,6 +323,9 @@ class _JsonVeilidAPI(VeilidAPI):
return VeilidState.from_json(
raise_api_result(await self.send_ndjson_request(Operation.GET_STATE))
)
async def is_shutdown(self) -> bool:
return raise_api_result(await self.send_ndjson_request(Operation.IS_SHUTDOWN))
async def attach(self):
raise_api_result(await self.send_ndjson_request(Operation.ATTACH))

View File

@ -5,6 +5,7 @@ from typing import Self
class Operation(StrEnum):
CONTROL = "Control"
GET_STATE = "GetState"
IS_SHUTDOWN = "IsShutdown"
ATTACH = "Attach"
DETACH = "Detach"
NEW_PRIVATE_ROUTE = "NewPrivateRoute"

View File

@ -81,6 +81,44 @@
}
}
},
{
"type": "object",
"anyOf": [
{
"type": "object",
"required": [
"value"
],
"properties": {
"value": {
"type": "boolean"
}
}
},
{
"type": "object",
"required": [
"error"
],
"properties": {
"error": {
"$ref": "#/definitions/VeilidAPIError"
}
}
}
],
"required": [
"op"
],
"properties": {
"op": {
"type": "string",
"enum": [
"IsShutdown"
]
}
}
},
{
"type": "object",
"anyOf": [
@ -4772,4 +4810,4 @@
}
}
}
}
}

View File

@ -38,6 +38,20 @@
}
}
},
{
"type": "object",
"required": [
"op"
],
"properties": {
"op": {
"type": "string",
"enum": [
"IsShutdown"
]
}
}
},
{
"type": "object",
"required": [
@ -1818,4 +1832,4 @@
]
}
}
}
}

View File

@ -326,6 +326,19 @@ pub fn get_veilid_state() -> Promise {
})
}
#[wasm_bindgen()]
pub fn is_shutdown() -> Promise {
wrap_api_future_json(async move {
let veilid_api = get_veilid_api();
if let Err(veilid_core::VeilidAPIError::NotInitialized) = veilid_api {
return APIResult::Ok(true);
}
let veilid_api = veilid_api.unwrap();
let is_shutdown = veilid_api.is_shutdown().await?;
APIResult::Ok(is_shutdown)
})
}
#[wasm_bindgen()]
pub fn attach() -> Promise {
wrap_api_future_void(async move {

View File

@ -160,6 +160,17 @@ impl VeilidClient {
APIRESULT_UNDEFINED
}
/// Check if Veilid is shutdown.
pub async fn isShutdown() -> APIResult<bool> {
let veilid_api = get_veilid_api();
if let Err(veilid_core::VeilidAPIError::NotInitialized) = veilid_api {
return APIResult::Ok(true);
}
let veilid_api = veilid_api.unwrap();
let is_shutdown = veilid_api.is_shutdown();
APIResult::Ok(is_shutdown)
}
/// Get a full copy of the current state of Veilid.
pub async fn getState() -> APIResult<VeilidState> {
let veilid_api = get_veilid_api()?;