Skip to content

Commit

Permalink
fix(fdc3) - Refactored DesktopAgent queries to find the appropriate i…
Browse files Browse the repository at this point in the history
…ntents via raiseIntent, findIntent, findIntentsByContext call. Removed unnecessary codes, tests, code cleanup
  • Loading branch information
lilla28 committed May 3, 2024
1 parent 43ac1b4 commit eadcf64
Show file tree
Hide file tree
Showing 14 changed files with 1,951 additions and 1,896 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ namespace MorganStanley.ComposeUI.Fdc3.DesktopAgent.Contracts;
internal sealed class FindIntentRequest
{
/// <summary>
/// Unique identifier from the application which sent the RaiseIntentRequest type of message.
/// eg.: the instanceId of the application which can be queried from the window.object.composeui.fdc3.config, if it's a webapplication.
/// Unique identifier from the application which sent the FindIntentRequest type of message.
/// eg.: the instanceId of the application which can be queried from the window.object.composeui.fdc3.config, if it's a WebApplication.
/// </summary>
public string Fdc3InstanceId { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ internal sealed class FindIntentsByContextRequest
{
/// <summary>
/// Unique identifier from the application which sent the FindIntentsByContextRequest type of message.
/// eg.: the instanceId of the application which can be queried from the window.object.composeui.fdc3.config, if it's a webapplication.
/// eg.: the instanceId of the application which can be queried from the window.object.composeui.fdc3.config, if it's a WebApplication.
/// </summary>
public string Fdc3InstanceId { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ internal sealed class RaiseIntentRequest

/// <summary>
/// Unique identifier from the application which sent the RaiseIntentRequest type of message.
/// eg.: the instanceId of the application which can be queried from the window.object.composeui.fdc3.config, if it's a webapplication.
/// eg.: the instanceId of the application which can be queried from the window.object.composeui.fdc3.config, if it's a WebApplication.
/// </summary>
public string Fdc3InstanceId { get; set; }

Expand All @@ -52,9 +52,4 @@ internal sealed class RaiseIntentRequest
/// Information about the app that should resolve the raised intent.
/// </summary>
public AppIdentifier? TargetAppIdentifier { get; set; }

/// <summary>
/// Contains error text if an error happened during executing the raiseIntent method.
/// </summary>
public string? Error { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,4 @@ public static class Fdc3DesktopAgentErrors
/// Indicates that getting the IntentResult from the backend, has no appropriate attribute.
/// </summary>
public const string ResponseHasNoAttribute = $"{nameof(ResponseHasNoAttribute)}";

/// <summary>
/// Indicates that <seealso cref="MorganStanley.ComposeUI.ModuleLoader.IModuleLoader"/> could not start a module via raiseIntent, etc.
/// </summary>
public const string StartModuleFailure = $"{nameof(StartModuleFailure)}";

/// <summary>
/// Indicates that querying raised intent invocations does not contain an element for the intent.
/// </summary>
public const string MissingIntent = $"{nameof(MissingIntent)}";
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,8 @@ public static Fdc3DesktopAgentException MissingAppFromRaisedIntentInvocations(st
new(Fdc3DesktopAgentErrors.MissingId, $"Missing Fdc3InstanceId: {instanceId}, when module has added its intent listener and FDC3 is enabled by the application.");

public static Fdc3DesktopAgentException MultipleIntentRegisteredToAnAppInstance(string intent) =>
new(Fdc3DesktopAgentErrors.MultipleIntent, $"Multiplpe intent were registered to the running instance. Intent: {intent}.");
new(Fdc3DesktopAgentErrors.MultipleIntent, $"Multiple intents were registered to the running instance. Intent: {intent}.");

Check warning on line 28 in src/fdc3/dotnet/DesktopAgent/src/DesktopAgent/Exceptions/ThrowHelper.cs

View check run for this annotation

Codecov / codecov/patch

src/fdc3/dotnet/DesktopAgent/src/DesktopAgent/Exceptions/ThrowHelper.cs#L28

Added line #L28 was not covered by tests

public static Fdc3DesktopAgentException TargetInstanceUnavailable() =>
new(ResolveError.TargetAppUnavailable, $"Target app is unavailable when intent was raised.");

public static Fdc3DesktopAgentException StartModuleFailure(Exception exception) =>
new(Fdc3DesktopAgentErrors.StartModuleFailure, $"Could not start module when intent was raised.", exception);

public static Fdc3DesktopAgentException MissingIntentForMessage(string messageId, string intent) =>
new(Fdc3DesktopAgentErrors.MissingIntent, $"Could not found raised intent invocations for message: {messageId} with intent: {intent}");
}
452 changes: 281 additions & 171 deletions src/fdc3/dotnet/DesktopAgent/src/DesktopAgent/Fdc3DesktopAgent.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ public async ValueTask<UserChannel> HandleAddUserChannel(string id)
internal ValueTask<FindChannelResponse?> HandleFindChannel(FindChannelRequest? request, MessageContext context)
{
return ValueTask.FromResult<FindChannelResponse?>(
_desktopAgent.FindChannel(request!.ChannelId, request!.ChannelType)
? FindChannelResponse.Success
: FindChannelResponse.Failure(ChannelError.NoChannelFound));
_desktopAgent.FindChannel(request!.ChannelId, request!.ChannelType)
? FindChannelResponse.Success
: FindChannelResponse.Failure(ChannelError.NoChannelFound));
}

internal async ValueTask<FindIntentResponse?> HandleFindIntent(FindIntentRequest? request, MessageContext context)
Expand All @@ -90,25 +90,18 @@ public async ValueTask<UserChannel> HandleAddUserChannel(string id)

internal async ValueTask<RaiseIntentResponse?> HandleRaiseIntent(RaiseIntentRequest? request, MessageContext context)
{
try
var result = await _desktopAgent.RaiseIntent(request);
if (result.RaiseIntentResolutionMessages.Any())
{
var result = await _desktopAgent.RaiseIntent(request);
if (result.RaiseIntentResolutionMessages.Any())
foreach (var message in result.RaiseIntentResolutionMessages)
{
foreach (var message in result.RaiseIntentResolutionMessages)
{
await _messageRouter.PublishAsync(
Fdc3Topic.RaiseIntentResolution(message.Intent, message.TargetModuleInstanceId),
MessageBuffer.Factory.CreateJson(message.Request, _jsonSerializerOptions));
}
await _messageRouter.PublishAsync(
Fdc3Topic.RaiseIntentResolution(message.Intent, message.TargetModuleInstanceId),
MessageBuffer.Factory.CreateJson(message.Request, _jsonSerializerOptions));
}

return result.Response;
}
catch (Fdc3DesktopAgentException)
{
throw;
}

return result.Response;
}

internal async ValueTask<IntentListenerResponse?> HandleAddIntentListener(IntentListenerRequest? request, MessageContext context)
Expand Down Expand Up @@ -186,13 +179,13 @@ public async Task StopAsync(CancellationToken cancellationToken)
{
var unregisteringTasks = new ValueTask[]
{
_messageRouter.UnregisterServiceAsync(Fdc3Topic.FindChannel, cancellationToken),
_messageRouter.UnregisterServiceAsync(Fdc3Topic.FindIntent, cancellationToken),
_messageRouter.UnregisterServiceAsync(Fdc3Topic.RaiseIntent, cancellationToken),
_messageRouter.UnregisterServiceAsync(Fdc3Topic.FindIntentsByContext, cancellationToken),
_messageRouter.UnregisterServiceAsync(Fdc3Topic.GetIntentResult, cancellationToken),
_messageRouter.UnregisterServiceAsync(Fdc3Topic.SendIntentResult, cancellationToken),
_messageRouter.UnregisterServiceAsync(Fdc3Topic.AddIntentListener, cancellationToken)
_messageRouter.UnregisterServiceAsync(Fdc3Topic.FindChannel, cancellationToken),
_messageRouter.UnregisterServiceAsync(Fdc3Topic.FindIntent, cancellationToken),
_messageRouter.UnregisterServiceAsync(Fdc3Topic.RaiseIntent, cancellationToken),
_messageRouter.UnregisterServiceAsync(Fdc3Topic.FindIntentsByContext, cancellationToken),
_messageRouter.UnregisterServiceAsync(Fdc3Topic.GetIntentResult, cancellationToken),
_messageRouter.UnregisterServiceAsync(Fdc3Topic.SendIntentResult, cancellationToken),
_messageRouter.UnregisterServiceAsync(Fdc3Topic.AddIntentListener, cancellationToken)
};

await SafeWaitAsync(unregisteringTasks);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,5 @@ public RaiseIntentResolutionInvocation(
public ChannelType? ResultChannelType { get; set; }
public bool? ResultVoid { get; set; }
public string? ResultError { get; set; }
public bool IsResolved { get; set; } = false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
using Finos.Fdc3.Context;
using Finos.Fdc3;
using MorganStanley.ComposeUI.Fdc3.DesktopAgent.Exceptions;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;

namespace MorganStanley.ComposeUI.Fdc3.DesktopAgent.Infrastructure.Internal;

Expand All @@ -24,13 +26,28 @@ internal class RaisedIntentRequestHandler
private readonly object _raiseIntentInvocationsLock = new();
private readonly List<string> _registeredIntentListeners = new();
private readonly List<RaiseIntentResolutionInvocation> _raiseIntentResolutions = new();
private readonly ILogger<RaisedIntentRequestHandler> _logger;

public IEnumerable<string> IntentListeners => _registeredIntentListeners;
public IEnumerable<RaiseIntentResolutionInvocation> RaiseIntentResolutions => _raiseIntentResolutions;

public RaisedIntentRequestHandler(ILogger<RaisedIntentRequestHandler>? logger = null)
{
_logger = logger ?? NullLogger<RaisedIntentRequestHandler>.Instance;
}

public RaisedIntentRequestHandler AddIntentListener(string intent)
{
lock (_intentListenersLock)
{
if (_registeredIntentListeners.Contains(intent))
{
if (_logger.IsEnabled(LogLevel.Warning))
{
_logger.LogWarning($"Multiple IntentHandlers are registered to the intent: {intent}.");

Check warning on line 47 in src/fdc3/dotnet/DesktopAgent/src/DesktopAgent/Infrastructure/Internal/RaisedIntentRequestHandler.cs

View check run for this annotation

Codecov / codecov/patch

src/fdc3/dotnet/DesktopAgent/src/DesktopAgent/Infrastructure/Internal/RaisedIntentRequestHandler.cs#L47

Added line #L47 was not covered by tests
}
}

_registeredIntentListeners.Add(intent);
return this;
}
Expand Down Expand Up @@ -84,6 +101,7 @@ public RaisedIntentRequestHandler AddIntentResult(
raisedIntentInvocation.ResultContext = context;
raisedIntentInvocation.ResultVoid = voidResult;
raisedIntentInvocation.ResultError = error;
raisedIntentInvocation.IsResolved = true;
}
else if (raisedIntentInvocations.Count() > 1)
{
Expand All @@ -98,7 +116,11 @@ public bool TryGetRaisedIntentResult(string messageId, string intent, out RaiseI
{
lock (_raiseIntentInvocationsLock)
{
var raiseIntentInvocations = _raiseIntentResolutions.Where(raiseIntentInvocation => raiseIntentInvocation.RaiseIntentMessageId == messageId && raiseIntentInvocation.Intent == intent);
var raiseIntentInvocations = _raiseIntentResolutions.Where(raiseIntentInvocation =>
raiseIntentInvocation.RaiseIntentMessageId == messageId
&& raiseIntentInvocation.Intent == intent
&& raiseIntentInvocation.IsResolved);

if (raiseIntentInvocations.Any())
{
if (raiseIntentInvocations.Count() > 1)
Expand Down
Loading

0 comments on commit eadcf64

Please sign in to comment.