2018-01-23 15:33:54 +01:00
/ *
2019-01-09 11:25:21 +01:00
* Copyright ( c ) 2019 ETH Zürich , Educational Development and Technology ( LET )
2018-01-23 15:33:54 +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/.
* /
2018-03-15 14:32:07 +01:00
using SafeExamBrowser.Contracts.Communication.Proxies ;
2018-01-23 15:33:54 +01:00
using SafeExamBrowser.Contracts.Configuration.Settings ;
2018-10-02 15:45:45 +02:00
using SafeExamBrowser.Contracts.Core.OperationModel ;
2018-10-03 14:35:27 +02:00
using SafeExamBrowser.Contracts.Core.OperationModel.Events ;
2018-01-24 12:34:32 +01:00
using SafeExamBrowser.Contracts.I18n ;
2018-01-23 15:33:54 +01:00
using SafeExamBrowser.Contracts.Logging ;
2018-08-31 10:06:27 +02:00
namespace SafeExamBrowser.Runtime.Operations
2018-01-23 15:33:54 +01:00
{
2018-10-12 11:16:59 +02:00
internal class ServiceOperation : SessionOperation
2018-01-23 15:33:54 +01:00
{
2018-02-12 12:21:55 +01:00
private bool connected , mandatory ;
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
2018-10-12 11:16:59 +02:00
public override event ActionRequiredEventHandler ActionRequired { add { } remove { } }
public override event StatusChangedEventHandler StatusChanged ;
2018-01-23 15:33:54 +01:00
2018-10-12 11:16:59 +02:00
public ServiceOperation ( ILogger logger , IServiceProxy service , SessionContext sessionContext ) : base ( sessionContext )
2018-01-23 15:33:54 +01:00
{
this . logger = logger ;
2018-10-12 11:16:59 +02:00
this . service = service ;
2018-01-23 15:33:54 +01:00
}
2018-10-12 11:16:59 +02:00
public override OperationResult Perform ( )
2018-01-23 15:33:54 +01:00
{
2018-03-21 10:23:15 +01:00
logger . Info ( $"Initializing service session..." ) ;
2018-10-03 15:42:50 +02:00
StatusChanged ? . Invoke ( TextKey . OperationStatus_InitializeServiceSession ) ;
2018-01-23 15:33:54 +01:00
2018-10-12 11:16:59 +02:00
mandatory = Context . Next . Settings . ServicePolicy = = ServicePolicy . Mandatory ;
2018-08-10 13:23:24 +02:00
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-10-10 09:19:03 +02:00
logger . Error ( "Failed to initialize a service session since 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-10-12 11:16:59 +02:00
public override OperationResult Repeat ( )
2018-02-01 08:37:12 +01:00
{
2018-10-10 09:19:03 +02:00
var result = Revert ( ) ;
if ( result ! = OperationResult . Success )
2018-03-21 10:23:15 +01:00
{
2018-10-10 09:19:03 +02:00
return result ;
2018-03-21 10:23:15 +01:00
}
2018-02-28 15:49:06 +01:00
2018-10-10 09:19:03 +02:00
return Perform ( ) ;
2018-02-01 08:37:12 +01:00
}
2018-10-12 11:16:59 +02:00
public override OperationResult Revert ( )
2018-01-23 15:33:54 +01:00
{
2018-03-21 10:23:15 +01:00
logger . Info ( "Finalizing service session..." ) ;
2018-10-03 15:42:50 +02:00
StatusChanged ? . Invoke ( TextKey . OperationStatus_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-10-10 09:19:03 +02:00
return OperationResult . Success ;
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 ( )
{
2018-10-12 11:16:59 +02:00
service . StartSession ( Context . Next . Id , Context . Next . Settings ) ;
2018-03-21 10:23:15 +01:00
}
private void StopServiceSession ( )
{
2018-10-12 11:16:59 +02:00
service . StopSession ( Context . Current . Id ) ;
2018-03-21 10:23:15 +01:00
}
2018-01-23 15:33:54 +01:00
}
}