private static async Task <bool> ExecuteActionAsync(string[] args, IOnDemandClientChannel channel, Dictionary <Type, IAction> actionsDictionary, int timeout) { var parsedCommandResult = Parser.ParseArguments(args, actionsDictionary.Values.Select(v => v.OptionsType).ToArray()); if (parsedCommandResult.Tag == ParserResultType.NotParsed) { WriteInfo("Invalid action or invalid arguments"); return(false); } if (!actionsDictionary.TryGetValue(parsedCommandResult.TypeInfo.Current, out var action)) { WriteInfo("Action type not found"); return(false); } var actionOptions = ((Parsed <object>)parsedCommandResult).Value; using (var cts = CreateCancellationTokenSource(timeout)) { try { await action.ExecuteAsync(actionOptions, channel, cts.Token); return(true); } catch (Exception ex) { WriteError(ex.Message); return(false); } } }
private async Task SendToChannelAsync(IOnDemandClientChannel channel, Envelope e) { try { if (e is Message) { await channel.SendMessageAsync((Message)e); } else if (e is Notification) { await channel.SendNotificationAsync((Notification)e); } else if (e is Command) { await channel.SendCommandAsync((Command)e); } } catch (Exception ex) { foreach (var handler in ChannelOperationFailedHandlers) { await handler( new FailedChannelInformation( null, channel.IsEstablished ? SessionState.Established : SessionState.Failed, null, null, channel.IsEstablished, ex, nameof(SendToChannelAsync))); } } }
private void CreateOnDemandClientChannel() { OnDemandClientChannel = _onDemandClientChannelFactory.Create(_channelCount); OnDemandClientChannel.ChannelCreatedHandlers.Add(ChannelCreatedAsync); OnDemandClientChannel.ChannelCreationFailedHandlers.Add(ChannelCreationFailedAsync); OnDemandClientChannel.ChannelDiscardedHandlers.Add(ChannelDiscardedAsync); OnDemandClientChannel.ChannelOperationFailedHandlers.Add(ChannelOperationFailedAsync); }
public BlipClient(IOnDemandClientChannel onDemandClientChannel, ILogger logger = null) { _onDemandClientChannel = onDemandClientChannel ?? throw new ArgumentNullException(nameof(onDemandClientChannel)); _logger = logger ?? LoggerProvider.Logger; _semaphore = new SemaphoreSlim(1, 1); _onDemandClientChannel.ChannelCreatedHandlers.Add(ChannelCreatedAsync); _onDemandClientChannel.ChannelCreationFailedHandlers.Add(ChannelCreationFailedAsync); _onDemandClientChannel.ChannelDiscardedHandlers.Add(ChannelDiscardedAsync); _onDemandClientChannel.ChannelOperationFailedHandlers.Add(ChannelOperationFailedAsync); }
private void SubstituteOnDemandClientChannel() { OnDemandClientChannel = Substitute.For <IOnDemandClientChannel>(); OnDemandClientChannel.ReceiveMessageAsync(CancellationToken.None).ReturnsForAnyArgs(callInfo => MessageProducer.Take()); OnDemandClientChannel.ReceiveNotificationAsync(CancellationToken.None).ReturnsForAnyArgs(callInfo => NotificationProducer.Take()); OnDemandClientChannel.ReceiveCommandAsync(CancellationToken.None).ReturnsForAnyArgs(callInfo => CommandProducer.Take()); OnDemandClientChannel.ProcessCommandAsync(null, CancellationToken.None).ReturnsForAnyArgs(Task.FromResult(new Command { Status = CommandStatus.Success })); OnDemandClientChannel.IsEstablished.Returns(true); }
private static async Task ExecuteInteractiveModeAsync(Options options, IOnDemandClientChannel channel, Dictionary <Type, IAction> actionsDictionary) { while (channel.IsEstablished) { Console.Write("> "); var input = Console.ReadLine(); if (string.IsNullOrEmpty(input) || input.Equals("exit", StringComparison.OrdinalIgnoreCase)) { break; } if (!channel.IsEstablished) { WriteInfo("Channel is not established"); break; } await ExecuteActionAsync(input.Split(' '), channel, actionsDictionary, options.Timeout); } }
public HttpMessagingHubConnection(IEnvelopeBuffer envelopeBuffer, IEnvelopeSerializer serializer, Application applicationSettings) { OnDemandClientChannel = new HttpOnDemandClientChannel(envelopeBuffer, serializer, applicationSettings); SendTimeout = applicationSettings.SendTimeout <= 0 ? DefaultSendTimeout : TimeSpan.FromMilliseconds(applicationSettings.SendTimeout); }