From d506e27d5a9e7b3e10b488de0e1dc91ad06dff83 Mon Sep 17 00:00:00 2001 From: Julien W <50249422+julien-wff@users.noreply.github.com> Date: Tue, 19 Dec 2023 20:24:43 +0100 Subject: [PATCH 1/9] fix: add new line at end of JSON and refactoring --- EasyLib/Api/JobManagerServerWorker.cs | 35 ++++++++++----------------- 1 file changed, 13 insertions(+), 22 deletions(-) diff --git a/EasyLib/Api/JobManagerServerWorker.cs b/EasyLib/Api/JobManagerServerWorker.cs index d76191a..2def44e 100644 --- a/EasyLib/Api/JobManagerServerWorker.cs +++ b/EasyLib/Api/JobManagerServerWorker.cs @@ -6,18 +6,9 @@ namespace EasyLib.Api; -public class Worker +public class Worker(TcpClient socket, JobManagerServer server) { - private readonly JobManagerServer _server; - private readonly TcpClient _socket; - private readonly Stream _stream; - - public Worker(TcpClient socket, JobManagerServer server) - { - this._socket = socket; - this._stream = socket.GetStream(); - this._server = server; - } + private readonly Stream _stream = socket.GetStream(); public void Start() { @@ -26,8 +17,8 @@ public void Start() public void Send(JsonApiRequest request) { - string json = Newtonsoft.Json.JsonConvert.SerializeObject(request); - byte[] buffer = Encoding.UTF8.GetBytes(json); + var json = Newtonsoft.Json.JsonConvert.SerializeObject(request) + "\n\r"; + var buffer = Encoding.UTF8.GetBytes(json); _stream.Write(buffer, 0, buffer.Length); } @@ -35,33 +26,33 @@ private void Run() { try { - byte[] buffer = new byte[2018]; + var buffer = new byte[2018]; while (true) { - int receivedBytes = _stream.Read(buffer, 0, buffer.Length); + var receivedBytes = _stream.Read(buffer, 0, buffer.Length); if (receivedBytes < 1) break; - JsonApiRequest request = + var request = Newtonsoft.Json.JsonConvert.DeserializeObject( Encoding.UTF8.GetString(buffer, 0, receivedBytes)); - _server.ExecuteJobCommand(request.Action, new LocalJob(request.Job)); - if (_server.CancellationTokenSource.IsCancellationRequested) + server.ExecuteJobCommand(request.Action, new LocalJob(request.Job)); + if (server.CancellationTokenSource.IsCancellationRequested) break; } } - catch (Exception e) + catch (Exception) { Close(); - lock (_server.ServerLockObject) + lock (server.ServerLockObject) { - _server.RemoveWorker(this); + server.RemoveWorker(this); } } } public void SendAllJobs(List jobs) { - foreach (Job.Job job in jobs) + foreach (var job in jobs) { Send(new JsonApiRequest() { Action = ApiAction.Create, Job = job.ToJsonJob() }); } From a572994678c5f0cb880e7ddf30ab64fd38d0abcb Mon Sep 17 00:00:00 2001 From: Julien W <50249422+julien-wff@users.noreply.github.com> Date: Wed, 20 Dec 2023 00:43:37 +0100 Subject: [PATCH 2/9] feat: create a remote job from JsonJob --- EasyLib/Job/RemoteJob.cs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/EasyLib/Job/RemoteJob.cs b/EasyLib/Job/RemoteJob.cs index b3e819b..cb9493a 100644 --- a/EasyLib/Job/RemoteJob.cs +++ b/EasyLib/Job/RemoteJob.cs @@ -1,10 +1,28 @@ using EasyLib.Enums; +using EasyLib.Json; namespace EasyLib.Job; public class RemoteJob(string name, string source, string destination, JobType type) : Job(name, source, destination, type) { + /// + /// Create a job instance from a JsonJob object + /// + /// JsonJob object + public RemoteJob(JsonJob job) : this(job.name, job.source_folder, job.destination_folder, JobType.Full) + { + Id = job.id; + Type = EnumConverter.ConvertToEnum(job.type); + State = EnumConverter.ConvertToEnum(job.state); + FilesCount = job.active_job_info?.total_file_count ?? 0; + FilesSizeBytes = job.active_job_info?.total_file_size ?? 0; + FilesCopied = job.active_job_info?.files_copied ?? 0; + FilesBytesCopied = job.active_job_info?.bytes_copied ?? 0; + CurrentFileSource = job.active_job_info?.current_file_source ?? string.Empty; + CurrentFileDestination = job.active_job_info?.current_file_destination ?? string.Empty; + } + public override bool Resume() { throw new NotImplementedException(); From 223d2f39d7c7cc7052346c4de409c9fd739a7fb2 Mon Sep 17 00:00:00 2001 From: Julien W <50249422+julien-wff@users.noreply.github.com> Date: Wed, 20 Dec 2023 00:44:32 +0100 Subject: [PATCH 3/9] feat: connect the remote job manager to the UI and the server --- EasyGUI/MainWindow.xaml.cs | 43 +++++++++++++-- EasyLib/Api/JobManagerClient.cs | 74 +++++++++++++++++++++++++- EasyLib/JobManager/RemoteJobManager.cs | 43 +++++++++++++-- 3 files changed, 150 insertions(+), 10 deletions(-) diff --git a/EasyGUI/MainWindow.xaml.cs b/EasyGUI/MainWindow.xaml.cs index 9856e37..98168dc 100644 --- a/EasyGUI/MainWindow.xaml.cs +++ b/EasyGUI/MainWindow.xaml.cs @@ -16,17 +16,14 @@ namespace EasyGUI; /// public partial class MainWindow { - private readonly JobManager _jobManager; + private JobManager _jobManager; public MainWindow() { InitializeComponent(); _jobManager = new LocalJobManager(); - foreach (var job in _jobManager.GetJobs()) - { - Jobs.Add(job); - } + _refreshJobs(); } public ObservableCollection Jobs { get; } = new(); @@ -196,5 +193,41 @@ private void JobsHeader_OnConnectButtonClick(object sender, RoutedEventArgs e) private void RemoteConnectPopup_OnConnect(object? sender, RemoteConnectEventArgs e) { var endpoint = e.EndPoint; + + // Instantiate and configure a new remote job manager + var remoteJobManager = new RemoteJobManager(); + remoteJobManager.JobListChanged += _refreshJobs; + + // Connect to the remote job manager + var connected = remoteJobManager.Connect(endpoint); + if (!connected) + { + remoteJobManager.JobListChanged -= _refreshJobs; + RemoteConnectPopup.ErrorMessage = Strings.RemoteConnectPopup_Error_HostUnreachable; + return; + } + + // Remove the old job manager and delete the jobs + _jobManager.CleanStop(); + Jobs.Clear(); + + // Change the job manager + _jobManager = remoteJobManager; + + // Close the popup + RemoteConnectPopup.Visibility = Visibility.Collapsed; + } + + private void _refreshJobs(object? sender = null, EventArgs? e = null) + { + var jobs = _jobManager.GetJobs(); + Dispatcher.Invoke(() => + { + Jobs.Clear(); + foreach (var job in jobs) + { + Jobs.Add(job); + } + }); } } diff --git a/EasyLib/Api/JobManagerClient.cs b/EasyLib/Api/JobManagerClient.cs index 940c036..64c1b6f 100644 --- a/EasyLib/Api/JobManagerClient.cs +++ b/EasyLib/Api/JobManagerClient.cs @@ -1,5 +1,75 @@ -namespace EasyLib.Api; +using System.Net.Sockets; +using System.Text; +using EasyLib.Enums; +using EasyLib.Job; +using EasyLib.JobManager; +using EasyLib.Json; +using Newtonsoft.Json; -public class JobManagerClient +namespace EasyLib.Api; + +public class JobManagerClient(RemoteJobManager remoteJobManager, Socket serverSocket) { + public void Listen() + { + while (true) + { + try + { + var buffer = new byte[1024]; + var received = serverSocket.Receive(buffer, SocketFlags.None); + if (received == 0) + break; + + var data = new byte[received]; + Array.Copy(buffer, data, received); + + var text = Encoding.ASCII.GetString(data); + + foreach (var line in text.Trim().Split("\n\r")) + { + if (string.IsNullOrWhiteSpace(line)) + continue; + _handleAction(line); + } + } + catch (Exception) + { + break; + } + } + } + + private void _handleAction(string jsonAction) + { + var request = JsonConvert.DeserializeObject(jsonAction); + var job = new RemoteJob(request.Job); + + switch (request.Action) + { + case ApiAction.Start: + break; + case ApiAction.Pause: + break; + case ApiAction.Resume: + break; + case ApiAction.Cancel: + break; + case ApiAction.Delete: + break; + case ApiAction.Edit: + break; + case ApiAction.Error: + break; + case ApiAction.State: + break; + case ApiAction.Progress: + break; + case ApiAction.Create: + remoteJobManager.AddJob(job); + break; + default: + return; + } + } } diff --git a/EasyLib/JobManager/RemoteJobManager.cs b/EasyLib/JobManager/RemoteJobManager.cs index 648af7c..7bdb03e 100644 --- a/EasyLib/JobManager/RemoteJobManager.cs +++ b/EasyLib/JobManager/RemoteJobManager.cs @@ -1,17 +1,54 @@ -using EasyLib.Enums; +using System.Net; +using System.Net.Sockets; +using EasyLib.Api; +using EasyLib.Enums; namespace EasyLib.JobManager; public class RemoteJobManager : JobManager { + private readonly Socket _clientSocket = new(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); + private JobManagerClient? _client; + private Thread? _clientThread; + public event EventHandler? JobListChanged; + + public bool Connect(EndPoint endPoint) + { + try + { + _clientSocket.Connect(endPoint); + } + catch (Exception) + { + return false; + } + + _client = new JobManagerClient(this, _clientSocket); + _clientThread = new Thread(_client.Listen); + _clientThread.Start(); + + return true; + } + + public override void CleanStop() + { + _clientSocket.Close(); + } + public override List GetJobs() { - throw new NotImplementedException(); + return Jobs; } public override bool FetchJobs() { - throw new NotImplementedException(); + return false; + } + + public void AddJob(Job.Job job) + { + Jobs.Add(job); + JobListChanged?.Invoke(this, EventArgs.Empty); } public override JobCheckRule EditJob(Job.Job job, string name, string source, string destination, JobType? type) From 698d430d92da134682d90e98e99b76fec461cd28 Mon Sep 17 00:00:00 2001 From: Julien W <50249422+julien-wff@users.noreply.github.com> Date: Wed, 20 Dec 2023 15:58:34 +0100 Subject: [PATCH 4/9] feat: no line break in sent JSON and refactoring --- EasyLib/Api/JobManagerServerWorker.cs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/EasyLib/Api/JobManagerServerWorker.cs b/EasyLib/Api/JobManagerServerWorker.cs index 2def44e..fd54ea4 100644 --- a/EasyLib/Api/JobManagerServerWorker.cs +++ b/EasyLib/Api/JobManagerServerWorker.cs @@ -3,6 +3,7 @@ using EasyLib.Enums; using EasyLib.Job; using EasyLib.Json; +using Newtonsoft.Json; namespace EasyLib.Api; @@ -17,7 +18,7 @@ public void Start() public void Send(JsonApiRequest request) { - var json = Newtonsoft.Json.JsonConvert.SerializeObject(request) + "\n\r"; + var json = JsonConvert.SerializeObject(request, Formatting.None) + "\n\r"; var buffer = Encoding.UTF8.GetBytes(json); _stream.Write(buffer, 0, buffer.Length); } @@ -30,12 +31,16 @@ private void Run() while (true) { var receivedBytes = _stream.Read(buffer, 0, buffer.Length); + if (receivedBytes < 1) break; - var request = - Newtonsoft.Json.JsonConvert.DeserializeObject( - Encoding.UTF8.GetString(buffer, 0, receivedBytes)); + + var request = JsonConvert.DeserializeObject( + Encoding.UTF8.GetString(buffer, 0, receivedBytes) + ); + server.ExecuteJobCommand(request.Action, new LocalJob(request.Job)); + if (server.CancellationTokenSource.IsCancellationRequested) break; } From 0a48d7729f0603f95e561b5b411fb8057f07d5aa Mon Sep 17 00:00:00 2001 From: Julien W <50249422+julien-wff@users.noreply.github.com> Date: Wed, 20 Dec 2023 16:24:13 +0100 Subject: [PATCH 5/9] fix: avoid data to be grouped --- EasyLib/Api/JobManagerServerWorker.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/EasyLib/Api/JobManagerServerWorker.cs b/EasyLib/Api/JobManagerServerWorker.cs index fd54ea4..6164542 100644 --- a/EasyLib/Api/JobManagerServerWorker.cs +++ b/EasyLib/Api/JobManagerServerWorker.cs @@ -21,6 +21,7 @@ public void Send(JsonApiRequest request) var json = JsonConvert.SerializeObject(request, Formatting.None) + "\n\r"; var buffer = Encoding.UTF8.GetBytes(json); _stream.Write(buffer, 0, buffer.Length); + _stream.Flush(); } private void Run() From 70bf6c3b2c13cbd8d7e3b2fd446c380f74249133 Mon Sep 17 00:00:00 2001 From: Julien W <50249422+julien-wff@users.noreply.github.com> Date: Wed, 20 Dec 2023 16:46:07 +0100 Subject: [PATCH 6/9] feat: reflect jobs progress --- EasyLib/Api/JobManagerClient.cs | 72 +++++++++++++++++++++++++-------- EasyLib/Json/JsonApiRequest.cs | 10 +++-- 2 files changed, 62 insertions(+), 20 deletions(-) diff --git a/EasyLib/Api/JobManagerClient.cs b/EasyLib/Api/JobManagerClient.cs index 64c1b6f..8206301 100644 --- a/EasyLib/Api/JobManagerClient.cs +++ b/EasyLib/Api/JobManagerClient.cs @@ -16,34 +16,51 @@ public void Listen() { try { - var buffer = new byte[1024]; - var received = serverSocket.Receive(buffer, SocketFlags.None); - if (received == 0) - break; - - var data = new byte[received]; - Array.Copy(buffer, data, received); + var buffer = new byte[2018]; + var receivedBytes = serverSocket.Receive(buffer); - var text = Encoding.ASCII.GetString(data); + if (receivedBytes < 1) + break; - foreach (var line in text.Trim().Split("\n\r")) - { - if (string.IsNullOrWhiteSpace(line)) - continue; - _handleAction(line); - } + var json = Encoding.UTF8.GetString(buffer, 0, receivedBytes); + _handleAction(json); } - catch (Exception) + catch (Exception e) { + Console.Error.WriteLine(e); break; } } } + public bool SendAction(ApiAction action, Job.Job job) + { + try + { + var json = JsonConvert.SerializeObject(new JsonApiRequest(action, job)) + "\n\r"; + var data = Encoding.ASCII.GetBytes(json); + serverSocket.Send(data); + return true; + } + catch (Exception) + { + return false; + } + } + private void _handleAction(string jsonAction) { - var request = JsonConvert.DeserializeObject(jsonAction); - var job = new RemoteJob(request.Job); + JsonApiRequest request; + try + { + request = JsonConvert.DeserializeObject(jsonAction); + } + catch (Exception) + { + return; + } + + var job = _createOrUpdateJob(request.Job); switch (request.Action) { @@ -60,10 +77,13 @@ private void _handleAction(string jsonAction) case ApiAction.Edit: break; case ApiAction.Error: + job.OnJobError(new ApplicationException("Unknown error")); break; case ApiAction.State: + job.OnJobStateChange(job.State, job); break; case ApiAction.Progress: + job.OnJobProgress(job); break; case ApiAction.Create: remoteJobManager.AddJob(job); @@ -72,4 +92,22 @@ private void _handleAction(string jsonAction) return; } } + + private Job.Job _createOrUpdateJob(JsonJob jsonJob) + { + var job = remoteJobManager.GetJobs().Find(j => j.Id == jsonJob.id) + ?? new RemoteJob(jsonJob, this); + job.Name = jsonJob.name; + job.SourceFolder = jsonJob.source_folder; + job.DestinationFolder = jsonJob.destination_folder; + job.Type = EnumConverter.ConvertToEnum(jsonJob.type); + job.State = EnumConverter.ConvertToEnum(jsonJob.state); + job.FilesCount = jsonJob.active_job_info?.total_file_count ?? 0; + job.FilesSizeBytes = jsonJob.active_job_info?.total_file_size ?? 0; + job.FilesCopied = jsonJob.active_job_info?.files_copied ?? 0; + job.FilesBytesCopied = jsonJob.active_job_info?.bytes_copied ?? 0; + job.CurrentFileSource = jsonJob.active_job_info?.current_file_source ?? string.Empty; + job.CurrentFileDestination = jsonJob.active_job_info?.current_file_destination ?? string.Empty; + return job; + } } diff --git a/EasyLib/Json/JsonApiRequest.cs b/EasyLib/Json/JsonApiRequest.cs index 3c4fdc0..d7a9522 100644 --- a/EasyLib/Json/JsonApiRequest.cs +++ b/EasyLib/Json/JsonApiRequest.cs @@ -2,8 +2,12 @@ namespace EasyLib.Json; -public struct JsonApiRequest +public readonly struct JsonApiRequest(ApiAction action, JsonJob job) { - public ApiAction Action { get; set; } - public JsonJob Job { get; set; } + public JsonApiRequest(ApiAction action, Job.Job job) : this(action, job.ToJsonJob()) + { + } + + public ApiAction Action { get; init; } = action; + public JsonJob Job { get; init; } = job; } From 164a7862e2c6183c3f9754862f56e72bfd617d3a Mon Sep 17 00:00:00 2001 From: Julien W <50249422+julien-wff@users.noreply.github.com> Date: Wed, 20 Dec 2023 17:12:41 +0100 Subject: [PATCH 7/9] feat: send pause, cancel, resume and stop commands --- EasyLib/Job/RemoteJob.cs | 17 ++++++++++------- EasyLib/JobManager/RemoteJobManager.cs | 16 +++++++++++----- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/EasyLib/Job/RemoteJob.cs b/EasyLib/Job/RemoteJob.cs index cb9493a..b4e0932 100644 --- a/EasyLib/Job/RemoteJob.cs +++ b/EasyLib/Job/RemoteJob.cs @@ -1,16 +1,19 @@ -using EasyLib.Enums; +using EasyLib.Api; +using EasyLib.Enums; using EasyLib.Json; namespace EasyLib.Job; -public class RemoteJob(string name, string source, string destination, JobType type) +public class RemoteJob(string name, string source, string destination, JobType type, JobManagerClient client) : Job(name, source, destination, type) { /// /// Create a job instance from a JsonJob object /// /// JsonJob object - public RemoteJob(JsonJob job) : this(job.name, job.source_folder, job.destination_folder, JobType.Full) + /// JobManagerClient instance + public RemoteJob(JsonJob job, JobManagerClient client) + : this(job.name, job.source_folder, job.destination_folder, JobType.Full, client) { Id = job.id; Type = EnumConverter.ConvertToEnum(job.type); @@ -25,21 +28,21 @@ public RemoteJob(JsonJob job) : this(job.name, job.source_folder, job.destinatio public override bool Resume() { - throw new NotImplementedException(); + return client.SendAction(ApiAction.Resume, this); } public override bool Run() { - throw new NotImplementedException(); + return client.SendAction(ApiAction.Start, this); } public override bool Pause() { - throw new NotImplementedException(); + return client.SendAction(ApiAction.Pause, this); } public override bool Cancel() { - throw new NotImplementedException(); + return client.SendAction(ApiAction.Cancel, this); } } diff --git a/EasyLib/JobManager/RemoteJobManager.cs b/EasyLib/JobManager/RemoteJobManager.cs index 7bdb03e..f1baa97 100644 --- a/EasyLib/JobManager/RemoteJobManager.cs +++ b/EasyLib/JobManager/RemoteJobManager.cs @@ -74,26 +74,32 @@ public override void DeleteJob(Job.Job job) public override void CancelJob(Job.Job job) { - throw new NotImplementedException(); + job.Cancel(); } public override void PauseJob(Job.Job job) { - throw new NotImplementedException(); + job.Pause(); } public override void ResumeJob(Job.Job job) { - throw new NotImplementedException(); + job.Resume(); } public override void PauseAllJobs() { - throw new NotImplementedException(); + foreach (var job in Jobs) + { + job.Pause(); + } } public override void ResumeAllJobs() { - throw new NotImplementedException(); + foreach (var job in Jobs) + { + job.Resume(); + } } } From f5872dbeabf46889f98bb2c565781f06faeb11e8 Mon Sep 17 00:00:00 2001 From: Julien W <50249422+julien-wff@users.noreply.github.com> Date: Wed, 20 Dec 2023 17:13:01 +0100 Subject: [PATCH 8/9] feat: broadcast state change --- EasyLib/JobManager/LocalJobManager.cs | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/EasyLib/JobManager/LocalJobManager.cs b/EasyLib/JobManager/LocalJobManager.cs index f401b06..222d154 100644 --- a/EasyLib/JobManager/LocalJobManager.cs +++ b/EasyLib/JobManager/LocalJobManager.cs @@ -50,10 +50,7 @@ public override void OnJobProgress(Job.Job job) StateManager.Instance.WriteJobs(Jobs); } - if (_server != null) - { - _server.Broadcast(ApiAction.Progress, job.ToJsonJob()); - } + _server?.Broadcast(ApiAction.Progress, job.ToJsonJob()); } public override void OnJobStateChange(JobState state, Job.Job job) @@ -64,16 +61,15 @@ public override void OnJobStateChange(JobState state, Job.Job job) } StateManager.Instance.WriteJobs(Jobs); + + _server?.Broadcast(ApiAction.State, job.ToJsonJob()); } public override void CleanStop() { _pauseJobEvent.Stop(); - if (_server != null) - { - _server.CancellationTokenSource.Cancel(); - _server.CleanInstance(); - } + _server?.CancellationTokenSource.Cancel(); + _server?.CleanInstance(); } public override List GetJobs() From e0182437706cc7639e8da8455bc456e0c10a7b4f Mon Sep 17 00:00:00 2001 From: Julien W <50249422+julien-wff@users.noreply.github.com> Date: Wed, 20 Dec 2023 17:25:56 +0100 Subject: [PATCH 9/9] feat: sync job running state --- EasyLib/Api/JobManagerClient.cs | 7 ++++--- EasyLib/Api/JobManagerServer.cs | 19 ++++++++++++------- EasyLib/Api/JobManagerServerWorker.cs | 7 ++++++- EasyLib/Job/Job.cs | 2 +- EasyLib/JobManager/LocalJobManager.cs | 4 ++-- EasyLib/Json/JsonApiRequest.cs | 5 +++-- 6 files changed, 28 insertions(+), 16 deletions(-) diff --git a/EasyLib/Api/JobManagerClient.cs b/EasyLib/Api/JobManagerClient.cs index 8206301..48e58ee 100644 --- a/EasyLib/Api/JobManagerClient.cs +++ b/EasyLib/Api/JobManagerClient.cs @@ -37,7 +37,7 @@ public bool SendAction(ApiAction action, Job.Job job) { try { - var json = JsonConvert.SerializeObject(new JsonApiRequest(action, job)) + "\n\r"; + var json = JsonConvert.SerializeObject(new JsonApiRequest(action, job, job.CurrentlyRunning)) + "\n\r"; var data = Encoding.ASCII.GetBytes(json); serverSocket.Send(data); return true; @@ -60,7 +60,7 @@ private void _handleAction(string jsonAction) return; } - var job = _createOrUpdateJob(request.Job); + var job = _createOrUpdateJob(request.Job, request.JobRunning); switch (request.Action) { @@ -93,7 +93,7 @@ private void _handleAction(string jsonAction) } } - private Job.Job _createOrUpdateJob(JsonJob jsonJob) + private Job.Job _createOrUpdateJob(JsonJob jsonJob, bool running) { var job = remoteJobManager.GetJobs().Find(j => j.Id == jsonJob.id) ?? new RemoteJob(jsonJob, this); @@ -108,6 +108,7 @@ private Job.Job _createOrUpdateJob(JsonJob jsonJob) job.FilesBytesCopied = jsonJob.active_job_info?.bytes_copied ?? 0; job.CurrentFileSource = jsonJob.active_job_info?.current_file_source ?? string.Empty; job.CurrentFileDestination = jsonJob.active_job_info?.current_file_destination ?? string.Empty; + job.CurrentlyRunning = running; return job; } } diff --git a/EasyLib/Api/JobManagerServer.cs b/EasyLib/Api/JobManagerServer.cs index e8fa548..42acc90 100644 --- a/EasyLib/Api/JobManagerServer.cs +++ b/EasyLib/Api/JobManagerServer.cs @@ -26,7 +26,7 @@ public JobManagerServer(LocalJobManager localJobManager) } } - public CancellationTokenSource CancellationTokenSource { get; } = new CancellationTokenSource(); + public CancellationTokenSource CancellationTokenSource { get; } = new(); private void _waitForConnection() { @@ -34,16 +34,16 @@ private void _waitForConnection() { while (true) { - TcpClient socket = _serverSocket.AcceptTcpClient(); + var socket = _serverSocket.AcceptTcpClient(); - Worker worker = new Worker(socket, this); + var worker = new Worker(socket, this); AddWorker(worker); worker.SendAllJobs(_localJobManager.GetJobs()); if (CancellationTokenSource.IsCancellationRequested) break; } } - catch (SocketException e) + catch (SocketException) { CleanInstance(); } @@ -67,13 +67,18 @@ public void RemoveWorker(Worker worker) } } - public void Broadcast(ApiAction action, JsonJob jsonJob) + public void Broadcast(ApiAction action, JsonJob jsonJob, bool jobRunning) { lock (ServerLockObject) { - foreach (Worker worker in _workers) + foreach (var worker in _workers) { - worker.Send(new JsonApiRequest() { Action = action, Job = jsonJob }); + worker.Send(new JsonApiRequest + { + Action = action, + Job = jsonJob, + JobRunning = jobRunning + }); } } } diff --git a/EasyLib/Api/JobManagerServerWorker.cs b/EasyLib/Api/JobManagerServerWorker.cs index 6164542..cff203b 100644 --- a/EasyLib/Api/JobManagerServerWorker.cs +++ b/EasyLib/Api/JobManagerServerWorker.cs @@ -60,7 +60,12 @@ public void SendAllJobs(List jobs) { foreach (var job in jobs) { - Send(new JsonApiRequest() { Action = ApiAction.Create, Job = job.ToJsonJob() }); + Send(new JsonApiRequest + { + Action = ApiAction.Create, + Job = job.ToJsonJob(), + JobRunning = job.CurrentlyRunning + }); } } diff --git a/EasyLib/Job/Job.cs b/EasyLib/Job/Job.cs index 32fd96a..95e2f26 100644 --- a/EasyLib/Job/Job.cs +++ b/EasyLib/Job/Job.cs @@ -75,7 +75,7 @@ public abstract class Job( /// True if the job is currently running, false otherwise /// If it's false but the state is not End, it means that the job is paused /// - public bool CurrentlyRunning { get; protected set; } + public bool CurrentlyRunning { get; set; } /// /// Subscribers to the job-related events diff --git a/EasyLib/JobManager/LocalJobManager.cs b/EasyLib/JobManager/LocalJobManager.cs index 222d154..a7a6a10 100644 --- a/EasyLib/JobManager/LocalJobManager.cs +++ b/EasyLib/JobManager/LocalJobManager.cs @@ -50,7 +50,7 @@ public override void OnJobProgress(Job.Job job) StateManager.Instance.WriteJobs(Jobs); } - _server?.Broadcast(ApiAction.Progress, job.ToJsonJob()); + _server?.Broadcast(ApiAction.Progress, job.ToJsonJob(), job.CurrentlyRunning); } public override void OnJobStateChange(JobState state, Job.Job job) @@ -62,7 +62,7 @@ public override void OnJobStateChange(JobState state, Job.Job job) StateManager.Instance.WriteJobs(Jobs); - _server?.Broadcast(ApiAction.State, job.ToJsonJob()); + _server?.Broadcast(ApiAction.State, job.ToJsonJob(), job.CurrentlyRunning); } public override void CleanStop() diff --git a/EasyLib/Json/JsonApiRequest.cs b/EasyLib/Json/JsonApiRequest.cs index d7a9522..53da938 100644 --- a/EasyLib/Json/JsonApiRequest.cs +++ b/EasyLib/Json/JsonApiRequest.cs @@ -2,12 +2,13 @@ namespace EasyLib.Json; -public readonly struct JsonApiRequest(ApiAction action, JsonJob job) +public readonly struct JsonApiRequest(ApiAction action, JsonJob job, bool running) { - public JsonApiRequest(ApiAction action, Job.Job job) : this(action, job.ToJsonJob()) + public JsonApiRequest(ApiAction action, Job.Job job, bool running) : this(action, job.ToJsonJob(), running) { } public ApiAction Action { get; init; } = action; public JsonJob Job { get; init; } = job; + public bool JobRunning { get; init; } = running; }