Skip to content

Commit

Permalink
refactor(ipc): simplify how response message is stringified
Browse files Browse the repository at this point in the history
  • Loading branch information
lars-berger committed Aug 3, 2023
1 parent 7d663ca commit 7e2daa9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 51 deletions.
78 changes: 29 additions & 49 deletions GlazeWM.Application.IpcServer/IpcMessageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,35 +78,42 @@ internal string GetResponseMessage(ClientMessage message)
.Select(match => match.Value)
.Where(match => match is not null);

return Parser.Default.ParseArguments<
var parsedArgs = Parser.Default.ParseArguments<
InvokeCommandMessage,
SubscribeMessage,
GetMonitorsMessage,
GetWorkspacesMessage,
GetWindowsMessage
>(messageParts).MapResult(
(InvokeCommandMessage message) =>
HandleInvokeCommandMessage(message, messageString),
(SubscribeMessage message) =>
HandleSubscribeMessage(message, sessionId, messageString),
(GetMonitorsMessage message) =>
HandleGetMonitorsMessage(message, messageString),
(GetWorkspacesMessage message) =>
HandleGetWorkspacesMessage(message, messageString),
(GetWindowsMessage message) =>
HandleGetWindowsMessage(message, messageString),
>(messageParts);

object? data = parsedArgs.Value switch
{
InvokeCommandMessage commandMsg => HandleInvokeCommandMessage(commandMsg),
SubscribeMessage subscribeMsg => HandleSubscribeMessage(subscribeMsg, sessionId),
GetMonitorsMessage => _monitorService.GetMonitors(),
GetWorkspacesMessage => _workspaceService.GetActiveWorkspaces(),
GetWindowsMessage => _windowService.GetWindows(),
_ => throw new Exception($"Invalid message '{messageString}'")
};

return ToResponseMessage(
success: true,
data: data,
clientMessage: messageString
);
}
catch (Exception exception)
{
return ToResponseMessage(false, null, messageString, exception.Message);
return ToResponseMessage<bool?>(
success: false,
data: null,
clientMessage: messageString,
error: exception.Message
);
}
}

private string HandleInvokeCommandMessage(
InvokeCommandMessage message,
string messageString)
private bool? HandleInvokeCommandMessage(InvokeCommandMessage message)
{
var contextContainer =
_containerService.GetContainerById(message.ContextContainerId) ??
Expand All @@ -119,14 +126,11 @@ private string HandleInvokeCommandMessage(
contextContainer
);

var commandResponse = _bus.Invoke((dynamic)command);
return ToResponseMessage(commandResponse);
_bus.Invoke((dynamic)command);
return null;
}

private string HandleSubscribeMessage(
SubscribeMessage message,
Guid sessionId,
string messageString)
private bool? HandleSubscribeMessage(SubscribeMessage message, Guid sessionId)
{
var eventNames = message.Events.Split(',');

Expand All @@ -142,36 +146,12 @@ private string HandleSubscribeMessage(
SubscribedSessions.Add(eventName, new List<Guid>() { sessionId });
}

return ToResponseMessage(CommandResponse.Ok);
}

private string HandleGetMonitorsMessage(
GetMonitorsMessage _,
string messageString)
{
var monitors = _monitorService.GetMonitors();
return ToResponseMessage(true, monitors as IEnumerable<Container>);
}

private string HandleGetWorkspacesMessage(
GetWorkspacesMessage _,
string messageString)
{
var workspaces = _workspaceService.GetActiveWorkspaces();
return ToResponseMessage(true, workspaces as IEnumerable<Container>);
}

private string HandleGetWindowsMessage(
GetWindowsMessage _,
string messageString)
{
var windows = _windowService.GetWindows();
return ToResponseMessage(true, windows as IEnumerable<Container>);
return null;
}

internal string ToResponseMessage<T>(
private string ToResponseMessage<T>(
bool success,
T data,
T? data,
string clientMessage,
string? error = null)
{
Expand Down
2 changes: 0 additions & 2 deletions GlazeWM.Application/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
using Microsoft.Extensions.Logging.Console;

//// TODO: Handle circular reference for that one workspace event.
//// TODO: Improve error handling in IPC server.
//// TODO: Move message interfaces to within IpcServer project.
//// TODO: Change naming of application directories.
//// * GlazeWM.App
//// * GlazeWM.App.Cli
Expand Down

0 comments on commit 7e2daa9

Please sign in to comment.