2018-02-06 15:12:11 +01:00
|
|
|
|
/*
|
2023-03-08 00:30:20 +01:00
|
|
|
|
* Copyright (c) 2023 ETH Zürich, Educational Development and Technology (LET)
|
2018-02-06 15:12:11 +01: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/.
|
|
|
|
|
*/
|
|
|
|
|
|
2019-08-30 09:55:26 +02:00
|
|
|
|
using SafeExamBrowser.Communication.Contracts;
|
|
|
|
|
using SafeExamBrowser.Core.Contracts.OperationModel;
|
|
|
|
|
using SafeExamBrowser.Core.Contracts.OperationModel.Events;
|
|
|
|
|
using SafeExamBrowser.I18n.Contracts;
|
|
|
|
|
using SafeExamBrowser.Logging.Contracts;
|
2018-02-06 15:12:11 +01:00
|
|
|
|
|
2018-08-31 10:06:27 +02:00
|
|
|
|
namespace SafeExamBrowser.Core.Operations
|
2018-02-06 15:12:11 +01:00
|
|
|
|
{
|
2018-03-06 14:35:10 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// An operation to handle the lifetime of an <see cref="ICommunicationHost"/>. The host is started during <see cref="Perform"/>,
|
|
|
|
|
/// stopped and restarted during <see cref="Repeat"/> (if not running) and stopped during <see cref="Revert"/>.
|
|
|
|
|
/// </summary>
|
2018-10-10 09:19:03 +02:00
|
|
|
|
public class CommunicationHostOperation : IRepeatableOperation
|
2018-02-06 15:12:11 +01:00
|
|
|
|
{
|
|
|
|
|
private ICommunicationHost host;
|
|
|
|
|
private ILogger logger;
|
|
|
|
|
|
2018-10-03 14:35:27 +02:00
|
|
|
|
public event ActionRequiredEventHandler ActionRequired { add { } remove { } }
|
|
|
|
|
public event StatusChangedEventHandler StatusChanged;
|
2018-02-06 15:12:11 +01:00
|
|
|
|
|
2018-09-28 11:05:49 +02:00
|
|
|
|
public CommunicationHostOperation(ICommunicationHost host, ILogger logger)
|
2018-02-06 15:12:11 +01:00
|
|
|
|
{
|
|
|
|
|
this.host = host;
|
|
|
|
|
this.logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-28 15:49:06 +01:00
|
|
|
|
public OperationResult Perform()
|
2018-02-06 15:12:11 +01:00
|
|
|
|
{
|
|
|
|
|
logger.Info("Starting communication host...");
|
2018-10-03 15:42:50 +02:00
|
|
|
|
StatusChanged?.Invoke(TextKey.OperationStatus_StartCommunicationHost);
|
2018-02-06 15:12:11 +01:00
|
|
|
|
|
|
|
|
|
host.Start();
|
2018-02-28 15:49:06 +01:00
|
|
|
|
|
|
|
|
|
return OperationResult.Success;
|
2018-02-06 15:12:11 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-28 15:49:06 +01:00
|
|
|
|
public OperationResult Repeat()
|
2018-02-06 15:12:11 +01:00
|
|
|
|
{
|
|
|
|
|
if (!host.IsRunning)
|
|
|
|
|
{
|
|
|
|
|
logger.Info("Restarting communication host...");
|
2018-10-03 15:42:50 +02:00
|
|
|
|
StatusChanged?.Invoke(TextKey.OperationStatus_RestartCommunicationHost);
|
2018-02-06 15:12:11 +01:00
|
|
|
|
|
|
|
|
|
host.Stop();
|
|
|
|
|
host.Start();
|
|
|
|
|
}
|
2018-02-28 15:49:06 +01:00
|
|
|
|
|
|
|
|
|
return OperationResult.Success;
|
2018-02-06 15:12:11 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-10 09:19:03 +02:00
|
|
|
|
public OperationResult Revert()
|
2018-02-06 15:12:11 +01:00
|
|
|
|
{
|
|
|
|
|
logger.Info("Stopping communication host...");
|
2018-10-03 15:42:50 +02:00
|
|
|
|
StatusChanged?.Invoke(TextKey.OperationStatus_StopCommunicationHost);
|
2018-02-06 15:12:11 +01:00
|
|
|
|
|
|
|
|
|
host.Stop();
|
2018-10-10 09:19:03 +02:00
|
|
|
|
|
|
|
|
|
return OperationResult.Success;
|
2018-02-06 15:12:11 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|