seb-win-refactoring/SafeExamBrowser.Core/Communication/ServiceProxy.cs

81 lines
1.7 KiB
C#

/*
* 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/.
*/
using System;
using SafeExamBrowser.Contracts.Communication;
using SafeExamBrowser.Contracts.Configuration.Settings;
using SafeExamBrowser.Contracts.Logging;
using SafeExamBrowser.Core.Communication.Messages;
namespace SafeExamBrowser.Core.Communication
{
public class ServiceProxy : BaseProxy, IServiceProxy
{
public bool Ignore { private get; set; }
public ServiceProxy(string address, ILogger logger) : base(address, logger)
{
}
public bool Connect()
{
if (IgnoreOperation(nameof(Connect)))
{
return false;
}
return base.Connect().ConnectionEstablished;
}
public void Disconnect()
{
if (IgnoreOperation(nameof(Disconnect)))
{
return;
}
FailIfNotConnected(nameof(Disconnect));
Disconnect(new Message { CommunicationToken = CommunicationToken.Value });
}
public void StartSession(Guid sessionId, ISettings settings)
{
if (IgnoreOperation(nameof(StartSession)))
{
return;
}
FailIfNotConnected(nameof(StartSession));
// TODO: Send(new StartSessionMessage { Id = sessionId, Settings = settings });
}
public void StopSession(Guid sessionId)
{
if (IgnoreOperation(nameof(StopSession)))
{
return;
}
FailIfNotConnected(nameof(StopSession));
// TODO
}
private bool IgnoreOperation(string operationName)
{
if (Ignore)
{
Logger.Debug($"Skipping {operationName} because the ignore flag is set.");
}
return Ignore;
}
}
}