Skip to content

Commit e7d7049

Browse files
committed
Add client_get_default_timeout and client_get_state functions; enhance ClientState with Display implementation
1 parent f17d7f2 commit e7d7049

3 files changed

Lines changed: 48 additions & 49 deletions

File tree

crates/clib/clib_openiap.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -691,8 +691,12 @@ void connect_async(struct ClientWrapper *client,
691691
int32_t request_id,
692692
ConnectCallback callback);
693693

694+
int32_t client_get_default_timeout(struct ClientWrapper *client_wrap);
695+
694696
void client_set_default_timeout(struct ClientWrapper *client_wrap, int32_t timeout);
695697

698+
const char *client_get_state(struct ClientWrapper *client_wrap);
699+
696700
void client_set_agent_name(struct ClientWrapper *client_wrap, const char *agent_name);
697701

698702
void client_set_agent_version(struct ClientWrapper *client_wrap, const char *agent_version);

crates/clib/src/lib.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,17 @@ pub extern "C" fn connect_async(client: *mut ClientWrapper, server_address: *con
781781
});
782782
}
783783
#[no_mangle]
784+
pub extern "C" fn client_get_default_timeout(client_wrap: *mut ClientWrapper) -> i32 {
785+
debug!("get_default_timeout");
786+
let client = match safe_wrapper( client_wrap ) {
787+
Some( wrap ) => wrap.client.clone().unwrap(),
788+
None => {
789+
Client::new()
790+
}
791+
};
792+
client.get_default_timeout().as_secs() as i32
793+
}
794+
#[no_mangle]
784795
pub extern "C" fn client_set_default_timeout(client_wrap: *mut ClientWrapper, timeout: i32) {
785796
debug!("set_default_timeout = {:?}", timeout);
786797
let client = match safe_wrapper( client_wrap ) {
@@ -796,6 +807,19 @@ pub extern "C" fn client_set_default_timeout(client_wrap: *mut ClientWrapper, ti
796807
client.set_default_timeout(_timeout);
797808
}
798809
#[no_mangle]
810+
pub extern "C" fn client_get_state(client_wrap: *mut ClientWrapper) -> *const c_char {
811+
debug!("get_state");
812+
let client = match safe_wrapper( client_wrap ) {
813+
Some( wrap ) => wrap.client.clone().unwrap(),
814+
None => {
815+
Client::new()
816+
}
817+
};
818+
let state = client.get_state().to_string();
819+
let state_str = CString::new(state).unwrap().into_raw();
820+
state_str
821+
}
822+
#[no_mangle]
799823
pub extern "C" fn client_set_agent_name(client_wrap: *mut ClientWrapper, agent_name: *const c_char) {
800824
let agent_name = c_char_to_str(agent_name);
801825
debug!("set_agent_name = {:?}", agent_name);

crates/client/src/lib.rs

Lines changed: 20 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub use openiap_proto::*;
2727
pub use prost_types::Timestamp;
2828
pub use protos::flow_service_client::FlowServiceClient;
2929
use sqids::Sqids;
30-
30+
use std::fmt::{Display,Formatter};
3131
use tokio::task::JoinHandle;
3232
use tokio_tungstenite::WebSocketStream;
3333
use tracing::{debug, error, info, trace};
@@ -227,6 +227,17 @@ pub enum ClientState {
227227
/// The client is signed in and connected
228228
Signedin
229229
}
230+
impl Display for ClientState {
231+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
232+
match self {
233+
ClientState::Disconnected => write!(f, "Disconnected"),
234+
ClientState::Connecting => write!(f, "Connecting"),
235+
ClientState::Connected => write!(f, "Connected"),
236+
ClientState::Signedin => write!(f, "Signedin"),
237+
}
238+
}
239+
}
240+
230241
/// The `Config` struct provides the configuration for the OpenIAP service we are connecting to.
231242
#[derive(Debug, Clone, serde::Deserialize)]
232243
#[allow(dead_code)]
@@ -797,12 +808,14 @@ impl Client {
797808
}
798809
if state == ClientState::Disconnected && !current.eq(&state) {
799810
let me = self.clone();
800-
tokio::task::spawn(async move {
801-
let mut reply_queue_guard = me.rpc_reply_queue.lock().await;
802-
let mut callback_guard = me.rpc_callback.lock().await;
803-
*reply_queue_guard = None;
804-
*callback_guard = None;
805-
});
811+
if let Ok(_handle) = tokio::runtime::Handle::try_current() {
812+
tokio::task::spawn(async move {
813+
let mut reply_queue_guard = me.rpc_reply_queue.lock().await;
814+
let mut callback_guard = me.rpc_callback.lock().await;
815+
*reply_queue_guard = None;
816+
*callback_guard = None;
817+
});
818+
}
806819
}
807820
if state == ClientState::Connecting && !current.eq(&state) {
808821
if let Ok(_handle) = tokio::runtime::Handle::try_current() {
@@ -811,14 +824,6 @@ impl Client {
811824
tokio::task::spawn(async move {
812825
me.event_sender.send(crate::ClientEvent::Connecting).await.unwrap();
813826
});
814-
// match tokio::task::Builder::new().name("setconnected-notify-connecting").spawn(async move {
815-
// me.event_sender.send(crate::ClientEvent::Connecting).await.unwrap();
816-
// }) {
817-
// Ok(_) => (),
818-
// Err(e) => {
819-
// error!("Failed to spawn setconnected-notify-connecting task: {:?}", e);
820-
// }
821-
// }
822827
}
823828

824829
}
@@ -829,14 +834,6 @@ impl Client {
829834
tokio::task::spawn(async move {
830835
me.event_sender.send(crate::ClientEvent::Connected).await.unwrap();
831836
});
832-
// match tokio::task::Builder::new().name("setconnected-notify-connected").spawn(async move {
833-
// me.event_sender.send(crate::ClientEvent::Connected).await.unwrap();
834-
// }) {
835-
// Ok(_) => (),
836-
// Err(e) => {
837-
// error!("Failed to spawn setconnected-notify-connected task: {:?}", e);
838-
// }
839-
// }
840837
}
841838
}
842839
if state == ClientState::Signedin && current != ClientState::Signedin {
@@ -845,14 +842,6 @@ impl Client {
845842
tokio::task::spawn(async move {
846843
me.event_sender.send(crate::ClientEvent::SignedIn).await.unwrap();
847844
});
848-
// match tokio::task::Builder::new().name("set_signedin-notify-connected").spawn(async move {
849-
// me.event_sender.send(crate::ClientEvent::SignedIn).await.unwrap();
850-
// }) {
851-
// Ok(_) => (),
852-
// Err(e) => {
853-
// error!("Failed to spawn set_signedin-notify-connected task: {:?}", e);
854-
// }
855-
// };
856845
}
857846
}
858847
if state == ClientState::Disconnected && !current.eq(&state) {
@@ -867,26 +856,14 @@ impl Client {
867856
Some(message) => message.to_string(),
868857
None => "".to_string(),
869858
};
870-
//if current != ClientState::Connecting {
871859
tokio::task::spawn(async move {
872860
me.event_sender.send(crate::ClientEvent::Disconnected(message)).await.unwrap();
873861
});
874-
// match tokio::task::Builder::new().name("setconnected-notify-disconnected").spawn(async move {
875-
// trace!("Disconnected: {}", message);
876-
// me.event_sender.send(crate::ClientEvent::Disconnected(message)).await.unwrap();
877-
// }) {
878-
// Ok(_) => (),
879-
// Err(e) => {
880-
// error!("Failed to spawn setconnected-notify-disconnected task: {:?}", e);
881-
// }
882-
// }
883-
//}
884862
}
885863

886864
self.kill_handles();
887865
if let Ok(_handle) = tokio::runtime::Handle::try_current() {
888866
let client = self.clone();
889-
// match tokio::task::Builder::new().name("kill_handles").spawn(async move {
890867
tokio::task::spawn(async move {
891868
{
892869
let inner = client.inner.lock().await;
@@ -941,12 +918,6 @@ impl Client {
941918
debug!("Reconnecting disabled, stop now");
942919
}
943920
});
944-
// {
945-
// Ok(_) => (),
946-
// Err(e) => {
947-
// error!("Failed to spawn kill_handles task: {:?}", e);
948-
// }
949-
// }
950921
}
951922

952923
}

0 commit comments

Comments
 (0)