Skip to content
This repository has been archived by the owner on Sep 3, 2024. It is now read-only.

Commit

Permalink
feat: working server
Browse files Browse the repository at this point in the history
  • Loading branch information
lukas-houille committed Dec 19, 2023
1 parent f50abb5 commit 36b7bef
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 21 deletions.
34 changes: 23 additions & 11 deletions EasyLib/Api/JobManagerServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Worker> _workers = new List<Worker>();
public readonly object ServerLockObject = new object();

public JobManagerServer(LocalJobManager localJobManager)
{
Expand All @@ -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;
}
Expand All @@ -49,7 +51,7 @@ private void _waitForConnection()

private void AddWorker(Worker worker)
{
lock (LockObject)
lock (ServerLockObject)
{
_workers.Add(worker);
worker.Start();
Expand All @@ -58,7 +60,7 @@ private void AddWorker(Worker worker)

public void RemoveWorker(Worker worker)
{
lock (LockObject)
lock (ServerLockObject)
{
_workers.Remove(worker);
worker.Close();
Expand All @@ -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)
{
Expand All @@ -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<uint>() { 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;
}
}
Expand All @@ -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();
}
}
26 changes: 19 additions & 7 deletions EasyLib/Api/JobManagerServerWorker.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Net.Sockets;
using System.Text;
using EasyLib.Enums;
using EasyLib.Job;
using EasyLib.Json;

Expand All @@ -18,18 +19,18 @@ 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);
byte[] buffer = Encoding.UTF8.GetBytes(json);
_stream.Write(buffer, 0, buffer.Length);
}

public void Start()
{
new Thread(Run).Start();
}

private void Run()
{
try
Expand All @@ -50,13 +51,24 @@ private void Run()
}
catch (Exception e)

Check warning on line 52 in EasyLib/Api/JobManagerServerWorker.cs

View workflow job for this annotation

GitHub Actions / Tests

The variable 'e' is declared but never used

Check warning on line 52 in EasyLib/Api/JobManagerServerWorker.cs

View workflow job for this annotation

GitHub Actions / Tests

The variable 'e' is declared but never used
{
// ignored
Close();
lock (_server.ServerLockObject)
{
_server.RemoveWorker(this);
}
}
}

public void SendAllJobs(List<Job.Job> jobs)
{
foreach (Job.Job job in jobs)
{
Send(new JsonApiRequest() { Action = ApiAction.Create, Job = job.ToJsonJob() });
}
}

public void Close()
{
_stream.Close();
_socket.Close();
}
}
3 changes: 1 addition & 2 deletions EasyLib/Enums/ApiAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ public enum ApiAction
Resume,
Cancel,
Delete,
Update,
Edit,
Error,
State,
Progress,
Jobs,
Create,
}
2 changes: 1 addition & 1 deletion EasyLib/JobManager/JobManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public virtual void OnJobStateChange(JobState state, Job.Job job)
/// </summary>
/// <param name="jobIds">List of IDs to select</param>
/// <returns>List of jobs</returns>
public virtual List<Job.Job> GetJobsFromIds(IEnumerable<uint> jobIds)
public virtual List<Job.Job> GetJobsFromIds(IEnumerable<int> jobIds)
{
var list = jobIds.ToList();
var ids = list.Select(id => (uint)id);
Expand Down

0 comments on commit 36b7bef

Please sign in to comment.