Namely, in DataStreamPublisher and PubSubServer. This is part of a broader paradigm shift towards Dependency Injection and Factory Pattern. https://learn.microsoft.com/en-us/dotnet/core/extensions/dependency-injection-guidelines * Use interfaces to decouple services: - Decouple ManagedSubscriber from ISubscriberCore impl - Decouple PubTableBroker from IPublisherCore impl - Decouple RemoteQueryManager from IManagedSubscriber, IPubTableBroker impl * Rename Publishers -> PublisherNodes in DataStreamPublisherConfig * Model config profiles after dotnet appsettings - Rename Configurations/<name>.json -> appsettings.<name>.json - Make json fields match corresponding csharp props verbatim * The Dependency Injection framework doesn't support JsonPropertyName attributes. Config class props must match json fields verbatim. * Move reusable startup code into new Bts.Extensions.Hosting proj * Add pubsub and md-publisher services to compose.yaml
86 lines
3.2 KiB
C#
86 lines
3.2 KiB
C#
using BCG;
|
|
using BCG.Utilities.General;
|
|
|
|
using Bts.PubSub;
|
|
|
|
namespace bts {
|
|
/// <summary>
|
|
/// Gets the subscription and publication state for one
|
|
/// server.
|
|
/// </summary>
|
|
// 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<string, TagPubSubInfo> _tag_map = new Dictionary<string, TagPubSubInfo>();
|
|
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);
|
|
|
|
}
|
|
}
|
|
}
|