diff --git a/src/DataStreamSuite/DataStream/Offloading/RemoteQueryManager.cs b/src/DataStreamSuite/DataStream/Offloading/RemoteQueryManager.cs index 223a7b4f9..be8b833a2 100644 --- a/src/DataStreamSuite/DataStream/Offloading/RemoteQueryManager.cs +++ b/src/DataStreamSuite/DataStream/Offloading/RemoteQueryManager.cs @@ -1,18 +1,25 @@ +using bts; + +using Bts.DataStream; +using Bts.DataStreamCore; +using Bts.DataStreamExpressions; +using Bts.PubSub; + +using Microsoft.Extensions.Hosting; + +using Spark.Core.Disposables; + using System; using System.Collections.Generic; using System.IO; -using System.Text; -using System.Runtime.InteropServices; -using Bts.DataStreamExpressions; -using BCG.Utilities.General; -using bts; -using Bts.PubSub; -using Bts.DataStreamCore; +using System.Reactive.Linq; +using System.Threading; +using System.Threading.Tasks; namespace BCG { // listens to the requests posted on "cloud_streams" request table, and // if unfulfilled starts a ViewPubsliher - public class RemoteQueryManager : IWorker { + public class RemoteQueryManager : IHostedService { public class StreamPubUnit { public string name; public string stream_expression; @@ -43,25 +50,46 @@ namespace BCG { } private IDictionary _stream_pub_map = new Dictionary(); - private ViewStream _stream = new TableStream("cloud_streams"); readonly IPubTableBroker _broker; readonly IManagedSubscriber _sub; + readonly object syncRoot = new(); + IDisposable? cloudStreamsSub; + IDisposable? remoteRequestsSub; + bool isDisposed; public RemoteQueryManager(IManagedSubscriber managedSubscriber, IPubTableBroker pubTableBroker) { _sub = managedSubscriber; _broker = pubTableBroker; - _stream.onChange += _on_str; - _sub.Subscribe("RemoteRequest", (string tag, byte[] data) => { - RemoteRequest req = RemoteRequest.construct_from_bytes(data); - _spawn_remote_stream(req.published_table_name, req.stream_expression, false); - }); } - private void _on_str(object obj) - { - if (obj is not DataPacket dp) { - return; + + public Task StartAsync(CancellationToken cancellationToken) { + lock (syncRoot) { + Log.assert(!isDisposed); + Log.isnull(cloudStreamsSub); + Log.isnull(remoteRequestsSub); + cloudStreamsSub = new TableStream("cloud_streams").AsObservable().Subscribe(_on_str); + remoteRequestsSub = _sub.Observe("RemoteRequest").Subscribe(data => { + RemoteRequest req = RemoteRequest.construct_from_bytes(data); + _spawn_remote_stream(req.published_table_name, req.stream_expression, false); + }); } + return Task.CompletedTask; + } + + public Task StopAsync(CancellationToken cancellationToken) { + lock (syncRoot) { + if (!isDisposed) { + isDisposed = true; + Disposable.dispose(ref cloudStreamsSub); + Disposable.dispose(ref remoteRequestsSub); + } + } + return Task.CompletedTask; + } + + private void _on_str(DataPacket dp) + { foreach (IBlueRow r in dp.newRows) { string name = r.get("name"); @@ -91,9 +119,5 @@ namespace BCG { private void _expose_to_ws(string name, ViewStream stream) { // TODO: john } - - public void doWork() { - - } } } diff --git a/src/DataStreamSuite/DataStream/Services/DataStreamServices.cs b/src/DataStreamSuite/DataStream/Services/DataStreamServices.cs index d35f836da..f4b34d7d6 100644 --- a/src/DataStreamSuite/DataStream/Services/DataStreamServices.cs +++ b/src/DataStreamSuite/DataStream/Services/DataStreamServices.cs @@ -1,11 +1,11 @@ namespace Bts.DataStream.Services; using BCG; -using BCG.Utilities.General; using Microsoft.Extensions.DependencyInjection; public static class DataStreamServices { public static IServiceCollection AddRemoteQueryManager(this IServiceCollection services) => - services.AddSingleton(); + services.AddSingleton() + .AddHostedService(sp => sp.GetRequiredService()); } diff --git a/src/DataStreamSuite/PubSub/Client/SubscriptionUtilities/IManagedSubscriber.cs b/src/DataStreamSuite/PubSub/Client/SubscriptionUtilities/IManagedSubscriber.cs index e4c6870f9..73db916a1 100644 --- a/src/DataStreamSuite/PubSub/Client/SubscriptionUtilities/IManagedSubscriber.cs +++ b/src/DataStreamSuite/PubSub/Client/SubscriptionUtilities/IManagedSubscriber.cs @@ -3,5 +3,5 @@ namespace Bts.PubSub; using System; public interface IManagedSubscriber { - void Subscribe(string tag, Action onData); + IObservable Observe(string tag); } diff --git a/src/DataStreamSuite/PubSub/Client/SubscriptionUtilities/ManagedSubscriber.cs b/src/DataStreamSuite/PubSub/Client/SubscriptionUtilities/ManagedSubscriber.cs index f4f032ee9..6e4f54884 100644 --- a/src/DataStreamSuite/PubSub/Client/SubscriptionUtilities/ManagedSubscriber.cs +++ b/src/DataStreamSuite/PubSub/Client/SubscriptionUtilities/ManagedSubscriber.cs @@ -2,12 +2,16 @@ using System; using System.Collections.Generic; using System.IO; using System.Linq; +using System.Reactive.Disposables; +using System.Reactive.Linq; using BCG.Utilities.General; +using bts; + using Bts.PubSub; -using OnData = System.Action; +using OnData = System.Action; namespace BCG { // public delegate void Handler(IPubItem pi); @@ -73,7 +77,7 @@ namespace BCG { // // BlueEventLog.logEvent("Trying to unsubscribe a PubItem tag that was never subsribed", BlueEventLevel.Error); // } - public void unsubcribe(string tag, OnData on_data) { + 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) { @@ -83,8 +87,11 @@ namespace BCG { } } - void IManagedSubscriber.Subscribe(string tag, OnData onData) => - subscribe(tag, onData); + IObservable IManagedSubscriber.Observe(string tag) => + Observable.Create(o => { + subscribe(tag, o.OnNext); + return Disposable.Create(() => unsubscribe(tag, o.OnNext)); + }); private void _fire_events(IEnumerable updates) { // TODO @@ -92,7 +99,7 @@ namespace BCG { 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()); + cb.Invoke(u.data ?? Array.Empty()); } } }