namespace Bts.PubSub; using BCG; using bts; using Bts.PubSub.Routing; using System; using System.Collections.Generic; public class PubSubConfig { static PubSubConfig? instance; public static PubSubConfig Instance => instance ?? throw new InvalidOperationException( "SetGlobalConfig must be called to initialize config static singleton"); public static void SetGlobalConfig(PubSubConfig cfg) { // This should only be called once Log.isnull(instance); instance = Log.notnull(cfg); } public short? ServerPort { get; set; } public bool? SimpleEchoServer { get; set; } // Used by the PubSubMessageReactor to pass to the ManagedTCPMessageHb (but unsed within it) public int? HeartbeatInterval { get; set; } = 15000; public int? MissingHeartbeatsBeforeGc { get; set; } = 50; // Milliseconds public int? DisposedConnectionCheckPeriod { get; set; } = 1000; public TagTypeToAssembly? TagTypeToAssemblyMap { get; set; } public PubSubTagHandlerFactoryConfig? TagHandlerFactory { get; set; } public PubSubRouterConfig? Routing { get; set; } public List? PublisherRoutes => Routing?.PublisherRoutes; public List? SubscriberRoutes => Routing?.SubscriberRoutes; public static PubSubConfig Default() { var rv = new PubSubConfig() { HeartbeatInterval = 15000, MissingHeartbeatsBeforeGc = 50, DisposedConnectionCheckPeriod = 1000, ServerPort = 12345, TagTypeToAssemblyMap = new(), TagHandlerFactory = PubSubTagHandlerFactoryConfig.Default(), SimpleEchoServer = false, Routing = new() { PublisherRoutes = new() { new("bts-cloud://127.0.0.1:12345", ".*") }, SubscriberRoutes = new() { new("bts-cloud://127.0.0.1:12345", ".*") } } }; return rv; } public void MergeWith(PubSubConfig other) { HeartbeatInterval ??= other.HeartbeatInterval; MissingHeartbeatsBeforeGc ??= other.MissingHeartbeatsBeforeGc; DisposedConnectionCheckPeriod ??= other.DisposedConnectionCheckPeriod; ServerPort ??= other.ServerPort; TagTypeToAssemblyMap ??= other.TagTypeToAssemblyMap; TagHandlerFactory ??= other.TagHandlerFactory; SimpleEchoServer ??= other.SimpleEchoServer; if (other.Routing is not null) { if (Routing is null) { Routing = other.Routing?.Clone(); } else { Routing.MergeWith(other.Routing); } } } } public static class PubSubConfigExtensions { public static TimeSpan ConnectionDropThreshold(this PubSubConfig pubSubConfig) { ArgumentNullException.ThrowIfNull(pubSubConfig.MissingHeartbeatsBeforeGc); ArgumentNullException.ThrowIfNull(pubSubConfig.HeartbeatInterval); return TimeSpan.FromMilliseconds(pubSubConfig.HeartbeatInterval!.Value * pubSubConfig.MissingHeartbeatsBeforeGc!.Value); } public static List SubscriberRoutesFor(this PubSubConfig pubSubConfig, string site) { return Log.notnull(pubSubConfig.SubscriberRoutes); } }