// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. #region Using directives using System; using System.ComponentModel; using System.Management.Automation; #endregion namespace Microsoft.Management.Infrastructure.CimCmdlets { /// /// /// Abstract Cimindication event args, which containing all elements related to /// an Cimindication. /// /// public abstract class CimIndicationEventArgs : EventArgs { /// /// /// Returns an Object value for an operation context /// /// public object Context { get { return context; } } internal object context; } /// /// Cimindication exception event args, which containing occurred exception. /// public class CimIndicationEventExceptionEventArgs : CimIndicationEventArgs { /// /// /// Returns an exception /// /// public Exception Exception { get { return exception; } } private Exception exception; /// /// /// Constructor /// /// /// public CimIndicationEventExceptionEventArgs(Exception theException) { context = null; this.exception = theException; } } /// /// Cimindication event args, which containing all elements related to /// an Cimindication. /// public class CimIndicationEventInstanceEventArgs : CimIndicationEventArgs { /// /// Get ciminstance of the indication object. /// public CimInstance NewEvent { get { return (result == null) ? null : result.Instance; } } /// /// Get MachineId of the indication object. /// public string MachineId { get { return (result == null) ? null : result.MachineId; } } /// /// Get BookMark of the indication object. /// public string Bookmark { get { return (result == null) ? null : result.Bookmark; } } /// /// /// Constructor /// /// /// public CimIndicationEventInstanceEventArgs(CimSubscriptionResult result) { context = null; this.result = result; } /// /// /// subscription result /// /// private CimSubscriptionResult result; } /// /// /// A public class used to start/stop the subscription to specific indication source, /// and listen to the incoming indications, event /// will be raised for each cimindication. /// /// public class CimIndicationWatcher { /// /// Status of object. /// internal enum Status { Default, Started, Stopped } /// /// /// CimIndication arrived event /// /// public event EventHandler CimIndicationArrived; /// /// /// Constructor with given computerName, namespace, queryExpression and timeout /// /// /// /// /// /// public CimIndicationWatcher( string computerName, string theNamespace, string queryDialect, string queryExpression, UInt32 operationTimeout) { ValidationHelper.ValidateNoNullorWhiteSpaceArgument(queryExpression, queryExpressionParameterName); computerName = ConstValue.GetComputerName(computerName); theNamespace = ConstValue.GetNamespace(theNamespace); Initialize(computerName, null, theNamespace, queryDialect, queryExpression, operationTimeout); } /// /// /// Constructor with given cimsession, namespace, queryExpression and timeout /// /// /// /// /// /// public CimIndicationWatcher( CimSession cimSession, string theNamespace, string queryDialect, string queryExpression, UInt32 operationTimeout) { ValidationHelper.ValidateNoNullorWhiteSpaceArgument(queryExpression, queryExpressionParameterName); ValidationHelper.ValidateNoNullArgument(cimSession, cimSessionParameterName); theNamespace = ConstValue.GetNamespace(theNamespace); Initialize(null, cimSession, theNamespace, queryDialect, queryExpression, operationTimeout); } /// /// /// Initialize /// /// private void Initialize( string theComputerName, CimSession theCimSession, string theNameSpace, string theQueryDialect, string theQueryExpression, UInt32 theOperationTimeout) { enableRaisingEvents = false; status = Status.Default; myLock = new object(); cimRegisterCimIndication = new CimRegisterCimIndication(); cimRegisterCimIndication.OnNewSubscriptionResult += NewSubscriptionResultHandler; this.cimSession = theCimSession; this.nameSpace = theNameSpace; this.queryDialect = ConstValue.GetQueryDialectWithDefault(theQueryDialect); this.queryExpression = theQueryExpression; this.operationTimeout = theOperationTimeout; this.computerName = theComputerName; } /// /// /// Handler of new subscription result /// /// /// /// private void NewSubscriptionResultHandler(object src, CimSubscriptionEventArgs args) { EventHandler temp = this.CimIndicationArrived; if (temp != null) { // raise the event CimSubscriptionResultEventArgs resultArgs = args as CimSubscriptionResultEventArgs; if (resultArgs != null) temp(this, new CimIndicationEventInstanceEventArgs(resultArgs.Result)); else { CimSubscriptionExceptionEventArgs exceptionArgs = args as CimSubscriptionExceptionEventArgs; if (exceptionArgs != null) temp(this, new CimIndicationEventExceptionEventArgs(exceptionArgs.Exception)); } } } /// /// /// Will be called by admin\monad\src\eengine\EventManager.cs: /// PSEventManager::ProcessNewSubscriber to start to listen to the Cim Indication. /// /// /// If set EnableRaisingEvents to false, which will be ignored /// /// [BrowsableAttribute(false)] public bool EnableRaisingEvents { get { return enableRaisingEvents; } set { DebugHelper.WriteLogEx(); if (value && !enableRaisingEvents) { enableRaisingEvents = value; Start(); } } } private bool enableRaisingEvents; /// /// /// Start the subscription /// /// public void Start() { DebugHelper.WriteLogEx(); lock (myLock) { if (status == Status.Default) { if (this.cimSession == null) { cimRegisterCimIndication.RegisterCimIndication( this.computerName, this.nameSpace, this.queryDialect, this.queryExpression, this.operationTimeout); } else { cimRegisterCimIndication.RegisterCimIndication( this.cimSession, this.nameSpace, this.queryDialect, this.queryExpression, this.operationTimeout); } status = Status.Started; } } } /// /// /// Unsubscribe the subscription /// /// public void Stop() { DebugHelper.WriteLogEx("Status = {0}", 0, this.status); lock (this.myLock) { if (status == Status.Started) { if (this.cimRegisterCimIndication != null) { DebugHelper.WriteLog("Dispose CimRegisterCimIndication object", 4); this.cimRegisterCimIndication.Dispose(); } status = Status.Stopped; } } } #region internal method /// /// Set the cmdlet object to throw ThrowTerminatingError /// in case there is a subscription failure. /// /// internal void SetCmdlet(Cmdlet cmdlet) { if (this.cimRegisterCimIndication != null) { this.cimRegisterCimIndication.Cmdlet = cmdlet; } } #endregion #region private members /// /// /// CimRegisterCimIndication object /// /// private CimRegisterCimIndication cimRegisterCimIndication; /// /// The status of object. /// private Status status; /// /// Lock started field. /// private object myLock; /// /// CimSession parameter name. /// private const string cimSessionParameterName = "cimSession"; /// /// QueryExpression parameter name. /// private const string queryExpressionParameterName = "queryExpression"; #region parameters /// /// /// parameters used to start the subscription /// /// private string computerName; private CimSession cimSession; private string nameSpace; private string queryDialect; private string queryExpression; private UInt32 operationTimeout; #endregion #endregion } }