2019-06-07 15:26:03 +02:00
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2019 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.Communication.Hosts;
|
2019-06-18 10:18:56 +02:00
|
|
|
|
using SafeExamBrowser.Contracts.Communication;
|
2019-06-07 15:26:03 +02:00
|
|
|
|
using SafeExamBrowser.Contracts.Communication.Data;
|
2019-06-18 10:18:56 +02:00
|
|
|
|
using SafeExamBrowser.Contracts.Communication.Events;
|
2019-06-07 15:26:03 +02:00
|
|
|
|
using SafeExamBrowser.Contracts.Communication.Hosts;
|
|
|
|
|
using SafeExamBrowser.Contracts.Logging;
|
|
|
|
|
|
|
|
|
|
namespace SafeExamBrowser.Service.Communication
|
|
|
|
|
{
|
|
|
|
|
internal class ServiceHost : BaseHost, IServiceHost
|
|
|
|
|
{
|
2019-06-18 10:18:56 +02:00
|
|
|
|
private readonly object @lock = new object();
|
|
|
|
|
|
|
|
|
|
private bool allowConnection;
|
|
|
|
|
|
|
|
|
|
public bool AllowConnection
|
|
|
|
|
{
|
|
|
|
|
get { lock (@lock) { return allowConnection; } }
|
|
|
|
|
set { lock (@lock) { allowConnection = value; } }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public event CommunicationEventHandler<SessionStartEventArgs> SessionStartRequested;
|
|
|
|
|
public event CommunicationEventHandler<SessionStopEventArgs> SessionStopRequested;
|
|
|
|
|
|
2019-06-07 15:26:03 +02:00
|
|
|
|
internal ServiceHost(string address, IHostObjectFactory factory, ILogger logger, int timeout_ms) : base(address, factory, logger, timeout_ms)
|
|
|
|
|
{
|
2019-06-18 10:18:56 +02:00
|
|
|
|
AllowConnection = true;
|
2019-06-07 15:26:03 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override bool OnConnect(Guid? token)
|
|
|
|
|
{
|
2019-06-18 10:18:56 +02:00
|
|
|
|
lock (@lock)
|
|
|
|
|
{
|
|
|
|
|
var allow = AllowConnection;
|
|
|
|
|
|
|
|
|
|
if (allow)
|
|
|
|
|
{
|
|
|
|
|
AllowConnection = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return allow;
|
|
|
|
|
}
|
2019-06-07 15:26:03 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-18 10:18:56 +02:00
|
|
|
|
protected override void OnDisconnect(Interlocutor interlocutor)
|
2019-06-07 15:26:03 +02:00
|
|
|
|
{
|
2019-06-18 10:18:56 +02:00
|
|
|
|
if (interlocutor == Interlocutor.Runtime)
|
|
|
|
|
{
|
|
|
|
|
lock (@lock)
|
|
|
|
|
{
|
|
|
|
|
AllowConnection = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-06-07 15:26:03 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override Response OnReceive(Message message)
|
|
|
|
|
{
|
2019-06-18 10:18:56 +02:00
|
|
|
|
switch (message)
|
|
|
|
|
{
|
|
|
|
|
case SessionStartMessage m:
|
|
|
|
|
SessionStartRequested?.InvokeAsync(new SessionStartEventArgs { Configuration = m.Configuration });
|
|
|
|
|
return new SimpleResponse(SimpleResponsePurport.Acknowledged);
|
|
|
|
|
case SessionStopMessage m:
|
|
|
|
|
SessionStopRequested?.InvokeAsync(new SessionStopEventArgs { SessionId = m.SessionId });
|
|
|
|
|
return new SimpleResponse(SimpleResponsePurport.Acknowledged);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new SimpleResponse(SimpleResponsePurport.UnknownMessage);
|
2019-06-07 15:26:03 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override Response OnReceive(SimpleMessagePurport message)
|
|
|
|
|
{
|
2019-06-18 10:18:56 +02:00
|
|
|
|
return new SimpleResponse(SimpleResponsePurport.UnknownMessage);
|
2019-06-07 15:26:03 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|