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-01-24 12:34:32 +01:00
using System ;
2018-02-01 08:37:12 +01:00
using SafeExamBrowser.Contracts.Behaviour.Operations ;
2018-01-23 15:33:54 +01:00
using SafeExamBrowser.Contracts.Communication ;
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 ;
namespace SafeExamBrowser.Runtime.Behaviour.Operations
{
internal class ServiceOperation : IOperation
{
2018-01-24 12:34:32 +01:00
private bool serviceAvailable ;
private bool serviceMandatory ;
2018-01-23 15:33:54 +01:00
private ILogger logger ;
2018-01-24 12:34:32 +01:00
private IServiceProxy service ;
2018-01-23 15:33:54 +01:00
private ISettingsRepository settingsRepository ;
2018-01-24 12:34:32 +01:00
private IText text ;
2018-01-23 15:33:54 +01:00
2018-02-01 08:37:12 +01:00
public bool Abort { get ; private set ; }
2018-01-23 15:33:54 +01:00
public ISplashScreen SplashScreen { private get ; set ; }
2018-01-24 12:34:32 +01:00
public ServiceOperation ( ILogger logger , IServiceProxy service , ISettingsRepository settingsRepository , IText text )
2018-01-23 15:33:54 +01:00
{
2018-01-24 12:34:32 +01:00
this . service = service ;
2018-01-23 15:33:54 +01:00
this . logger = logger ;
this . settingsRepository = settingsRepository ;
2018-01-24 12:34:32 +01:00
this . text = text ;
2018-01-23 15:33:54 +01:00
}
public void Perform ( )
{
2018-01-24 12:34:32 +01:00
logger . Info ( $"Initializing service connection..." ) ;
SplashScreen . UpdateText ( TextKey . SplashScreen_InitializeServiceConnection ) ;
2018-01-23 15:33:54 +01:00
2018-01-24 12:34:32 +01:00
try
{
serviceMandatory = settingsRepository . Current . ServicePolicy = = ServicePolicy . Mandatory ;
serviceAvailable = service . Connect ( ) ;
}
catch ( Exception e )
{
2018-02-01 08:37:12 +01:00
LogException ( e ) ;
2018-01-24 12:34:32 +01:00
}
2018-01-25 09:50:46 +01:00
if ( serviceMandatory & & ! serviceAvailable )
2018-01-24 12:34:32 +01:00
{
2018-02-01 08:37:12 +01:00
Abort = true ;
2018-01-24 12:34:32 +01:00
logger . Info ( "Aborting startup because the service is mandatory but not available!" ) ;
}
else
{
2018-02-01 08:37:12 +01:00
service . Ignore = ! serviceAvailable ;
logger . Info ( $"The service is {(serviceMandatory ? " mandatory " : " optional ")} and {(serviceAvailable ? " available . " : " not available . All service - related operations will be ignored ! ")}" ) ;
2018-01-24 12:34:32 +01:00
}
2018-01-23 15:33:54 +01:00
}
2018-02-01 08:37:12 +01:00
public void Repeat ( )
{
// TODO
}
2018-01-23 15:33:54 +01:00
public void Revert ( )
{
2018-01-24 12:34:32 +01:00
logger . Info ( "Closing service connection..." ) ;
SplashScreen . UpdateText ( TextKey . SplashScreen_CloseServiceConnection ) ;
if ( serviceAvailable )
{
try
{
service . Disconnect ( ) ;
}
catch ( Exception e )
{
logger . Error ( "Failed to disconnect from service component!" , e ) ;
}
}
2018-01-23 15:33:54 +01:00
}
2018-02-01 08:37:12 +01:00
private void LogException ( Exception e )
{
var message = "Failed to connect to the service component!" ;
if ( serviceMandatory )
{
logger . Error ( message , e ) ;
}
else
{
logger . Info ( $"{message} Reason: {e.Message}" ) ;
}
}
2018-01-23 15:33:54 +01:00
}
}