using BCG; using BCG.Utilities.General; using Bts.PubSub; using Bts.SharedStreams.Tables; using Spark.Core.Diagnostics; namespace bts { /// /// Gets the subscription and publication state for one /// server. /// // TODO: Use dependency injection to instantiate this stream, and others like it that depend on // ISubscriberCore(Factory). [DataStreamUserRespresentation("pss_state")] public class PubSubServerStateStream : ViewStream, IWorker { private IDictionary _tag_map = new Dictionary(); private readonly string _tag = "System_AllSubscriptions"; readonly ISubscriberCore _sub_core; private ViewStreamDatabase _vsd; public PubSubServerStateStream() : this(new SubscriberCoreFactory(), ViewStreamDatabase.instance) { } public PubSubServerStateStream(ISubscriberCoreFactory subscriberCoreFactory, ViewStreamDatabase vsd) : this(subscriberCoreFactory.Create(), vsd) { } PubSubServerStateStream(ISubscriberCore subscriberCore, ViewStreamDatabase vsd) { _sub_core = subscriberCore; _vsd = vsd; _sub_core = new SubscriberCore(); } static IBlueTableSchema _schema => PubSubServerStateRow.Schema; protected override void myOnFirstSubscriber() { _sub_core.subscribe(_tag); _vsd.addWorker(this); } protected override void myOnLastSubscriber() { _sub_core.unsubscribe(_tag); _vsd.removeWorker(this); } #region IWorker Members public void doWork() { var updates = _sub_core.get_updates(TimeSpan.Zero); //this is where the pubsub updates are converted to rows if (updates.Count() > 0) { foreach (var u in updates) { if (u.tag != _tag) { continue; } var tpsi_list = TagPubSubInfo.deserialize(ExceptionAssert.NotNull(u.data)); foreach (var tsi in tpsi_list) { _tag_map[tsi.tag] = tsi; } } } _fire_dp(); } #endregion private void _fire_dp() { BlueTable bt = new BlueTable(_schema); var sorted_tags = _tag_map.Keys.OrderBy(t => t); foreach (var kv in _tag_map) { var tsi = kv.Value; bt.addRow(new PubSubServerStateRow() { Id = bt.Count, Tag = tsi.tag, Server = tsi.pubsub_server, Subscribers = tsi.subscribers_as_string, Publishers = tsi.publishers_as_string, DeadPublishers = tsi.dead_publishers_as_string, }); } var dp = BlueDataUtility.build_dp_inc(bt, (myLastValue as IDataPacket)?.newRows, true); if (dp.addedRows.Count + dp.deletedRows.Count > 0) myFireEvent(dp); } } }