diff --git a/EasyLib/Api/JobManagerServer.cs b/EasyLib/Api/JobManagerServer.cs index 10310c9..e8fa548 100644 --- a/EasyLib/Api/JobManagerServer.cs +++ b/EasyLib/Api/JobManagerServer.cs @@ -9,10 +9,10 @@ namespace EasyLib.Api; public class JobManagerServer { - private static readonly object LockObject = new object(); private readonly LocalJobManager _localJobManager; private readonly TcpListener _serverSocket = null!; private readonly List _workers = new List(); + public readonly object ServerLockObject = new object(); public JobManagerServer(LocalJobManager localJobManager) { @@ -35,8 +35,10 @@ private void _waitForConnection() while (true) { TcpClient socket = _serverSocket.AcceptTcpClient(); + Worker worker = new Worker(socket, this); AddWorker(worker); + worker.SendAllJobs(_localJobManager.GetJobs()); if (CancellationTokenSource.IsCancellationRequested) break; } @@ -49,7 +51,7 @@ private void _waitForConnection() private void AddWorker(Worker worker) { - lock (LockObject) + lock (ServerLockObject) { _workers.Add(worker); worker.Start(); @@ -58,7 +60,7 @@ private void AddWorker(Worker worker) public void RemoveWorker(Worker worker) { - lock (LockObject) + lock (ServerLockObject) { _workers.Remove(worker); worker.Close(); @@ -67,7 +69,7 @@ public void RemoveWorker(Worker worker) public void Broadcast(ApiAction action, JsonJob jsonJob) { - lock (LockObject) + lock (ServerLockObject) { foreach (Worker worker in _workers) { @@ -78,25 +80,31 @@ public void Broadcast(ApiAction action, JsonJob jsonJob) public void ExecuteJobCommand(ApiAction action, Job.Job job) { + var localJob = _localJobManager.GetJobsFromIds(new[] { (int)job.Id }); + if (localJob.Count != 1) + return; switch (action) { case ApiAction.Start: - var localJob = _localJobManager.GetJobsFromIds(new List() { job.Id }); - if (localJob.Count == 1) - { - _localJobManager.ExecuteJob(localJob[0]); - } - + _localJobManager.ExecuteJob(localJob[0]); break; case ApiAction.Cancel: + _localJobManager.CancelJob(localJob[0]); break; case ApiAction.Pause: + _localJobManager.PauseJob(localJob[0]); break; case ApiAction.Edit: + _localJobManager.EditJob(localJob[0], job.Name, job.SourceFolder, job.DestinationFolder, job.Type); break; case ApiAction.Delete: + _localJobManager.DeleteJob(localJob[0]); break; case ApiAction.Resume: + _localJobManager.ResumeJob(localJob[0]); + break; + case ApiAction.Create: + _localJobManager.CreateJob(job.Name, job.SourceFolder, job.DestinationFolder, job.Type); break; } } @@ -106,9 +114,13 @@ public void CleanInstance() var numWorkers = _workers.Count; for (var i = 0; i < numWorkers; i++) { - RemoveWorker(_workers[0]); + lock (ServerLockObject) + { + _workers[i].Close(); + } } + _workers.Clear(); _serverSocket.Stop(); } } diff --git a/EasyLib/Api/JobManagerServerWorker.cs b/EasyLib/Api/JobManagerServerWorker.cs index 9f321a3..d76191a 100644 --- a/EasyLib/Api/JobManagerServerWorker.cs +++ b/EasyLib/Api/JobManagerServerWorker.cs @@ -1,5 +1,6 @@ using System.Net.Sockets; using System.Text; +using EasyLib.Enums; using EasyLib.Job; using EasyLib.Json; @@ -18,6 +19,11 @@ public Worker(TcpClient socket, JobManagerServer server) this._server = server; } + public void Start() + { + new Thread(Run).Start(); + } + public void Send(JsonApiRequest request) { string json = Newtonsoft.Json.JsonConvert.SerializeObject(request); @@ -25,11 +31,6 @@ public void Send(JsonApiRequest request) _stream.Write(buffer, 0, buffer.Length); } - public void Start() - { - new Thread(Run).Start(); - } - private void Run() { try @@ -50,13 +51,24 @@ private void Run() } catch (Exception e) { - // ignored + Close(); + lock (_server.ServerLockObject) + { + _server.RemoveWorker(this); + } + } + } + + public void SendAllJobs(List jobs) + { + foreach (Job.Job job in jobs) + { + Send(new JsonApiRequest() { Action = ApiAction.Create, Job = job.ToJsonJob() }); } } public void Close() { _stream.Close(); - _socket.Close(); } } diff --git a/EasyLib/Enums/ApiAction.cs b/EasyLib/Enums/ApiAction.cs index 719790f..276f158 100644 --- a/EasyLib/Enums/ApiAction.cs +++ b/EasyLib/Enums/ApiAction.cs @@ -7,10 +7,9 @@ public enum ApiAction Resume, Cancel, Delete, - Update, Edit, Error, State, Progress, - Jobs, + Create, } diff --git a/EasyLib/JobManager/JobManager.cs b/EasyLib/JobManager/JobManager.cs index 6cc50d1..4541c71 100644 --- a/EasyLib/JobManager/JobManager.cs +++ b/EasyLib/JobManager/JobManager.cs @@ -130,7 +130,7 @@ public virtual void OnJobStateChange(JobState state, Job.Job job) /// /// List of IDs to select /// List of jobs - public virtual List GetJobsFromIds(IEnumerable jobIds) + public virtual List GetJobsFromIds(IEnumerable jobIds) { var list = jobIds.ToList(); var ids = list.Select(id => (uint)id);