예제 #1
0
        private void OptimizeQueue()
        {
            if (_notificationQueue.Count == 1)
            {
                return;
            }
            SessionNotification[] notifications = _notificationQueue.ToArray();
            _notificationQueue.Clear();
            SessionNotification currentNotification = null;

            foreach (SessionNotification notification in notifications)
            {
                if (currentNotification == null)
                {
                    currentNotification = notification;
                    continue;
                }
                if (notification.NotificationType == currentNotification.NotificationType && notification.Capabilities == currentNotification.Capabilities && notification.Identity == currentNotification.Identity)
                {
                    currentNotification = MergeNotificationFullNames(currentNotification, notification);
                    continue;
                }
                _notificationQueue.Enqueue(currentNotification);
                currentNotification = notification;
            }
            if (currentNotification != null)
            {
                _notificationQueue.Enqueue(currentNotification);
            }
        }
예제 #2
0
        public virtual async Task NotifyAsync(SessionNotification notification)
        {
            lock (_notificationQueue)
            {
                _notificationQueue.Enqueue(notification);
                if (_notificationQueue.Count > 1)
                {
                    return;
                }
                _queueEmpty = new TaskCompletionSource <bool>();
            }
            while (true)
            {
                lock (_notificationQueue)
                {
                    if (_notificationQueue.Count == 0)
                    {
                        _queueEmpty.SetResult(true);
                        return;
                    }
                    OptimizeQueue();
                    notification = _notificationQueue.Peek();
                }
                await NotifyInternal(notification);

                lock (_notificationQueue)
                {
                    _notificationQueue.Dequeue();
                }
            }
        }
 public virtual async Task HandleNotificationAsync(SessionNotification notification)
 {
     await New <IProgressBackground>().WorkAsync($"{nameof(HandleNotificationAsync)} {notification.NotificationType}",
                                                 async(IProgressContext progress) =>
     {
         progress.NotifyLevelStart();
         try
         {
             await HandleNotificationInternalAsync(notification, progress).Free();
         }
         catch (AxCryptException acex)
         {
             return(new FileOperationContext(acex.DisplayContext, acex.Messages(), ErrorStatus.Exception));
         }
         catch (Exception ex)
         {
             return(new FileOperationContext(string.Empty, ex.Messages(), ErrorStatus.Exception));
         }
         finally
         {
             progress.NotifyLevelFinished();
         }
         return(new FileOperationContext(String.Empty, ErrorStatus.Success));
     },
                                                 async(FileOperationContext status) =>
     {
         if (status.ErrorStatus == ErrorStatus.Success)
         {
             return;
         }
         _statusChecker.CheckStatusAndShowMessage(status.ErrorStatus, status.FullName, $"{status.InternalMessage} ({notification.NotificationType})");
     },
                                                 new ProgressContext()).Free();
 }
예제 #4
0
        private async Task NotifyInternal(SessionNotification notification)
        {
            try
            {
                foreach (Func <SessionNotification, Task> priorityCommand in SafeCopy(_priorityCommands))
                {
                    await priorityCommand(notification).Free();
                }

                foreach (Func <SessionNotification, Task> command in SafeCopy(_commands))
                {
                    await command(notification).Free();
                }
                if (notification.NotificationType != SessionNotificationType.SessionChange)
                {
                    New <InactivitySignOut>().RestartInactivityTimer();
                }
            }
            catch (Exception ex)
            {
                ex.ReportAndDisplay();
            }
        }
예제 #5
0
        private static SessionNotification MergeNotificationFullNames(SessionNotification currentNotification, SessionNotification nextNotification)
        {
            IEnumerable <string> allFullNames = currentNotification.FullNames.Union(nextNotification.FullNames);

            return(new SessionNotification(currentNotification.NotificationType, currentNotification.Identity, allFullNames, currentNotification.Capabilities));
        }
        private async Task HandleNotificationInternalAsync(SessionNotification notification, IProgressContext progress)
        {
            if (Resolve.Log.IsInfoEnabled)
            {
                Resolve.Log.LogInfo("Received notification type '{0}'.".InvariantFormat(notification.NotificationType));
            }
            EncryptionParameters encryptionParameters;

            switch (notification.NotificationType)
            {
            case SessionNotificationType.WatchedFolderAdded:
            case SessionNotificationType.WatchedFolderOptionsChanged:
                progress.NotifyLevelStart();
                try
                {
                    foreach (string fullName in notification.FullNames)
                    {
                        WatchedFolder watchedFolder = _fileSystemState.WatchedFolders.First(wf => wf.Path == fullName);

                        encryptionParameters = new EncryptionParameters(Resolve.CryptoFactory.Default(New <ICryptoPolicy>()).CryptoId, notification.Identity);
                        await encryptionParameters.AddAsync(await watchedFolder.KeyShares.ToAvailableKnownPublicKeysAsync(notification.Identity));

                        IDataContainer container = New <IDataContainer>(watchedFolder.Path);
                        progress.Display = container.Name;
                        IDataContainer[] dc = new IDataContainer[] { container };
                        await _axCryptFile.EncryptFoldersUniqueWithBackupAndWipeAsync(dc, encryptionParameters, progress);
                    }
                }
                finally
                {
                    progress.NotifyLevelFinished();
                }
                progress.Totals.ShowNotification();
                break;

            case SessionNotificationType.WatchedFolderRemoved:
                foreach (string fullName in notification.FullNames)
                {
                    IDataContainer removedFolderInfo = New <IDataContainer>(fullName);
                    progress.Display = removedFolderInfo.Name;
                    if (removedFolderInfo.IsAvailable)
                    {
                        await _axCryptFile.DecryptFilesInsideFolderUniqueWithWipeOfOriginalAsync(removedFolderInfo, notification.Identity, _statusChecker, progress).Free();
                    }
                }
                break;

            case SessionNotificationType.SignIn:
                await EncryptWatchedFoldersIfSupportedAsync(notification.Identity, notification.Capabilities, progress);

                break;

            case SessionNotificationType.SignOut:
                New <IInternetState>().Clear();
                New <ICache>().RemoveItem(CacheKey.RootKey);
                New <UserPublicKeyUpdateStatus>().Clear();
                break;

            case SessionNotificationType.EncryptPendingFiles:
                await _activeFileAction.ClearExceptionState();

                await _activeFileAction.PurgeActiveFiles(progress);

                if (_knownIdentities.DefaultEncryptionIdentity != LogOnIdentity.Empty)
                {
                    await EncryptWatchedFoldersIfSupportedAsync(_knownIdentities.DefaultEncryptionIdentity, notification.Capabilities, progress);
                }
                break;

            case SessionNotificationType.UpdateActiveFiles:
                await _fileSystemState.UpdateActiveFiles(notification.FullNames);

                break;

            case SessionNotificationType.ActiveFileChange:
            case SessionNotificationType.SessionStart:
            case SessionNotificationType.WatchedFolderChange:
            case SessionNotificationType.ProcessExit:
            case SessionNotificationType.KnownKeyChange:
            case SessionNotificationType.SessionChange:
            case SessionNotificationType.WorkFolderChange:
                await _activeFileAction.CheckActiveFiles(progress);

                break;

            case SessionNotificationType.LicensePolicyChanged:
            case SessionNotificationType.RefreshLicensePolicy:
                break;

            default:
                throw new InvalidOperationException("Unhandled notification received");
            }
        }
예제 #7
0
 public SessionNotificationEventArgs(SessionNotification notification)
 {
     Notification = notification;
 }