Skip to content

Commit df06cef

Browse files
committed
gh-8 Fix unit test
Signed-off-by: Victor Chang <[email protected]>
1 parent 8fe39c5 commit df06cef

File tree

83 files changed

+3698
-100
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+3698
-100
lines changed

src/InformaticsGateway/Logging/Log.800.Hl7Service.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,8 @@ public static partial class Log
2222

2323
[LoggerMessage(EventId = 804, Level = LogLevel.Error, Message = "Error sending HL7 acknowledgment.")]
2424
public static partial void ErrorSendingHl7Acknowledgment(this ILogger logger, Exception ex);
25+
26+
[LoggerMessage(EventId = 805, Level = LogLevel.Information, Message = "Maximum number {maximumAllowedConcurrentConnections} of clients reached.")]
27+
public static partial void MaxedOutHl7Connections(this ILogger logger, int maximumAllowedConcurrentConnections);
2528
}
2629
}

src/InformaticsGateway/Program.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@
3030
using Monai.Deploy.InformaticsGateway.Configuration;
3131
using Monai.Deploy.InformaticsGateway.Database;
3232
using Monai.Deploy.InformaticsGateway.Repositories;
33+
using Monai.Deploy.InformaticsGateway.Services.Common;
3334
using Monai.Deploy.InformaticsGateway.Services.Connectors;
3435
using Monai.Deploy.InformaticsGateway.Services.DicomWeb;
35-
using Monai.Deploy.InformaticsGateway.Services.Export;
3636
using Monai.Deploy.InformaticsGateway.Services.HealthLevel7;
3737
using Monai.Deploy.InformaticsGateway.Services.Http;
3838
using Monai.Deploy.InformaticsGateway.Services.Scp;
@@ -121,6 +121,8 @@ internal static IHostBuilder CreateHostBuilder(string[] args) =>
121121
services.AddSingleton<IMonaiServiceLocator, MonaiServiceLocator>();
122122
services.AddSingleton<IStorageInfoProvider, StorageInfoProvider>();
123123
services.AddSingleton<IMonaiAeChangedNotificationService, MonaiAeChangedNotificationService>();
124+
services.AddSingleton<ITcpListenerFactory, TcpListenerFactory>();
125+
services.AddSingleton<IMllpClientFactory, MllpClientFactory>();
124126
services.AddSingleton<IApplicationEntityManager, ApplicationEntityManager>();
125127
services.AddSingleton<SpaceReclaimerService>();
126128
services.AddSingleton<ScpService>();
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// SPDX-FileCopyrightText: © 2022 MONAI Consortium
2+
// SPDX-License-Identifier: Apache License 2.0
3+
4+
using System;
5+
using System.Threading;
6+
using System.Threading.Tasks;
7+
8+
namespace Monai.Deploy.InformaticsGateway.Services.Common
9+
{
10+
internal interface INetworkStream : IDisposable
11+
{
12+
int ReadTimeout { get; set; }
13+
int WriteTimeout { get; set; }
14+
15+
ValueTask WriteAsync(ReadOnlyMemory<byte> ackData, CancellationToken cancellationToken = default);
16+
17+
Task FlushAsync(CancellationToken cancellationToken = default);
18+
19+
ValueTask<int> ReadAsync(Memory<byte> messageBuffer, CancellationToken cancellationToken = default);
20+
}
21+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// SPDX-FileCopyrightText: © 2022 MONAI Consortium
2+
// SPDX-License-Identifier: Apache License 2.0
3+
4+
using System;
5+
using System.Net;
6+
7+
namespace Monai.Deploy.InformaticsGateway.Services.Common
8+
{
9+
internal interface ITcpClientAdapter : IDisposable
10+
{
11+
EndPoint RemoteEndPoint { get; }
12+
13+
INetworkStream GetStream();
14+
}
15+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// SPDX-FileCopyrightText: © 2022 MONAI Consortium
2+
// SPDX-License-Identifier: Apache License 2.0
3+
4+
using System.Net;
5+
using System.Threading;
6+
using System.Threading.Tasks;
7+
using Ardalis.GuardClauses;
8+
9+
namespace Monai.Deploy.InformaticsGateway.Services.Common
10+
{
11+
internal interface ITcpListener
12+
{
13+
void Start();
14+
15+
void Stop();
16+
17+
ValueTask<ITcpClientAdapter> AcceptTcpClientAsync(CancellationToken cancellationToken = default);
18+
}
19+
20+
internal class TcpListener : ITcpListener
21+
{
22+
private readonly System.Net.Sockets.TcpListener _tcpListener;
23+
24+
public TcpListener(IPAddress ipAddress, int port)
25+
{
26+
Guard.Against.Null(ipAddress, nameof(ipAddress));
27+
28+
_tcpListener = new System.Net.Sockets.TcpListener(ipAddress, port);
29+
}
30+
31+
public void Start()
32+
{
33+
_tcpListener.Start();
34+
}
35+
36+
public void Stop()
37+
{
38+
_tcpListener.Stop();
39+
}
40+
41+
public async ValueTask<ITcpClientAdapter> AcceptTcpClientAsync(CancellationToken cancellationToken = default)
42+
{
43+
return new TcpClientAdapter(await _tcpListener.AcceptTcpClientAsync(cancellationToken).ConfigureAwait(false));
44+
}
45+
}
46+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// SPDX-FileCopyrightText: © 2022 MONAI Consortium
2+
// SPDX-License-Identifier: Apache License 2.0
3+
4+
using System.Net;
5+
6+
namespace Monai.Deploy.InformaticsGateway.Services.Common
7+
{
8+
internal interface ITcpListenerFactory
9+
{
10+
ITcpListener CreateTcpListener(IPAddress ipaddress, int port);
11+
}
12+
13+
internal class TcpListenerFactory : ITcpListenerFactory
14+
{
15+
public ITcpListener CreateTcpListener(IPAddress ipaddress, int port) => new TcpListener(ipaddress, port);
16+
}
17+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// SPDX-FileCopyrightText: © 2022 MONAI Consortium
2+
// SPDX-License-Identifier: Apache License 2.0
3+
4+
using System;
5+
using System.Net.Sockets;
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
9+
namespace Monai.Deploy.InformaticsGateway.Services.Common
10+
{
11+
internal class NetworkStreamAdapter : INetworkStream
12+
{
13+
private readonly NetworkStream _networkStream;
14+
private bool _disposedValue;
15+
16+
public int ReadTimeout { get => _networkStream.ReadTimeout; set => _networkStream.ReadTimeout = value; }
17+
public int WriteTimeout { get => _networkStream.WriteTimeout; set => _networkStream.WriteTimeout = value; }
18+
19+
public NetworkStreamAdapter(NetworkStream networkStream)
20+
{
21+
_networkStream = networkStream ?? throw new ArgumentNullException(nameof(networkStream));
22+
}
23+
24+
public async Task FlushAsync(CancellationToken cancellationToken = default)
25+
=> await _networkStream.FlushAsync(cancellationToken).ConfigureAwait(false);
26+
27+
public async ValueTask<int> ReadAsync(Memory<byte> messageBuffer, CancellationToken cancellationToken = default)
28+
=> await _networkStream.ReadAsync(messageBuffer, cancellationToken).ConfigureAwait(false);
29+
30+
public async ValueTask WriteAsync(ReadOnlyMemory<byte> ackData, CancellationToken cancellationToken = default)
31+
=> await _networkStream.WriteAsync(ackData, cancellationToken).ConfigureAwait(false);
32+
33+
protected virtual void Dispose(bool disposing)
34+
{
35+
if (!_disposedValue)
36+
{
37+
if (disposing)
38+
{
39+
_networkStream.Dispose();
40+
}
41+
42+
_disposedValue = true;
43+
}
44+
}
45+
46+
public void Dispose()
47+
{
48+
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
49+
Dispose(disposing: true);
50+
GC.SuppressFinalize(this);
51+
}
52+
}
53+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// SPDX-FileCopyrightText: © 2022 MONAI Consortium
2+
// SPDX-License-Identifier: Apache License 2.0
3+
4+
using System;
5+
using System.Net;
6+
7+
namespace Monai.Deploy.InformaticsGateway.Services.Common
8+
{
9+
internal class TcpClientAdapter : ITcpClientAdapter
10+
{
11+
private readonly System.Net.Sockets.TcpClient _tcpClient;
12+
private bool _disposedValue;
13+
14+
public EndPoint? RemoteEndPoint
15+
{
16+
get
17+
{
18+
return _tcpClient.Client.RemoteEndPoint;
19+
}
20+
}
21+
22+
public TcpClientAdapter(System.Net.Sockets.TcpClient tcpClient)
23+
=> _tcpClient = tcpClient ?? throw new ArgumentNullException(nameof(tcpClient));
24+
25+
public INetworkStream GetStream()
26+
{
27+
return new NetworkStreamAdapter(_tcpClient.GetStream());
28+
}
29+
30+
protected virtual void Dispose(bool disposing)
31+
{
32+
if (!_disposedValue)
33+
{
34+
if (disposing)
35+
{
36+
_tcpClient.Dispose();
37+
}
38+
39+
_disposedValue = true;
40+
}
41+
}
42+
43+
public void Dispose()
44+
{
45+
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
46+
Dispose(disposing: true);
47+
GC.SuppressFinalize(this);
48+
}
49+
}
50+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// SPDX-FileCopyrightText: © 2022 MONAI Consortium
2+
// SPDX-License-Identifier: Apache License 2.0
3+
4+
using System;
5+
using System.Threading;
6+
using System.Threading.Tasks;
7+
8+
namespace Monai.Deploy.InformaticsGateway.Services.HealthLevel7
9+
{
10+
internal interface IMllpClient
11+
{
12+
Guid ClientId { get; }
13+
14+
void Dispose();
15+
16+
Task Start(Action<IMllpClient, MllpClientResult> onDisconnect, CancellationToken cancellationToken);
17+
}
18+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// SPDX-FileCopyrightText: © 2022 MONAI Consortium
2+
// SPDX-License-Identifier: Apache License 2.0
3+
4+
using Microsoft.Extensions.Logging;
5+
using Monai.Deploy.InformaticsGateway.Configuration;
6+
using Monai.Deploy.InformaticsGateway.Services.Common;
7+
8+
namespace Monai.Deploy.InformaticsGateway.Services.HealthLevel7
9+
{
10+
internal interface IMllpClientFactory
11+
{
12+
IMllpClient CreateClient(ITcpClientAdapter client, Hl7Configuration configurations, ILogger<MllpClient> logger);
13+
}
14+
15+
internal class MllpClientFactory : IMllpClientFactory
16+
{
17+
public IMllpClient CreateClient(ITcpClientAdapter client, Hl7Configuration configurations, ILogger<MllpClient> logger) => new MllpClient(client, configurations, logger);
18+
}
19+
}

0 commit comments

Comments
 (0)