Skip to content

Commit

Permalink
remove waitable callback iterface
Browse files Browse the repository at this point in the history
  • Loading branch information
youyuanwu committed Nov 23, 2023
1 parent 9906fa8 commit a99ea25
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 20 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,7 @@ jobs:
# - name: resolve service
# run: |
# sleep 120 # wait for app to be up
# sfctl service resolve --service-id EchoApp/EchoAppService
# sfctl service resolve --service-id EchoApp/EchoAppService

- name: run all unittests
run: cargo test --all
30 changes: 18 additions & 12 deletions crates/fabric/ext/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,39 @@ use std::sync::{Arc, Condvar, Mutex};

use fabric_base::FabricCommon::{
IFabricAsyncOperationCallback, IFabricAsyncOperationCallback_Impl,
IFabricAsyncOperationCallback_Vtbl, IFabricAsyncOperationContext,
IFabricAsyncOperationContext_Impl, IFabricStringResult, IFabricStringResult_Impl,
IFabricAsyncOperationContext, IFabricAsyncOperationContext_Impl, IFabricStringResult,
IFabricStringResult_Impl,
};
use log::info;
use windows::core::implement;
use windows_core::{HSTRING, PCWSTR};

// Interface for waitable async callback.
// This is a common use case to combine fabric Begin* and End* apis.
#[windows::core::interface("ce5d1e03-90f0-44a3-9d87-849973970761")]
pub unsafe trait IFabricWaitableCallback: IFabricAsyncOperationCallback {
pub unsafe fn wait(&self);
}

#[derive(Debug)]
#[implement(IFabricWaitableCallback, IFabricAsyncOperationCallback)]
#[implement(IFabricAsyncOperationCallback)]
pub struct WaitableCallback {
pair_: Arc<(Mutex<bool>, Condvar)>,
}

pub struct WaitableToken {
pair_: Arc<(Mutex<bool>, Condvar)>,
}

impl Default for WaitableCallback {
fn default() -> Self {
Self::new()
}
}

impl WaitableCallback {
pub fn channel() -> (WaitableToken, IFabricAsyncOperationCallback) {
let callback = WaitableCallback::new();
let token = WaitableToken {
pair_: callback.pair_.clone(),
};
let i_callbaack = callback.into();
(token, i_callbaack)
}

pub fn new() -> WaitableCallback {
WaitableCallback {
pair_: Arc::new((Mutex::new(false), Condvar::new())),
Expand All @@ -61,8 +67,8 @@ impl IFabricAsyncOperationCallback_Impl for WaitableCallback {
}
}

impl IFabricWaitableCallback_Impl for WaitableCallback {
unsafe fn wait(&self) {
impl WaitableToken {
pub fn wait(&self) {
//println!("WaitableCallback wait.");
// Wait for callback to be invoked
let (lock, cvar) = &*self.pair_;
Expand Down
6 changes: 3 additions & 3 deletions crates/samples/client/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use fabric_base::FabricCommon::{FabricClient::*, IFabricAsyncOperationCallback};
use fabric_base::{FABRIC_NODE_QUERY_DESCRIPTION, FABRIC_NODE_QUERY_RESULT_ITEM};
use fabric_ext::{IFabricWaitableCallback, WaitableCallback};
use fabric_ext::WaitableCallback;
use windows_core::{ComInterface, Interface};

fn main() -> windows::core::Result<()> {
Expand All @@ -18,7 +18,7 @@ fn main() -> windows::core::Result<()> {
// todo: figure out owner ship
let c: IFabricQueryClient = unsafe { IFabricQueryClient::from_raw(rawclient) };

let callback: IFabricWaitableCallback = WaitableCallback::new().into();
let (token, callback) = WaitableCallback::channel();

let callback_arg: IFabricAsyncOperationCallback = callback.cast().expect("castfailed");

Expand All @@ -30,7 +30,7 @@ fn main() -> windows::core::Result<()> {
};

// wait callback to be triggered
unsafe { callback.wait() };
token.wait();

// note: there must be a variable to hold COM object, ortherwise it is released.
// result.expect().get_NodeList() will give a released/garbage node description pointer.
Expand Down
7 changes: 3 additions & 4 deletions crates/samples/echomain/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use fabric_base::FabricCommon::FabricRuntime::{
IFabricRuntime,
};
use fabric_base::FabricCommon::IFabricAsyncOperationCallback;
use fabric_ext::{IFabricWaitableCallback, WaitableCallback};
use fabric_ext::WaitableCallback;
use log::info;
use std::sync::mpsc::channel;
use windows::core::w;
Expand Down Expand Up @@ -68,15 +68,14 @@ fn get_port(activation_ctx: &IFabricCodePackageActivationContext) -> u32 {
}

fn get_hostname() -> HSTRING {
// let result = String::from_utf16_lossy(std::slice::from_raw_parts(
let callback: IFabricWaitableCallback = WaitableCallback::new().into();
let (token, callback) = WaitableCallback::channel();

let callback_arg = callback
.cast::<IFabricAsyncOperationCallback>()
.expect("castfailed");
let ctx = unsafe { FabricBeginGetNodeContext(1000, &callback_arg).expect("getctx failed") };

unsafe { callback.wait() };
token.wait();

let result_raw = unsafe { FabricEndGetNodeContext(&ctx).expect("end failed") };

Expand Down

0 comments on commit a99ea25

Please sign in to comment.