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-03-06 11:49:51 +01:00
using SafeExamBrowser.Contracts.Behaviour.OperationModel ;
2018-01-23 15:33:54 +01:00
using SafeExamBrowser.Contracts.Communication ;
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 ;
namespace SafeExamBrowser.Runtime.Behaviour.Operations
{
2018-02-12 12:21:55 +01:00
internal class ServiceConnectionOperation : 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-02-12 12:21:55 +01:00
public ServiceConnectionOperation ( 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-01-24 12:34:32 +01:00
logger . Info ( $"Initializing service connection..." ) ;
2018-02-02 09:30:41 +01:00
ProgressIndicator ? . UpdateText ( TextKey . ProgressIndicator_InitializeServiceConnection ) ;
2018-01-23 15:33:54 +01:00
2018-01-24 12:34:32 +01:00
try
{
2018-02-12 12:21:55 +01:00
mandatory = configuration . CurrentSettings . ServicePolicy = = ServicePolicy . Mandatory ;
connected = service . Connect ( ) ;
2018-01-24 12:34:32 +01:00
}
catch ( Exception e )
{
2018-02-01 08:37:12 +01:00
LogException ( e ) ;
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 ! ")}" ) ;
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-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-01-24 12:34:32 +01:00
logger . Info ( "Closing service connection..." ) ;
2018-02-02 09:30:41 +01:00
ProgressIndicator ? . UpdateText ( TextKey . ProgressIndicator_CloseServiceConnection ) ;
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
{
try
{
service . Disconnect ( ) ;
}
catch ( Exception e )
{
2018-02-14 15:26:05 +01:00
logger . Error ( "Failed to disconnect from the service!" , e ) ;
2018-01-24 12:34:32 +01:00
}
}
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!" ;
2018-02-12 12:21:55 +01:00
if ( mandatory )
2018-02-01 08:37:12 +01:00
{
logger . Error ( message , e ) ;
}
else
{
logger . Info ( $"{message} Reason: {e.Message}" ) ;
}
}
2018-01-23 15:33:54 +01:00
}
}