Skip to content

Commit

Permalink
Full implementations of mailbox configurations
Browse files Browse the repository at this point in the history
  • Loading branch information
tjaskula committed Dec 12, 2022
1 parent 0de165b commit 46fc078
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 8 deletions.
138 changes: 134 additions & 4 deletions src/Vlingo.Xoom.Actors/IMailboxConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
// one at https://mozilla.org/MPL/2.0/.

using System.Globalization;
using Vlingo.Xoom.Actors.Plugin;
using Vlingo.Xoom.Actors.Plugin.Mailbox.AgronaMPSCArrayQueue;
using Vlingo.Xoom.Actors.Plugin.Mailbox.ConcurrentQueue;
using Vlingo.Xoom.Actors.Plugin.Mailbox.SharedRingBuffer;

namespace Vlingo.Xoom.Actors;

Expand All @@ -24,6 +28,20 @@ public interface IMailboxConfiguration<out T>
static IConcurrentQueueConfiguration ConcurrentQueueConfiguration() => new BasicConcurrentQueueConfiguration();

static ISharedRingBufferConfiguration SharedRingBufferConfiguration() => new BasicSharedRingBufferConfiguration();

Configuration Configuration { get; }

string MailboxName { get; }

bool IsDefaultMailbox { get; }

IMailboxProvider MailboxProvider { get; }

IPlugin Plugin { get; }

IPluginConfiguration PluginConfiguration { get; }

PluginProperties PluginProperties { get; }
}

public interface IArrayQueueConfiguration : IMailboxConfiguration<IArrayQueueConfiguration>
Expand Down Expand Up @@ -65,15 +83,21 @@ public interface ISharedRingBufferConfiguration : IMailboxConfiguration<ISharedR

public abstract class BaseMailboxConfiguration<T> : IMailboxConfiguration<T>
{
private readonly Configuration _configuration;
private bool _defaultMailbox;
private string? _mailboxImplementationClassname;
private string? _pluginName;

protected string? MailboxName;

protected string? BaseMailboxName;
protected IPlugin? BasePlugin;
protected IPluginConfiguration? BasePluginConfiguration;
protected PluginProperties? BasePluginProperties;

protected BaseMailboxConfiguration() => _configuration = Configuration.Define();

public T WithMailboxName(string? mailboxName)
{
MailboxName = mailboxName;
BaseMailboxName = mailboxName;

return (T)(object) this;
}
Expand All @@ -96,22 +120,50 @@ public virtual Properties ToProperties()
{
var properties = new Properties();

properties.SetProperty(PluginDeclaration(), "true");
properties.SetProperty(BaseMailboxName!, _mailboxImplementationClassname);
properties.SetProperty(PluginName(), "true");
properties.SetProperty(PluginName() + ".classname", _mailboxImplementationClassname);
properties.SetProperty(PluginName() + ".defaultMailbox", _defaultMailbox.ToString());

return properties;
}

protected string PluginDeclaration() => "plugin.name." + BaseMailboxName;

public Configuration Configuration => _configuration;
public string MailboxName => BaseMailboxName!;
public bool IsDefaultMailbox => _defaultMailbox;
public IMailboxProvider MailboxProvider => (IMailboxProvider)Plugin;
public abstract IPlugin Plugin { get; }
public abstract IPluginConfiguration PluginConfiguration { get; }

public PluginProperties PluginProperties
{
get
{
if (BasePluginProperties == null)
{
BasePluginProperties = new PluginProperties(BaseMailboxName!, ToProperties());
}

return BasePluginProperties;
}
}

protected string PluginName()
{
if (_pluginName == null)
{
_pluginName = "plugin.name." + MailboxName;
_pluginName = "plugin." + MailboxName;
}

return _pluginName;
}

protected TPc TypedPluginConfiguration<TPc>() {
return (TPc) PluginConfiguration;
}
}

public class BasicArrayQueueConfiguration : BaseMailboxConfiguration<IArrayQueueConfiguration>, IArrayQueueConfiguration
Expand Down Expand Up @@ -169,6 +221,32 @@ public override Properties ToProperties()

return properties;
}

public override IPlugin Plugin
{
get
{
if (BasePlugin == null)
{
BasePlugin = new ManyToOneConcurrentArrayQueuePlugin(TypedPluginConfiguration<IPluginConfiguration>());
}

return BasePlugin;
}
}

public override IPluginConfiguration PluginConfiguration {
get
{
if (BasePluginConfiguration == null)
{
BasePluginConfiguration = ManyToOneConcurrentArrayQueuePluginConfiguration.Define();
BasePluginConfiguration.BuildWith(Configuration, PluginProperties);
}

return BasePluginConfiguration;
}
}
}

public class BasicConcurrentQueueConfiguration : BaseMailboxConfiguration<IConcurrentQueueConfiguration>, IConcurrentQueueConfiguration
Expand Down Expand Up @@ -208,6 +286,32 @@ public override Properties ToProperties()

return properties;
}

public override IPlugin Plugin
{
get
{
if (BasePlugin == null)
{
BasePlugin = new ConcurrentQueueMailboxPlugin(TypedPluginConfiguration<IPluginConfiguration>());
}

return BasePlugin;
}
}

public override IPluginConfiguration PluginConfiguration {
get
{
if (BasePluginConfiguration == null)
{
BasePluginConfiguration = ConcurrentQueueMailboxPluginConfiguration.Define();
BasePluginConfiguration.BuildWith(Configuration, PluginProperties);
}

return BasePluginConfiguration;
}
}
}

public class BasicSharedRingBufferConfiguration : BaseMailboxConfiguration<ISharedRingBufferConfiguration>, ISharedRingBufferConfiguration
Expand Down Expand Up @@ -256,4 +360,30 @@ public override Properties ToProperties()

return properties;
}

public override IPlugin Plugin
{
get
{
if (BasePlugin == null)
{
BasePlugin = new SharedRingBufferMailboxPlugin(TypedPluginConfiguration<IPluginConfiguration>());
}

return BasePlugin;
}
}

public override IPluginConfiguration PluginConfiguration {
get
{
if (BasePluginConfiguration == null)
{
BasePluginConfiguration = SharedRingBufferMailboxPluginConfiguration.Define();
BasePluginConfiguration.BuildWith(Configuration, PluginProperties);
}

return BasePluginConfiguration;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class ManyToOneConcurrentArrayQueuePlugin : AbstractPlugin, IMailboxProvi
public ManyToOneConcurrentArrayQueuePlugin(string? name = null) =>
_configuration = ManyToOneConcurrentArrayQueuePluginConfiguration.Define();

public ManyToOneConcurrentArrayQueuePlugin(IPluginConfiguration configuration) =>
internal ManyToOneConcurrentArrayQueuePlugin(IPluginConfiguration configuration) =>
_configuration = (ManyToOneConcurrentArrayQueuePluginConfiguration)configuration;

public override void Close()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class ConcurrentQueueMailboxPlugin : AbstractPlugin, IMailboxProvider

public ConcurrentQueueMailboxPlugin(string name) => _configuration = ConcurrentQueueMailboxPluginConfiguration.Define(name);

public ConcurrentQueueMailboxPlugin(IPluginConfiguration configuration) => _configuration = (ConcurrentQueueMailboxPluginConfiguration)configuration;
internal ConcurrentQueueMailboxPlugin(IPluginConfiguration configuration) => _configuration = (ConcurrentQueueMailboxPluginConfiguration)configuration;

public override string Name => _configuration.Name;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public SharedRingBufferMailboxPlugin(string? name = null)
_dispatchers = new ConcurrentDictionary<int, RingBufferDispatcher>(16, 1);
}

public SharedRingBufferMailboxPlugin(IPluginConfiguration configuration)
internal SharedRingBufferMailboxPlugin(IPluginConfiguration configuration)
{
_configuration = (SharedRingBufferMailboxPluginConfiguration)configuration;
_dispatchers = new ConcurrentDictionary<int, RingBufferDispatcher>(16, 1);
Expand Down
2 changes: 1 addition & 1 deletion src/Vlingo.Xoom.Actors/World.cs
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ public void RegisterMailboxProviderKeeper(IMailboxProviderKeeper keeper)
/// <param name="mailboxConfiguration">The <see cref="IMailboxConfiguration{T}"/> describing the mailbox type to register</param>
/// <typeparam name="T">The type of the mailbox</typeparam>
public void RegisterMailboxType<T>(IMailboxConfiguration<T> mailboxConfiguration) =>
mailboxConfiguration.Plugin().Start(this);
mailboxConfiguration.Plugin.Start(this);

/// <summary>
/// Registers the dynamic <paramref name="value"/> with the <paramref name="name"/> key.
Expand Down

0 comments on commit 46fc078

Please sign in to comment.