2018-02-12 12:21:55 +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/.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
using System;
|
2018-03-15 14:32:07 +01:00
|
|
|
|
using SafeExamBrowser.Contracts.Communication.Data;
|
2018-06-21 07:56:25 +02:00
|
|
|
|
using SafeExamBrowser.Contracts.Communication.Events;
|
2018-03-15 14:32:07 +01:00
|
|
|
|
using SafeExamBrowser.Contracts.Communication.Hosts;
|
2018-02-12 12:21:55 +01:00
|
|
|
|
using SafeExamBrowser.Contracts.Logging;
|
2018-03-16 09:28:33 +01:00
|
|
|
|
using SafeExamBrowser.Core.Communication.Hosts;
|
2018-02-12 12:21:55 +01:00
|
|
|
|
|
|
|
|
|
namespace SafeExamBrowser.Client.Communication
|
|
|
|
|
{
|
|
|
|
|
internal class ClientHost : BaseHost, IClientHost
|
|
|
|
|
{
|
2018-02-27 15:28:54 +01:00
|
|
|
|
private bool allowConnection = true;
|
2018-02-16 13:15:16 +01:00
|
|
|
|
private int processId;
|
|
|
|
|
|
2018-02-14 15:26:05 +01:00
|
|
|
|
public Guid StartupToken { private get; set; }
|
|
|
|
|
|
2018-02-20 15:15:26 +01:00
|
|
|
|
public event CommunicationEventHandler Shutdown;
|
|
|
|
|
|
2018-03-16 15:46:53 +01:00
|
|
|
|
public ClientHost(string address, IHostObjectFactory factory, ILogger logger, int processId) : base(address, factory, logger)
|
2018-02-12 12:21:55 +01:00
|
|
|
|
{
|
2018-02-16 13:15:16 +01:00
|
|
|
|
this.processId = processId;
|
2018-02-12 12:21:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-14 15:26:05 +01:00
|
|
|
|
protected override bool OnConnect(Guid? token)
|
2018-02-12 12:21:55 +01:00
|
|
|
|
{
|
2018-02-27 15:28:54 +01:00
|
|
|
|
var authenticated = StartupToken == token;
|
|
|
|
|
var accepted = allowConnection && authenticated;
|
|
|
|
|
|
|
|
|
|
if (accepted)
|
|
|
|
|
{
|
|
|
|
|
allowConnection = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return accepted;
|
2018-02-12 12:21:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-14 15:26:05 +01:00
|
|
|
|
protected override void OnDisconnect()
|
2018-02-12 12:21:55 +01:00
|
|
|
|
{
|
2018-02-27 15:28:54 +01:00
|
|
|
|
// Nothing to do here...
|
2018-02-12 12:21:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-15 15:42:54 +01:00
|
|
|
|
protected override Response OnReceive(Message message)
|
2018-02-12 12:21:55 +01:00
|
|
|
|
{
|
2018-02-16 13:15:16 +01:00
|
|
|
|
return new SimpleResponse(SimpleResponsePurport.UnknownMessage);
|
2018-02-14 15:26:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-16 13:15:16 +01:00
|
|
|
|
protected override Response OnReceive(SimpleMessagePurport message)
|
2018-02-14 15:26:05 +01:00
|
|
|
|
{
|
2018-02-16 13:15:16 +01:00
|
|
|
|
switch (message)
|
|
|
|
|
{
|
|
|
|
|
case SimpleMessagePurport.Authenticate:
|
|
|
|
|
return new AuthenticationResponse { ProcessId = processId };
|
2018-02-20 15:15:26 +01:00
|
|
|
|
case SimpleMessagePurport.Shutdown:
|
|
|
|
|
Shutdown?.Invoke();
|
|
|
|
|
return new SimpleResponse(SimpleResponsePurport.Acknowledged);
|
2018-02-16 13:15:16 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new SimpleResponse(SimpleResponsePurport.UnknownMessage);
|
2018-02-12 12:21:55 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|