-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FabricRuntime] Support Package change notification callbacks (#96)
End user applications probably want to handle package change notifications. This PR currently tackles only ConfigurationPackage. If you're happy with the approach, I'll do the same exact implementation (edit: in additional PRs) for CodePackage and DataPackage and that will close out #97.
- Loading branch information
1 parent
c18570a
commit f8cf77f
Showing
7 changed files
with
243 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
// ------------------------------------------------------------ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License (MIT). See License.txt in the repo root for license information. | ||
// ------------------------------------------------------------ | ||
//! Handle callbacks for configuration package changes | ||
//! TODO: We probably should also provide a helpful callback to use in conjunction with the config-rs support (so that it processes configuration changes) | ||
use mssf_com::FabricRuntime::{ | ||
IFabricConfigurationPackageChangeHandler, IFabricConfigurationPackageChangeHandler_Impl, | ||
}; | ||
|
||
use crate::runtime::config::ConfigurationPackage; | ||
|
||
use super::ConfigurationPackageChangeEvent; | ||
|
||
/// Rust trait to turn rust code into IFabricConfigurationPackageChangeHandler. | ||
/// Not exposed to user | ||
pub trait ConfigurationPackageChangeEventHandler: 'static { | ||
fn on_change(&self, change: &ConfigurationPackageChangeEvent); | ||
} | ||
|
||
// Bridge implementation for the change handler to turn rust code into SF com object. | ||
#[windows_core::implement(IFabricConfigurationPackageChangeHandler)] | ||
#[allow(non_camel_case_types)] // Suppress lint for _Impl struct | ||
pub struct ConfigurationPackageChangeEventHandlerBridge<T> | ||
where | ||
T: ConfigurationPackageChangeEventHandler, | ||
{ | ||
inner: T, | ||
} | ||
|
||
impl<T> ConfigurationPackageChangeEventHandlerBridge<T> | ||
where | ||
T: ConfigurationPackageChangeEventHandler, | ||
{ | ||
pub fn new(inner: T) -> Self { | ||
Self { inner } | ||
} | ||
} | ||
|
||
impl<T> IFabricConfigurationPackageChangeHandler_Impl | ||
for ConfigurationPackageChangeEventHandlerBridge_Impl<T> | ||
where | ||
T: ConfigurationPackageChangeEventHandler, | ||
{ | ||
fn OnPackageAdded( | ||
&self, | ||
_source: Option<&mssf_com::FabricRuntime::IFabricCodePackageActivationContext>, | ||
configpackage: Option<&mssf_com::FabricRuntime::IFabricConfigurationPackage>, | ||
) { | ||
let new_package = ConfigurationPackage::from_com(configpackage.unwrap().clone()); | ||
let event = ConfigurationPackageChangeEvent::Addition { new_package }; | ||
self.inner.on_change(&event) | ||
} | ||
|
||
fn OnPackageRemoved( | ||
&self, | ||
_source: Option<&mssf_com::FabricRuntime::IFabricCodePackageActivationContext>, | ||
configpackage: Option<&mssf_com::FabricRuntime::IFabricConfigurationPackage>, | ||
) { | ||
let previous_package = ConfigurationPackage::from_com(configpackage.unwrap().clone()); | ||
let event = ConfigurationPackageChangeEvent::Removal { previous_package }; | ||
self.inner.on_change(&event) | ||
} | ||
|
||
fn OnPackageModified( | ||
&self, | ||
_source: Option<&mssf_com::FabricRuntime::IFabricCodePackageActivationContext>, | ||
previousconfigpackage: Option<&mssf_com::FabricRuntime::IFabricConfigurationPackage>, | ||
configpackage: Option<&mssf_com::FabricRuntime::IFabricConfigurationPackage>, | ||
) { | ||
let new_package = ConfigurationPackage::from_com(configpackage.unwrap().clone()); | ||
let previous_package = | ||
ConfigurationPackage::from_com(previousconfigpackage.unwrap().clone()); | ||
let event = ConfigurationPackageChangeEvent::Modification { | ||
previous_package, | ||
new_package, | ||
}; | ||
self.inner.on_change(&event) | ||
} | ||
} | ||
|
||
/// Lambda implementation of ConfigurationPackageChangeEventHandler trait. | ||
/// This is used in FabricClientBuilder to build function into handler. | ||
/// Not exposed to user. | ||
/// Strictly speaking we don't need this layer. But it would allow us to open the door to trait implementations someday | ||
pub(crate) struct LambdaConfigurationPackageEventHandler<T> | ||
where | ||
T: Fn(&ConfigurationPackageChangeEvent), | ||
{ | ||
f: T, | ||
} | ||
|
||
impl<T> LambdaConfigurationPackageEventHandler<T> | ||
where | ||
T: Fn(&ConfigurationPackageChangeEvent) + 'static, | ||
{ | ||
pub fn new(f: T) -> Self { | ||
Self { f } | ||
} | ||
} | ||
|
||
impl<T> ConfigurationPackageChangeEventHandler for LambdaConfigurationPackageEventHandler<T> | ||
where | ||
T: Fn(&ConfigurationPackageChangeEvent) + 'static, | ||
{ | ||
fn on_change(&self, change: &ConfigurationPackageChangeEvent) { | ||
(self.f)(change) | ||
} | ||
} | ||
|
||
pub struct ConfigurationPackageChangeCallbackHandle(pub(crate) i64); | ||
|
||
impl ConfigurationPackageChangeCallbackHandle { | ||
/// # Safety | ||
/// Caller ensures this is a registered callback id | ||
pub const unsafe fn from_com(com: i64) -> Self { | ||
Self(com) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// ------------------------------------------------------------ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License (MIT). See License.txt in the repo root for license information. | ||
// ------------------------------------------------------------ | ||
//! This module supports implementing callbacks when Service Fabric Packages are changed | ||
//! | ||
pub mod config; | ||
|
||
/// The ways a given Service Fabric Package (e.g. ConfigurationPackage or DataPackage) can change | ||
#[derive(Debug, PartialEq, Eq, Clone)] | ||
pub enum PackageChangeEvent<T> { | ||
Addition { new_package: T }, | ||
Removal { previous_package: T }, | ||
Modification { previous_package: T, new_package: T }, | ||
} | ||
|
||
pub type ConfigurationPackageChangeEvent = PackageChangeEvent<super::config::ConfigurationPackage>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters