using BCG; using BCG.Utilities.General; using Bts.PubSub; 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). Until then, hide it from DSQA. [DataStreamUserRespresentation(/*"pss_state"*/ false)] public class PubSubServerStateStream : ViewStream, IWorker { private IDictionary _tag_map = new Dictionary(); private readonly string _tag = "System_AllSubscriptions"; private IBlueTableSchema _schema = null; readonly ISubscriberCore _sub_core; private ViewStreamDatabase _vsd; public PubSubServerStateStream(ISubscriberCoreFactory subscriberCoreFactory, ViewStreamDatabase vsd) : this(subscriberCoreFactory.Create(), vsd) { } PubSubServerStateStream(ISubscriberCore subscriberCore, ViewStreamDatabase vsd) { _sub_core = subscriberCore; // TODO, construct schema _schema = BlueDataUtility.form_schema("pubsub_server_state", Tuple.Create("id", TypeCode.Int32), Tuple.Create("tag", TypeCode.String), Tuple.Create("ps_server", TypeCode.String), Tuple.Create("subscibers", TypeCode.String), Tuple.Create("publisher", TypeCode.String), Tuple.Create("dead_publishers", TypeCode.String)); _vsd = vsd; _sub_core = new SubscriberCore(); } 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) { int id = 0; foreach (var u in updates) { if (u.tag != _tag) { continue; } var tpsi_list = TagPubSubInfo.deserialize(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; IBlueRow r = new BlueRow(_schema, bt.Count, tsi.tag, tsi.pubsub_server, tsi.subscribers_as_string, tsi.publishers_as_string, tsi.dead_publishers_as_string); bt.addRow(r); } var dp = BlueDataUtility.build_dp_inc(bt, myLastValue as DataPacket, true); if (dp.addedRows.Count + dp.deletedRows.Count > 0) myFireEvent(dp); } } }