private async Task ReceiveEnvelopeAsync(Envelope envelope, Dispatcher dispatcher) { var envelopeViewModel = new EnvelopeViewModel(); envelopeViewModel.Envelope = envelope; envelopeViewModel.Direction = DataOperation.Receive; await await dispatcher.InvokeAsync(async () => { this.Envelopes.Add(envelopeViewModel); foreach (var macro in Macros.Where(m => m.IsActive)) { await macro.Macro.ProcessAsync(envelopeViewModel, this); } }); }
public Task ProcessAsync(EnvelopeViewModel envelopeViewModel, SessionViewModel sessionViewModel) { if (envelopeViewModel == null) { throw new ArgumentNullException("envelopeViewModel"); } if (sessionViewModel == null) { throw new ArgumentNullException("sessionViewModel"); } var session = envelopeViewModel.Envelope as Session; if (session != null) { sessionViewModel.LastSessionState = session.State; if (session.State == SessionState.Established) { sessionViewModel.LocalNode = session.To; sessionViewModel.RemoteNode = session.From; } } return Task.FromResult<object>(null); }
public async Task TraceAsync(string data, DataOperation operation) { var envelopeViewModel = new EnvelopeViewModel(false) { IsRaw = true, Json = data, Direction = operation }; await App.Current.Dispatcher.InvokeAsync(() => this.Envelopes.Add(envelopeViewModel)); }
private async Task ReceiveEnvelopeAsync(Envelope envelope, Dispatcher dispatcher) { var envelopeViewModel = new EnvelopeViewModel(); envelopeViewModel.Envelope = envelope; envelopeViewModel.Direction = DataOperation.Receive; await await dispatcher.InvokeAsync(async() => { this.Envelopes.Add(envelopeViewModel); foreach (var macro in Macros.Where(m => m.IsActive)) { await macro.Macro.ProcessAsync(envelopeViewModel, this); } }); }
private void Validate() { try { var jsonObject = JObject.Parse(InputJson); if (jsonObject.HasValues) { var envelopeViewModel = new EnvelopeViewModel(); envelopeViewModel.Json = InputJson; if (envelopeViewModel.Envelope != null) { AddStatusMessage(string.Format("The input is a valid {0} JSON Envelope", envelopeViewModel.Envelope.GetType().Name)); } else { AddStatusMessage("The input is a valid JSON document, but is not an Envelope", true); } var variables = InputJson.GetVariables(); foreach (var variable in variables) { if (!this.Variables.Any(v => v.Name.Equals(variable, StringComparison.OrdinalIgnoreCase))) { var variableViewModel = new VariableViewModel() { Name = variable }; this.Variables.Add(variableViewModel); } } } else { AddStatusMessage("The input is a invalid or empty JSON document", true); } } catch { AddStatusMessage("The input is a invalid JSON document", true); } }
private async Task SendAsync() { await ExecuteAsync(async() => { AddStatusMessage("Sending..."); if (ParseBeforeSend) { this.InputJson = ParseInput(this.InputJson, this.Variables); } var timeoutCancellationToken = _operationTimeout.ToCancellationToken(); var envelopeViewModel = new EnvelopeViewModel(false); envelopeViewModel.Json = InputJson; var envelope = envelopeViewModel.Envelope; envelopeViewModel.Direction = DataOperation.Send; if (SendAsRaw) { envelopeViewModel.IsRaw = true; var stream = TcpClient.GetStream(); var envelopeBytes = Encoding.UTF8.GetBytes(envelopeViewModel.Json); await stream.WriteAsync(envelopeBytes, 0, envelopeBytes.Length, timeoutCancellationToken); } else { await Transport.SendAsync(envelope, timeoutCancellationToken); envelopeViewModel.IndentJson(); } this.Envelopes.Add(envelopeViewModel); if (this.ClearAfterSent) { this.InputJson = string.Empty; } AddStatusMessage(string.Format("{0} envelope sent", envelope.GetType().Name)); }); }
public Task ProcessAsync(EnvelopeViewModel envelopeViewModel, SessionViewModel sessionViewModel) { if (envelopeViewModel == null) { throw new ArgumentNullException("envelopeViewModel"); } if (sessionViewModel == null) { throw new ArgumentNullException("sessionViewModel"); } var notification = envelopeViewModel.Envelope as Notification; if (notification != null) { sessionViewModel.LastNotificationEvent = notification.Event; } return Task.FromResult<object>(null); }
public async Task ProcessAsync(EnvelopeViewModel envelopeViewModel, SessionViewModel sessionViewModel) { if (envelopeViewModel == null) { throw new ArgumentNullException("envelopeViewModel"); } if (sessionViewModel == null) { throw new ArgumentNullException("sessionViewModel"); } var session = envelopeViewModel.Envelope as Session; if (session != null && new[] { SessionState.Finished, SessionState.Failed }.Contains(session.State) && sessionViewModel.CloseTransportCommand.CanExecute(null)) { await sessionViewModel.CloseTransportCommand.ExecuteAsync(null); } }
public async Task ProcessAsync(EnvelopeViewModel envelopeViewModel, SessionViewModel sessionViewModel) { if (envelopeViewModel == null) { throw new ArgumentNullException("envelopeViewModel"); } if (sessionViewModel == null) { throw new ArgumentNullException("sessionViewModel"); } var session = envelopeViewModel.Envelope as Session; var transport = sessionViewModel.Transport; if (session != null && session.State == SessionState.Negotiating && session.Compression.HasValue && session.Encryption.HasValue && transport != null) { var cancellationToken = TimeSpan.FromSeconds(15).ToCancellationToken(); if (transport.Compression != session.Compression.Value) { await transport.SetCompressionAsync(session.Compression.Value, cancellationToken); } if (transport.Encryption != session.Encryption.Value) { await transport.SetEncryptionAsync(session.Encryption.Value, cancellationToken); if (session.Encryption.Value != SessionEncryption.None) { sessionViewModel.CanSendAsRaw = false; } } } }
public async Task ProcessAsync(EnvelopeViewModel envelopeViewModel, SessionViewModel sessionViewModel) { if (envelopeViewModel == null) { throw new ArgumentNullException("envelopeViewModel"); } if (sessionViewModel == null) { throw new ArgumentNullException("sessionViewModel"); } var command = envelopeViewModel.Envelope as Command; if (command != null && command.Status == CommandStatus.Pending && command.Uri != null && command.Uri.Path.Equals(UriTemplates.PING)) { var commandResponse = new Command() { Id = command.Id, Status = CommandStatus.Success, To = command.From, Resource = new Ping() }; var commandEnvelopeViewModel = new EnvelopeViewModel() { Envelope = commandResponse }; sessionViewModel.InputJson = commandEnvelopeViewModel.Json; if (sessionViewModel.SendCommand.CanExecute(null)) { await sessionViewModel.SendCommand.ExecuteAsync(null); } } }
public async Task ProcessAsync(EnvelopeViewModel envelopeViewModel, SessionViewModel sessionViewModel) { if (envelopeViewModel == null) { throw new ArgumentNullException("envelopeViewModel"); } if (sessionViewModel == null) { throw new ArgumentNullException("sessionViewModel"); } var message = envelopeViewModel.Envelope as Message; if (message != null && message.Id != Guid.Empty) { var notification = new Notification() { Id = message.Id, To = message.From, Event = Event.Consumed }; var notificationEnvelopeViewModel = new EnvelopeViewModel() { Envelope = notification }; sessionViewModel.InputJson = notificationEnvelopeViewModel.Json; if (sessionViewModel.SendCommand.CanExecute(null)) { await sessionViewModel.SendCommand.ExecuteAsync(null); } } }
public Task ProcessAsync(EnvelopeViewModel envelopeViewModel, SessionViewModel sessionViewModel) { if (envelopeViewModel == null) { throw new ArgumentNullException("envelopeViewModel"); } if (sessionViewModel == null) { throw new ArgumentNullException("sessionViewModel"); } var session = envelopeViewModel.Envelope as Session; if (session != null && session.Id != Guid.Empty) { var sessionIdVariableViewModel = sessionViewModel .Variables .FirstOrDefault(v => v.Name.Equals("sessionId")); if (sessionIdVariableViewModel == null) { sessionIdVariableViewModel = new VariableViewModel() { Name = "sessionId" }; sessionViewModel.Variables.Add(sessionIdVariableViewModel); } sessionIdVariableViewModel.Value = session.Id.ToString(); } return Task.FromResult<object>(null); }
private async Task SendAsync() { await ExecuteAsync(async () => { AddStatusMessage("Sending..."); if (ParseBeforeSend) { this.InputJson = ParseInput(this.InputJson, this.Variables); } var timeoutCancellationToken = _operationTimeout.ToCancellationToken(); var envelopeViewModel = new EnvelopeViewModel(false); envelopeViewModel.Json = InputJson; var envelope = envelopeViewModel.Envelope; envelopeViewModel.Direction = DataOperation.Send; if (SendAsRaw) { envelopeViewModel.IsRaw = true; var stream = TcpClient.GetStream(); var envelopeBytes = Encoding.UTF8.GetBytes(envelopeViewModel.Json); await stream.WriteAsync(envelopeBytes, 0, envelopeBytes.Length, timeoutCancellationToken); } else { await Transport.SendAsync(envelope, timeoutCancellationToken); envelopeViewModel.IndentJson(); } this.Envelopes.Add(envelopeViewModel); if (this.ClearAfterSent) { this.InputJson = string.Empty; } AddStatusMessage(string.Format("{0} envelope sent", envelope.GetType().Name)); }); }
private void Validate() { try { var jsonObject = JsonObject.ParseJson(InputJson); if (jsonObject.Any()) { var envelopeViewModel = new EnvelopeViewModel(); envelopeViewModel.Json = InputJson; if (envelopeViewModel.Envelope != null) { AddStatusMessage(string.Format("The input is a valid {0} JSON Envelope", envelopeViewModel.Envelope.GetType().Name)); } else { AddStatusMessage("The input is a valid JSON document, but is not an Envelope", true); } var variables = InputJson.GetVariables(); foreach (var variable in variables) { if (!this.Variables.Any(v => v.Name.Equals(variable, StringComparison.OrdinalIgnoreCase))) { var variableViewModel = new VariableViewModel() { Name = variable }; this.Variables.Add(variableViewModel); } } } else { AddStatusMessage("The input is a invalid or empty JSON document", true); } } catch (ArgumentException) { AddStatusMessage("The input is a invalid JSON document", true); } }
private async Task SendAsync(object parameter) { var times = 0; var repeatCountVariable = Variables.FirstOrDefault(v => v.Name == "repeatCount"); if (repeatCountVariable == null) { repeatCountVariable = new VariableViewModel() { Name = "repeatCount" }; Variables.Add(repeatCountVariable); } do { repeatCountVariable.Value = (times + 1).ToString(); await ExecuteAsync(async() => { AddStatusMessage("Sending..."); var inputJson = parameter.ToString(); if (ParseBeforeSend) { inputJson = ParseInput(inputJson, Variables); } var timeoutCancellationToken = _operationTimeout.ToCancellationToken(); var envelopeViewModel = new EnvelopeViewModel(false); envelopeViewModel.Json = inputJson; var envelope = envelopeViewModel.Envelope; envelopeViewModel.Direction = DataOperation.Send; if (SendAsRaw) { envelopeViewModel.IsRaw = true; var stream = TcpClient.GetStream(); var envelopeBytes = Encoding.UTF8.GetBytes(envelopeViewModel.Json); await stream.WriteAsync(envelopeBytes, 0, envelopeBytes.Length, timeoutCancellationToken); } else { await Transport.SendAsync(envelope, timeoutCancellationToken); envelopeViewModel.IndentJson(); } Envelopes.Add(envelopeViewModel); if (ClearAfterSent) { InputJson = string.Empty; } AddStatusMessage(string.Format("{0} envelope sent", envelope.GetType().Name)); }); } while (Repeat && RepeatTimes > ++times); }