using bts; using Bts.PubSub; using Bts.PubSub.Routing; using Spark.Core.Collections; using System; using System.Collections.Generic; using System.Reactive.Linq; using System.Reactive.Subjects; namespace BCG { /// /// This class serves as the subscrition interface to the pubsub. The only methods /// it exposes are getUpdates and subscribe. Internally, it manages a SingleServerSubscriberCore /// foreach server. So this class does the routing part, and each SingleServerSubscriberCore /// handles dealing sending (sub/unsub) and receiving (data) messages from the transport. /// There is no sharing with respect to this class. Ie, it creates new SingleServerSubscriberCore /// to the servers even though there might be so created by other instances of the SubscriberCore /// /// public class SubscriberCore : ISubscriberCore { readonly Subject> publisher = new(); private Dictionary _server_route_to_subcore_map = new(); private IPubSubRouter _tag_router; private PubSubServerConnectionFactory _connection_factory = new PubSubServerConnectionFactory(true); readonly object syncRoot = new(); readonly RefCountMap tagRefs; public event PubSubConnectionChange? onPubSubConnectionChange; #region constructors public SubscriberCore(IPubSubRouter router) { _tag_router = router; } #endregion public IEnumerable get_updates(TimeSpan timeout) { List rv = new List(); lock (syncRoot) { foreach (var kv in _server_route_to_subcore_map) { var sub_core = kv.Value; rv.AddRange(sub_core.get_updates(timeout)); } } publisher.OnNext(rv); return rv; } public IObservable> Updates() => publisher.AsObservable(); public void subscribe(params string[] tags) { lock (syncRoot) { foreach (string tag in tags) { if (!tagRefs.increment_adds_key(tag)) { continue; } RemoteConnectionInfo server_rci = _tag_router.get_sub_route_for_tag(tag); var server_sub_core = _find_subscribercore(server_rci); server_sub_core.subscribe(tag); } } } static PubSubRouterConfig routerConfig(string? siteName = null) { var pubSubCfg = PubSubConfig.Instance; if (siteName is null) { return Log.notnull(pubSubCfg.Routing); } var routes = PubSubConfig.Instance.SubscriberRoutesFor(siteName); return new() { PublisherRoutes = routes, SubscriberRoutes = routes, }; } private ISingleServerSubscriberCore _find_subscribercore(RemoteConnectionInfo server_rci) { if (!_server_route_to_subcore_map.TryGetValue(server_rci, out var rv)) { rv = new SingleServerSubscriberCore(server_rci, _connection_factory); _server_route_to_subcore_map.Add(server_rci, rv); rv.onPubSubConnectionChange += _handle_pubsubserver_connection_change; } return rv; } public void _handle_pubsubserver_connection_change(bool is_connected, string server_url) { if (onPubSubConnectionChange != null) onPubSubConnectionChange(is_connected, server_url); } public void unsubscribe(params string[] tags) { lock (syncRoot) { foreach (string tag in tags) { if (!tagRefs.decrement_removes_key(tag)) { continue; } var server_rci = _tag_router.get_sub_route_for_tag(tag); var server_subcore = _find_subscribercore(server_rci); server_subcore.unsubscribe(tag); } } } public IPubSubRouter get_router() { return _tag_router; } #region IDisposable Members public void Dispose() { lock (syncRoot) { foreach (var sssc in _server_route_to_subcore_map.Values) { sssc.Dispose(); } _connection_factory.Dispose(); } } #endregion } }