internal async Task <TResult> ExecuteAsync <TParams, TResult>(string method, TParams parameters, int ttl = Request.DEFAULT_RESPONSE_TIMEOUT_SECONDS) { TaskCompletionSource <TResult> tcs = new TaskCompletionSource <TResult>(); if (mProtocol == null) { string message = "Setup has not been performed"; tcs.SetException(new KeyNotFoundException(message)); Log(LogLevel.Error, message); return(await tcs.Task); } if (ttl < 1) { ttl = Request.DEFAULT_RESPONSE_TIMEOUT_SECONDS; } Task <ResponseTaskResult <ExecuteResult> > task = mClient.Session.ExecuteAsync(mProtocol, method, parameters, TimeSpan.FromSeconds(ttl)); ResponseTaskResult <ExecuteResult> result = await task; if (task.IsFaulted) { tcs.SetException(task.Exception); } else { tcs.SetResult(result.Result.ResultAs <TResult>()); } return(await tcs.Task); }
protected async Task <TResult> ExecuteAsync <TParams, TResult>(string method, TParams parameters) { TaskCompletionSource <TResult> tcs = new TaskCompletionSource <TResult>(); if (mProtocol == null) { string message = string.Format("Setup for {0} has not been performed", mService); tcs.SetException(new KeyNotFoundException(message)); mLogger.LogError(message); return(await tcs.Task); } Task <ResponseTaskResult <ExecuteResult> > task = mClient.Session.ExecuteAsync(mProtocol, method, parameters); ResponseTaskResult <ExecuteResult> result = await task; if (task.IsFaulted) { tcs.SetException(task.Exception); } else { tcs.SetResult(result.Result.ResultAs <TResult>()); } return(await tcs.Task); }
private static bool Test2(Client client) { // Test execute of signalwire setup with an inner params object containing a protocol field that has an invalid value for restoring Task <ResponseTaskResult <ExecuteResult> > setupTask = null; ResponseTaskResult <ExecuteResult> setupTaskResult = null; Logger.LogInformation("Started"); try { setupTask = client.Session.ExecuteAsync("signalwire", "setup", new JObject { ["protocol"] = "invalid" }); setupTaskResult = setupTask.Result; } catch (AggregateException exc) { if (exc.InnerExceptions.Count != 1) { Logger.LogError(exc, "Received an unexpected count of errors on signalwire setup"); sCompleted.Set(); return(false); } var inner = exc.InnerExceptions[0]; if (inner.GetType() != typeof(InvalidOperationException)) { // Successful, it detected bad arguments and responded Logger.LogError(exc, "Received an exception that was not InvalidOperationException on signalwire setup"); sCompleted.Set(); return(false); } } catch (TimeoutException exc) { // Timeout means there was no response, that's bad Logger.LogError(exc, "Received a timeout on signalwire setup"); sCompleted.Set(); return(false); } catch (Exception exc) { // Anything else failed in an unexpected way Logger.LogError(exc, "Received a unexpected error on signalwire setup"); sCompleted.Set(); return(false); } if (setupTaskResult != null) { Logger.LogError("Result is not null as expected"); sCompleted.Set(); return(false); } return(true); }
public async Task <string> SetupAsync() { if (SetupCompleted) { return(mProtocol); } return(await Task.Run(async() => { if (mClient.Session.State != UpstreamSession.SessionState.Running) { string message = "Setup failed because the session is not running"; Log(LogLevel.Error, message); throw new InvalidOperationException(message); } if (!await CheckProtocolAvailableAsync("signalwire", TimeSpan.FromSeconds(5))) { string message = "Setup failed due to timeout waiting for protocol 'signalwire'"; Log(LogLevel.Error, message); throw new TimeoutException(message); } SetupParams setupParams = new SetupParams(); if (!string.IsNullOrWhiteSpace(mProtocol)) { setupParams.Protocol = mProtocol; } Task <ResponseTaskResult <ExecuteResult> > setupTask = mClient.Session.ExecuteAsync("signalwire", "setup", setupParams); ResponseTaskResult <ExecuteResult> setupTaskResult = await setupTask; if (setupTask.IsFaulted) { Log(LogLevel.Error, string.Format("Setup Faulted\n{0}", setupTask.Exception)); throw setupTask.Exception; } SetupResult setupResult = setupTaskResult.Result.ResultAs <SetupResult>(); if (!await CheckProtocolAvailableAsync(setupResult.Protocol, TimeSpan.FromSeconds(5))) { string message = string.Format("Setup failed due to timeout waiting for protocol '{0}'", setupResult.Protocol); Log(LogLevel.Error, message); throw new TimeoutException(message); } mClient.Session.RegisterSubscriptionHandler(setupResult.Protocol, "notifications", (s, r, p) => OnNotification?.Invoke(mClient, p)); Task <ResponseTaskResult <SubscriptionResult> > subscriptionTask = mClient.Session.SubscriptionAddAsync(setupResult.Protocol, "notifications"); ResponseTaskResult <SubscriptionResult> subscriptionTaskResult = await subscriptionTask; if (subscriptionTask.IsFaulted) { Log(LogLevel.Error, string.Format("Setup subscription faulted\n{0}", subscriptionTask.Exception)); throw subscriptionTask.Exception; } // @todo check subscribe_channels mProtocol = setupResult.Protocol; mSetupCompleted = true; return mProtocol; })); }
protected async Task <string> LL_SetupAsync() { return(await Task.Run(async() => { if (mClient.Session.State != Blade.UpstreamSession.SessionState.Running) { string message = string.Format("Setup failed for '{0}' because the session is not running", mService); mLogger.LogError(message); throw new InvalidOperationException(message); } if (!await CheckProtocolAvailableAsync("signalwire", TimeSpan.FromSeconds(5))) { string message = string.Format("Setup failed for '{0}' due to timeout waiting for protocol 'signalwire'", mService); mLogger.LogError(message); throw new TimeoutException(message); } SetupParams setupParams = new SetupParams() { Service = mService }; if (!string.IsNullOrWhiteSpace(mProtocol)) { setupParams.Protocol = mProtocol; } Task <ResponseTaskResult <ExecuteResult> > setupTask = mClient.Session.ExecuteAsync("signalwire", "setup", setupParams); ResponseTaskResult <ExecuteResult> setupTaskResult = await setupTask; if (setupTask.IsFaulted) { mLogger.LogError("Setup fault for '{0}', {1}", mService, setupTask.Exception); throw setupTask.Exception; } SetupResult setupResult = setupTaskResult.Result.ResultAs <SetupResult>(); if (!await CheckProtocolAvailableAsync(setupResult.Protocol, TimeSpan.FromSeconds(5))) { string message = string.Format("Setup failed for '{0}' due to timeout waiting for protocol '{1}'", mService, setupResult.Protocol); mLogger.LogError(message); throw new TimeoutException(message); } mClient.Session.RegisterSubscriptionHandler(setupResult.Protocol, "notifications", (s, r, p) => OnEvent(mClient, p)); Task <ResponseTaskResult <SubscriptionResult> > subscriptionTask = mClient.Session.SubscriptionAddAsync(setupResult.Protocol, "notifications"); ResponseTaskResult <SubscriptionResult> subscriptionTaskResult = await subscriptionTask; if (subscriptionTask.IsFaulted) { mLogger.LogError("Setup subscription fault for '{0}', {1}", mService, subscriptionTask.Exception); throw subscriptionTask.Exception; } // @todo check subscribe_channels mProtocol = setupResult.Protocol; return mProtocol; })); }