mirror of
https://github.com/logos-messaging/logos-messaging-rust-bindings.git
synced 2026-01-03 22:43:07 +00:00
Free strings on peers
This commit is contained in:
parent
0ea46ed464
commit
fe7b2e75e6
@ -126,7 +126,7 @@ impl WakuNodeHandle<Running> {
|
|||||||
/// The peer must be already known.
|
/// The peer must be already known.
|
||||||
/// It must have been added before with [`WakuNodeHandle::add_peer`] or previously dialed with [`WakuNodeHandle::connect_peer_with_address`]
|
/// It must have been added before with [`WakuNodeHandle::add_peer`] or previously dialed with [`WakuNodeHandle::connect_peer_with_address`]
|
||||||
/// As per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_connect_peeridchar-peerid-int-timeoutms)
|
/// As per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_connect_peeridchar-peerid-int-timeoutms)
|
||||||
pub fn connect_peer_with_id(&self, peer_id: PeerId, timeout: Option<Duration>) -> Result<()> {
|
pub fn connect_peer_with_id(&self, peer_id: &PeerId, timeout: Option<Duration>) -> Result<()> {
|
||||||
peers::waku_connect_peer_with_id(peer_id, timeout)
|
peers::waku_connect_peer_with_id(peer_id, timeout)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -12,22 +12,31 @@ use crate::general::{JsonResponse, PeerId, ProtocolId, Result};
|
|||||||
/// Add a node multiaddress and protocol to the waku node’s peerstore.
|
/// Add a node multiaddress and protocol to the waku node’s peerstore.
|
||||||
/// As per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_add_peerchar-address-char-protocolid)
|
/// As per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_add_peerchar-address-char-protocolid)
|
||||||
pub fn waku_add_peers(address: &Multiaddr, protocol_id: ProtocolId) -> Result<PeerId> {
|
pub fn waku_add_peers(address: &Multiaddr, protocol_id: ProtocolId) -> Result<PeerId> {
|
||||||
let response = unsafe {
|
let address_ptr = CString::new(address.to_string())
|
||||||
CStr::from_ptr(waku_sys::waku_add_peer(
|
.expect("CString should build properly from the address")
|
||||||
CString::new(address.to_string())
|
.into_raw();
|
||||||
.expect("CString should build properly from the address")
|
let protocol_id_ptr = CString::new(protocol_id.to_string())
|
||||||
.into_raw(),
|
.expect("CString should build properly from the protocol id")
|
||||||
CString::new(protocol_id.to_string())
|
.into_raw();
|
||||||
.expect("CString should build properly from the protocol id")
|
|
||||||
.into_raw(),
|
let response_ptr = unsafe {
|
||||||
))
|
let res = waku_sys::waku_add_peer(address_ptr, protocol_id_ptr);
|
||||||
}
|
drop(CString::from_raw(address_ptr));
|
||||||
.to_str()
|
drop(CString::from_raw(protocol_id_ptr));
|
||||||
.expect("&str should build properly from the returning response");
|
res
|
||||||
|
};
|
||||||
|
|
||||||
|
let response = unsafe { CStr::from_ptr(response_ptr) }
|
||||||
|
.to_str()
|
||||||
|
.expect("&str should build properly from the returning response");
|
||||||
|
|
||||||
let result: JsonResponse<PeerId> =
|
let result: JsonResponse<PeerId> =
|
||||||
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);
|
||||||
|
}
|
||||||
|
|
||||||
result.into()
|
result.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -40,22 +49,31 @@ pub fn waku_connect_peer_with_address(
|
|||||||
address: &Multiaddr,
|
address: &Multiaddr,
|
||||||
timeout: Option<Duration>,
|
timeout: Option<Duration>,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let response = unsafe {
|
let address_ptr = CString::new(address.to_string())
|
||||||
CStr::from_ptr(waku_sys::waku_connect(
|
.expect("CString should build properly from multiaddress")
|
||||||
CString::new(address.to_string())
|
.into_raw();
|
||||||
.expect("CString should build properly from multiaddress")
|
let response_ptr = unsafe {
|
||||||
.into_raw(),
|
let res = waku_sys::waku_connect(
|
||||||
|
address_ptr,
|
||||||
timeout
|
timeout
|
||||||
.map(|duration| duration.as_millis().try_into().unwrap_or(i32::MAX))
|
.map(|duration| duration.as_millis().try_into().unwrap_or(i32::MAX))
|
||||||
.unwrap_or(0),
|
.unwrap_or(0),
|
||||||
))
|
);
|
||||||
}
|
drop(CString::from_raw(address_ptr));
|
||||||
.to_str()
|
res
|
||||||
.expect("&str should build properly from the returning response");
|
};
|
||||||
|
|
||||||
|
let response = unsafe { CStr::from_ptr(response_ptr) }
|
||||||
|
.to_str()
|
||||||
|
.expect("&str should build properly from the returning response");
|
||||||
|
|
||||||
let result: JsonResponse<bool> =
|
let result: 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);
|
||||||
|
}
|
||||||
|
|
||||||
Result::from(result).map(|_| ())
|
Result::from(result).map(|_| ())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -64,55 +82,77 @@ pub fn waku_connect_peer_with_address(
|
|||||||
/// The peer must be already known.
|
/// The peer must be already known.
|
||||||
/// It must have been added before with [`waku_add_peers`] or previously dialed with [`waku_connect_peer_with_address`]
|
/// It must have been added before with [`waku_add_peers`] or previously dialed with [`waku_connect_peer_with_address`]
|
||||||
/// As per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_connect_peeridchar-peerid-int-timeoutms)
|
/// As per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_connect_peeridchar-peerid-int-timeoutms)
|
||||||
pub fn waku_connect_peer_with_id(peer_id: PeerId, timeout: Option<Duration>) -> Result<()> {
|
pub fn waku_connect_peer_with_id(peer_id: &PeerId, timeout: Option<Duration>) -> Result<()> {
|
||||||
let response = unsafe {
|
let peer_id_ptr = CString::new(peer_id.as_bytes())
|
||||||
CStr::from_ptr(waku_sys::waku_connect_peerid(
|
.expect("CString should build properly from peer id")
|
||||||
CString::new(peer_id)
|
.into_raw();
|
||||||
.expect("CString should build properly from peer id")
|
let response_ptr = unsafe {
|
||||||
.into_raw(),
|
let res = waku_sys::waku_connect_peerid(
|
||||||
|
peer_id_ptr,
|
||||||
timeout
|
timeout
|
||||||
.map(|duration| duration.as_millis().try_into().unwrap_or(i32::MAX))
|
.map(|duration| duration.as_millis().try_into().unwrap_or(i32::MAX))
|
||||||
.unwrap_or(0),
|
.unwrap_or(0),
|
||||||
))
|
);
|
||||||
}
|
drop(CString::from_raw(peer_id_ptr));
|
||||||
.to_str()
|
res
|
||||||
.expect("&str should build properly from the returning response");
|
};
|
||||||
|
|
||||||
|
let response = unsafe { CStr::from_ptr(response_ptr) }
|
||||||
|
.to_str()
|
||||||
|
.expect("&str should build properly from the returning response");
|
||||||
|
|
||||||
let result: JsonResponse<bool> =
|
let result: 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);
|
||||||
|
}
|
||||||
|
|
||||||
Result::from(result).map(|_| ())
|
Result::from(result).map(|_| ())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Disconnect a peer using its peer id
|
/// Disconnect a peer using its peer id
|
||||||
/// As per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_disconnect_peerchar-peerid)
|
/// As per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_disconnect_peerchar-peerid)
|
||||||
pub fn waku_disconnect_peer_with_id(peer_id: &PeerId) -> Result<()> {
|
pub fn waku_disconnect_peer_with_id(peer_id: &PeerId) -> Result<()> {
|
||||||
let response = unsafe {
|
let peer_id_ptr = CString::new(peer_id.as_bytes())
|
||||||
CStr::from_ptr(waku_sys::waku_disconnect(
|
.expect("CString should build properly from peer id")
|
||||||
CString::new(peer_id.as_bytes())
|
.into_raw();
|
||||||
.expect("CString should build properly from peer id")
|
|
||||||
.into_raw(),
|
let response_ptr = unsafe {
|
||||||
))
|
let res = waku_sys::waku_disconnect(peer_id_ptr);
|
||||||
}
|
drop(CString::from_raw(peer_id_ptr));
|
||||||
.to_str()
|
res
|
||||||
.expect("&str should build properly from the returning response");
|
};
|
||||||
|
let response = unsafe { CStr::from_ptr(response_ptr) }
|
||||||
|
.to_str()
|
||||||
|
.expect("&str should build properly from the returning response");
|
||||||
|
|
||||||
let result: JsonResponse<bool> =
|
let result: 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);
|
||||||
|
}
|
||||||
|
|
||||||
Result::from(result).map(|_| ())
|
Result::from(result).map(|_| ())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get number of connected peers
|
/// Get number of connected peers
|
||||||
/// As per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_peer_count)
|
/// As per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_peer_count)
|
||||||
pub fn waku_peer_count() -> Result<usize> {
|
pub fn waku_peer_count() -> Result<usize> {
|
||||||
let response = unsafe { CStr::from_ptr(waku_sys::waku_peer_cnt()) }
|
let response_ptr = unsafe { waku_sys::waku_peer_cnt() };
|
||||||
|
|
||||||
|
let response = unsafe { CStr::from_ptr(response_ptr) }
|
||||||
.to_str()
|
.to_str()
|
||||||
.expect("&str should build properly from the returning response");
|
.expect("&str should build properly from the returning response");
|
||||||
|
|
||||||
let result: JsonResponse<usize> =
|
let result: JsonResponse<usize> =
|
||||||
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);
|
||||||
|
}
|
||||||
|
|
||||||
result.into()
|
result.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -164,13 +204,18 @@ pub type WakuPeers = Vec<WakuPeerData>;
|
|||||||
/// Retrieve the list of peers known by the Waku node
|
/// Retrieve the list of peers known by the Waku node
|
||||||
/// As per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_peers)
|
/// As per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_peers)
|
||||||
pub fn waku_peers() -> Result<WakuPeers> {
|
pub fn waku_peers() -> Result<WakuPeers> {
|
||||||
let response = unsafe { CStr::from_ptr(waku_sys::waku_peers()) }
|
let response_ptr = unsafe { waku_sys::waku_peers() };
|
||||||
|
let response = unsafe { CStr::from_ptr(response_ptr) }
|
||||||
.to_str()
|
.to_str()
|
||||||
.expect("&str should build properly from the returning response");
|
.expect("&str should build properly from the returning response");
|
||||||
|
|
||||||
let result: JsonResponse<WakuPeers> =
|
let result: JsonResponse<WakuPeers> =
|
||||||
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);
|
||||||
|
}
|
||||||
|
|
||||||
result.into()
|
result.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user