2019-06-18 10:18:56 +02:00
|
|
|
|
/*
|
2024-03-05 18:37:42 +01:00
|
|
|
|
* Copyright (c) 2024 ETH Zürich, IT Services
|
2019-06-18 10:18:56 +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/.
|
|
|
|
|
*/
|
|
|
|
|
|
2019-06-18 15:51:35 +02:00
|
|
|
|
using System;
|
2019-06-18 10:18:56 +02:00
|
|
|
|
using System.Threading;
|
2019-08-30 09:55:26 +02:00
|
|
|
|
using SafeExamBrowser.Core.Contracts.OperationModel;
|
|
|
|
|
using SafeExamBrowser.Logging.Contracts;
|
2019-06-18 10:18:56 +02:00
|
|
|
|
|
|
|
|
|
namespace SafeExamBrowser.Service.Operations
|
|
|
|
|
{
|
|
|
|
|
internal class SessionInitializationOperation : SessionOperation
|
|
|
|
|
{
|
|
|
|
|
private ILogger logger;
|
2019-06-19 15:40:21 +02:00
|
|
|
|
private Func<string, EventWaitHandle> serviceEventFactory;
|
2019-06-18 10:18:56 +02:00
|
|
|
|
|
2019-07-04 09:12:28 +02:00
|
|
|
|
public SessionInitializationOperation(ILogger logger, Func<string, EventWaitHandle> serviceEventFactory, SessionContext sessionContext) : base(sessionContext)
|
2019-06-18 10:18:56 +02:00
|
|
|
|
{
|
|
|
|
|
this.logger = logger;
|
2019-06-19 15:40:21 +02:00
|
|
|
|
this.serviceEventFactory = serviceEventFactory;
|
2019-06-18 10:18:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override OperationResult Perform()
|
|
|
|
|
{
|
|
|
|
|
logger.Info("Initializing new session...");
|
|
|
|
|
logger.Info($" -> Client-ID: {Context.Configuration.AppConfig.ClientId}");
|
|
|
|
|
logger.Info($" -> Runtime-ID: {Context.Configuration.AppConfig.RuntimeId}");
|
|
|
|
|
logger.Info($" -> Session-ID: {Context.Configuration.SessionId}");
|
|
|
|
|
|
2019-06-26 10:13:11 +02:00
|
|
|
|
logger.Info("Stopping auto-restore mechanism...");
|
|
|
|
|
Context.AutoRestoreMechanism.Stop();
|
|
|
|
|
|
2019-06-18 15:51:35 +02:00
|
|
|
|
InitializeServiceEvent();
|
2019-06-18 10:18:56 +02:00
|
|
|
|
|
|
|
|
|
return OperationResult.Success;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override OperationResult Revert()
|
|
|
|
|
{
|
2019-06-18 15:51:35 +02:00
|
|
|
|
var success = true;
|
2019-09-25 08:25:51 +02:00
|
|
|
|
var wasRunning = Context.IsRunning;
|
2019-06-18 10:18:56 +02:00
|
|
|
|
|
2019-09-25 08:25:51 +02:00
|
|
|
|
logger.Info("Starting auto-restore mechanism...");
|
|
|
|
|
Context.AutoRestoreMechanism.Start();
|
2019-06-18 10:18:56 +02:00
|
|
|
|
|
2019-09-25 08:25:51 +02:00
|
|
|
|
logger.Info("Clearing session data...");
|
|
|
|
|
Context.Configuration = null;
|
|
|
|
|
Context.IsRunning = false;
|
|
|
|
|
|
|
|
|
|
if (Context.ServiceEvent != null && wasRunning)
|
2019-06-18 10:18:56 +02:00
|
|
|
|
{
|
2019-06-18 15:51:35 +02:00
|
|
|
|
success = Context.ServiceEvent.Set();
|
|
|
|
|
|
|
|
|
|
if (success)
|
|
|
|
|
{
|
|
|
|
|
logger.Info("Successfully informed runtime about session termination.");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
logger.Error("Failed to inform runtime about session termination!");
|
|
|
|
|
}
|
2019-06-18 10:18:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return success ? OperationResult.Success : OperationResult.Failed;
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-18 15:51:35 +02:00
|
|
|
|
private void InitializeServiceEvent()
|
2019-06-18 10:18:56 +02:00
|
|
|
|
{
|
2019-06-18 15:51:35 +02:00
|
|
|
|
if (Context.ServiceEvent != null)
|
2019-06-18 10:18:56 +02:00
|
|
|
|
{
|
|
|
|
|
logger.Info("Closing service event from previous session...");
|
2019-06-18 15:51:35 +02:00
|
|
|
|
Context.ServiceEvent.Close();
|
2019-06-18 10:18:56 +02:00
|
|
|
|
logger.Info("Service event successfully closed.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logger.Info("Attempting to create new service event...");
|
2019-06-19 15:40:21 +02:00
|
|
|
|
Context.ServiceEvent = serviceEventFactory.Invoke(Context.Configuration.AppConfig.ServiceEventName);
|
2019-06-18 10:18:56 +02:00
|
|
|
|
logger.Info("Service event successfully created.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|