/// <summary> /// Call all subscribers in parallel and wait for all to complete. /// </summary> /// <typeparam name="T">The type of the results.</typeparam> /// <param name="ServerTimestamp">The timestamp of the event.</param> /// <param name="OCPIAPI">The sending OCPI/HTTP API.</param> /// <param name="Request">The incoming request.</param> /// <param name="Response">The outgoing response.</param> /// <param name="VerifyResult">A delegate to verify and filter results.</param> /// <param name="Timeout">A timeout for this operation.</param> /// <param name="DefaultResult">A default result in case of errors or a timeout.</param> public Task <T> WhenFirst <T>(DateTime ServerTimestamp, HTTPAPI OCPIAPI, OCPIRequest Request, OCPIResponse Response, Func <T, Boolean> VerifyResult, TimeSpan?Timeout = null, Func <TimeSpan, T> DefaultResult = null) { #region Data List <Task> _invocationList; Task WorkDone; Task <T> Result; DateTime StartTime = DateTime.UtcNow; Task TimeoutTask = null; #endregion lock (subscribers) { _invocationList = subscribers. Select(callback => callback(ServerTimestamp, OCPIAPI, Request, Response)). ToList(); if (Timeout.HasValue) { _invocationList.Add(TimeoutTask = Task.Run(() => System.Threading.Thread.Sleep(Timeout.Value))); } } do { try { WorkDone = Task.WhenAny(_invocationList); _invocationList.Remove(WorkDone); if (WorkDone != TimeoutTask) { Result = WorkDone as Task <T>; if (Result != null && !EqualityComparer <T> .Default.Equals(Result.Result, default(T)) && VerifyResult(Result.Result)) { return(Result); } } } catch (Exception e) { DebugX.LogT(e.Message); WorkDone = null; } }while (!(WorkDone == TimeoutTask || _invocationList.Count == 0)); return(Task.FromResult(DefaultResult(DateTime.UtcNow - StartTime))); }
/// <summary> /// Call all subscribers in parallel and wait for all to complete. /// </summary> /// <param name="ServerTimestamp">The timestamp of the event.</param> /// <param name="OCPIAPI">The sending OCPI/HTTP API.</param> /// <param name="Request">The incoming request.</param> /// <param name="Response">The outgoing response.</param> public Task WhenAll(DateTime ServerTimestamp, HTTPAPI OCPIAPI, OCPIRequest Request, OCPIResponse Response) { Task[] _invocationList; lock (subscribers) { _invocationList = subscribers. Select(callback => callback(ServerTimestamp, OCPIAPI, Request, Response)). ToArray(); } return(Task.WhenAll(_invocationList)); }
/// <summary> /// Call all subscribers sequentially. /// </summary> /// <param name="ServerTimestamp">The timestamp of the event.</param> /// <param name="OCPIAPI">The sending OCPI/HTTP API.</param> /// <param name="Request">The incoming request.</param> /// <param name="Response">The outgoing response.</param> public async Task InvokeAsync(DateTime ServerTimestamp, HTTPAPI OCPIAPI, OCPIRequest Request, OCPIResponse Response) { OCPIResponseLogHandler[] _invocationList; lock (subscribers) { _invocationList = subscribers.ToArray(); } foreach (var callback in _invocationList) { await callback(ServerTimestamp, OCPIAPI, Request, Response).ConfigureAwait(false); } }
/// <summary> /// Call all subscribers in parallel and wait for any to complete. /// </summary> /// <param name="ServerTimestamp">The timestamp of the event.</param> /// <param name="OCPIAPI">The sending OCPI/HTTP API.</param> /// <param name="Request">The incoming request.</param> /// <param name="Response">The outgoing response.</param> /// <param name="Timeout">A timeout for this operation.</param> public Task WhenAny(DateTime ServerTimestamp, HTTPAPI OCPIAPI, OCPIRequest Request, OCPIResponse Response, TimeSpan?Timeout = null) { List <Task> _invocationList; lock (subscribers) { _invocationList = subscribers. Select(callback => callback(ServerTimestamp, OCPIAPI, Request, Response)). ToList(); if (Timeout.HasValue) { _invocationList.Add(Task.Delay(Timeout.Value)); } } return(Task.WhenAny(_invocationList)); }