public ScenarioNotificationData(ScenarioData scenarioData, NotifyFilter filter) { Assert.IsNotNull(scenarioData, "scenarioData"); Assert.IsNotNull(filter, "filter"); this.ScenarioData = scenarioData; this.Filter = filter; }
/// <inheritdoc /> /// <remarks> /// You can add the same routine again and we will call you once for each registered callback. Caller should handle that logic /// </remarks> public async Task RegisterCallbackAsync(NotifyFilter filter, Func <ScenarioData, Task> callbackRoutine, CancellationToken token) { Assert.IsNotNull(filter, "filter"); Assert.IsNotNull(callbackRoutine, "callbackRoutine"); this.Logger.LogMessage("RegisterCallbackAsync:: Registering callback with Filter '{0}'", filter); if (!this.GetCallbackSupportedScenario().Contains(filter.TargetScenario)) { throw new NotSupportedException( string.Format(CultureInfo.InvariantCulture, "Scenario : '{0}' not supported for callback", filter.TargetScenario)); } await this.singleAccessLock.WaitAsync(token).ConfigureAwait(false); try { if (!this.callbackMap.ContainsKey(filter)) { this.callbackMap[filter] = new List <Func <ScenarioData, Task> > { callbackRoutine }; } else { this.callbackMap[filter].Add(callbackRoutine); } if (this.newEventSearcherTask == null) { this.Logger.LogMessage("RegisterCallbackAsync:: Kicking off Searcher Task"); // kick off tasks that continuously monitors for new signals. this.newEventSearcherTask = this.taskRunner.Run("NewEventSearcherTask", () => this.SearchForNewSignalsInClusterAsync(token), this.InternalTokenSource.Token); } if (this.eventDispatcherTask == null) { this.Logger.LogMessage("RegisterCallbackAsync:: Kicking off Dispatcher Task"); // kick off a task that continuously looks for new signals seen and invoke callbacks associated with them. this.eventDispatcherTask = this.taskRunner.Run("CallbackDispatcherTask", () => this.DispatchSignalsAsync(token), this.InternalTokenSource.Token); } } finally { this.singleAccessLock.Release(); } }
/// <inheritdoc /> public async Task UnRegisterCallbackAsync(NotifyFilter filter, Func <ScenarioData, Task> callbackRoutine, CancellationToken token) { Assert.IsNotNull(filter, "filter"); Assert.IsNotNull(callbackRoutine, "callbackRoutine"); this.Logger.LogMessage("UnRegisterCallbackAsync:: UnRegistering callback with Filter '{0}'", filter); if (!this.GetCallbackSupportedScenario().Contains(filter.TargetScenario)) { throw new NotSupportedException( string.Format(CultureInfo.InvariantCulture, "Scenario : '{0}' not supported for callback", filter.TargetScenario)); } await this.singleAccessLock.WaitAsync(token).ConfigureAwait(false); try { if (!this.callbackMap.ContainsKey(filter)) { throw new CallbackNotRegisteredException(string.Format(CultureInfo.InvariantCulture, "Callback for Filter: {0} not registered", filter)); } if (!this.callbackMap[filter].Contains(callbackRoutine)) { throw new CallbackNotRegisteredException( string.Format(CultureInfo.InvariantCulture, "Callback for Filter: {0} with Delegage: {1} not registered", filter, callbackRoutine)); } this.callbackMap[filter].Remove(callbackRoutine); // if there are no callbacks for this filter, remove the entry from map. if (!this.callbackMap[filter].Any()) { this.callbackMap[filter] = null; this.callbackMap.Remove(filter); } } finally { this.singleAccessLock.Release(); } }
private async Task <IEnumerable <ScenarioData> > FilterNewSignalsAsync( IEnumerable <TraceRecord> traceRecords, NotifyFilter filter, CancellationToken token) { var newSignalsMatchingFilter = new List <ScenarioData>(); foreach (var oneTraceRecord in traceRecords.OrderBy(record => record.TimeStamp)) { // Encapsulate the new information into a shareable abstraction. We mark the event seen time as current time // which may not be completely accurate but it's close enough for all purposes. var scenarioData = new ScenarioData(filter.TargetScenario, oneTraceRecord, DateTime.UtcNow); if (DateTime.UtcNow - oneTraceRecord.TimeStamp > filter.SignalAvailableForAtLeastDuration) { if (!await this.IsSignalAlreadyReportedAsync(scenarioData, token).ConfigureAwait(false)) { newSignalsMatchingFilter.Add(scenarioData); } } } return(newSignalsMatchingFilter); }
private async Task <IList <Func <ScenarioData, Task> > > GetCallbacksForFilterAsync(NotifyFilter filter, CancellationToken token) { await this.singleAccessLock.WaitAsync(token).ConfigureAwait(false); try { return(this.callbackMap[filter]); } catch (KeyNotFoundException) { this.Logger.LogWarning("GetCallbacksForFilterAsync:: Key {0} not found", filter); throw; } finally { this.singleAccessLock.Release(); } }