using Quasar.Common.Enums;
using Quasar.Common.Messages;
using Quasar.Common.Models;
using Quasar.Common.Networking;
using Quasar.Server.Networking;
namespace Quasar.Server.Messages
{
///
/// Handles messages for the interaction with remote tasks.
///
public class TaskManagerHandler : MessageProcessorBase
{
///
/// Represents the method that will handle the result of a process action.
///
/// The message processor which raised the event.
/// The process action which was performed.
/// The result of the performed process action.
public delegate void ProcessActionPerformedEventHandler(object sender, ProcessAction action, bool result);
///
/// Raised when a result of a started process is received.
///
///
/// Handlers registered with this event will be invoked on the
/// chosen when the instance was constructed.
///
public event ProcessActionPerformedEventHandler ProcessActionPerformed;
///
/// Reports the result of a started process.
///
/// The process action which was performed.
/// The result of the performed process action.
private void OnProcessActionPerformed(ProcessAction action, bool result)
{
SynchronizationContext.Post(r =>
{
var handler = ProcessActionPerformed;
handler?.Invoke(this, action, (bool)r);
}, result);
}
///
/// The client which is associated with this remote execution handler.
///
private readonly Client _client;
///
/// Initializes a new instance of the class using the given client.
///
/// The associated client.
public TaskManagerHandler(Client client) : base(true)
{
_client = client;
}
public override bool CanExecute(IMessage message) => message is DoProcessResponse ||
message is GetProcessesResponse;
public override bool CanExecuteFrom(ISender sender) => _client.Equals(sender);
public override void Execute(ISender sender, IMessage message)
{
switch (message)
{
case DoProcessResponse execResp:
Execute(sender, execResp);
break;
case GetProcessesResponse procResp:
Execute(sender, procResp);
break;
}
}
///
/// Starts a new process remotely.
///
/// The remote path used for starting the new process.
/// Decides whether the process is a client update.
public void StartProcess(string remotePath, bool isUpdate = false)
{
_client.Send(new DoProcessStart { FilePath = remotePath, IsUpdate = isUpdate });
}
///
/// Downloads a file from the web and executes it remotely.
///
/// The URL to download and execute.
/// Decides whether the file is a client update.
public void StartProcessFromWeb(string url, bool isUpdate = false)
{
_client.Send(new DoProcessStart { DownloadUrl = url, IsUpdate = isUpdate});
}
///
/// Refreshes the current started processes.
///
public void RefreshProcesses()
{
_client.Send(new GetProcesses());
}
///
/// Ends a started process given the process id.
///
/// The process id to end.
public void EndProcess(int pid)
{
_client.Send(new DoProcessEnd { Pid = pid });
}
private void Execute(ISender client, DoProcessResponse message)
{
OnProcessActionPerformed(message.Action, message.Result);
}
private void Execute(ISender client, GetProcessesResponse message)
{
OnReport(message.Processes);
}
}
}