This repository has been archived by the owner on Sep 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #66 from julien-wff/feat-remote-client
feat: remote client
- Loading branch information
Showing
9 changed files
with
280 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,114 @@ | ||
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[2018]; | ||
var receivedBytes = serverSocket.Receive(buffer); | ||
|
||
if (receivedBytes < 1) | ||
break; | ||
|
||
var json = Encoding.UTF8.GetString(buffer, 0, receivedBytes); | ||
_handleAction(json); | ||
} | ||
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, job.CurrentlyRunning)) + "\n\r"; | ||
var data = Encoding.ASCII.GetBytes(json); | ||
serverSocket.Send(data); | ||
return true; | ||
} | ||
catch (Exception) | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
private void _handleAction(string jsonAction) | ||
{ | ||
JsonApiRequest request; | ||
try | ||
{ | ||
request = JsonConvert.DeserializeObject<JsonApiRequest>(jsonAction); | ||
} | ||
catch (Exception) | ||
{ | ||
return; | ||
} | ||
|
||
var job = _createOrUpdateJob(request.Job, request.JobRunning); | ||
|
||
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: | ||
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); | ||
break; | ||
default: | ||
return; | ||
} | ||
} | ||
|
||
private Job.Job _createOrUpdateJob(JsonJob jsonJob, bool running) | ||
{ | ||
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<JobType>.ConvertToEnum(jsonJob.type); | ||
job.State = EnumConverter<JobState>.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; | ||
job.CurrentlyRunning = running; | ||
return job; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,48 @@ | ||
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) | ||
{ | ||
/// <summary> | ||
/// Create a job instance from a JsonJob object | ||
/// </summary> | ||
/// <param name="job">JsonJob object</param> | ||
/// <param name="client">JobManagerClient instance</param> | ||
public RemoteJob(JsonJob job, JobManagerClient client) | ||
: this(job.name, job.source_folder, job.destination_folder, JobType.Full, client) | ||
{ | ||
Id = job.id; | ||
Type = EnumConverter<JobType>.ConvertToEnum(job.type); | ||
State = EnumConverter<JobState>.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(); | ||
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); | ||
} | ||
} |
Oops, something went wrong.