Free strings on management
This commit is contained in:
parent
c69caf2721
commit
dc292e44f5
|
@ -12,69 +12,103 @@ use crate::general::{JsonResponse, PeerId, Result};
|
||||||
/// as per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_newchar-jsonconfig)
|
/// as per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_newchar-jsonconfig)
|
||||||
pub fn waku_new(config: Option<WakuNodeConfig>) -> Result<bool> {
|
pub fn waku_new(config: Option<WakuNodeConfig>) -> Result<bool> {
|
||||||
let config = config.unwrap_or_default();
|
let config = config.unwrap_or_default();
|
||||||
let s_config = serde_json::to_string(&config)
|
|
||||||
.expect("Serialization from properly built NodeConfig should never fail");
|
let config_ptr = CString::new(
|
||||||
let result: &str = unsafe {
|
serde_json::to_string(&config)
|
||||||
CStr::from_ptr(waku_sys::waku_new(
|
.expect("Serialization from properly built NodeConfig should never fail"),
|
||||||
CString::new(s_config)
|
)
|
||||||
.expect("CString should build properly from the serialized node config")
|
.expect("CString should build properly from the config")
|
||||||
.into_raw(),
|
.into_raw();
|
||||||
))
|
|
||||||
}
|
let result_ptr = unsafe {
|
||||||
.to_str()
|
let res = waku_sys::waku_new(config_ptr);
|
||||||
.expect("Response should always succeed to load to a &str");
|
drop(CString::from_raw(config_ptr));
|
||||||
|
res
|
||||||
|
};
|
||||||
|
|
||||||
|
let result = unsafe { CStr::from_ptr(result_ptr) }
|
||||||
|
.to_str()
|
||||||
|
.expect("Response should always succeed to load to a &str");
|
||||||
|
|
||||||
let json_response: JsonResponse<bool> =
|
let json_response: JsonResponse<bool> =
|
||||||
serde_json::from_str(result).expect("JsonResponse should always succeed to deserialize");
|
serde_json::from_str(result).expect("JsonResponse should always succeed to deserialize");
|
||||||
|
|
||||||
|
unsafe {
|
||||||
|
waku_sys::waku_utils_free(result_ptr);
|
||||||
|
}
|
||||||
|
|
||||||
json_response.into()
|
json_response.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Start a Waku node mounting all the protocols that were enabled during the Waku node instantiation.
|
/// Start a Waku node mounting all the protocols that were enabled during the Waku node instantiation.
|
||||||
/// as per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_start)
|
/// as per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_start)
|
||||||
pub fn waku_start() -> Result<bool> {
|
pub fn waku_start() -> Result<bool> {
|
||||||
let response = unsafe { CStr::from_ptr(waku_sys::waku_start()) }
|
let response_ptr = unsafe { waku_sys::waku_start() };
|
||||||
|
let response = unsafe { CStr::from_ptr(response_ptr) }
|
||||||
.to_str()
|
.to_str()
|
||||||
.expect("Response should always succeed to load to a &str");
|
.expect("Response should always succeed to load to a &str");
|
||||||
|
|
||||||
let json_response: JsonResponse<bool> =
|
let json_response: JsonResponse<bool> =
|
||||||
serde_json::from_str(response).expect("JsonResponse should always succeed to deserialize");
|
serde_json::from_str(response).expect("JsonResponse should always succeed to deserialize");
|
||||||
|
|
||||||
|
unsafe {
|
||||||
|
waku_sys::waku_utils_free(response_ptr);
|
||||||
|
}
|
||||||
|
|
||||||
json_response.into()
|
json_response.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Stops a Waku node
|
/// Stops a Waku node
|
||||||
/// as per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_stop)
|
/// as per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_stop)
|
||||||
pub fn waku_stop() -> Result<bool> {
|
pub fn waku_stop() -> Result<bool> {
|
||||||
let response = unsafe { CStr::from_ptr(waku_sys::waku_stop()) }
|
let response_ptr = unsafe { waku_sys::waku_start() };
|
||||||
|
let response = unsafe { CStr::from_ptr(response_ptr) }
|
||||||
.to_str()
|
.to_str()
|
||||||
.expect("Response should always succeed to load to a &str");
|
.expect("Response should always succeed to load to a &str");
|
||||||
|
|
||||||
let json_response: JsonResponse<bool> =
|
let json_response: JsonResponse<bool> =
|
||||||
serde_json::from_str(response).expect("JsonResponse should always succeed to deserialize");
|
serde_json::from_str(response).expect("JsonResponse should always succeed to deserialize");
|
||||||
|
|
||||||
|
unsafe {
|
||||||
|
waku_sys::waku_utils_free(response_ptr);
|
||||||
|
}
|
||||||
|
|
||||||
json_response.into()
|
json_response.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// If the execution is successful, the result is the peer ID as a string (base58 encoded)
|
/// If the execution is successful, the result is the peer ID as a string (base58 encoded)
|
||||||
/// as per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_stop)
|
/// as per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_stop)
|
||||||
pub fn waku_peer_id() -> Result<PeerId> {
|
pub fn waku_peer_id() -> Result<PeerId> {
|
||||||
let response = unsafe { CStr::from_ptr(waku_sys::waku_peerid()) }
|
let response_ptr = unsafe { waku_sys::waku_start() };
|
||||||
|
let response = unsafe { CStr::from_ptr(response_ptr) }
|
||||||
.to_str()
|
.to_str()
|
||||||
.expect("Response should always succeed to load to a &str");
|
.expect("Response should always succeed to load to a &str");
|
||||||
|
|
||||||
let json_response: JsonResponse<String> =
|
let json_response: JsonResponse<String> =
|
||||||
serde_json::from_str(response).expect("JsonResponse should always succeed to deserialize");
|
serde_json::from_str(response).expect("JsonResponse should always succeed to deserialize");
|
||||||
|
|
||||||
|
unsafe {
|
||||||
|
waku_sys::waku_utils_free(response_ptr);
|
||||||
|
}
|
||||||
|
|
||||||
json_response.into()
|
json_response.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the multiaddresses the Waku node is listening to
|
/// Get the multiaddresses the Waku node is listening to
|
||||||
/// as per [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_listen_addresses)
|
/// as per [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_listen_addresses)
|
||||||
pub fn waku_listen_addresses() -> Result<Vec<Multiaddr>> {
|
pub fn waku_listen_addresses() -> Result<Vec<Multiaddr>> {
|
||||||
let response = unsafe { CStr::from_ptr(waku_sys::waku_listen_addresses()) }
|
let response_ptr = unsafe { waku_sys::waku_start() };
|
||||||
|
let response = unsafe { CStr::from_ptr(response_ptr) }
|
||||||
.to_str()
|
.to_str()
|
||||||
.expect("Response should always succeed to load to a &str");
|
.expect("Response should always succeed to load to a &str");
|
||||||
|
|
||||||
let json_response: JsonResponse<Vec<Multiaddr>> =
|
let json_response: JsonResponse<Vec<Multiaddr>> =
|
||||||
serde_json::from_str(response).expect("JsonResponse should always succeed to deserialize");
|
serde_json::from_str(response).expect("JsonResponse should always succeed to deserialize");
|
||||||
|
|
||||||
|
unsafe {
|
||||||
|
waku_sys::waku_utils_free(response_ptr);
|
||||||
|
}
|
||||||
|
|
||||||
json_response.into()
|
json_response.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue