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
45 lines
1.7 KiB
C#
45 lines
1.7 KiB
C#
using Bts.PubSub;
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace bts {
|
|
// This caches all the published data. This class is created and held by
|
|
// PublisherCore. When data is publsihed , PublisherCore passes it to this
|
|
// class via the publish call. On a reconnection to the PubSubServer,
|
|
// PublisherCore calls get_content to get the data to re-publish, to
|
|
// re-seed the PubSubServer
|
|
public class PublishedCache {
|
|
readonly IPubSubTagHandlerFactory tagHandlerFactory;
|
|
private Dictionary<string, SingleTagData> _data_map = new Dictionary<string, SingleTagData>();
|
|
|
|
public PublishedCache(IPubSubTagHandlerFactory tagHandlerFactory) =>
|
|
this.tagHandlerFactory = tagHandlerFactory;
|
|
|
|
public void publish(PublishMsg msg) {
|
|
Log.Gen.info($"PublishedCache.publish msg: {msg}");
|
|
ArgumentException.ThrowIfNullOrEmpty(msg.tag);
|
|
if (!_data_map.TryGetValue(msg.tag, out var tag_data)) {
|
|
ArgumentException.ThrowIfNullOrEmpty(msg.tag_type);
|
|
var handler = Log.notnull(tagHandlerFactory.GetHandler(msg.tag_type));
|
|
tag_data = new SingleTagData(msg.tag, msg.tag_type, handler);
|
|
_data_map.Add(msg.tag, tag_data);
|
|
}
|
|
tag_data.handle_publish(msg);
|
|
}
|
|
|
|
public Tuple<string, byte[]> get_content(string tag) =>
|
|
_data_map.TryGetValue(tag, out var tag_data)
|
|
? new(tag_data.tag_type, tag_data.get_current_content())
|
|
: new(string.Empty, Array.Empty<byte>());
|
|
|
|
public void clear(string tag) {
|
|
if (_data_map.ContainsKey(tag)) {
|
|
_data_map.Remove(tag);
|
|
} else {
|
|
bts.Log.Gen.warn($"AllPublishTagState told to clear nonexisting tag: {tag} ");
|
|
}
|
|
}
|
|
}
|
|
}
|