2017-07-07 15:46:32 +02:00
|
|
|
|
/*
|
2024-03-05 18:37:42 +01:00
|
|
|
|
* Copyright (c) 2024 ETH Zürich, IT Services
|
2017-07-07 15:46:32 +02:00
|
|
|
|
*
|
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
using System;
|
2017-07-13 08:51:00 +02:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2019-08-30 09:55:26 +02:00
|
|
|
|
using SafeExamBrowser.Core.Contracts.OperationModel;
|
|
|
|
|
using SafeExamBrowser.Core.Contracts.OperationModel.Events;
|
|
|
|
|
using SafeExamBrowser.Logging.Contracts;
|
2017-07-07 15:46:32 +02:00
|
|
|
|
|
2018-08-31 10:06:27 +02:00
|
|
|
|
namespace SafeExamBrowser.Core.OperationModel
|
2017-07-07 15:46:32 +02:00
|
|
|
|
{
|
2018-03-06 14:35:10 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Default implementation of the <see cref="IOperationSequence"/>.
|
|
|
|
|
/// </summary>
|
2018-02-01 08:37:12 +01:00
|
|
|
|
public class OperationSequence : IOperationSequence
|
2017-07-07 15:46:32 +02:00
|
|
|
|
{
|
2018-10-10 09:19:03 +02:00
|
|
|
|
protected ILogger logger;
|
|
|
|
|
protected Queue<IOperation> operations = new Queue<IOperation>();
|
|
|
|
|
protected Stack<IOperation> stack = new Stack<IOperation>();
|
2017-07-13 08:51:00 +02:00
|
|
|
|
|
2018-10-03 14:35:27 +02:00
|
|
|
|
public event ActionRequiredEventHandler ActionRequired
|
|
|
|
|
{
|
|
|
|
|
add { operations.ForEach(o => o.ActionRequired += value); }
|
|
|
|
|
remove { operations.ForEach(o => o.ActionRequired -= value); }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public event ProgressChangedEventHandler ProgressChanged;
|
|
|
|
|
|
|
|
|
|
public event StatusChangedEventHandler StatusChanged
|
|
|
|
|
{
|
|
|
|
|
add { operations.ForEach(o => o.StatusChanged += value); }
|
|
|
|
|
remove { operations.ForEach(o => o.StatusChanged -= value); }
|
|
|
|
|
}
|
2018-02-02 09:18:35 +01:00
|
|
|
|
|
|
|
|
|
public OperationSequence(ILogger logger, Queue<IOperation> operations)
|
2017-07-07 15:46:32 +02:00
|
|
|
|
{
|
2017-07-17 16:59:50 +02:00
|
|
|
|
this.logger = logger;
|
2018-02-02 09:18:35 +01:00
|
|
|
|
this.operations = new Queue<IOperation>(operations);
|
2017-07-07 15:46:32 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-10 09:19:03 +02:00
|
|
|
|
public virtual OperationResult TryPerform()
|
2017-07-07 15:46:32 +02:00
|
|
|
|
{
|
2018-02-28 15:49:06 +01:00
|
|
|
|
var result = OperationResult.Failed;
|
2018-01-19 14:04:12 +01:00
|
|
|
|
|
2017-07-07 15:46:32 +02:00
|
|
|
|
try
|
|
|
|
|
{
|
2018-02-02 09:18:35 +01:00
|
|
|
|
Initialize();
|
2018-02-28 15:49:06 +01:00
|
|
|
|
result = Perform();
|
2018-01-19 14:04:12 +01:00
|
|
|
|
|
2018-02-28 15:49:06 +01:00
|
|
|
|
if (result != OperationResult.Success)
|
2018-01-19 14:04:12 +01:00
|
|
|
|
{
|
2018-02-06 15:12:11 +01:00
|
|
|
|
Revert(true);
|
2018-01-19 14:04:12 +01:00
|
|
|
|
}
|
2018-02-01 08:37:12 +01:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2018-02-02 09:18:35 +01:00
|
|
|
|
logger.Error("Failed to perform operations!", e);
|
2018-02-01 08:37:12 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-28 15:49:06 +01:00
|
|
|
|
return result;
|
2018-02-01 08:37:12 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-10 09:19:03 +02:00
|
|
|
|
public virtual OperationResult TryRevert()
|
2018-02-01 08:37:12 +01:00
|
|
|
|
{
|
2018-02-28 15:49:06 +01:00
|
|
|
|
var result = OperationResult.Failed;
|
2018-02-02 09:18:35 +01:00
|
|
|
|
|
2018-02-01 08:37:12 +01:00
|
|
|
|
try
|
|
|
|
|
{
|
2018-02-06 15:12:11 +01:00
|
|
|
|
Initialize(true);
|
2018-10-10 09:19:03 +02:00
|
|
|
|
result = Revert();
|
2017-07-13 08:51:00 +02:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2018-02-02 09:18:35 +01:00
|
|
|
|
logger.Error("Failed to revert operations!", e);
|
2017-07-13 08:51:00 +02:00
|
|
|
|
}
|
2018-01-19 14:04:12 +01:00
|
|
|
|
|
2018-10-10 09:19:03 +02:00
|
|
|
|
return result;
|
2017-07-13 08:51:00 +02:00
|
|
|
|
}
|
2017-07-07 15:46:32 +02:00
|
|
|
|
|
2018-10-10 09:19:03 +02:00
|
|
|
|
protected virtual void Initialize(bool indeterminate = false)
|
2018-02-01 08:37:12 +01:00
|
|
|
|
{
|
2018-02-02 09:18:35 +01:00
|
|
|
|
if (indeterminate)
|
2018-02-01 08:37:12 +01:00
|
|
|
|
{
|
2018-10-10 09:19:03 +02:00
|
|
|
|
UpdateProgress(new ProgressChangedEventArgs { IsIndeterminate = true });
|
2018-02-01 08:37:12 +01:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2018-10-10 09:19:03 +02:00
|
|
|
|
UpdateProgress(new ProgressChangedEventArgs { CurrentValue = 0, MaxValue = operations.Count });
|
2018-02-01 08:37:12 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-10 09:19:03 +02:00
|
|
|
|
protected virtual OperationResult Perform()
|
2017-07-13 08:51:00 +02:00
|
|
|
|
{
|
2017-07-21 12:05:31 +02:00
|
|
|
|
foreach (var operation in operations)
|
2017-07-21 10:04:27 +02:00
|
|
|
|
{
|
2018-02-28 15:49:06 +01:00
|
|
|
|
var result = OperationResult.Failed;
|
|
|
|
|
|
2017-07-21 12:05:31 +02:00
|
|
|
|
stack.Push(operation);
|
2018-01-19 14:04:12 +01:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2018-02-28 15:49:06 +01:00
|
|
|
|
result = operation.Perform();
|
2018-01-19 14:04:12 +01:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2018-02-28 15:49:06 +01:00
|
|
|
|
logger.Error($"Caught unexpected exception while performing operation '{operation.GetType().Name}'!", e);
|
2018-01-19 14:04:12 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-28 15:49:06 +01:00
|
|
|
|
if (result != OperationResult.Success)
|
2018-01-19 14:04:12 +01:00
|
|
|
|
{
|
2018-02-28 15:49:06 +01:00
|
|
|
|
return result;
|
2018-01-19 14:04:12 +01:00
|
|
|
|
}
|
2017-07-07 15:46:32 +02:00
|
|
|
|
|
2018-10-10 09:19:03 +02:00
|
|
|
|
UpdateProgress(new ProgressChangedEventArgs { Progress = true });
|
2018-02-02 09:18:35 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-28 15:49:06 +01:00
|
|
|
|
return OperationResult.Success;
|
2018-02-02 09:18:35 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-10 09:19:03 +02:00
|
|
|
|
protected virtual OperationResult Revert(bool regress = false)
|
2017-07-13 08:51:00 +02:00
|
|
|
|
{
|
2018-02-01 08:37:12 +01:00
|
|
|
|
var success = true;
|
|
|
|
|
|
2017-07-21 12:05:31 +02:00
|
|
|
|
while (stack.Any())
|
2017-07-21 10:04:27 +02:00
|
|
|
|
{
|
2017-07-21 12:05:31 +02:00
|
|
|
|
var operation = stack.Pop();
|
2017-07-13 08:51:00 +02:00
|
|
|
|
|
2017-10-09 12:11:33 +02:00
|
|
|
|
try
|
|
|
|
|
{
|
2018-10-10 09:19:03 +02:00
|
|
|
|
var result = operation.Revert();
|
|
|
|
|
|
|
|
|
|
if (result != OperationResult.Success)
|
|
|
|
|
{
|
|
|
|
|
success = false;
|
|
|
|
|
}
|
2017-10-09 12:11:33 +02:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2018-10-10 09:19:03 +02:00
|
|
|
|
logger.Error($"Caught unexpected exception while reverting operation '{operation.GetType().Name}'!", e);
|
2018-02-01 08:37:12 +01:00
|
|
|
|
success = false;
|
2017-10-09 12:11:33 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-01 08:37:12 +01:00
|
|
|
|
if (regress)
|
|
|
|
|
{
|
2018-10-10 09:19:03 +02:00
|
|
|
|
UpdateProgress(new ProgressChangedEventArgs { Regress = true });
|
2018-02-01 08:37:12 +01:00
|
|
|
|
}
|
2017-07-21 10:04:27 +02:00
|
|
|
|
}
|
2017-07-13 08:51:00 +02:00
|
|
|
|
|
2018-10-10 09:19:03 +02:00
|
|
|
|
return success ? OperationResult.Success : OperationResult.Failed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void UpdateProgress(ProgressChangedEventArgs args)
|
|
|
|
|
{
|
|
|
|
|
ProgressChanged?.Invoke(args);
|
2017-07-13 08:51:00 +02:00
|
|
|
|
}
|
2017-07-07 15:46:32 +02:00
|
|
|
|
}
|
|
|
|
|
}
|