Files
spark/src/DataStreamSuite/SharedStreams/System/PS3/Subscription/PubsubHeartbeatStream.cs
T

82 lines
2.7 KiB
C#

namespace bts;
using BCG;
using BCG.Utilities.General;
using Bts.PubSub;
using Bts.SharedStreams.Tables;
// TODO: Use dependency injection to instantiate this stream, and others like it that depend on
// ISubscriberCore(Factory).
[DataStreamUserRespresentation("pss_hb")]
public class PubsubHeartbeatStream : ViewStream, IWorker {
private ViewStreamDatabase _vsd;
readonly ISubscriberCore _subcore;
PubSubHeartbeatRow? _row = null;
public PubsubHeartbeatStream()
: this(new SubscriberCoreFactory(), ViewStreamDatabase.instance) { }
public PubsubHeartbeatStream(ISubscriberCoreFactory subscriberCore, ViewStreamDatabase vsd)
: this(subscriberCore.Create(), vsd) { }
PubsubHeartbeatStream(ISubscriberCore subscriberCore, ViewStreamDatabase vsd) {
_subcore = subscriberCore;
_vsd = vsd;
}
static IBlueTableSchema _schema => PubSubHeartbeatRow.Schema;
protected override void myOnFirstSubscriber() {
_subcore.subscribe("pss_hb");
_vsd.addWorker(this);
}
protected override void myOnLastSubscriber() {
_vsd.removeWorker(this);
_subcore.unsubscribe("pss_hb");
}
public void doWork() {
var updates = _subcore.get_updates(TimeSpan.FromMilliseconds(1));
if (updates.Count() > 0) {
foreach (var m in updates) {
if (m.data != null && m.data.Length > 0) {
BinaryReader br = new BinaryReader(new MemoryStream(m.data));
Int64 hb_sent_time = br.ReadInt64();
Int64 hb_recv_time = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
// _row = _row == null ? new MutableBlueRow(_schema) : _row;
var now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
_row = new() {
Id = 0,
SendTime = hb_sent_time,
RecvTime = hb_recv_time,
Latency = hb_recv_time - hb_sent_time,
RecvElapsed = now - hb_recv_time,
};
// TODO, add to table and fire
}
}
} else {
if (_row != null) {
var now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
_row = new() {
Id = _row.Id,
SendTime = _row.SendTime,
RecvTime = _row.RecvTime,
Latency = _row.Latency,
RecvElapsed = now - _row.RecvTime,
};
// TODO, add to table and fire
}
// update the row
}
if (_row != null) {
var bt = new BlueTable(_schema);
bt.addRow(_row);
var dp = BlueDataUtility.build_dp_inc(bt, myLastValue as IDataPacket, true);
myFireEvent(dp);
}
}
}