Exemplo n.º 1
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);
                }
            }
        }
Exemplo n.º 2
0
        public override Task ExecuteAsync(SMTPImpostorHostSettings settings)
        {
            _impostor.AddHost(settings);

            return(Task.CompletedTask);
        }