Skip to content

Commit

Permalink
simplify factory APIs for creating RouteBEchonetLiteHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
smdn committed Jan 8, 2025
1 parent 83180eb commit d80fddf
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 72 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// SPDX-FileCopyrightText: 2025 smdn <smdn@smdn.jp>
// SPDX-License-Identifier: MIT
using System;

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

using Smdn.Devices.BP35XX;
using Smdn.Net.EchonetLite.RouteB.Transport.SkStackIP;

namespace Smdn.Net.EchonetLite.RouteB.Transport.BP35XX;

public sealed class BP35A1RouteBEchonetLiteHandler : SkStackRouteBUdpEchonetLiteHandler {
public BP35A1RouteBEchonetLiteHandler(
BP35A1 client,
SkStackRouteBSessionConfiguration sessionConfiguration,
bool shouldDisposeClient = false,
IServiceProvider? serviceProvider = null
)
: base(
client: client,
sessionConfiguration: sessionConfiguration,
shouldDisposeClient: shouldDisposeClient,
logger: serviceProvider?.GetService<ILoggerFactory>()?.CreateLogger<BP35A1RouteBEchonetLiteHandler>(),
serviceProvider: serviceProvider
)
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Threading.Tasks;

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

using Smdn.Devices.BP35XX;
using Smdn.Net.EchonetLite.RouteB.Transport.SkStackIP;
Expand All @@ -16,9 +15,6 @@ namespace Smdn.Net.EchonetLite.RouteB.Transport.BP35XX;
public sealed class BP35A1RouteBEchonetLiteHandlerFactory : SkStackRouteBEchonetLiteHandlerFactory {
private readonly Action<BP35A1Configurations> configure;

/// <inheritdoc/>
protected override SkStackRouteBTransportProtocol TransportProtocol => SkStackRouteBTransportProtocol.Udp;

#pragma warning disable IDE0290
public BP35A1RouteBEchonetLiteHandlerFactory(
IServiceCollection services,
Expand All @@ -30,7 +26,9 @@ Action<BP35A1Configurations> configure
}
#pragma warning restore IDE0290

protected override async ValueTask<SkStackClient> CreateClientAsync(
protected override async ValueTask<RouteBEchonetLiteHandler> CreateAsyncCore(
SkStackRouteBSessionConfiguration sessionConfiguration,
Action<SkStackClient>? configureSkStackClient,
IServiceProvider serviceProvider,
CancellationToken cancellationToken
)
Expand All @@ -42,16 +40,21 @@ CancellationToken cancellationToken
if (string.IsNullOrEmpty(configurations.SerialPortName))
throw new InvalidOperationException($"{configurations.SerialPortName} is not valid");

return await BP35A1.CreateAsync(
cancellationToken.ThrowIfCancellationRequested();

var client = await BP35A1.CreateAsync(
configurations: configurations,
serviceProvider: serviceProvider,
cancellationToken: cancellationToken
).ConfigureAwait(false);
}

protected override ValueTask<ILogger?> CreateLoggerAsync(
IServiceProvider serviceProvider,
CancellationToken cancellationToken
)
=> new(serviceProvider.GetService<ILoggerFactory>()?.CreateLogger<BP35A1RouteBEchonetLiteHandlerFactory>());
configureSkStackClient?.Invoke(client);

return new BP35A1RouteBEchonetLiteHandler(
client: client,
sessionConfiguration: sessionConfiguration,
shouldDisposeClient: true,
serviceProvider: serviceProvider
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Threading.Tasks;

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

using Smdn.Net.SkStackIP;

Expand All @@ -17,62 +16,35 @@ public abstract class SkStackRouteBEchonetLiteHandlerFactory(IServiceCollection
public Action<SkStackClient>? ConfigureSkStackClient { get; set; }
public Action<SkStackRouteBSessionConfiguration>? ConfigureRouteBSessionConfiguration { get; set; }

/// <summary>
/// Gets the value specifying the transport protocol to be used by the handler which this factory creates.
/// </summary>
protected abstract SkStackRouteBTransportProtocol TransportProtocol { get; }

protected abstract ValueTask<SkStackClient> CreateClientAsync(
IServiceProvider serviceProvider,
CancellationToken cancellationToken
);

protected virtual ValueTask<ILogger?> CreateLoggerAsync(
IServiceProvider serviceProvider,
CancellationToken cancellationToken
)
=> new((ILogger?)null);

public virtual async ValueTask<RouteBEchonetLiteHandler> CreateAsync(
public virtual ValueTask<RouteBEchonetLiteHandler> CreateAsync(
CancellationToken cancellationToken
)
{
#if SYSTEM_THREADING_TASKS_VALUETASK_FROMCANCELED
if (cancellationToken.IsCancellationRequested)
return ValueTask.FromCanceled<RouteBEchonetLiteHandler>(cancellationToken);
#else
// TODO
#endif

var sessionConfiguration = new SkStackRouteBSessionConfiguration();

ConfigureRouteBSessionConfiguration?.Invoke(sessionConfiguration);

var serviceProvider = services.BuildServiceProvider();

var client = await CreateClientAsync(
return CreateAsyncCore(
sessionConfiguration: sessionConfiguration,
configureSkStackClient: ConfigureSkStackClient,
serviceProvider: serviceProvider,
cancellationToken: cancellationToken
).ConfigureAwait(false);

var logger = await CreateLoggerAsync(
serviceProvider: serviceProvider,
cancellationToken: cancellationToken
).ConfigureAwait(false);

ConfigureSkStackClient?.Invoke(client);

return TransportProtocol switch {
SkStackRouteBTransportProtocol.Tcp => new SkStackRouteBTcpEchonetLiteHandler(
client: client,
sessionConfiguration: sessionConfiguration,
shouldDisposeClient: true,
logger: logger,
serviceProvider: serviceProvider
),

SkStackRouteBTransportProtocol.Udp => new SkStackRouteBUdpEchonetLiteHandler(
client: client,
sessionConfiguration: sessionConfiguration,
shouldDisposeClient: true,
logger: logger,
serviceProvider: serviceProvider
),

_ => throw new InvalidOperationException($"There is no hanlder that supports '{TransportProtocol}' for the transport protocol."),
};
);
}

protected abstract ValueTask<RouteBEchonetLiteHandler> CreateAsyncCore(
SkStackRouteBSessionConfiguration sessionConfiguration,
Action<SkStackClient>? configureSkStackClient,
IServiceProvider serviceProvider,
CancellationToken cancellationToken
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

namespace Smdn.Net.EchonetLite.RouteB.Transport.SkStackIP;

public sealed class SkStackRouteBTcpEchonetLiteHandler : SkStackRouteBEchonetLiteHandler {
public class SkStackRouteBTcpEchonetLiteHandler : SkStackRouteBEchonetLiteHandler {
public SkStackRouteBTcpEchonetLiteHandler(
SkStackClient client,
SkStackRouteBSessionConfiguration sessionConfiguration,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

namespace Smdn.Net.EchonetLite.RouteB.Transport.SkStackIP;

public sealed class SkStackRouteBUdpEchonetLiteHandler : SkStackRouteBEchonetLiteHandler {
public class SkStackRouteBUdpEchonetLiteHandler : SkStackRouteBEchonetLiteHandler {
public SkStackRouteBUdpEchonetLiteHandler(
SkStackClient client,
SkStackRouteBSessionConfiguration sessionConfiguration,
Expand Down

0 comments on commit d80fddf

Please sign in to comment.