Skip to content

Commit

Permalink
chore: better request handling
Browse files Browse the repository at this point in the history
  • Loading branch information
jdaar committed May 9, 2023
1 parent d3bb3e8 commit 3e9ee6b
Showing 1 changed file with 126 additions and 141 deletions.
267 changes: 126 additions & 141 deletions fsBuddyService/PipeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using ConnectionInterface;
using Configuration;
using Serilog;
using Microsoft.EntityFrameworkCore.Metadata.Internal;

namespace Service
{
Expand All @@ -26,6 +27,8 @@ class PipeManager : BackgroundService
const string PIPE_NAME = "fsbuddy-service";
const int PIPE_STREAM_END_BYTE = -1;

private Dictionary<t_PipeCommand, Func<PipeRequest, Task<PipeResponse>>> Handlers;

public PipeManager(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
Expand All @@ -36,6 +39,14 @@ public PipeManager(IServiceProvider serviceProvider)
_configurationManager = _serviceScope.ServiceProvider.GetRequiredService<ManagerConfiguration>();
_pipeServer = PipeFactory();

Handlers = new() {
{ t_PipeCommand.CREATE_WATCHER, HandleCreateWatcher },
{ t_PipeCommand.UPDATE_WATCHER, HandleUpdateWatcher },
{ t_PipeCommand.DELETE_WATCHER, HandleDeleteWatcher },
{ t_PipeCommand.GET_ALL_WATCHER, HandleGetAllWatcher },
{ t_PipeCommand.GET_WATCHER, HandleGetWatcher }
};

_writer = new StreamWriter(_pipeServer);
_reader = new StreamReader(_pipeServer);
}
Expand Down Expand Up @@ -84,168 +95,142 @@ private async Task SendPipeResponse(PipeResponse pipeResponse)
await _writer.FlushAsync();
}

private async Task<PipeResponse> ProcessPipeRequest(PipeRequest pipeRequest)
private async Task<PipeResponse> HandleCreateWatcher(PipeRequest pipeRequest)
{
List<Watcher> watchers;
switch (pipeRequest.Command)
if (pipeRequest?.Payload?.WatcherData == null)
{
case t_PipeCommand.CREATE_WATCHER:
if (pipeRequest?.Payload?.WatcherData == null)
{
return new PipeResponse
{
Status = t_ResponseStatus.FAILURE,
Payload = new PipeResponsePayload {
ErrorMessage = "Watcher data not defined"
}
};
return new PipeResponse
{
Status = t_ResponseStatus.FAILURE,
Payload = new PipeResponsePayload {
ErrorMessage = "Watcher data not defined"
}
Log.Information("Creating watcher {@WatcherData}", pipeRequest.Payload.WatcherData);
};
}
Log.Information("Creating watcher {@WatcherData}", pipeRequest.Payload.WatcherData);

await _configurationManager.CreateWatcher(pipeRequest.Payload.WatcherData);
watchers = await _configurationManager.GetWatchers();
_watcherManager.RefreshWatchers(watchers);
await _configurationManager.CreateWatcher(pipeRequest.Payload.WatcherData);
var watchers = await _configurationManager.GetWatchers();
_watcherManager.RefreshWatchers(watchers);

return new PipeResponse
{
Status = t_ResponseStatus.SUCCESS,
Payload = new PipeResponsePayload { }
};
case t_PipeCommand.UPDATE_WATCHER:
if (pipeRequest?.Payload?.WatcherId == null || pipeRequest?.Payload?.WatcherData == null)
{
return new PipeResponse
{
Status = t_ResponseStatus.FAILURE,
Payload = new PipeResponsePayload {
ErrorMessage = "Watcher id or data not defined"
}
};
return new PipeResponse
{
Status = t_ResponseStatus.SUCCESS,
Payload = new PipeResponsePayload { }
};
}
private async Task<PipeResponse> HandleUpdateWatcher(PipeRequest pipeRequest)
{
if (pipeRequest?.Payload?.WatcherId == null || pipeRequest?.Payload?.WatcherData == null)
{
return new PipeResponse
{
Status = t_ResponseStatus.FAILURE,
Payload = new PipeResponsePayload {
ErrorMessage = "Watcher id or data not defined"
}
};
}

Log.Information("Getting watcher with id {WatcherId}", pipeRequest.Payload.WatcherId);
await _configurationManager.UpdateWatcher(pipeRequest.Payload.WatcherId ?? -1, pipeRequest.Payload.WatcherData);
Log.Information("Getting watcher with id {WatcherId}", pipeRequest.Payload.WatcherId);
await _configurationManager.UpdateWatcher(pipeRequest.Payload.WatcherId ?? -1, pipeRequest.Payload.WatcherData);

watchers = await _configurationManager.GetWatchers();
_watcherManager.RefreshWatchers(watchers);
var watchers = await _configurationManager.GetWatchers();
_watcherManager.RefreshWatchers(watchers);

return new PipeResponse
{
Status = t_ResponseStatus.SUCCESS,
Payload = new PipeResponsePayload {
}
};
case t_PipeCommand.DELETE_WATCHER:
if (pipeRequest?.Payload?.WatcherId == null)
{
return new PipeResponse
{
Status = t_ResponseStatus.FAILURE,
Payload = new PipeResponsePayload {
ErrorMessage = "Watcher id is not defined"
}
};
return new PipeResponse
{
Status = t_ResponseStatus.SUCCESS,
Payload = new PipeResponsePayload {
}
};
}
private async Task<PipeResponse> HandleDeleteWatcher(PipeRequest pipeRequest)
{
if (pipeRequest?.Payload?.WatcherId == null)
{
return new PipeResponse
{
Status = t_ResponseStatus.FAILURE,
Payload = new PipeResponsePayload {
ErrorMessage = "Watcher id is not defined"
}
};
}

Log.Information("Deleting watcher with id {WatcherId}", pipeRequest.Payload.WatcherId);
await _configurationManager.DeleteWatcher(pipeRequest.Payload.WatcherId ?? -1);
Log.Information("Deleting watcher with id {WatcherId}", pipeRequest.Payload.WatcherId);
await _configurationManager.DeleteWatcher(pipeRequest.Payload.WatcherId ?? -1);

watchers = await _configurationManager.GetWatchers();
_watcherManager.RefreshWatchers(watchers);
var watchers = await _configurationManager.GetWatchers();
_watcherManager.RefreshWatchers(watchers);

return new PipeResponse
{
Status = t_ResponseStatus.SUCCESS,
Payload = new PipeResponsePayload {
}
};
case t_PipeCommand.GET_ALL_WATCHER:
Log.Information("Getting all watchers");
watchers = await _configurationManager.GetWatchers();

return new PipeResponse
{
Status = t_ResponseStatus.SUCCESS,
Payload = new PipeResponsePayload {
Watchers = watchers
}
};
case t_PipeCommand.GET_WATCHER:
if (pipeRequest?.Payload?.WatcherId == null)
{
return new PipeResponse
{
Status = t_ResponseStatus.FAILURE,
Payload = new PipeResponsePayload {
ErrorMessage = "Watcher id not defined"
}
};
}
return new PipeResponse
{
Status = t_ResponseStatus.SUCCESS,
Payload = new PipeResponsePayload {
}
};
}
private async Task<PipeResponse> HandleGetAllWatcher(PipeRequest pipeRequest)
{
Log.Information("Getting all watchers");
var watchers = await _configurationManager.GetWatchers();

Log.Information("Getting watcher with id {WatcherId}", pipeRequest.Payload.WatcherId);
watchers = new List<Watcher>() {
await _configurationManager.GetWatcher(pipeRequest.Payload.WatcherId ?? -1)
};
return new PipeResponse
{
Status = t_ResponseStatus.SUCCESS,
Payload = new PipeResponsePayload {
Watchers = watchers
}
};
case t_PipeCommand.PAUSE_WATCHER:
if (pipeRequest?.Payload?.WatcherId == null)
{
return new PipeResponse
{
Status = t_ResponseStatus.FAILURE,
Payload = new PipeResponsePayload {
ErrorMessage = "Watcher id not defined"
}
};
}
return new PipeResponse
{
Status = t_ResponseStatus.SUCCESS,
Payload = new PipeResponsePayload {
Watchers = watchers
}
};
}

_watcherManager.StopFsDisposable(pipeRequest.Payload.WatcherId ?? -1);

Log.Information("Stopping watcher with id {WatcherId}", pipeRequest.Payload.WatcherId);
return new PipeResponse
{
Status = t_ResponseStatus.SUCCESS,
Payload = new PipeResponsePayload {
}
};
case t_PipeCommand.GET_WATCHER_STATUS:
if (pipeRequest?.Payload?.WatcherId == null)
{
return new PipeResponse
{
Status = t_ResponseStatus.FAILURE,
Payload = new PipeResponsePayload {
ErrorMessage = "Watcher id not defined"
}
};
private async Task<PipeResponse> HandleGetWatcher(PipeRequest pipeRequest)
{
if (pipeRequest?.Payload?.WatcherId == null)
{
return new PipeResponse
{
Status = t_ResponseStatus.FAILURE,
Payload = new PipeResponsePayload {
ErrorMessage = "Watcher id not defined"
}
};
}

var isWatcherRunning = _watcherManager.IsFsDisposableRunning(pipeRequest.Payload.WatcherId ?? -1);
Log.Information("Getting watcher with id {WatcherId}", pipeRequest.Payload.WatcherId);
var watchers = new List<Watcher>() {
await _configurationManager.GetWatcher(pipeRequest.Payload.WatcherId ?? -1)
};
return new PipeResponse
{
Status = t_ResponseStatus.SUCCESS,
Payload = new PipeResponsePayload {
Watchers = watchers
}
};

Log.Information("Stopping watcher with id {WatcherId}", pipeRequest.Payload.WatcherId);
return new PipeResponse
{
Status = t_ResponseStatus.SUCCESS,
Payload = new PipeResponsePayload {
WatcherStatus = isWatcherRunning
}
};
default:
return new PipeResponse
}


private async Task<PipeResponse> ProcessPipeRequest(PipeRequest pipeRequest)
{
var handler = Handlers.GetValueOrDefault(pipeRequest.Command);

if (handler == null)
{
return new PipeResponse
{
Status = t_ResponseStatus.FAILURE,
Payload = new PipeResponsePayload
{
Status = t_ResponseStatus.FAILURE,
Payload = new PipeResponsePayload
{
ErrorMessage = "Command not defined"
}
};
ErrorMessage = "Command not defined"
}
};
}

return await handler(pipeRequest);
}

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
Expand Down

0 comments on commit 3e9ee6b

Please sign in to comment.