Skip to content

Commit

Permalink
rename Endpoint.. to Contract..
Browse files Browse the repository at this point in the history
- simplifications
  • Loading branch information
eduard-dumitru committed Nov 27, 2024
1 parent 780752d commit 99b8c7c
Show file tree
Hide file tree
Showing 22 changed files with 101 additions and 342 deletions.
2 changes: 1 addition & 1 deletion src/Playground/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private static async Task Main(string[] args)
Endpoints = new()
{
typeof(Contracts.IServerOperations), // DEVINE
new EndpointSettings(typeof(Contracts.IServerOperations)) // ASTALALT
new ContractSettings(typeof(Contracts.IServerOperations)) // ASTALALT
{
BeforeIncomingCall = async (callInfo, _) =>
{
Expand Down
1 change: 1 addition & 0 deletions src/UiPath.CoreIpc/Client/ServiceClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ private Connection? LatestConnection
public ServiceClientProper(IpcClient client, Type interfaceType) : base(interfaceType)
{
_client = client;
client.Transport.Validate();
_clientState = client.Transport.CreateState();
}

Expand Down
16 changes: 0 additions & 16 deletions src/UiPath.CoreIpc/Config/ClientConfig.cs

This file was deleted.

3 changes: 3 additions & 0 deletions src/UiPath.CoreIpc/Config/ClientTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

public abstract record ClientTransport
{
private protected ClientTransport() { }

internal abstract IClientState CreateState();

internal abstract void Validate();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,19 @@

namespace UiPath.Ipc;

public class EndpointCollection : IEnumerable<EndpointSettings>
public class ContractCollection : IEnumerable<ContractSettings>
{
internal readonly Dictionary<Type, EndpointSettings> Endpoints = new();
internal readonly Dictionary<Type, ContractSettings> Endpoints = new();

public void Add(Type type) => Add(type, instance: null);
public void Add(Type contractType, object? instance) => Add(new EndpointSettings(contractType, instance));
public void Add(EndpointSettings endpointSettings)
public void Add(Type contractType) => Add(contractType, instance: null);
public void Add(Type contractType, object? instance) => Add(new ContractSettings(contractType, instance));
public void Add(ContractSettings endpointSettings)
{
if (endpointSettings is null) throw new ArgumentNullException(nameof(endpointSettings));
// endpointSettings.Validate();

Endpoints[endpointSettings.Service.Type] = endpointSettings;
}
public IEnumerator<EndpointSettings> GetEnumerator() => Endpoints.Values.GetEnumerator();
public IEnumerator<ContractSettings> GetEnumerator() => Endpoints.Values.GetEnumerator();

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
28 changes: 6 additions & 22 deletions src/UiPath.CoreIpc/Config/IpcClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public sealed class IpcClient : IpcBase, IClientConfig
{
public EndpointCollection? Callbacks { get; set; }
public ContractCollection? Callbacks { get; set; }

public ILogger? Logger { get; init; }
public BeforeConnectHandler? BeforeConnect { get; set; }
Expand All @@ -23,24 +23,6 @@ private ServiceClient GetServiceClient(Type proxyType)
}
public TProxy GetProxy<TProxy>() where TProxy : class => GetServiceClient(typeof(TProxy)).GetProxy<TProxy>();

// TODO: should decommission?
internal void Validate()
{
var haveDeferredInjectedCallbacks = Callbacks?.Any(x => x.Service.MaybeGetServiceProvider() is null && x.Service.MaybeGetInstance() is null) ?? false;

if (haveDeferredInjectedCallbacks && ServiceProvider is null)
{
throw new InvalidOperationException("ServiceProvider is required when you register injectable callbacks. Consider registering a callback instance.");
}

if (Transport is null)
{
throw new InvalidOperationException($"{Transport} is required.");
}

Transport.Validate();
}

internal ILogger? GetLogger(string name)
{
if (Logger is not null)
Expand All @@ -59,9 +41,11 @@ internal void Validate()
internal RouterConfig CreateCallbackRouterConfig()
=> RouterConfig.From(
Callbacks.OrDefault(),
endpoint => endpoint with
endpoint =>
{
BeforeIncomingCall = null, // callbacks don't support BeforeCall
Scheduler = endpoint.Scheduler ?? Scheduler
var clone = new ContractSettings(endpoint);
clone.BeforeIncomingCall = null; // callbacks don't support BeforeIncomingCall
clone.Scheduler ??= Scheduler;
return clone;
});
}
8 changes: 5 additions & 3 deletions src/UiPath.CoreIpc/Config/IpcServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace UiPath.Ipc;

public sealed class IpcServer : IpcBase, IAsyncDisposable
{
public required EndpointCollection Endpoints { get; init; }
public required ContractCollection Endpoints { get; init; }
public required ServerTransport Transport { get; init; }

private readonly object _lock = new();
Expand Down Expand Up @@ -87,9 +87,11 @@ private void OnNewConnectionError(Exception ex)

internal RouterConfig CreateRouterConfig(IpcServer server) => RouterConfig.From(
server.Endpoints,
endpoint => endpoint with
endpoint =>
{
Scheduler = endpoint.Scheduler ?? server.Scheduler
var clone = new ContractSettings(endpoint);
clone.Scheduler ??= server.Scheduler;
return clone;
});

private sealed class ObserverAdapter<T> : IObserver<T>
Expand Down
2 changes: 2 additions & 0 deletions src/UiPath.CoreIpc/Config/ServerTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ namespace UiPath.Ipc;

public abstract class ServerTransport
{
private protected ServerTransport() { }

public int ConcurrentAccepts { get; set; } = 5;
public byte MaxReceivedMessageSizeInMegabytes { get; set; } = 2;

Expand Down
2 changes: 1 addition & 1 deletion src/UiPath.CoreIpc/GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
global using BeforeCallHandler = System.Func<UiPath.Ipc.CallInfo, System.Threading.CancellationToken, System.Threading.Tasks.Task>;
global using InvokeDelegate = System.Func<UiPath.Ipc.ServiceClient, System.Reflection.MethodInfo, object?[], object?>;
global using Accept = System.Func<System.Threading.CancellationToken, System.Threading.Tasks.Task<System.Net.WebSockets.WebSocket>>;
global using ContractToSettingsMap = System.Collections.Generic.Dictionary<string, UiPath.Ipc.EndpointSettings>;
global using ContractToSettingsMap = System.Collections.Generic.Dictionary<string, UiPath.Ipc.ContractSettings>;
global using AccessControlDelegate = System.Action<System.IO.Pipes.PipeSecurity>;
2 changes: 1 addition & 1 deletion src/UiPath.CoreIpc/Helpers/DefaultsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ internal static class DefaultsExtensions
public static BeforeCallHandler OrDefault(this BeforeCallHandler? beforeCallHandler) => beforeCallHandler ?? DefaultBeforeCallHandler;
public static TaskScheduler OrDefault(this TaskScheduler? scheduler) => scheduler ?? TaskScheduler.Default;
public static ContractToSettingsMap OrDefault(this ContractToSettingsMap? map) => map ?? EmptyContractToSettingsMap;
public static EndpointCollection OrDefault(this EndpointCollection? endpoints) => endpoints ?? new();
public static ContractCollection OrDefault(this ContractCollection? endpoints) => endpoints ?? new();

public static Func<T>? MaybeCreateServiceFactory<T>(this IServiceProvider? serviceProvider) where T : class
{
Expand Down
6 changes: 3 additions & 3 deletions src/UiPath.CoreIpc/Helpers/Router.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace UiPath.Ipc;

internal readonly record struct RouterConfig(IReadOnlyDictionary<string, EndpointSettings> Endpoints)
internal readonly record struct RouterConfig(IReadOnlyDictionary<string, ContractSettings> Endpoints)
{
public static RouterConfig From(EndpointCollection endpoints, Func<EndpointSettings, EndpointSettings> transform)
public static RouterConfig From(ContractCollection endpoints, Func<ContractSettings, ContractSettings> transform)
{
ContractToSettingsMap nameToEndpoint = [];

Expand Down Expand Up @@ -127,7 +127,7 @@ public override ServiceFactory WithProvider(IServiceProvider? serviceProvider)

internal readonly struct Route
{
public static Route From(IServiceProvider? serviceProvider, EndpointSettings endpointSettings)
public static Route From(IServiceProvider? serviceProvider, ContractSettings endpointSettings)
=> new Route()
{
Service = endpointSettings.Service.WithProvider(serviceProvider),
Expand Down
Loading

0 comments on commit 99b8c7c

Please sign in to comment.