2018-01-23 15:33:54 +01:00
/ *
* Copyright ( c ) 2018 ETH Zürich , Educational Development and Technology ( LET )
*
* 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/.
* /
2018-08-31 10:06:27 +02:00
using SafeExamBrowser.Contracts.Core.OperationModel ;
2018-03-15 14:32:07 +01:00
using SafeExamBrowser.Contracts.Communication.Proxies ;
2018-02-06 15:12:11 +01:00
using SafeExamBrowser.Contracts.Configuration ;
2018-01-23 15:33:54 +01:00
using SafeExamBrowser.Contracts.Configuration.Settings ;
2018-01-24 12:34:32 +01:00
using SafeExamBrowser.Contracts.I18n ;
2018-01-23 15:33:54 +01:00
using SafeExamBrowser.Contracts.Logging ;
using SafeExamBrowser.Contracts.UserInterface ;
2018-08-31 10:06:27 +02:00
namespace SafeExamBrowser.Runtime.Operations
2018-01-23 15:33:54 +01:00
{
2018-03-21 10:23:15 +01:00
internal class ServiceOperation : IOperation
2018-01-23 15:33:54 +01:00
{
2018-02-12 12:21:55 +01:00
private bool connected , mandatory ;
2018-02-06 15:12:11 +01:00
private IConfigurationRepository configuration ;
2018-01-23 15:33:54 +01:00
private ILogger logger ;
2018-01-24 12:34:32 +01:00
private IServiceProxy service ;
private IText text ;
2018-01-23 15:33:54 +01:00
2018-02-02 09:18:35 +01:00
public IProgressIndicator ProgressIndicator { private get ; set ; }
2018-01-23 15:33:54 +01:00
2018-03-21 10:23:15 +01:00
public ServiceOperation ( IConfigurationRepository configuration , ILogger logger , IServiceProxy service , IText text )
2018-01-23 15:33:54 +01:00
{
2018-02-06 15:12:11 +01:00
this . configuration = configuration ;
2018-01-24 12:34:32 +01:00
this . service = service ;
2018-01-23 15:33:54 +01:00
this . logger = logger ;
2018-01-24 12:34:32 +01:00
this . text = text ;
2018-01-23 15:33:54 +01:00
}
2018-02-28 15:49:06 +01:00
public OperationResult Perform ( )
2018-01-23 15:33:54 +01:00
{
2018-03-21 10:23:15 +01:00
logger . Info ( $"Initializing service session..." ) ;
ProgressIndicator ? . UpdateText ( TextKey . ProgressIndicator_InitializeServiceSession ) ;
2018-01-23 15:33:54 +01:00
2018-08-10 13:23:24 +02:00
mandatory = configuration . CurrentSettings . ServicePolicy = = ServicePolicy . Mandatory ;
connected = service . Connect ( ) ;
2018-01-24 12:34:32 +01:00
2018-02-12 12:21:55 +01:00
if ( mandatory & & ! connected )
2018-01-24 12:34:32 +01:00
{
2018-02-14 15:26:05 +01:00
logger . Error ( "Aborting startup because the service is mandatory but not available!" ) ;
2018-02-28 15:49:06 +01:00
return OperationResult . Failed ;
2018-01-24 12:34:32 +01:00
}
2018-02-28 15:49:06 +01:00
service . Ignore = ! connected ;
logger . Info ( $"The service is {(mandatory ? " mandatory " : " optional ")} and {(connected ? " available . " : " not available . All service - related operations will be ignored ! ")}" ) ;
2018-03-21 10:23:15 +01:00
if ( connected )
{
StartServiceSession ( ) ;
}
2018-02-28 15:49:06 +01:00
return OperationResult . Success ;
2018-01-23 15:33:54 +01:00
}
2018-02-28 15:49:06 +01:00
public OperationResult Repeat ( )
2018-02-01 08:37:12 +01:00
{
2018-02-28 09:45:29 +01:00
// TODO: Re-check if mandatory, if so, try to connect (if not connected) - otherwise, no action required (except maybe logging of status?)
2018-03-21 10:23:15 +01:00
if ( connected )
{
StopServiceSession ( ) ;
StartServiceSession ( ) ;
}
2018-02-28 15:49:06 +01:00
return OperationResult . Success ;
2018-02-01 08:37:12 +01:00
}
2018-01-23 15:33:54 +01:00
public void Revert ( )
{
2018-03-21 10:23:15 +01:00
logger . Info ( "Finalizing service session..." ) ;
ProgressIndicator ? . UpdateText ( TextKey . ProgressIndicator_FinalizeServiceSession ) ;
2018-01-24 12:34:32 +01:00
2018-02-12 12:21:55 +01:00
if ( connected )
2018-01-24 12:34:32 +01:00
{
2018-03-21 10:23:15 +01:00
StopServiceSession ( ) ;
2018-08-10 13:23:24 +02:00
var success = service . Disconnect ( ) ;
if ( success )
2018-01-24 12:34:32 +01:00
{
2018-08-10 13:23:24 +02:00
logger . Info ( "Successfully disconnected from the service." ) ;
2018-01-24 12:34:32 +01:00
}
2018-08-10 13:23:24 +02:00
else
2018-01-24 12:34:32 +01:00
{
2018-08-10 13:23:24 +02:00
logger . Error ( "Failed to disconnect from the service!" ) ;
2018-01-24 12:34:32 +01:00
}
}
2018-01-23 15:33:54 +01:00
}
2018-02-01 08:37:12 +01:00
2018-03-21 10:23:15 +01:00
private void StartServiceSession ( )
{
service . StartSession ( configuration . CurrentSession . Id , configuration . CurrentSettings ) ;
}
private void StopServiceSession ( )
{
service . StopSession ( configuration . CurrentSession . Id ) ;
}
2018-01-23 15:33:54 +01:00
}
}