File size: 16,463 Bytes
8c763fb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#region Using directives
using System;
using System.Globalization;
using System.Management.Automation;
#endregion
namespace Microsoft.Management.Infrastructure.CimCmdlets
{
#region AsyncResultType
/// <summary>
/// <para>
/// Async result type
/// </para>
/// </summary>
public enum AsyncResultType
{
Result,
Exception,
Completion
}
#endregion
#region CimResultContext
/// <summary>
/// Cim Result Context.
/// </summary>
internal class CimResultContext
{
/// <summary>
/// Initializes a new instance of the <see cref="CimResultContext"/> class.
/// </summary>
/// <param name="ErrorSource"></param>
internal CimResultContext(object ErrorSource)
{
this.ErrorSource = ErrorSource;
}
/// <summary>
/// ErrorSource property.
/// </summary>
internal object ErrorSource { get; }
}
#endregion
#region AsyncResultEventArgsBase
/// <summary>
/// <para>
/// Base class of async result event argument
/// </para>
/// </summary>
internal abstract class AsyncResultEventArgsBase : EventArgs
{
/// <summary>
/// Initializes a new instance of the <see cref="AsyncResultEventArgsBase"/> class.
/// </summary>
/// <param name="session"></param>
/// <param name="observable"></param>
/// <param name="resultType"></param>
protected AsyncResultEventArgsBase(
CimSession session,
IObservable<object> observable,
AsyncResultType resultType)
{
this.session = session;
this.observable = observable;
this.resultType = resultType;
}
/// <summary>
/// Initializes a new instance of the <see cref="AsyncResultEventArgsBase"/> class.
/// </summary>
/// <param name="session"></param>
/// <param name="observable"></param>
/// <param name="resultType"></param>
/// <param name="context"></param>
protected AsyncResultEventArgsBase(
CimSession session,
IObservable<object> observable,
AsyncResultType resultType,
CimResultContext cimResultContext)
{
this.session = session;
this.observable = observable;
this.resultType = resultType;
this.context = cimResultContext;
}
public readonly CimSession session;
public readonly IObservable<object> observable;
public readonly AsyncResultType resultType;
// property ErrorSource
public readonly CimResultContext context;
}
#endregion
#region AsyncResult*Args
/// <summary>
/// <para>
/// operation successfully completed event argument
/// </para>
/// </summary>
internal class AsyncResultCompleteEventArgs : AsyncResultEventArgsBase
{
/// <summary>
/// Initializes a new instance of the <see cref="AsyncResultCompleteEventArgs"/> class.
/// </summary>
/// <param name="session"><see cref="CimSession"/> object.</param>
/// <param name="cancellationDisposable"></param>
public AsyncResultCompleteEventArgs(
CimSession session,
IObservable<object> observable)
: base(session, observable, AsyncResultType.Completion)
{
}
}
/// <summary>
/// <para>
/// async result argument with object
/// </para>
/// </summary>
internal class AsyncResultObjectEventArgs : AsyncResultEventArgsBase
{
/// <summary>
/// Initializes a new instance of the <see cref="AsyncResultObjectEventArgs"/> class.
/// </summary>
/// <param name="session"></param>
/// <param name="observable"></param>
/// <param name="resultObject"></param>
public AsyncResultObjectEventArgs(
CimSession session,
IObservable<object> observable,
object resultObject)
: base(session, observable, AsyncResultType.Result)
{
this.resultObject = resultObject;
}
public readonly object resultObject;
}
/// <summary>
/// <para>
/// operation completed with exception event argument
/// </para>
/// </summary>
internal class AsyncResultErrorEventArgs : AsyncResultEventArgsBase
{
/// <summary>
/// Initializes a new instance of the <see cref="AsyncResultErrorEventArgs"/> class.
/// </summary>
/// <param name="session"></param>
/// <param name="observable"></param>
/// <param name="error"></param>
public AsyncResultErrorEventArgs(
CimSession session,
IObservable<object> observable,
Exception error)
: base(session, observable, AsyncResultType.Exception)
{
this.error = error;
}
/// <summary>
/// Initializes a new instance of the <see cref="AsyncResultErrorEventArgs"/> class.
/// </summary>
/// <param name="session"></param>
/// <param name="observable"></param>
/// <param name="error"></param>
/// <param name="context"></param>
public AsyncResultErrorEventArgs(
CimSession session,
IObservable<object> observable,
Exception error,
CimResultContext cimResultContext)
: base(session, observable, AsyncResultType.Exception, cimResultContext)
{
this.error = error;
}
public readonly Exception error;
}
#endregion
#region CimResultObserver
/// <summary>
/// <para>
/// Observer to consume results from asynchronous operations, such as,
/// EnumerateInstancesAsync operation of <see cref="CimSession"/> object.
/// </para>
/// <para>
/// (See https://channel9.msdn.com/posts/J.Van.Gogh/Reactive-Extensions-API-in-depth-Contract/)
/// for the IObserver/IObservable contact
/// - the only possible sequence is OnNext* (OnCompleted|OnError)?
/// - callbacks are serialized
/// - Subscribe never throws
/// </para>
/// </summary>
/// <typeparam name="T">object type</typeparam>
internal class CimResultObserver<T> : IObserver<T>
{
/// <summary>
/// Define an Event based on the NewActionHandler.
/// </summary>
public event EventHandler<AsyncResultEventArgsBase> OnNewResult;
/// <summary>
/// Initializes a new instance of the <see cref="CimResultObserver{T}"/> class.
/// </summary>
/// <param name="session"><see cref="CimSession"/> object that issued the operation.</param>
/// <param name="observable">Operation that can be observed.</param>
public CimResultObserver(CimSession session, IObservable<object> observable)
{
this.CurrentSession = session;
this.observable = observable;
}
/// <summary>
/// Initializes a new instance of the <see cref="CimResultObserver{T}"/> class.
/// </summary>
/// <param name="session"><see cref="CimSession"/> object that issued the operation.</param>
/// <param name="observable">Operation that can be observed.</param>
public CimResultObserver(CimSession session,
IObservable<object> observable,
CimResultContext cimResultContext)
{
this.CurrentSession = session;
this.observable = observable;
this.context = cimResultContext;
}
/// <summary>
/// <para>
/// Operation completed successfully
/// </para>
/// </summary>
public virtual void OnCompleted()
{
// callbacks should never throw any exception to
// protocol layer, otherwise the client process will be
// terminated because of unhandled exception, same with
// OnNext, OnError
try
{
AsyncResultCompleteEventArgs completeArgs = new(
this.CurrentSession, this.observable);
this.OnNewResult(this, completeArgs);
}
catch (Exception ex)
{
this.OnError(ex);
DebugHelper.WriteLogEx("{0}", 0, ex);
}
}
/// <summary>
/// <para>
/// Operation completed with an error
/// </para>
/// </summary>
/// <param name="error">Error object.</param>
public virtual void OnError(Exception error)
{
try
{
AsyncResultErrorEventArgs errorArgs = new(
this.CurrentSession, this.observable, error, this.context);
this.OnNewResult(this, errorArgs);
}
catch (Exception ex)
{
// !!ignore the exception
DebugHelper.WriteLogEx("{0}", 0, ex);
}
}
/// <summary>
/// Deliver the result value.
/// </summary>
/// <param name="value"></param>
protected void OnNextCore(object value)
{
DebugHelper.WriteLogEx("value = {0}.", 1, value);
try
{
AsyncResultObjectEventArgs resultArgs = new(
this.CurrentSession, this.observable, value);
this.OnNewResult(this, resultArgs);
}
catch (Exception ex)
{
this.OnError(ex);
DebugHelper.WriteLogEx("{0}", 0, ex);
}
}
/// <summary>
/// <para>
/// Operation got a new result object
/// </para>
/// </summary>
/// <param name="value">Result object.</param>
public virtual void OnNext(T value)
{
DebugHelper.WriteLogEx("value = {0}.", 1, value);
// do not allow null value
if (value == null)
{
return;
}
this.OnNextCore(value);
}
#region members
/// <summary>
/// Session object of the operation.
/// </summary>
protected CimSession CurrentSession { get; }
/// <summary>
/// Async operation that can be observed.
/// </summary>
private readonly IObservable<object> observable;
/// <summary>
/// <see cref="CimResultContext"/> object used during delivering result.
/// </summary>
private readonly CimResultContext context;
#endregion
}
/// <summary>
/// CimSubscriptionResultObserver class definition.
/// </summary>
internal class CimSubscriptionResultObserver : CimResultObserver<CimSubscriptionResult>
{
/// <summary>
/// Initializes a new instance of the <see cref="CimSubscriptionResultObserver"/> class.
/// </summary>
/// <param name="session"></param>
/// <param name="observable"></param>
public CimSubscriptionResultObserver(CimSession session, IObservable<object> observable)
: base(session, observable)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="CimSubscriptionResultObserver"/> class.
/// </summary>
/// <param name="session"></param>
/// <param name="observable"></param>
public CimSubscriptionResultObserver(
CimSession session,
IObservable<object> observable,
CimResultContext context)
: base(session, observable, context)
{
}
/// <summary>
/// Override the OnNext method.
/// </summary>
/// <param name="value"></param>
public override void OnNext(CimSubscriptionResult value)
{
DebugHelper.WriteLogEx();
base.OnNextCore(value);
}
}
/// <summary>
/// CimMethodResultObserver class definition.
/// </summary>
internal class CimMethodResultObserver : CimResultObserver<CimMethodResultBase>
{
/// <summary>
/// Initializes a new instance of the <see cref="CimMethodResultObserver"/> class.
/// </summary>
/// <param name="session"></param>
/// <param name="observable"></param>
public CimMethodResultObserver(CimSession session, IObservable<object> observable)
: base(session, observable)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="CimMethodResultObserver"/> class.
/// </summary>
/// <param name="session"></param>
/// <param name="observable"></param>
/// <param name="context"></param>
public CimMethodResultObserver(
CimSession session,
IObservable<object> observable,
CimResultContext context)
: base(session, observable, context)
{
}
/// <summary>
/// Override the OnNext method.
/// </summary>
/// <param name="value"></param>
public override void OnNext(CimMethodResultBase value)
{
DebugHelper.WriteLogEx();
const string PSTypeCimMethodResult = @"Microsoft.Management.Infrastructure.CimMethodResult";
const string PSTypeCimMethodStreamedResult = @"Microsoft.Management.Infrastructure.CimMethodStreamedResult";
const string PSTypeCimMethodResultTemplate = @"{0}#{1}#{2}";
string resultObjectPSType = null;
PSObject resultObject = null;
if (value is CimMethodResult methodResult)
{
resultObjectPSType = PSTypeCimMethodResult;
resultObject = new PSObject();
foreach (CimMethodParameter param in methodResult.OutParameters)
{
resultObject.Properties.Add(new PSNoteProperty(param.Name, param.Value));
}
}
else
{
if (value is CimMethodStreamedResult methodStreamedResult)
{
resultObjectPSType = PSTypeCimMethodStreamedResult;
resultObject = new PSObject();
resultObject.Properties.Add(new PSNoteProperty(@"ParameterName", methodStreamedResult.ParameterName));
resultObject.Properties.Add(new PSNoteProperty(@"ItemType", methodStreamedResult.ItemType));
resultObject.Properties.Add(new PSNoteProperty(@"ItemValue", methodStreamedResult.ItemValue));
}
}
if (resultObject != null)
{
resultObject.Properties.Add(new PSNoteProperty(@"PSComputerName", this.CurrentSession.ComputerName));
resultObject.TypeNames.Insert(0, resultObjectPSType);
resultObject.TypeNames.Insert(0, string.Format(CultureInfo.InvariantCulture, PSTypeCimMethodResultTemplate, resultObjectPSType, ClassName, MethodName));
base.OnNextCore(resultObject);
}
}
/// <summary>
/// Methodname.
/// </summary>
internal string MethodName
{
get;
set;
}
/// <summary>
/// Classname.
/// </summary>
internal string ClassName
{
get;
set;
}
}
/// <summary>
/// IgnoreResultObserver class definition.
/// </summary>
internal class IgnoreResultObserver : CimResultObserver<CimInstance>
{
/// <summary>
/// Initializes a new instance of the <see cref="IgnoreResultObserver"/> class.
/// </summary>
/// <param name="session"></param>
/// <param name="observable"></param>
public IgnoreResultObserver(CimSession session, IObservable<object> observable)
: base(session, observable)
{
}
/// <summary>
/// Override the OnNext method.
/// </summary>
/// <param name="value"></param>
public override void OnNext(CimInstance value)
{
DebugHelper.WriteLogEx();
}
}
#endregion
}
|