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/.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.ServiceModel;
|
|
|
|
|
using SafeExamBrowser.Contracts.Communication;
|
|
|
|
|
using SafeExamBrowser.Contracts.Communication.Messages;
|
|
|
|
|
using SafeExamBrowser.Contracts.Communication.Responses;
|
|
|
|
|
using SafeExamBrowser.Contracts.Logging;
|
|
|
|
|
|
|
|
|
|
namespace SafeExamBrowser.Core.Communication
|
|
|
|
|
{
|
2018-02-14 15:26:05 +01:00
|
|
|
|
public abstract class BaseProxy : ICommunicationProxy
|
2018-01-23 15:33:54 +01:00
|
|
|
|
{
|
|
|
|
|
private string address;
|
2018-01-24 12:34:32 +01:00
|
|
|
|
private ICommunication channel;
|
2018-02-14 15:26:05 +01:00
|
|
|
|
private Guid? communicationToken;
|
2018-01-23 15:33:54 +01:00
|
|
|
|
|
2018-01-24 12:34:32 +01:00
|
|
|
|
protected ILogger Logger { get; private set; }
|
|
|
|
|
|
2018-02-06 15:12:11 +01:00
|
|
|
|
public BaseProxy(string address, ILogger logger)
|
2018-01-23 15:33:54 +01:00
|
|
|
|
{
|
|
|
|
|
this.address = address;
|
2018-01-24 12:34:32 +01:00
|
|
|
|
this.Logger = logger;
|
2018-01-23 15:33:54 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-14 15:26:05 +01:00
|
|
|
|
public virtual bool Connect(Guid? token = null)
|
2018-01-23 15:33:54 +01:00
|
|
|
|
{
|
|
|
|
|
var endpoint = new EndpointAddress(address);
|
|
|
|
|
|
2018-01-24 12:34:32 +01:00
|
|
|
|
channel = ChannelFactory<ICommunication>.CreateChannel(new NetNamedPipeBinding(NetNamedPipeSecurityMode.Transport), endpoint);
|
2018-02-06 15:12:11 +01:00
|
|
|
|
(channel as ICommunicationObject).Closed += BaseProxy_Closed;
|
|
|
|
|
(channel as ICommunicationObject).Closing += BaseProxy_Closing;
|
|
|
|
|
(channel as ICommunicationObject).Faulted += BaseProxy_Faulted;
|
|
|
|
|
(channel as ICommunicationObject).Opened += BaseProxy_Opened;
|
|
|
|
|
(channel as ICommunicationObject).Opening += BaseProxy_Opening;
|
2018-01-23 15:33:54 +01:00
|
|
|
|
|
2018-02-16 13:15:16 +01:00
|
|
|
|
Logger.Debug($"Trying to connect to endpoint {address}{(token.HasValue ? $" with authentication token '{token}'" : string.Empty)}...");
|
2018-02-15 15:42:54 +01:00
|
|
|
|
|
2018-01-23 15:33:54 +01:00
|
|
|
|
var response = channel.Connect(token);
|
|
|
|
|
|
2018-02-14 15:26:05 +01:00
|
|
|
|
communicationToken = response.CommunicationToken;
|
2018-02-15 15:42:54 +01:00
|
|
|
|
Logger.Debug($"Connection was {(response.ConnectionEstablished ? "established" : "refused")}.");
|
2018-01-23 15:33:54 +01:00
|
|
|
|
|
2018-02-14 15:26:05 +01:00
|
|
|
|
return response.ConnectionEstablished;
|
2018-01-23 15:33:54 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-14 15:26:05 +01:00
|
|
|
|
public virtual bool Disconnect()
|
2018-01-23 15:33:54 +01:00
|
|
|
|
{
|
2018-02-14 15:26:05 +01:00
|
|
|
|
FailIfNotConnected(nameof(Disconnect));
|
|
|
|
|
|
|
|
|
|
var message = new DisconnectionMessage { CommunicationToken = communicationToken.Value };
|
|
|
|
|
var response = channel.Disconnect(message);
|
2018-01-23 15:33:54 +01:00
|
|
|
|
|
2018-02-14 15:26:05 +01:00
|
|
|
|
Logger.Debug($"{(response.ConnectionTerminated ? "Disconnected" : "Failed to disconnect")} from {address}.");
|
|
|
|
|
|
|
|
|
|
return response.ConnectionTerminated;
|
2018-01-23 15:33:54 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-15 15:42:54 +01:00
|
|
|
|
protected Response Send(Message message)
|
2018-01-23 15:33:54 +01:00
|
|
|
|
{
|
2018-02-14 15:26:05 +01:00
|
|
|
|
FailIfNotConnected(nameof(Send));
|
2018-01-23 15:33:54 +01:00
|
|
|
|
|
2018-02-14 15:26:05 +01:00
|
|
|
|
message.CommunicationToken = communicationToken.Value;
|
2018-01-23 15:33:54 +01:00
|
|
|
|
|
2018-02-14 15:26:05 +01:00
|
|
|
|
var response = channel.Send(message);
|
2018-01-23 15:33:54 +01:00
|
|
|
|
|
2018-02-16 13:15:16 +01:00
|
|
|
|
Logger.Debug($"Sent message '{ToString(message)}', got response '{ToString(response)}'.");
|
2018-01-23 15:33:54 +01:00
|
|
|
|
|
2018-02-14 15:26:05 +01:00
|
|
|
|
return response;
|
2018-01-24 12:34:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-16 13:15:16 +01:00
|
|
|
|
protected Response Send(SimpleMessagePurport purport)
|
2018-01-23 15:33:54 +01:00
|
|
|
|
{
|
2018-02-16 13:15:16 +01:00
|
|
|
|
return Send(new SimpleMessage(purport));
|
2018-01-23 15:33:54 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-06 15:12:11 +01:00
|
|
|
|
private void BaseProxy_Closed(object sender, EventArgs e)
|
2018-01-23 15:33:54 +01:00
|
|
|
|
{
|
2018-01-24 12:34:32 +01:00
|
|
|
|
Logger.Debug("Communication channel has been closed.");
|
2018-01-23 15:33:54 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-06 15:12:11 +01:00
|
|
|
|
private void BaseProxy_Closing(object sender, EventArgs e)
|
2018-01-23 15:33:54 +01:00
|
|
|
|
{
|
2018-02-06 15:12:11 +01:00
|
|
|
|
Logger.Debug("Communication channel is closing...");
|
2018-01-23 15:33:54 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-06 15:12:11 +01:00
|
|
|
|
private void BaseProxy_Faulted(object sender, EventArgs e)
|
2018-01-23 15:33:54 +01:00
|
|
|
|
{
|
2018-01-25 09:50:46 +01:00
|
|
|
|
Logger.Debug("Communication channel has faulted!");
|
2018-01-23 15:33:54 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-06 15:12:11 +01:00
|
|
|
|
private void BaseProxy_Opened(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
Logger.Debug("Communication channel has been opened.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void BaseProxy_Opening(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
Logger.Debug("Communication channel is opening...");
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-14 15:26:05 +01:00
|
|
|
|
private void FailIfNotConnected(string operationName)
|
|
|
|
|
{
|
|
|
|
|
if (!communicationToken.HasValue)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException($"Cannot perform '{operationName}' before being connected to endpoint!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (channel == null || (channel as ICommunicationObject).State != CommunicationState.Opened)
|
|
|
|
|
{
|
|
|
|
|
throw new CommunicationException($"Tried to perform {operationName}, but channel was {GetChannelState()}!");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-23 15:33:54 +01:00
|
|
|
|
private string GetChannelState()
|
|
|
|
|
{
|
|
|
|
|
return channel == null ? "null" : $"in state '{(channel as ICommunicationObject).State}'";
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-15 15:42:54 +01:00
|
|
|
|
private string ToString(Message message)
|
2018-01-23 15:33:54 +01:00
|
|
|
|
{
|
2018-02-16 13:15:16 +01:00
|
|
|
|
return message != null ? message.ToString() : "<null>";
|
2018-01-23 15:33:54 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-15 15:42:54 +01:00
|
|
|
|
private string ToString(Response response)
|
2018-01-23 15:33:54 +01:00
|
|
|
|
{
|
2018-02-16 13:15:16 +01:00
|
|
|
|
return response != null ? response.ToString() : "<null>";
|
2018-01-23 15:33:54 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|