From 67851184cd15306ec3095b1ec04b5e89aa654a95 Mon Sep 17 00:00:00 2001 From: Youyuan Wu Date: Tue, 1 Oct 2024 09:55:01 -0700 Subject: [PATCH] fix fabric client role --- crates/libs/core/src/client/mod.rs | 41 ++++++++++++------- crates/libs/core/src/client/tests.rs | 4 +- crates/libs/core/src/sync/mod.rs | 5 +-- crates/libs/core/src/types/client/mod.rs | 4 +- crates/samples/echomain-stateful2/src/test.rs | 4 +- crates/samples/echomain/src/test.rs | 4 +- 6 files changed, 36 insertions(+), 26 deletions(-) diff --git a/crates/libs/core/src/client/mod.rs b/crates/libs/core/src/client/mod.rs index 3e77401..d0a6d98 100644 --- a/crates/libs/core/src/client/mod.rs +++ b/crates/libs/core/src/client/mod.rs @@ -5,7 +5,7 @@ use connection::{ClientConnectionEventHandlerBridge, LambdaClientConnectionNotificationHandler}; use mssf_com::FabricClient::{ - FabricCreateLocalClient4, IFabricClientConnectionEventHandler, + FabricCreateLocalClient3, FabricCreateLocalClient4, IFabricClientConnectionEventHandler, IFabricPropertyManagementClient2, IFabricQueryClient10, IFabricServiceManagementClient6, IFabricServiceNotificationEventHandler, }; @@ -37,19 +37,25 @@ fn create_local_client_internal( client_connection_handler: Option<&IFabricClientConnectionEventHandler>, client_role: Option, ) -> T { - let role = client_role.unwrap_or(ClientRole::User); - assert_ne!( - role, - ClientRole::Unknown, - "Unknown role should not be used." - ); - let raw = unsafe { - FabricCreateLocalClient4( - service_notification_handler, - client_connection_handler, - role.into(), - &T::IID, - ) + let role = client_role.unwrap_or(ClientRole::Unknown); + let raw = if role == ClientRole::Unknown { + // unknown role should use the SF function without role param. + unsafe { + FabricCreateLocalClient3( + service_notification_handler, + client_connection_handler, + &T::IID, + ) + } + } else { + unsafe { + FabricCreateLocalClient4( + service_notification_handler, + client_connection_handler, + role.into(), + &T::IID, + ) + } } .expect("failed to create fabric client"); // if params are right, client should be created. There is no network call involved during obj creation. @@ -75,7 +81,7 @@ impl FabricClientBuilder { Self { sn_handler: None, cc_handler: None, - client_role: ClientRole::User, + client_role: ClientRole::Unknown, } } @@ -168,6 +174,11 @@ pub struct FabricClient { } impl FabricClient { + /// Get a builder + pub fn builder() -> FabricClientBuilder { + FabricClientBuilder::new() + } + // Get a copy of COM object pub fn get_com(&self) -> IFabricPropertyManagementClient2 { self.com_property_client.clone() diff --git a/crates/libs/core/src/client/tests.rs b/crates/libs/core/src/client/tests.rs index 23831e7..4e63a44 100644 --- a/crates/libs/core/src/client/tests.rs +++ b/crates/libs/core/src/client/tests.rs @@ -12,14 +12,14 @@ use tokio_util::sync::CancellationToken; use windows_core::HSTRING; use crate::{ - client::{svc_mgmt_client::PartitionKeyType, FabricClientBuilder}, + client::{svc_mgmt_client::PartitionKeyType, FabricClient}, error::FabricErrorCode, types::{NodeQueryDescription, NodeStatusFilter, PagedQueryDescription}, }; #[tokio::test] async fn test_fabric_client() { - let c = FabricClientBuilder::new().build(); + let c = FabricClient::builder().build(); let qc = c.get_query_manager(); let timeout = Duration::from_secs(1); let paging_status; diff --git a/crates/libs/core/src/sync/mod.rs b/crates/libs/core/src/sync/mod.rs index 2d7ee8d..369bce2 100644 --- a/crates/libs/core/src/sync/mod.rs +++ b/crates/libs/core/src/sync/mod.rs @@ -320,8 +320,7 @@ mod tests { impl FabricQueryClient { pub fn new() -> FabricQueryClient { FabricQueryClient { - com: crate::client::FabricClientBuilder::new() - .build_interface::(), + com: crate::client::FabricClient::builder().build_interface::(), } } @@ -511,7 +510,7 @@ mod tests { #[test] fn local_client_create() { - let _mgmt = crate::client::FabricClientBuilder::new() + let _mgmt = crate::client::FabricClient::builder() .build_interface::(); } diff --git a/crates/libs/core/src/types/client/mod.rs b/crates/libs/core/src/types/client/mod.rs index aa1f6d6..28b5679 100644 --- a/crates/libs/core/src/types/client/mod.rs +++ b/crates/libs/core/src/types/client/mod.rs @@ -59,8 +59,8 @@ impl From<&ServiceNotificationFilterDescription> // FABRIC_CLIENT_ROLE #[derive(Debug, PartialEq, Clone)] pub enum ClientRole { - Unknown, // Do not pass this in SF api, use User instead. - User, + Unknown, // Default client role. + User, // User client role. Must set client certificate for tls endpoints. Admin, // ElevatedAdmin not supported by SF 6.x sdk yet. } diff --git a/crates/samples/echomain-stateful2/src/test.rs b/crates/samples/echomain-stateful2/src/test.rs index cdde932..6a89439 100644 --- a/crates/samples/echomain-stateful2/src/test.rs +++ b/crates/samples/echomain-stateful2/src/test.rs @@ -11,7 +11,7 @@ use mssf_core::{ PartitionKeyType, ResolvedServiceEndpoint, ResolvedServicePartition, ServiceEndpointRole, ServicePartitionKind, }, - FabricClient, FabricClientBuilder, + FabricClient, }, error::FabricErrorCode, types::{ @@ -233,7 +233,7 @@ impl TestClient { // Uses fabric client to perform various actions for this service. #[tokio::test] async fn test_partition_info() { - let fc = FabricClientBuilder::new().build(); + let fc = FabricClient::builder().build(); let tc = TestClient::new(fc.clone()); let timeout = Duration::from_secs(1); diff --git a/crates/samples/echomain/src/test.rs b/crates/samples/echomain/src/test.rs index c36638b..eba071f 100644 --- a/crates/samples/echomain/src/test.rs +++ b/crates/samples/echomain/src/test.rs @@ -11,7 +11,7 @@ use mssf_core::{ PartitionKeyType, ResolvedServiceEndpoint, ResolvedServicePartitionInfo, ServiceEndpointRole, ServicePartitionKind, }, - FabricClient, FabricClientBuilder, GatewayInformationResult, ServiceNotification, + FabricClient, GatewayInformationResult, ServiceNotification, }, error::FabricErrorCode, types::{ @@ -120,7 +120,7 @@ async fn test_fabric_client() { let (sn_tx, mut sn_rx) = tokio::sync::mpsc::channel::(1); // channel for client connection notification let (cc_tx, mut cc_rx) = tokio::sync::mpsc::channel::(1); - let fc = FabricClientBuilder::new() + let fc = FabricClient::builder() .with_on_service_notification(move |notification| { sn_tx .blocking_send(notification.clone())