-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Messaging.Host packge with in-process connection implementation
- Loading branch information
1 parent
700ceef
commit 2498b0b
Showing
22 changed files
with
646 additions
and
315 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
src/messaging/dotnet/src/Host/Client/Internal/InProcessConnection.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// 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. | ||
|
||
using System.Threading.Channels; | ||
using MorganStanley.ComposeUI.Messaging.Client.Abstractions; | ||
using MorganStanley.ComposeUI.Messaging.Protocol.Messages; | ||
using MorganStanley.ComposeUI.Messaging.Server; | ||
using MorganStanley.ComposeUI.Messaging.Server.Abstractions; | ||
|
||
namespace MorganStanley.ComposeUI.Messaging.Client.Internal; | ||
|
||
internal sealed class InProcessConnection : IConnection, IClientConnection | ||
{ | ||
public InProcessConnection(IMessageRouterServer server) | ||
{ | ||
_server = server; | ||
} | ||
|
||
ValueTask IClientConnection.SendAsync(Message message, CancellationToken cancellationToken = default) | ||
{ | ||
return _serverToClient.Writer.WriteAsync(message, cancellationToken); | ||
} | ||
|
||
ValueTask<Message> IClientConnection.ReceiveAsync(CancellationToken cancellationToken = default) | ||
{ | ||
return _clientToServer.Reader.ReadAsync(cancellationToken); | ||
} | ||
|
||
public ValueTask DisposeAsync() | ||
{ | ||
_clientToServer.Writer.TryComplete(); | ||
_serverToClient.Writer.TryComplete(); | ||
|
||
return ValueTask.CompletedTask; | ||
} | ||
|
||
ValueTask IConnection.ConnectAsync(CancellationToken cancellationToken = default) | ||
{ | ||
return _server.ClientConnected(this); | ||
} | ||
|
||
ValueTask IConnection.SendAsync(Message message, CancellationToken cancellationToken = default) | ||
{ | ||
return _clientToServer.Writer.WriteAsync(message, cancellationToken); | ||
} | ||
|
||
ValueTask<Message> IConnection.ReceiveAsync(CancellationToken cancellationToken = default) | ||
{ | ||
return _serverToClient.Reader.ReadAsync(cancellationToken); | ||
} | ||
|
||
private readonly IMessageRouterServer _server; | ||
private readonly Channel<Message> _clientToServer = Channel.CreateUnbounded<Message>(); | ||
private readonly Channel<Message> _serverToClient = Channel.CreateUnbounded<Message>(); | ||
} |
35 changes: 35 additions & 0 deletions
35
src/messaging/dotnet/src/Host/Client/Internal/MessageRouterBuilderInProcessExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// 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. | ||
|
||
using MorganStanley.ComposeUI.Messaging.Client.Abstractions; | ||
using MorganStanley.ComposeUI.Messaging.Client.Internal; | ||
|
||
// ReSharper disable once CheckNamespace | ||
namespace Microsoft.Extensions.DependencyInjection; | ||
|
||
/// <summary> | ||
/// Adds extension methods for configuring an in-process Message Router client. | ||
/// </summary> | ||
public static class MessageRouterBuilderInProcessExtensions | ||
{ | ||
/// <summary> | ||
/// Configures the Message Router to connect to the in-process server. | ||
/// The server must be set up using <see cref="ServiceCollectionMessageRouterServerExtensions.AddMessageRouterServer"/>. | ||
/// </summary> | ||
/// <returns></returns> | ||
public static MessageRouterBuilder UseServer(this MessageRouterBuilder builder) | ||
{ | ||
builder.ServiceCollection.AddTransient<IConnection, InProcessConnection>(); | ||
|
||
return builder; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/messaging/dotnet/src/Host/MorganStanley.ComposeUI.Messaging.Host.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<GenerateDocumentationFile>True</GenerateDocumentationFile> | ||
<RootNamespace>MorganStanley.ComposeUI.Messaging</RootNamespace> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="../Core/MorganStanley.ComposeUI.Messaging.Core.csproj" /> | ||
<ProjectReference Include="../Client/MorganStanley.ComposeUI.Messaging.Client.csproj" /> | ||
<ProjectReference Include="../Server/MorganStanley.ComposeUI.Messaging.Server.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
src/messaging/dotnet/test/Host.Tests/Client/Internal/InProcessConnection.Tests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// 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. | ||
|
||
using MorganStanley.ComposeUI.Messaging.Client.Abstractions; | ||
using MorganStanley.ComposeUI.Messaging.Protocol.Messages; | ||
using MorganStanley.ComposeUI.Messaging.Server; | ||
using MorganStanley.ComposeUI.Messaging.Server.Abstractions; | ||
|
||
namespace MorganStanley.ComposeUI.Messaging.Client.Internal; | ||
|
||
public class InProcessConnectionTests | ||
{ | ||
[Fact] | ||
public async Task Connect_registers_itself_at_the_server() | ||
{ | ||
var server = new Mock<IMessageRouterServer>(); | ||
var connection = (IConnection)new InProcessConnection(server.Object); | ||
|
||
await connection.ConnectAsync(); | ||
|
||
server.Verify(_ => _.ClientConnected(It.IsAny<IClientConnection>()),Times.Once()); | ||
} | ||
|
||
[Fact] | ||
public async Task The_client_can_send_messages_to_the_server() | ||
{ | ||
var connection = new InProcessConnection(Mock.Of<IMessageRouterServer>()); | ||
var clientSideConnection = (IConnection) connection; | ||
var serverSideConnection = (IClientConnection) connection; | ||
|
||
await clientSideConnection.ConnectAsync(); | ||
|
||
var message = new ConnectRequest(); | ||
|
||
await clientSideConnection.SendAsync(message); | ||
var received = await serverSideConnection.ReceiveAsync(); | ||
|
||
received.Should().Be(message); | ||
} | ||
|
||
[Fact] | ||
public async Task The_server_can_send_messages_to_the_client() | ||
{ | ||
var connection = new InProcessConnection(Mock.Of<IMessageRouterServer>()); | ||
var clientSideConnection = (IConnection) connection; | ||
var serverSideConnection = (IClientConnection) connection; | ||
|
||
await clientSideConnection.ConnectAsync(); | ||
|
||
var message = new Protocol.Messages.TopicMessage(); | ||
|
||
await serverSideConnection.SendAsync(message); | ||
var received = await clientSideConnection.ReceiveAsync(); | ||
|
||
received.Should().Be(message); | ||
} | ||
} |
Oops, something went wrong.