119 lines
4.2 KiB
C#
119 lines
4.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
|
|
using BCG.Utilities.General;
|
|
|
|
using Bts.PubSub;
|
|
|
|
using OnData = System.Action<string, byte[]>;
|
|
|
|
namespace BCG {
|
|
// public delegate void Handler(IPubItem pi);
|
|
// public delegate void RawDataHandler(string tag, Stream s);
|
|
|
|
/// <summary>
|
|
/// Provides a callback based interface to the SubscriptionCore (this class is mainly here to allow backward compatibility
|
|
/// with PubSub, as some clients are still using ManagedSubcribers)
|
|
/// </summary>
|
|
public class ManagedSubscriber : IManagedSubscriber, IWorker, IDisposable {
|
|
private static ManagedSubscriber? _self;
|
|
private ISubscriberCore _subcore;
|
|
// private IDictionary myTypedDispatchTable = new Hashtable(),
|
|
// myRawDispatchTable = new Hashtable();
|
|
private Dictionary<string, List<OnData>> _dispatch_table = new Dictionary<string, List<OnData>>();
|
|
|
|
|
|
public ManagedSubscriber() : this(new SubscriberCore()) { }
|
|
|
|
public ManagedSubscriber(ISubscriberCore subsCore) { _subcore = subsCore; }
|
|
|
|
public void doWork() {
|
|
var msg_map = new Dictionary<string, List<bts.PubSubServerMessage>>();
|
|
var updates = _subcore.get_updates(TimeSpan.Zero);
|
|
if (updates.Count() == 0)
|
|
return;
|
|
_fire_events(updates);
|
|
}
|
|
|
|
public void subscribe(string tag, OnData on_data) {
|
|
if (!_dispatch_table.TryGetValue(tag, out var tag_cbs)) {
|
|
tag_cbs = new List<OnData>();
|
|
_dispatch_table.Add(tag, tag_cbs);
|
|
_subcore.subscribe(tag); ;
|
|
}
|
|
tag_cbs.Add(on_data);
|
|
}
|
|
|
|
// public void subscribe(string tag, Handler handler) {
|
|
// myTypedDispatchTable[tag] = !myTypedDispatchTable.Contains(tag) ?
|
|
// new TypedPubItemHandler(tag, mySubsCore) : myTypedDispatchTable[tag];
|
|
// (myTypedDispatchTable[tag] as TypedPubItemHandler).addSubscription(handler);
|
|
// }
|
|
|
|
// public void subscribe(string tag, Handler handler, IGenericFormatter formatter) {
|
|
// myTypedDispatchTable[tag] = !myTypedDispatchTable.Contains(tag) ?
|
|
// new TypedPubItemHandler(tag, mySubsCore, formatter) : myTypedDispatchTable[tag];
|
|
// (myTypedDispatchTable[tag] as TypedPubItemHandler).addSubscription(handler);
|
|
// }
|
|
|
|
// public void subscribe(string tag, RawDataHandler handler) {
|
|
// myRawDispatchTable[tag] = !myRawDispatchTable.Contains(tag) ?
|
|
// new RawPubItemHandler(tag, mySubsCore) : myRawDispatchTable[tag];
|
|
// (myRawDispatchTable[tag] as RawPubItemHandler).addSubscription(handler);
|
|
// }
|
|
|
|
// public void unsubscribe(string tag, Handler handler) {
|
|
// if (myTypedDispatchTable.Contains(tag)) {
|
|
// (myTypedDispatchTable[tag] as TypedPubItemHandler).removeSubscription(handler);
|
|
// if ((myTypedDispatchTable[tag] as SingleSubHandler).nSubscribers == 0)
|
|
// myTypedDispatchTable.Remove(tag);
|
|
// } else { }
|
|
// // BlueEventLog.logEvent("Trying to unsubscribe a PubItem tag that was never subsribed", BlueEventLevel.Error);
|
|
// }
|
|
|
|
public void unsubcribe(string tag, OnData on_data) {
|
|
if (_dispatch_table.TryGetValue(tag, out var tag_cbs)) {
|
|
tag_cbs.Remove(on_data);
|
|
if (tag_cbs.Count == 0) {
|
|
_dispatch_table.Remove(tag);
|
|
_subcore.unsubscribe(tag);
|
|
}
|
|
}
|
|
}
|
|
|
|
void IManagedSubscriber.Subscribe(string tag, OnData onData) =>
|
|
subscribe(tag, onData);
|
|
|
|
private void _fire_events(IEnumerable<bts.PubSubServerMessage> updates) {
|
|
// TODO
|
|
foreach (var u in updates) {
|
|
ArgumentNullException.ThrowIfNull(u.tag);
|
|
if (_dispatch_table.TryGetValue(u.tag, out var cbs)) {
|
|
foreach (var cb in cbs) {
|
|
cb.Invoke(u.tag, u.data ?? Array.Empty<byte>());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public IPubSubRouter router {
|
|
get {
|
|
return _subcore.get_router();
|
|
}
|
|
}
|
|
|
|
public static ManagedSubscriber instance =>
|
|
_self ??= new();
|
|
|
|
#region IDisposable Members
|
|
|
|
public void Dispose() {
|
|
_subcore.Dispose();
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|