Skip to content

Commit

Permalink
Extend interface to enable message broadcasting
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanGreve committed Aug 17, 2024
1 parent 993ad6a commit c1e8162
Showing 1 changed file with 47 additions and 7 deletions.
54 changes: 47 additions & 7 deletions AdvancedSystems.Core.Abstractions/IMessageBus.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,43 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;

namespace AdvancedSystems.Core.Abstractions;

/// <summary>
/// Defines an in-memory message bus for publishing and subscribing to messages of type <see cref="IMessage"/>.
/// Defines an in-memory message bus for publishing and subscribing to messages of type <see cref="IMessage"/> via broadcasts.
/// </summary>
public interface IMessageBus : IAsyncDisposable
{
/// <summary>
/// Registers a new channel with the specified channel name and optional topic.
/// </summary>
/// <param name="channelName">
/// The name of the channel to register.
/// </param>
/// <param name="topic">
/// An optional topic associated with the channel. If not provided, the default topic will be used.
/// </param>
/// <returns>
/// Returns <c>true</c> if the channel is successfully registered; otherwise, <c>false</c> if a
/// channel with the same name already exists.
/// </returns>
bool Register(string channelName, string? topic = default);

/// <summary>
/// Unregisters the channel with the specified channel name and optional topic.
/// </summary>
/// <param name="channelName">
/// The name of the channel to unregister.
/// </param>
/// <returns>
/// Returns <c>true</c> if the channel is successfully unregistered; otherwise, <c>false</c> if
/// no channel with the specified name exists.
/// </returns>
bool Unregister(string channelName);

/// <summary>
/// Publishes a message to the message bus.
/// </summary>
Expand All @@ -19,6 +47,10 @@ public interface IMessageBus : IAsyncDisposable
/// <param name="message">
/// The message to be published.
/// </param>
/// <param name="topic">
/// An optional topic used to filter which channels are meant to receive the message. If not provided,
/// the default topic will be used.
/// </param>
/// <param name="cancellationToken">
/// Propagates notification that operations should be cancelled.
/// </param>
Expand All @@ -28,27 +60,35 @@ public interface IMessageBus : IAsyncDisposable
/// <remarks>
/// This method is used to send messages to the bus. Subscribers will receive these messages asynchronously.
/// </remarks>
ValueTask PublishAsync<T>(T message, CancellationToken cancellationToken = default) where T : class, IMessage;
/// <exception cref="InvalidOperationException">
/// Thrown when no channels with the specified topic are registered before attempting to publish a message.
/// </exception>
ValueTask PublishAsync<T>(T message, string? topic = default, CancellationToken cancellationToken = default) where T : class, IMessage;

/// <summary>
/// Subscribes to messages of type <typeparamref name="T"/> from the message bus.
/// </summary>
/// <typeparam name="T">
/// The type of the message to subscribe to. Must implement <see cref="IMessage"/>.
/// </typeparam>
/// <param name="channelName">
/// The name of the channel to subscribe to.
/// </param>
/// <param name="topic">
/// An optional topic to filter messages. If not provided, the default topic will be used.
/// </param>
/// <param name="cancellationToken">
/// Propagates notification that operations should be cancelled.
/// </param>
/// <returns>
/// This method is used to receive messages of the specified type from the bus. If no messages
/// of the type are currently available, it will wait until one becomes available.
/// </returns>
/// <exception cref="InvalidCastException">
/// Thrown when a message retrieved from the channel cannot be cast to the type <typeparamref name="T"/>.
/// <exception cref="KeyNotFoundException">
/// Thrown when no channel with the specified name is found.
/// </exception>
/// <exception cref="ChannelClosedException">
/// Thrown when the channel is closed and no more messages are available. This indicates that the channel
/// has been marked as complete and will not accept any more messages.
/// Thrown if the channel is closed before a message can be received.
/// </exception>
ValueTask<T> SubscribeAsync<T>(CancellationToken cancellationToken = default) where T : class, IMessage;
ValueTask<T> SubscribeAsync<T>(string channelName, string? topic = default, CancellationToken cancellationToken = default) where T : class, IMessage;
}

0 comments on commit c1e8162

Please sign in to comment.