Skip to content

Commit

Permalink
SubsystemLauncher, SubsystemLauncherCommunicator API changes added Su…
Browse files Browse the repository at this point in the history
…bsystemLAuncher tests
  • Loading branch information
lilla28 committed Apr 4, 2023
1 parent cd1ff00 commit bab2af6
Show file tree
Hide file tree
Showing 8 changed files with 394 additions and 159 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,67 +14,57 @@ namespace ProcessExplorer.Abstractions.Subsystems;

public interface ISubsystemLauncher
{
/// <summary>
/// Sets the available subsystems, which are initialized through ModuleLoader.
/// </summary>
/// <param name="subsystems"></param>
void SetSubsystems(ReadOnlySpan<KeyValuePair<Guid, SubsystemInfo>> subsystems);

/// <summary>
/// Modifies a state of a subsystem in the subsystemController's list.
/// </summary>
/// <param name="subsystemId"></param>
/// <param name="state"></param>
/// <returns></returns>
Task ModifySubsystemState(Guid subsystemId, string state);

/// <summary>
/// Sends launch command to the subsystem module via the selected communication route.
/// </summary>
/// <param name="subsystemId"></param>
/// <param name="subsystemName"></param>
/// <returns></returns>
Task<string> LaunchSubsystem(Guid subsystemId);
Task<string> LaunchSubsystem(Guid subsystemId, string subsystemName);

/// <summary>
/// Sends launch command (after a timeperiod) to the subsystem module via the selected communication route.
/// </summary>
/// <param name="subsystemId"></param>
/// <param name="subsystemName"></param>
/// <param name="periodOfTime"></param>
/// <returns></returns>
Task<string> LaunchSubsystemAfterTime(Guid subsystemId, int periodOfTime);
Task<string> LaunchSubsystemAfterTime(Guid subsystemId, string subsystemName, int periodOfTime);

/// <summary>
/// Sends launch command to the subsystem module via the selected communication route.
/// </summary>
/// <param name="subsystems"></param>
/// <returns></returns>
Task<IEnumerable<KeyValuePair<Guid, string>>> LaunchSubsystems(IEnumerable<Guid> subsystems);
Task<IEnumerable<KeyValuePair<Guid, string>>> LaunchSubsystems(IEnumerable<KeyValuePair<Guid, string>> subsystems);

/// <summary>
/// Sends restart command to the subsystem module via the selected communication route.
/// </summary>
/// <param name="subsystemId"></param>
/// <param name="subsystemName"></param>
/// <returns></returns>
Task<string> RestartSubsystem(Guid subsystemId);
Task<string> RestartSubsystem(Guid subsystemId, string subsystemName);

/// <summary>
/// Sends restart command to the subsystem module via the selected communication route.
/// </summary>
/// <param name="subsystems"></param>
/// <returns></returns>
Task<IEnumerable<KeyValuePair<Guid, string>>> RestartSubsystems(IEnumerable<Guid> subsystems);
Task<IEnumerable<KeyValuePair<Guid, string>>> RestartSubsystems(IEnumerable<KeyValuePair<Guid, string>> subsystems);

/// <summary>
/// Sends shutdown command to the subsystem module via the selected communication route.
/// </summary>
/// <param name="subsystemId"></param>
/// <param name="subsystemName"></param>
/// <returns></returns>
Task<string> ShutdownSubsystem(Guid subsystemId);
Task<string> ShutdownSubsystem(Guid subsystemId, string subsystemName);

/// <summary>
/// Sends shutdown command to the subsystem module via the selected communication route.
/// </summary>
/// <param name="subsystems"></param>
/// <returns></returns>
Task<IEnumerable<KeyValuePair<Guid, string>>> ShutdownSubsystems(IEnumerable<Guid> subsystems);
Task<IEnumerable<KeyValuePair<Guid, string>>> ShutdownSubsystems(IEnumerable<KeyValuePair<Guid, string>> subsystems);
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,29 @@ public interface ISubsystemLauncherCommunicator
/// Sends a launch request to the launcher, which will launch the subsystem after the given timeperiod.
/// </summary>
/// <param name="subsystemId"></param>
/// <param name="subsystemName"></param>
/// <param name="periodOfTime"></param>
/// <returns>A state of the sent subsystem, regarding the success of the launch.</returns>
Task SendLaunchSubsystemAfterTimeRequest(Guid subsystemId, int periodOfTime);
Task SendLaunchSubsystemAfterTimeRequest(Guid subsystemId, string subsystemName, int periodOfTime);

/// <summary>
/// Sends launch requests to the launcher.
/// </summary>
/// <param name="subsystems"></param>
/// <returns>A list of the states of the sent subsystems, regarding the success of the launches.</returns>
Task SendLaunchSubsystemsRequest(IEnumerable<Guid> subsystems);
Task SendLaunchSubsystemsRequest(IEnumerable<KeyValuePair<Guid, string>> subsystems);

/// <summary>
/// Sends restart requests to the launcher.
/// </summary>
/// <param name="subsystems"></param>
/// <returns>A list of the states of the sent subsystems, regarding the success of the restarts.</returns>
Task SendRestartSubsystemsRequest(IEnumerable<Guid> subsystems);
Task SendRestartSubsystemsRequest(IEnumerable<KeyValuePair<Guid, string>> subsystems);

/// <summary>
/// Sends shutdown requests to the launcher.
/// </summary>
/// <param name="subsystems"></param>
/// <returns>A list of the states of the sent subsystems, regarding the success of the shutdowns.</returns>
Task SendShutdownSubsystemsRequest(IEnumerable<Guid> subsystems);
Task SendShutdownSubsystemsRequest(IEnumerable<KeyValuePair<Guid, string>> subsystems);
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ public SubsystemController(
public async Task InitializeSubsystems(IEnumerable<KeyValuePair<Guid, SubsystemInfo>> subsystems)
{
UpdateOrAddElements(subsystems);

await SendModifiedSubsystems();
await LaunchSubsystemsAutomatically();
}
Expand All @@ -62,23 +61,22 @@ public async Task LaunchAllRegisteredSubsystem()
{
try
{
var guids = Enumerable.Empty<Guid>();
var subsystemIds = Enumerable.Empty<KeyValuePair<Guid, string>>();

lock (_subsystemLock)
{
if (_subsystems.Any())
{
//Must have key
guids = _subsystems
.Select(subsystem => subsystem.Key);
subsystemIds = _subsystems
.Select(subsystem => new KeyValuePair<Guid, string>(subsystem.Key, subsystem.Value.Name));
}

if (!guids.Any()) return;
if (!subsystemIds.Any()) return;
}

await SendRequest(
_subsystemLauncher.LaunchSubsystems(guids),
_subsystemLauncherCommunicator.SendLaunchSubsystemsRequest(guids));
_subsystemLauncher.LaunchSubsystems(subsystemIds),
_subsystemLauncherCommunicator.SendLaunchSubsystemsRequest(subsystemIds));

}
catch (Exception exception)
Expand All @@ -89,9 +87,16 @@ await SendRequest(

public async Task LaunchSubsystemAfterTime(Guid subsystemId, int periodOfTime)
{
SubsystemInfo? subsystem;

lock (_subsystemLock)
if(!_subsystems.TryGetValue(subsystemId, out subsystem)) return;

if (subsystem == null) return;

await SendRequest(
_subsystemLauncherCommunicator.SendLaunchSubsystemAfterTimeRequest(subsystemId, periodOfTime),
_subsystemLauncher.LaunchSubsystemAfterTime(subsystemId, periodOfTime));
_subsystemLauncherCommunicator.SendLaunchSubsystemAfterTimeRequest(subsystemId, subsystem.Name, periodOfTime),
_subsystemLauncher.LaunchSubsystemAfterTime(subsystemId, subsystem.Name, periodOfTime));
}

private Task SendRequest(
Expand Down Expand Up @@ -120,6 +125,7 @@ public async Task LaunchSubsystemAutomatically(Guid subsystemId)
public async Task LaunchSubsystemsAutomatically()
{
var subsystemIds = new SynchronizedCollection<string>();

lock (_subsystemLock)
{
if (_subsystems.Any())
Expand All @@ -141,70 +147,87 @@ public async Task LaunchSubsystems(IEnumerable<string> subsystems)
{
try
{
if (subsystems.Any())
{
var subsystemIds = subsystems
.Select(id => new Guid(id));
var ids = new Dictionary<Guid, string>();

await SendRequest(
_subsystemLauncherCommunicator.SendLaunchSubsystemsRequest(subsystemIds),
_subsystemLauncher.LaunchSubsystems(subsystemIds));
if (!subsystems.Any()) return;

lock (_subsystemLock)
{
foreach (var id in subsystems)
{
var guidId = Guid.Parse(id);
if(!_subsystems.TryGetValue(guidId, out var subsystem)) continue;
ids.Add(guidId, subsystem.Name);
}
}

await SendRequest(
_subsystemLauncherCommunicator.SendLaunchSubsystemsRequest(ids),
_subsystemLauncher.LaunchSubsystems(ids));
}
catch (Exception exception)
{
_logger.CannotSendLaunchRequestError(exception);
}
}

private KeyValuePair<Guid, string?> GetSubsystem(string subsystemId)
{
SubsystemInfo? subsystem;
var guidId = Guid.Parse(subsystemId);

lock (_subsystemLock)
{
if (!_subsystems.TryGetValue(guidId, out subsystem)) return default;
}

return new(guidId, subsystem.Name);
}

public async Task LaunchSubsystem(string subsystemId)
{
var subsystem = GetSubsystem(subsystemId);
if(subsystem.Value == null) return;

await SendRequest(
_subsystemLauncherCommunicator.SendLaunchSubsystemsRequest(new List<Guid> { new(subsystemId) }),
_subsystemLauncher.LaunchSubsystem(new Guid(subsystemId)));
_subsystemLauncherCommunicator.SendLaunchSubsystemsRequest(new Dictionary<Guid, string> { { subsystem.Key, subsystem.Value } }),
_subsystemLauncher.LaunchSubsystem(subsystem.Key, subsystem.Value));
}

public async Task RestartSubsystems(IEnumerable<string> subsystems)
{
foreach (var subsystemId in subsystems)
{
bool succeed;
SubsystemInfo? registeredSubsystem;

lock (_subsystemLock)
succeed = _subsystems.TryGetValue(Guid.Parse(subsystemId), out registeredSubsystem);

if (registeredSubsystem == null || !succeed) continue;
var subsystem = GetSubsystem(subsystemId);
if (subsystem.Value == null) return;

await SendRequest(
_subsystemLauncherCommunicator.SendRestartSubsystemsRequest(new List<Guid> { new(subsystemId) }),
_subsystemLauncher.RestartSubsystem(new Guid(subsystemId)));
_subsystemLauncherCommunicator.SendRestartSubsystemsRequest(new Dictionary<Guid, string> { { subsystem.Key, subsystem.Value } }),
_subsystemLauncher.RestartSubsystem(subsystem.Key, subsystem.Value));
}
}

public async Task RestartSubsystem(string subsystemId)
{
var subsystem = GetSubsystem(subsystemId);
if (subsystem.Value == null) return;

await SendRequest(
_subsystemLauncherCommunicator.SendRestartSubsystemsRequest(new List<Guid> { new(subsystemId) }),
_subsystemLauncher.RestartSubsystem(new Guid(subsystemId)));
_subsystemLauncherCommunicator.SendRestartSubsystemsRequest(new Dictionary<Guid, string> { { subsystem.Key, subsystem.Value } }),
_subsystemLauncher.RestartSubsystem(subsystem.Key, subsystem.Value));
}

public async Task ShutdownAllRegisteredSubsystem()
{
try
{
IEnumerable<KeyValuePair<Guid, SubsystemInfo>> copySubsystems;

lock (_subsystemLock)
{
copySubsystems = _subsystems.ToList();
}

var copySubsystems = GetCopySubsystems();

if (copySubsystems.Any())
{
await SendRequest(
_subsystemLauncherCommunicator.SendShutdownSubsystemsRequest(copySubsystems.Select(element => element.Key)),
_subsystemLauncher.ShutdownSubsystems(copySubsystems.Select(x => x.Key)));
_subsystemLauncherCommunicator.SendShutdownSubsystemsRequest(copySubsystems),
_subsystemLauncher.ShutdownSubsystems(copySubsystems));
}
}
catch (Exception exception)
Expand All @@ -213,23 +236,42 @@ await SendRequest(
}
}

private IEnumerable<KeyValuePair<Guid, string>> GetCopySubsystems()
{
IEnumerable<KeyValuePair<Guid, string>> copySubsystems = Enumerable.Empty<KeyValuePair<Guid, string>>();

lock (_subsystemLock)
{
copySubsystems = _subsystems.Select(subsystem => new KeyValuePair<Guid, string>(subsystem.Key, subsystem.Value.Name));
}

return copySubsystems;
}

public async Task ShutdownSubsystem(string subsystemId)
{
var subsystem = GetSubsystem(subsystemId);
if (subsystem.Value == null) return;

await SendRequest(
_subsystemLauncherCommunicator.SendShutdownSubsystemsRequest(new List<Guid>() { new Guid(subsystemId) }),
_subsystemLauncher.ShutdownSubsystem(new Guid(subsystemId)));
_subsystemLauncherCommunicator.SendShutdownSubsystemsRequest(new Dictionary<Guid, string>() { { subsystem.Key, subsystem.Value } }),
_subsystemLauncher.ShutdownSubsystem(subsystem.Key, subsystem.Value));
}

public async Task ShutdownSubsystems(IEnumerable<string> subsystems)
{
try
{
var subsystemIds = subsystems
.Select(id => new Guid(id));
IEnumerable<KeyValuePair<Guid, string>> copySubsystems;

lock (_subsystemLock)
{
copySubsystems = _subsystems.Select(subsystem => new KeyValuePair<Guid, string>(subsystem.Key, subsystem.Value.Name));
}

await SendRequest(
_subsystemLauncherCommunicator.SendShutdownSubsystemsRequest(subsystemIds),
_subsystemLauncher.ShutdownSubsystems(subsystemIds));
_subsystemLauncherCommunicator.SendShutdownSubsystemsRequest(copySubsystems),
_subsystemLauncher.ShutdownSubsystems(copySubsystems));
}
catch (Exception exception)
{
Expand Down Expand Up @@ -369,7 +411,7 @@ public void SetUiDelegate(Func<Func<IUIHandler, Task>, Task> updateInfoOnUI)

public IEnumerable<KeyValuePair<Guid, SubsystemInfo>> GetSubsystems()
{
lock(_subsystemLock)
lock (_subsystemLock)
return _subsystems;
}
}
Loading

0 comments on commit bab2af6

Please sign in to comment.