public async Task Send_Command_And_Receive_Response_With_Success() { //Arrange var commandId = Guid.NewGuid().ToString(); var commandResponse = new Command { Id = commandId, Status = CommandStatus.Success, }; OnDemandClientChannel.IsEstablished.Returns(true); OnDemandClientChannel.ProcessCommandAsync(null, CancellationToken.None).ReturnsForAnyArgs(commandResponse); await MessagingHubConnection.ConnectAsync(); //Act var result = await MessagingHubSender.SendCommandAsync(new Command { Id = commandId }, CancellationToken.None); await Task.Delay(TIME_OUT); //Assert result.ShouldNotBeNull(); result.Status.ShouldBe(CommandStatus.Success); result.Id.ShouldBe(commandId); }
public async Task Start_Then_Stop_Should_Finish_OnDemandClientChannel() { //Arrange await MessagingHubConnection.ConnectAsync(); // Act await MessagingHubConnection.DisconnectAsync(); // Assert OnDemandClientChannel.ReceivedWithAnyArgs(1).FinishAsync(CancellationToken.None).Wait(); }
public async Task ProcessComandShouldCallOnDemandClientChannel() { // Arrange var command = Dummy.CreateCommand(); var target = await GetAndStartTarget(); // Act await target.ProcessCommandAsync(command, CancellationToken); // Assert OnDemandClientChannel.Received(1).ProcessCommandAsync(command, CancellationToken); }
public async Task StopShouldStopListenerAndFinishOnDemandClientChannel() { // Arrange var target = await GetAndStartTarget(); // Act await target.StopAsync(CancellationToken); // Assert ChannelListener.Received(1).Stop(); OnDemandClientChannel.Received(1).FinishAsync(CancellationToken); }
public async Task StartShouldStartListenerAndEstablishOnDemandClientChannel() { // Arrange var target = GetTarget(); // Act await target.StartAsync(ChannelListener, CancellationToken); // Assert ChannelListener.Received(1).Start(OnDemandClientChannel); OnDemandClientChannel.Received(1).EstablishAsync(CancellationToken); }
public async Task SendNotificationShouldCallOnDemandClientChannel() { // Arrange var notification = Dummy.CreateNotification(Event.Received); var target = await GetAndStartTarget(); // Act await target.SendNotificationAsync(notification, CancellationToken); // Assert OnDemandClientChannel.Received(1).SendNotificationAsync(notification, CancellationToken); }
public async Task SendMessageShouldCallOnDemandClientChannel() { // Arrange var message = Dummy.CreateMessage(Dummy.CreateTextContent()); var target = await GetAndStartTarget(); // Act await target.SendMessageAsync(message, CancellationToken); // Assert OnDemandClientChannel.Received(1).SendMessageAsync(message, CancellationToken); }
private IOnDemandClientChannel CreateOnDemandClientChannel(IEstablishedClientChannelBuilder establishedClientChannelBuilder) { IOnDemandClientChannel onDemandClientChannel; // Avoid the overhead of the MultiplexerClientChannel for a single connection if (ChannelCount == 1) { onDemandClientChannel = new OnDemandClientChannel(establishedClientChannelBuilder); } else { onDemandClientChannel = new MultiplexerClientChannel(establishedClientChannelBuilder, ChannelCount); } return(onDemandClientChannel); }
static async Task MainAsync(string[] args) { Console.Write("Host name (ENTER for default): "); var hostName = Console.ReadLine(); if (string.IsNullOrWhiteSpace(hostName)) { hostName = Dns.GetHostName(); } Console.Write("Port number (ENTER for default): "); int portNumber; if (!int.TryParse(Console.ReadLine(), out portNumber)) { portNumber = 55321; } Console.Write("Identity (name@domain - ENTER for none): "); Identity identity; if (!Identity.TryParse(Console.ReadLine(), out identity)) { identity = null; } string password = null; if (identity != null) { Console.Write("Password: "******"net.tcp://{hostName}:{portNumber}"); var transport = new TcpTransport(traceWriter: new DebugTraceWriter()); // Creates a new client channel var builder = ClientChannelBuilder .Create(transport, serverUri) .AddBuiltHandler((channel, token) => { channel.CommandModules.Add(new ReplyPingChannelModule(channel)); return(TaskUtil.CompletedTask); }) .CreateEstablishedClientChannelBuilder() .AddEstablishedHandler(async(c, t) => await c.SetResourceAsync( new LimeUri(UriTemplates.PRESENCE), new Presence() { Status = PresenceStatus.Available }, t)) .AddEstablishedHandler(async(c, t) => await c.SetResourceAsync( new LimeUri(UriTemplates.RECEIPT), new Receipt() { Events = new[] { Event.Received, Event.Dispatched } }, t)); if (identity == null) { builder = builder.WithAuthentication <GuestAuthentication>(); } else { builder = builder .WithIdentity(identity) .WithPlainAuthentication(password); } var onDemandChannel = new OnDemandClientChannel(builder); var running = true; onDemandChannel.ChannelCreationFailedHandlers.Add(information => { Console.Write("Could not establish the session: {0}", information.Exception.Message); Console.WriteLine(); running = false; return(TaskUtil.FalseCompletedTask); }); var channelListener = new ChannelListener(message => { Console.ForegroundColor = ConsoleColor.DarkRed; Console.WriteLine("Message with id '{0}' received from '{1}': {2}", message.Id, message.GetSender(), message.Content); Console.ResetColor(); return(TaskUtil.TrueCompletedTask); }, notification => { Console.ForegroundColor = ConsoleColor.DarkBlue; Console.WriteLine("Notification with id {0} received from '{1}' - Event: {2}", notification.Id, notification.GetSender(), notification.Event); Console.ResetColor(); return(TaskUtil.TrueCompletedTask); }, command => { Console.ForegroundColor = ConsoleColor.DarkGreen; Console.WriteLine("Command with id '{0}' received from '{1}' - Method: {2} - URI: {3}", command.Id, command.GetSender(), command.Method, command.Uri); Console.ResetColor(); return(TaskUtil.TrueCompletedTask); }); channelListener.Start(onDemandChannel); while (running) { Console.Write("Destination node (Type EXIT to quit): "); var toInput = Console.ReadLine(); if (toInput != null && toInput.Equals("exit", StringComparison.InvariantCultureIgnoreCase)) { break; } Node to = null; if (string.IsNullOrEmpty(toInput) || Node.TryParse(toInput, out to)) { Console.Write("Message text: "); var message = new Message { To = to, Content = new PlainText { Text = Console.ReadLine() } }; await onDemandChannel.SendMessageAsync(message, CancellationToken.None); } } channelListener.Stop(); await Task.WhenAll( channelListener.MessageListenerTask, channelListener.NotificationListenerTask, channelListener.CommandListenerTask); await onDemandChannel.FinishAsync(CancellationToken.None); Console.WriteLine("Press any key to exit."); Console.Read(); }
static async Task Main(string[] args) { Console.Write("Server URI (ENTER for default): "); var serverUriValue = Console.ReadLine(); if (string.IsNullOrWhiteSpace(serverUriValue)) { serverUriValue = $"net.tcp://{Dns.GetHostName()}:{55321}"; } Console.Write("Identity (name@domain - ENTER for none): "); if (!Identity.TryParse(Console.ReadLine(), out var identity)) { identity = null; } string password = null; if (identity != null) { Console.Write("Password: "******"Number of channels (ENTER for 1): "); if (!int.TryParse(Console.ReadLine(), out var channelCount)) { channelCount = 1; } var setPresence = false; var setReceipts = false; // Creates a new transport and connect to the server var serverUri = new Uri(serverUriValue); Func <ITransport> transportFactory = () => CreateTransportForUri(serverUri); // Creates a new client channel var builder = ClientChannelBuilder .Create(transportFactory, serverUri) .AddBuiltHandler((channel, token) => { channel.CommandModules.Add(new ReplyPingChannelModule(channel)); return(TaskUtil.CompletedTask); }) .CreateEstablishedClientChannelBuilder() .WithEncryption(SessionEncryption.None) .AddEstablishedHandler(async(c, t) => { if (setPresence) { await c.SetResourceAsync( new LimeUri(UriTemplates.PRESENCE), new Presence() { Status = PresenceStatus.Available, RoutingRule = RoutingRule.Identity, RoundRobin = true }, t); } }) .AddEstablishedHandler(async(c, t) => { if (setReceipts) { await c.SetResourceAsync( new LimeUri(UriTemplates.RECEIPT), new Receipt() { Events = new[] { Event.Received, Event.Consumed } }, t); } }); if (identity == null) { builder = builder.WithAuthentication <GuestAuthentication>(); } else { builder = builder .WithIdentity(identity) .WithPlainAuthentication(password); } IOnDemandClientChannel onDemandClientChannel; if (channelCount == 1) { onDemandClientChannel = new OnDemandClientChannel(builder); } else { onDemandClientChannel = new MultiplexerClientChannel(builder); } var running = true; onDemandClientChannel.ChannelCreationFailedHandlers.Add(information => { Console.Write("Could not establish the session: {0}", information.Exception.Message); Console.WriteLine(); running = false; return(TaskUtil.FalseCompletedTask); }); var channelListener = new ChannelListener(message => { Console.ForegroundColor = ConsoleColor.DarkRed; Console.WriteLine("Message with id '{0}' received from '{1}': {2}", message.Id, message.GetSender(), message.Content); Console.ResetColor(); return(TaskUtil.TrueCompletedTask); }, notification => { Console.ForegroundColor = ConsoleColor.DarkBlue; Console.WriteLine("Notification with id {0} received from '{1}' - Event: {2}", notification.Id, notification.GetSender(), notification.Event); Console.ResetColor(); return(TaskUtil.TrueCompletedTask); }, command => { Console.ForegroundColor = ConsoleColor.DarkGreen; Console.WriteLine("Command with id '{0}' received from '{1}' - Method: {2} - URI: {3}", command.Id, command.GetSender(), command.Method, command.Uri); Console.ResetColor(); return(TaskUtil.TrueCompletedTask); }); await onDemandClientChannel.EstablishAsync(CancellationToken.None); channelListener.Start(onDemandClientChannel); while (running) { Console.Write("Destination node (Type EXIT to quit): "); var toInput = Console.ReadLine(); if (toInput != null && toInput.Equals("exit", StringComparison.OrdinalIgnoreCase)) { break; } Node to = null; if (string.IsNullOrEmpty(toInput) || Node.TryParse(toInput, out to)) { Console.Write("Message text: "); var text = Console.ReadLine(); var stopwatch = Stopwatch.StartNew(); Console.Write("Number of times to send (ENTER to 1): "); int count; if (!int.TryParse(Console.ReadLine(), out count)) { count = 1; } await Task.WhenAll( Enumerable .Range(0, count) .Select(async i => { var message = new Message { Id = i.ToString(), To = to, Content = new PlainText { Text = text } }; await onDemandClientChannel.SendMessageAsync(message, CancellationToken.None); })); stopwatch.Stop(); Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("Elapsed: {0} ms", stopwatch.ElapsedMilliseconds); Console.ResetColor(); } } channelListener.Stop(); await Task.WhenAll( channelListener.MessageListenerTask, channelListener.NotificationListenerTask, channelListener.CommandListenerTask); await onDemandClientChannel.FinishAsync(CancellationToken.None); Console.WriteLine("Press any key to exit."); Console.Read(); }
private static async Task <IOnDemandClientChannel> EstablishChannelAsync(ConnectionInformation connectionInformation, CancellationToken cancellationToken) { ITransport transportFactory() => CreateTransportForUri(connectionInformation.ServerUri); // Creates a new client channel var builder = ClientChannelBuilder .Create(transportFactory, connectionInformation.ServerUri) .AddBuiltHandler((channel, handlerCancellationToken) => { channel.CommandModules.Add(new ReplyPingChannelModule(channel)); return(Task.CompletedTask); }) .CreateEstablishedClientChannelBuilder() .WithEncryption(SessionEncryption.None) .WithInstance(connectionInformation.Instance) .AddEstablishedHandler(async(channel, handlerCancellationToken) => { await channel.SetResourceAsync( new LimeUri(UriTemplates.PRESENCE), connectionInformation.Presence, handlerCancellationToken); }) .AddEstablishedHandler(async(channel, handlerCancellationToken) => { await channel.SetResourceAsync( new LimeUri(UriTemplates.RECEIPT), connectionInformation.Receipt, handlerCancellationToken); }); if (connectionInformation.Identity == null) { builder = builder.WithAuthentication <GuestAuthentication>(); } else { builder = builder .WithIdentity(connectionInformation.Identity) .WithPlainAuthentication(connectionInformation.Password); } var clientChannel = new OnDemandClientChannel(builder); clientChannel.ChannelCreationFailedHandlers.Add(information => { WriteError("Could not establish the session: {0}", information.Exception.Message); return(TaskUtil.FalseCompletedTask); }); var channelListener = new ChannelListener(message => { WriteInfo("Message with id '{0}' received from '{1}': {2}", message.Id, message.GetSender(), message.Content); return(TaskUtil.TrueCompletedTask); }, notification => { WriteInfo("Notification with id {0} received from '{1}' - Event: {2}", notification.Id, notification.GetSender(), notification.Event); return(TaskUtil.TrueCompletedTask); }, command => { WriteInfo("Command with id '{0}' received from '{1}' - Method: {2} - URI: {3}", command.Id, command.GetSender(), command.Method, command.Uri); return(TaskUtil.TrueCompletedTask); }); await clientChannel.EstablishAsync(cancellationToken); channelListener.Start(clientChannel); return(clientChannel); }