100 lines
3.0 KiB
C#
100 lines
3.0 KiB
C#
using BCG;
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
|
|
using mbts.config;
|
|
using bts.utils;
|
|
using System.Threading.Tasks;
|
|
using Bts.DataStreamExpressions;
|
|
using System.Collections.Generic;
|
|
using BCG.Utilities.General;
|
|
|
|
namespace BCG
|
|
{
|
|
// listens to the requests posted on "cloud_streams" request table, and
|
|
// if unfulfilled starts a ViewPubsliher
|
|
public class CloudStreamManager : IWorker
|
|
{
|
|
public class StreamPubUnit
|
|
{
|
|
public string name;
|
|
public string stream_expression;
|
|
public ViewStream stream;
|
|
public bool persist;
|
|
public ViewPublisher view_publisher;
|
|
|
|
public StreamPubUnit(string name_, string stream_expression_, ViewStream stream_, bool persist_, ViewPublisher view_publisher_)
|
|
{
|
|
name = name_;
|
|
stream_expression = stream_expression_;
|
|
stream = stream_;
|
|
persist = persist_;
|
|
view_publisher = view_publisher_;
|
|
}
|
|
}
|
|
static ViewStream build_stream(string stream_expression)
|
|
{
|
|
try
|
|
{
|
|
var exp = DataStreamExpressionsInterpreter.Evaluate(stream_expression);
|
|
var rv = exp.getView(new Dictionary<string, StatementExpression>(0));
|
|
return rv;
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
bts.Log.assert(false, $"Could not build stream: {stream_expression} ex: {ex}");
|
|
return null;
|
|
}
|
|
}
|
|
private IDictionary<string, StreamPubUnit>
|
|
_stream_pub_map = new Dictionary<string, StreamPubUnit>();
|
|
private ViewStream _stream = new TableStream("cloud_streams");
|
|
private PubTableBroker _broker = new PubTableBroker();
|
|
|
|
public CloudStreamManager()
|
|
{
|
|
_stream.onChange += _on_str;
|
|
|
|
}
|
|
private void _on_str(object obj)
|
|
{
|
|
DataPacket dp = obj as DataPacket;
|
|
foreach (IBlueRow r in dp.newRows)
|
|
{
|
|
string name = r.get<string>("name");
|
|
bool persist = r.get<bool>("persist");
|
|
string stream_exp = r.get<string>("stream_exp");
|
|
if (!_stream_pub_map.ContainsKey(name))
|
|
{
|
|
_assign(name, stream_exp, persist);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void _assign(string name, string stream_expression, bool persist)
|
|
{
|
|
bts.Log.Gen.info($"CloudStreamManager.assign name: {name} stream_expression: {stream_expression}");
|
|
ViewStream stream = build_stream(stream_expression);
|
|
_expose_to_ws(name, stream);
|
|
if (persist)
|
|
{
|
|
stream = new PersistorStream(stream, name, "C:\\Temp\\", 1000000); // 10MBs
|
|
}
|
|
ViewPublisher view_pub = new ViewPublisher(name, _broker, stream);
|
|
StreamPubUnit u = new StreamPubUnit(name, stream_expression, stream, persist, view_pub);
|
|
_stream_pub_map.Add(name, u);
|
|
}
|
|
|
|
private void _expose_to_ws(string name, ViewStream stream)
|
|
{
|
|
// TODO: john
|
|
}
|
|
|
|
public void doWork()
|
|
{
|
|
|
|
}
|
|
}
|
|
} |