Ejemplo n.º 1
0
        public static async Task <TResponse> ExecuteAsync <TAction, TResponse>(
            this IActionExecutor executor)
            where TResponse : class
        {
            var typeName = ActionBase.GetName(typeof(TAction));

            return((await executor.ExecuteAsync(typeName)) as TResponse);
        }
        async Task ListenAsync(ISMTPImpostorHubClient client)
        {
            try
            {
                while (client.State == WebSocketState.Open)
                {
                    try
                    {
                        var data = await client.ReceiveAsync();

                        if (!string.IsNullOrWhiteSpace(data))
                        {
                            var message = Newtonsoft.Json.JsonConvert
                                          .DeserializeObject <SMTPImpostorHubMessage>(data);
                            _messages.OnNext(message);

                            try
                            {
                                var result = await _executor.ExecuteAsync(message.Type, message.Data);

                                if (result != null &&
                                    result != ActionNull.Instance)
                                {
                                    await client.SendAsync(
                                        _serialization.Serialize(
                                            CreateMessageFrom(result)
                                            ));
                                }
                            }
                            catch (Exception ex)
                            {
                                _logger.LogError(ex, $"Executor failed\n{data}");
                            }
                        }
                    }
                    catch (WebSocketException ex)
                    {
                        _logger.LogError(ex, "ListenAsync");
                    }
                }
            }
            catch (Exception ex)
            {
                if (client.State == WebSocketState.Open)
                {
                    _logger.LogError(ex, $"Could not receive message");
                    await client.CloseAsync();
                }
            }
        }
Ejemplo n.º 3
0
        protected override async Task ExecuteAsync(CancellationToken _)
        {
            if (Environment.UserInteractive)
            {
                if (UWPHelper.IsUwp())
                {
                    try
                    {
                        _notifier = ToastNotificationManager.CreateToastNotifier();
                        UWPHelper.SendStartupMessage(_notifier, _settings);
                    }
                    catch (Exception ex)
                    {
                        _logger.LogError(ex, "Error sending UWP start notification");
                    }
                }
            }

            if (Mutex.TryOpenExisting(MUTEX_NAME, out var __))
            {
                await StopAsync(CancellationToken.None);

                return;
            }

            _mutex          = new Mutex(false, MUTEX_NAME);
            _impostorEvents = _impostor.Events.Subscribe(async e =>
            {
                _logger.LogInformation($"{e.GetType().Name}");
                try
                {
                    if (e is SMTPImpostorStoppedEvent)
                    {
                        await _hub.SendAsync(new WorkerState(null, null));
                        await StopAsync(CancellationToken.None);
                    }
                    else if (e is SMTPImpostorMessageReceivedEvent mre)
                    {
                        var host = _impostor.Hosts[mre.HostId];
                        await host.Messages.PutAsync(mre.Data);

                        await _hub.SendAsync(
                            new HostMessageReceived(host.Settings.Id, mre.Data.Map())
                            );
                    }
                    else if (e is SMTPImpostorMessageRemovedEvent mde)
                    {
                        await _hub.SendAsync(
                            new HostMessageRemoved(mde.HostId, mde.MessageId)
                            );
                    }
                    else if (e is SMTPImpostorMessageAddedEvent mae)
                    {
                        await _hub.SendAsync(
                            new HostMessageAdded(mae.HostId, mae.MessageId)
                            );
                    }
                    else if (
                        (e is SMTPImpostorHostStateChangeEvent || e is SMTPImpostorHostUpdatedEvent) &&
                        e is ISMTPImpostorHostEvent he)
                    {
                        var status = await _executor
                                     .ExecuteAsync <LoadWorkerStateAction, WorkerState>();
                        var hostState = status.Hosts
                                        .FirstOrDefault(h => h.Id == he.HostId);
                        if (hostState == null)
                        {
                            return;
                        }

                        await _hub.SendAsync(hostState);
                        await _hostsSettings.SaveAsync(status.ToSettings());
                    }
                    else if (e is SMTPImpostorHostRemovedEvent ||
                             e is SMTPImpostorHostAddedEvent)
                    {
                        var status = await _executor
                                     .ExecuteAsync <LoadWorkerStateAction, WorkerState>();
                        await _hub.SendAsync(status);

                        await _hostsSettings.SaveAsync(status.ToSettings());
                    }
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, "Event Error");
                }
            });

            var settings = await _hostsSettings.LoadAsync();

            if (settings != null)
            {
                foreach (var hostSetttings in settings)
                {
                    _impostor.AddHost(hostSetttings);
                }
            }
        }
Ejemplo n.º 4
0
 public static Task <object> ExecuteAsync(
     this IActionExecutor executor, string typeName)
 {
     return(executor.ExecuteAsync(typeName, null));
 }
Ejemplo n.º 5
0
 public static async Task <TResponse> ExecuteAsync <TResponse>(
     this IActionExecutor executor, string typeName, string request)
     where TResponse : class
 {
     return((await executor.ExecuteAsync(typeName, request)) as TResponse);
 }