Skip to content

Commit

Permalink
Added tests, IUIHandler fix, renaming files
Browse files Browse the repository at this point in the history
  • Loading branch information
lilla28 committed Apr 20, 2023
1 parent bab2af6 commit c2196af
Show file tree
Hide file tree
Showing 36 changed files with 2,085 additions and 617 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ProcessExplorer.Core.Tests"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ProcessExplorer.Server.IntegrationTests", "test\ProcessExplorer.Server.EndToEndTests\ProcessExplorer.Server.IntegrationTests.csproj", "{18B9042E-0D77-4917-97FF-14BC34085385}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestConsoleApp", "test\TestConsoleApp\TestConsoleApp.csproj", "{B9A770BB-7FBE-46A9-A62E-5E7FDAD8EAA3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestConsoleApp2", "test\TestConsoleApp2\TestConsoleApp2.csproj", "{F9BCB268-66F2-4551-ABB6-58E1843B5A64}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "TestApplications", "TestApplications", "{23E408F5-CA2E-4CBD-BAB1-48FFAC9F5357}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -51,6 +57,14 @@ Global
{18B9042E-0D77-4917-97FF-14BC34085385}.Debug|Any CPU.Build.0 = Debug|Any CPU
{18B9042E-0D77-4917-97FF-14BC34085385}.Release|Any CPU.ActiveCfg = Release|Any CPU
{18B9042E-0D77-4917-97FF-14BC34085385}.Release|Any CPU.Build.0 = Release|Any CPU
{B9A770BB-7FBE-46A9-A62E-5E7FDAD8EAA3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B9A770BB-7FBE-46A9-A62E-5E7FDAD8EAA3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B9A770BB-7FBE-46A9-A62E-5E7FDAD8EAA3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B9A770BB-7FBE-46A9-A62E-5E7FDAD8EAA3}.Release|Any CPU.Build.0 = Release|Any CPU
{F9BCB268-66F2-4551-ABB6-58E1843B5A64}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F9BCB268-66F2-4551-ABB6-58E1843B5A64}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F9BCB268-66F2-4551-ABB6-58E1843B5A64}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F9BCB268-66F2-4551-ABB6-58E1843B5A64}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -62,6 +76,9 @@ Global
{51110571-1082-4790-AA00-3DDDEE80641A} = {3413C687-1DD1-4751-8C4F-0CD308248CB2}
{3C2B4F9E-CA84-4FB1-AE2B-CC61886A3CE7} = {55657FBD-CD23-40A2-B8E9-2BBE734924CC}
{18B9042E-0D77-4917-97FF-14BC34085385} = {55657FBD-CD23-40A2-B8E9-2BBE734924CC}
{B9A770BB-7FBE-46A9-A62E-5E7FDAD8EAA3} = {23E408F5-CA2E-4CBD-BAB1-48FFAC9F5357}
{F9BCB268-66F2-4551-ABB6-58E1843B5A64} = {23E408F5-CA2E-4CBD-BAB1-48FFAC9F5357}
{23E408F5-CA2E-4CBD-BAB1-48FFAC9F5357} = {55657FBD-CD23-40A2-B8E9-2BBE734924CC}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {819F883C-DDD0-4F2B-82C5-820D4D6AB446}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using LocalCollector.Modules;
using LocalCollector.Registrations;
using ProcessExplorer.Abstractions.Infrastructure;
using ProcessExplorer.Abstractions.Processes;
using ProcessExplorer.Abstractions.Subsystems;

namespace ProcessExplorer.Abstractions;
Expand All @@ -36,6 +37,11 @@ public interface IProcessInfoAggregator : IDisposable
/// </summary>
public ISubsystemController? SubsystemController { get; }

/// <summary>
/// Handles the communication between the server and clients.
/// </summary>
public IUiHandler UiHandler { get; }

/// <summary>
/// Removes a module information from the collection.
/// </summary>
Expand All @@ -60,20 +66,7 @@ public interface IProcessInfoAggregator : IDisposable
/// </summary>
/// <param name="delay"></param>
void SetDeadProcessRemovalDelay(int delay);

/// <summary>
/// Adds a UIClient to the collection. Keeps track of the UIClients.
/// </summary>
/// <param name="id"></param>
/// <param name="uiHandler"></param>
void AddUiConnection(Guid id, IUIHandler uiHandler);

/// <summary>
/// Removes a UIClient from the collection.
/// </summary>
/// <param name="handler"></param>
void RemoveUiConnection(KeyValuePair<Guid, IUIHandler> handler);


/// <summary>
/// Enables to watch processes through ProcessMonitor.
/// Only available for Windows OS.
Expand Down Expand Up @@ -103,13 +96,7 @@ public interface IProcessInfoAggregator : IDisposable
/// </summary>
/// <returns></returns>
IEnumerable<KeyValuePair<string, ProcessInfoCollectorData>> GetRuntimeInformation();

/// <summary>
/// Returns the connected clients.
/// </summary>
/// <returns></returns>
IEnumerable<KeyValuePair<Guid, IUIHandler>> GetUiClients();


/// <summary>
/// Adds a runtime information to the collection.
/// </summary>
Expand Down Expand Up @@ -160,9 +147,15 @@ public interface IProcessInfoAggregator : IDisposable
Task AddProcesses(ReadOnlySpan<int> processIds);

/// <summary>
/// Asynchronusly dequeue the changes of the registered subsystems, and send to the initialized UI's.
/// Asynchronously dequeue the changes of the registered subsystems, and send to the initialized UI's.
/// </summary>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task RunSubsystemStateQueue(CancellationToken cancellationToken);

/// <summary>
/// Returns the initialized processes.
/// </summary>
/// <returns></returns>
IEnumerable<ProcessInfoData> GetProcesses();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Morgan Stanley makes this available to you under the Apache License,
// Version 2.0 (the "License"). You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0.
//
// See the NOTICE file distributed with this work for additional information
// regarding copyright ownership. Unless required by applicable law or agreed
// to in writing, software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
// or implied. See the License for the specific language governing permissions
// and limitations under the License.

namespace ProcessExplorer.Abstractions.Infrastructure;

public interface IClientConnection<T>
{
Task SendMessage(T message);
}

Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@

namespace ProcessExplorer.Abstractions.Infrastructure;

public interface IUIHandler
public interface IUiHandler
{
void AddClientConnection<T>(Guid id, IClientConnection<T> connection);
void RemoveClientConnection(Guid id);

/// <summary>
/// Adds a list of processes to the collection.
/// </summary>
Expand Down Expand Up @@ -135,4 +138,10 @@ public interface IUIHandler
/// <param name="process"></param>
/// <returns></returns>
Task UpdateProcessStatus(KeyValuePair<int, Status> process);

/// <summary>
/// Updates the client that the connection has been established.
/// </summary>
/// <returns></returns>
Task SubscriptionIsAliveUpdate();
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public static partial class SourceGeneratedLoggerExtensions
[LoggerMessage(Level = LogLevel.Error, Message = "Cannot send launch request to the SubsystemLauncher. Detailed exception: `{exception}`", SkipEnabledCheck = true)]
private static partial void CannotSendLaunchRequest(this ILogger logger, Exception ex, Exception exception);

[LoggerMessage(Level = LogLevel.Error, Message = "Cannot terminate a subsystem. Detailed exception: `{exception}`", SkipEnabledCheck = true)]
[LoggerMessage(Level = LogLevel.Error, Message = "Cannot terminate a subsystem/subsystems. Detailed exception: `{exception}`", SkipEnabledCheck = true)]
private static partial void CannotTerminateSubsystem(this ILogger logger, Exception ex, Exception exception);

[LoggerMessage(Level = LogLevel.Debug, Message = "The Process Explorer list is initialized", SkipEnabledCheck = false)]
Expand All @@ -88,6 +88,9 @@ public static partial class SourceGeneratedLoggerExtensions
[LoggerMessage(Level = LogLevel.Warning, Message = "The process `{pid}` does not exist in the ProcessMonitor list", SkipEnabledCheck = false)]
public static partial void ProcessNotFoundWarning(this ILogger logger, int pid);

[LoggerMessage(Level = LogLevel.Warning, Message = "The subsystem with `{id}` couldn't be terminated. Subsystem name: `{name}`", SkipEnabledCheck = false)]
public static partial void SubsystemTerminationAddWarning(this ILogger logger, string id, string name);

[LoggerMessage(Level = LogLevel.Error, Message = "Couldn't read the child process. Detailed exception: `{exception}`", SkipEnabledCheck = true)]
private static partial void ManagementObjectChild(this ILogger logger, Exception ex, Exception exception);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Google.Protobuf" Version="3.22.1" />
<PackageReference Include="Google.Protobuf" Version="3.22.0" />
<PackageReference Include="Grpc" Version="2.46.6" />
<PackageReference Include="Grpc.Tools" Version="2.53.0">
<PackageReference Include="Grpc.Tools" Version="2.50.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,14 @@

namespace ProcessExplorer.Abstractions.Processes;

//TODO(Lilla): Add description
public interface IProcessInfoManager : IDisposable
//TODO(Lilla): Add description, should be async
public interface IProcessInfoMonitor : IDisposable
{
ReadOnlySpan<int> AddChildProcesses(int processId, string? processName);
void AddProcess(int processId);
bool CheckIfIsComposeProcess(int processId);
void ClearProcessIds();
bool ContainsId(int processId);
float GetCpuUsage(int processId, string processName);
float GetMemoryUsage(int processId, string processName);
int? GetParentId(int processId, string processName);
ReadOnlySpan<int> GetProcessIds();
void RemoveProcessId(int processId);
void SendNewProcessUpdate(int processId);
void SendProcessModifiedUpdate(int processId);
void SendTerminatedProcessUpdate(int processId);
void SetHandlers(
ProcessModifiedHandler processModifiedHandler,
ProcessTerminatedHandler processTerminatedHandler,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

namespace ProcessExplorer.Abstractions.Processes;

public abstract class ProcessInfoManager : IProcessInfoManager
public abstract class ProcessInfoMonitor : IProcessInfoMonitor
{
private ProcessCreatedHandler? _processCreatedHandler;
private ProcessModifiedHandler? _processModifiedHandler;
Expand All @@ -31,7 +31,7 @@ public abstract class ProcessInfoManager : IProcessInfoManager
private readonly ObservableCollection<int> _processIds = new();
private readonly object _locker = new();

public ProcessInfoManager(ILogger? logger)
public ProcessInfoMonitor(ILogger? logger)
{
_logger = logger ?? NullLogger.Instance;
_processIds.CollectionChanged += ProcessIdsChanged;
Expand Down Expand Up @@ -109,9 +109,20 @@ public void SetProcessIds(
{
foreach (var id in processIds)
{
if (_processIds.Contains(id)) continue;
_processIds.Add(id);
AddChildProcesses(id, Process.GetProcessById(id).ProcessName);
try
{
if (_processIds.Contains(id)) continue;

var process = Process.GetProcessById(id);

_processIds.Add(id);

AddChildProcesses(id, process.ProcessName);
}
catch (Exception exception)
{
_logger.ProcessExpected(exception);
}
}

if (mainProcessId != 0 && !_processIds.Contains(mainProcessId))
Expand Down Expand Up @@ -188,8 +199,6 @@ private bool IsComposeProcess(int processId)
/// <summary>
/// Searches for child processes to watch.
/// </summary>
/// <param name="processInfo"></param>
/// <returns></returns>
public abstract ReadOnlySpan<int> AddChildProcesses(int processId, string? processName);

/// <summary>
Expand All @@ -201,20 +210,6 @@ public bool CheckIfIsComposeProcess(int processId)
return IsComposeProcess(processId);
}

public void SendNewProcessUpdate(int processId)
{
_processCreatedHandler?.Invoke(processId);
}

/// <summary>
/// Sends a terminated process information to publish
/// </summary>
/// <param name="processId"></param>
public void SendTerminatedProcessUpdate(int processId)
{
ProcessTerminated(processId);
}

/// <summary>
/// Sends a modified process information to publish
/// </summary>
Expand All @@ -224,7 +219,7 @@ public void SendProcessModifiedUpdate(int processId)
_processModifiedHandler?.Invoke(processId);
}

~ProcessInfoManager()
~ProcessInfoMonitor()
{
Dispose();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public interface ISubsystemController
/// Sets the updating method for the UI's.
/// </summary>
/// <param name="updateInfoOnUI"></param>
void SetUiDelegate(Func<Func<IUIHandler, Task>, Task> updateInfoOnUI);
void SetUiDelegate(Func<Func<IUiHandler, Task>, Task> updateInfoOnUI);

/// <summary>
/// It will send a launch request for a subsystem.
Expand Down Expand Up @@ -124,7 +124,7 @@ public interface ISubsystemController
/// </summary>
/// <param name="subsystemId"></param>
/// <returns></returns>
void RemoveSubsystem(Guid subsystemId);
Task RemoveSubsystem(Guid subsystemId);

/// <summary>
/// Modifies a state of a subsystem with the given data. Send update to the registered UIs.
Expand All @@ -134,13 +134,6 @@ public interface ISubsystemController
/// <returns></returns>
Task ModifySubsystemState(Guid subsystemId, string state);

/// <summary>
/// Sends the subsystems to the defined Ui's.
/// </summary>
/// <param name="handlers"></param>
/// <returns></returns>
Task SendInitializedSubsystemInfoToUis(ReadOnlySpan<IUIHandler> handlers);

/// <summary>
/// Returns the initialized subsystems.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@
using ProcessExplorer.Abstractions;
using ProcessExplorer.Abstractions.Processes;
using ProcessExplorer.Abstractions.Subsystems;
using ProcessExplorer.Core;
using ProcessExplorer.Core.DependencyInjection;
using ProcessExplorer.Core.Processes;
using ProcessExplorer.Core.Subsystems;

namespace ProcessExplorer.Server.DependencyInjection;
namespace ProcessExplorer.Core.DependencyInjection;

public static class ServiceCollectionProcessExplorerExtensions
{
Expand All @@ -35,7 +33,7 @@ public static IServiceCollection AddProcessMonitorWindows(
this IServiceCollection serviceCollection)
{
#pragma warning disable CA1416 // Validate platform compatibility
serviceCollection.TryAddSingleton<IProcessInfoManager, WindowsProcessInfoManager>();
serviceCollection.TryAddSingleton<IProcessInfoMonitor, WindowsProcessInfoMonitor>();
#pragma warning restore CA1416 // Validate platform compatibility
return serviceCollection;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,20 @@

using Microsoft.Extensions.Logging;
using ProcessExplorer.Abstractions;
using ProcessExplorer.Abstractions.Infrastructure;
using ProcessExplorer.Abstractions.Processes;
using ProcessExplorer.Abstractions.Subsystems;

namespace ProcessExplorer.Core.Factories;

public static class ProcessAggregatorFactory
{
public static IProcessInfoAggregator CreateProcessInfoAggregator(ILogger<IProcessInfoAggregator> logger, ProcessInfoManager processInfoManager, ISubsystemController? subsystemController = null)
public static IProcessInfoAggregator CreateProcessInfoAggregator(
ProcessInfoMonitor processInfoManager,
IUiHandler handler,
ISubsystemController? subsystemController = null,
ILogger<IProcessInfoAggregator> logger = null)
{
return new ProcessInfoAggregator(logger, processInfoManager, subsystemController);
return new ProcessInfoAggregator(processInfoManager, handler, subsystemController, logger);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
namespace ProcessExplorer.Core.Factories;
public static class ProcessMonitorFactory
{
public static ProcessInfoManager CreateProcessInfoGeneratorWindows(ILogger<ProcessInfoManager> logger)
public static ProcessInfoMonitor CreateProcessInfoGeneratorWindows(ILogger<ProcessInfoMonitor> logger)
{
#pragma warning disable CA1416 // Validate platform compatibility
return new WindowsProcessInfoManager(logger);
return new WindowsProcessInfoMonitor(logger);
#pragma warning restore CA1416 // Validate platform compatibility
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ public Communicator(IProcessInfoAggregator processAggregator)
_aggregator = processAggregator;
}

public void SetProcessInfoAggregator(IProcessInfoAggregator aggregator)
{
_aggregator = aggregator;
}

public async ValueTask AddRuntimeInfo(IEnumerable<KeyValuePair<AssemblyInformation, ProcessInfoCollectorData>> listOfRuntimeInfos)
{
if (listOfRuntimeInfos == null) return;
Expand Down
Loading

0 comments on commit c2196af

Please sign in to comment.