// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. #region Using directives using System; using System.Globalization; using System.Management.Automation; using System.Threading; #endregion namespace Microsoft.Management.Infrastructure.CimCmdlets { /// /// /// Subscription result event args /// /// internal abstract class CimSubscriptionEventArgs : EventArgs { /// /// /// Returns an Object value for an operation context /// /// public object Context { get { return context; } } protected object context; } /// /// /// Subscription result event args /// /// internal class CimSubscriptionResultEventArgs : CimSubscriptionEventArgs { /// /// /// subscription result /// /// public CimSubscriptionResult Result { get; } /// /// Initializes a new instance of the class. /// /// public CimSubscriptionResultEventArgs( CimSubscriptionResult theResult) { this.context = null; this.Result = theResult; } } /// /// /// Subscription result event args /// /// internal class CimSubscriptionExceptionEventArgs : CimSubscriptionEventArgs { /// /// /// subscription result /// /// public Exception Exception { get; } /// /// Initializes a new instance of the class. /// /// public CimSubscriptionExceptionEventArgs( Exception theException) { this.context = null; this.Exception = theException; } } /// /// /// Implements operations of register-cimindication cmdlet. /// /// internal sealed class CimRegisterCimIndication : CimAsyncOperation { /// /// /// New subscription result event /// /// public event EventHandler OnNewSubscriptionResult; /// /// Initializes a new instance of the class. /// public CimRegisterCimIndication() : base() { this.ackedEvent = new ManualResetEventSlim(false); } /// /// Start an indication subscription target to the given computer. /// /// Null stands for localhost. /// /// /// /// public void RegisterCimIndication( string computerName, string nameSpace, string queryDialect, string queryExpression, uint operationTimeout) { DebugHelper.WriteLogEx("queryDialect = '{0}'; queryExpression = '{1}'", 0, queryDialect, queryExpression); this.TargetComputerName = computerName; CimSessionProxy proxy = CreateSessionProxy(computerName, operationTimeout); proxy.SubscribeAsync(nameSpace, queryDialect, queryExpression); WaitForAckMessage(); } /// /// Start an indication subscription through a given . /// /// Cannot be null. /// /// /// /// /// Throw if cimSession is null. public void RegisterCimIndication( CimSession cimSession, string nameSpace, string queryDialect, string queryExpression, uint operationTimeout) { DebugHelper.WriteLogEx("queryDialect = '{0}'; queryExpression = '{1}'", 0, queryDialect, queryExpression); ArgumentNullException.ThrowIfNull(cimSession, string.Format(CultureInfo.CurrentUICulture, CimCmdletStrings.NullArgument, nameof(cimSession))); this.TargetComputerName = cimSession.ComputerName; CimSessionProxy proxy = CreateSessionProxy(cimSession, operationTimeout); proxy.SubscribeAsync(nameSpace, queryDialect, queryExpression); WaitForAckMessage(); } #region override methods /// /// /// Subscribe to the events issued by . /// /// /// protected override void SubscribeToCimSessionProxyEvent(CimSessionProxy proxy) { DebugHelper.WriteLog("SubscribeToCimSessionProxyEvent", 4); // Raise event instead of write object to ps proxy.OnNewCmdletAction += this.CimIndicationHandler; proxy.OnOperationCreated += this.OperationCreatedHandler; proxy.OnOperationDeleted += this.OperationDeletedHandler; proxy.EnableMethodResultStreaming = false; } /// /// /// Handler used to handle new action event from /// object. /// /// /// /// object raised the event /// /// Event argument. private void CimIndicationHandler(object cimSession, CmdletActionEventArgs actionArgs) { DebugHelper.WriteLogEx("action is {0}. Disposed {1}", 0, actionArgs.Action, this.Disposed); if (this.Disposed) { return; } // NOTES: should move after this.Disposed, but need to log the exception if (actionArgs.Action is CimWriteError cimWriteError) { this.Exception = cimWriteError.Exception; if (!this.ackedEvent.IsSet) { // an exception happened DebugHelper.WriteLogEx("an exception happened", 0); this.ackedEvent.Set(); return; } EventHandler temp = this.OnNewSubscriptionResult; if (temp != null) { DebugHelper.WriteLog("Raise an exception event", 2); temp(this, new CimSubscriptionExceptionEventArgs(this.Exception)); } DebugHelper.WriteLog("Got an exception: {0}", 2, Exception); } if (actionArgs.Action is CimWriteResultObject cimWriteResultObject) { if (cimWriteResultObject.Result is CimSubscriptionResult result) { EventHandler temp = this.OnNewSubscriptionResult; if (temp != null) { DebugHelper.WriteLog("Raise an result event", 2); temp(this, new CimSubscriptionResultEventArgs(result)); } } else { if (!this.ackedEvent.IsSet) { // an ACK message returned DebugHelper.WriteLogEx("an ack message happened", 0); this.ackedEvent.Set(); return; } else { DebugHelper.WriteLogEx("an ack message should not happen here", 0); } } } } /// /// Block the ps thread until ACK message or Error happened. /// private void WaitForAckMessage() { DebugHelper.WriteLogEx(); this.ackedEvent.Wait(); if (this.Exception != null) { DebugHelper.WriteLogEx("error happened", 0); if (this.Cmdlet != null) { DebugHelper.WriteLogEx("Throw Terminating error", 1); // throw terminating error ErrorRecord errorRecord = ErrorToErrorRecord.ErrorRecordFromAnyException( new InvocationContext(this.TargetComputerName, null), this.Exception, null); this.Cmdlet.ThrowTerminatingError(errorRecord); } else { DebugHelper.WriteLogEx("Throw exception", 1); // throw exception out throw this.Exception; } } DebugHelper.WriteLogEx("ACK happened", 0); } #endregion #region internal property /// /// The cmdlet object who issue this subscription, /// to throw ThrowTerminatingError /// in case there is a subscription failure. /// /// internal Cmdlet Cmdlet { get; set; } /// /// Target computername. /// internal string TargetComputerName { get; set; } #endregion #region private methods /// /// /// Create and set properties /// /// /// /// /// private CimSessionProxy CreateSessionProxy( string computerName, uint timeout) { CimSessionProxy proxy = CreateCimSessionProxy(computerName); proxy.OperationTimeout = timeout; return proxy; } /// /// Create and set properties. /// /// /// /// private CimSessionProxy CreateSessionProxy( CimSession session, uint timeout) { CimSessionProxy proxy = CreateCimSessionProxy(session); proxy.OperationTimeout = timeout; return proxy; } #endregion #region private members /// /// Exception occurred while start the subscription. /// internal Exception Exception { get; private set; } #endregion } }