122 lines
4.3 KiB
C#
122 lines
4.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Reactive.Linq;
|
|
|
|
using BCG.Utilities.General;
|
|
|
|
using bts;
|
|
|
|
using Bts.PubSub;
|
|
|
|
using Spark.Core.Disposables;
|
|
|
|
using OnData = System.Action<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>>();
|
|
IDisposable? updatesSub;
|
|
|
|
public ManagedSubscriber(ISubscriberCore subsCore) {
|
|
_subcore = subsCore;
|
|
updatesSub = subsCore.Updates().Subscribe(_fire_events);
|
|
}
|
|
|
|
public void doWork() {
|
|
_subcore.get_updates(TimeSpan.Zero);
|
|
}
|
|
|
|
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 unsubscribe(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);
|
|
}
|
|
}
|
|
}
|
|
|
|
IObservable<byte[]> IManagedSubscriber.Observe(string tag) =>
|
|
Observable.Create<byte[]>(o => {
|
|
subscribe(tag, o.OnNext);
|
|
return Disposable.Create(() => unsubscribe(tag, o.OnNext));
|
|
});
|
|
|
|
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.data ?? Array.Empty<byte>());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public IPubSubRouter router {
|
|
get {
|
|
return _subcore.get_router();
|
|
}
|
|
}
|
|
|
|
#region IDisposable Members
|
|
|
|
public void Dispose() {
|
|
Disposable.dispose(ref updatesSub);
|
|
_subcore.Dispose();
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|