112 lines
4.5 KiB
C#
112 lines
4.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using BCG.Utilities.General;
|
|
using System.Collections;
|
|
using System.IO;
|
|
|
|
namespace BCG {
|
|
/// <summary>
|
|
/// Gets the subscription and publication state for one
|
|
/// server.
|
|
/// </summary>
|
|
public class PubSubServerStateStream : ViewStream, IWorker {
|
|
private static Dictionary<ViewStreamDatabase, Dictionary<string, ViewStream>> _stream_map =
|
|
new Dictionary<ViewStreamDatabase, Dictionary<string, ViewStream>>();
|
|
private KeysBasedTable2 _table =
|
|
new KeysBasedTable2(new BlueTableSchema("tagInfo",
|
|
new ColumnInfo("id", 0, typeof(int)),
|
|
new ColumnInfo("tag", 1, typeof(string)),
|
|
new ColumnInfo("server", 2, typeof(string)),
|
|
new ColumnInfo("subscribers", 3, typeof(string)),
|
|
new ColumnInfo("publishers", 4, typeof(string)),
|
|
new ColumnInfo("deadPublishers", 5, typeof(string))),
|
|
new string[] { "tag" });
|
|
private SubscriberCore _sub_core;
|
|
private ViewStreamDatabase _vsd;
|
|
|
|
private static ViewStream _get_pub_sub_server_state_stream(ViewStreamDatabase vsd, string server_url) {
|
|
if (!_stream_map.ContainsKey(vsd))
|
|
_stream_map.Add(vsd, new Dictionary<string, ViewStream>());
|
|
if (!_stream_map[vsd].ContainsKey(server_url))
|
|
_stream_map[vsd].Add(server_url, new PubSubServerStateStream(server_url, vsd));
|
|
return _stream_map[vsd][server_url];
|
|
}
|
|
|
|
|
|
// public static ViewStream getPubSubServerStateStream(ViewStreamDatabase vsd, string server_uRL)
|
|
// {
|
|
// return getPubSubServerStateStream(vsd, server_uRL);
|
|
// }
|
|
|
|
|
|
public PubSubServerStateStream(string server_url)
|
|
: this(server_url, ViewStreamDatabase.instance) { }
|
|
|
|
public PubSubServerStateStream(string server_url, ViewStreamDatabase vsd) {
|
|
_sub_core = new SubscriberCore(new PubSubRouter(new PubSubRoute(server_url, ".*")));
|
|
_vsd = vsd;
|
|
}
|
|
|
|
|
|
public PubSubServerStateStream()
|
|
: this(ViewStreamDatabase.instance) { }
|
|
|
|
public PubSubServerStateStream(ViewStreamDatabase vsd) {
|
|
mySetRealSource(new PubSubServerStateStream(vsd, PubSub3Config.instance.getSubRouting()));
|
|
}
|
|
|
|
public PubSubServerStateStream(ViewStreamDatabase vsd, PubSubRoute[] routes) {
|
|
List<ViewStream> vss = new List<ViewStream>();
|
|
foreach (var r in routes)
|
|
vss.Add(_get_pub_sub_server_state_stream(vsd, r.serverAddress));
|
|
mySetRealSource(new AppendStream(vss.ToArray()));
|
|
}
|
|
|
|
protected override void myOnFirstSubscriber() {
|
|
_sub_core.subscribe("System_AllSubscriptions");
|
|
_vsd.addWorker(this);
|
|
}
|
|
|
|
protected override void myOnLastSubscriber() {
|
|
_sub_core.unsubscribe("System_AllSubscriptions");
|
|
_vsd.removeWorker(this);
|
|
}
|
|
|
|
#region IWorker Members
|
|
|
|
public void doWork() {
|
|
var updates = _sub_core.get_updates(TimeSpan.Zero);
|
|
// this is where the pubsub updates are converted to rows
|
|
if (updates.Count() > 0) {
|
|
int id = 0;
|
|
BlueTable addedRows = new BlueTable(_table.schema);
|
|
foreach (var u in updates) {
|
|
BinaryReader br = new BinaryReader(new MemoryStream(u.data));
|
|
// TODO: check this
|
|
int n_updates = br.ReadInt32();
|
|
for (int i = 0; i < n_updates; i++) {
|
|
TagPubSubInfo tsi = new TagPubSubInfo(new MemoryStream(u.data));
|
|
addedRows.addRow(new BlueRow(_table.schema, id++, tsi.tag, tsi.server, tsi.subscribersInString, tsi.publishersInString, tsi.deadPublishersInString));
|
|
}
|
|
}
|
|
IBlueRowCollection dRows, aRows;
|
|
_table.updateMultipleRows(addedRows, out dRows, out aRows);
|
|
myFireEvent(new DataPacket(_table, aRows, dRows));
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
// public static string convertURLToTag(string URL) {
|
|
// string[] urlWords = URL.Split('.');
|
|
// string[] lastPart = urlWords[3].Split(':');
|
|
// //the first one has "bluto://" in front of it
|
|
// //we want to chop that off
|
|
// string result = "bluto_" + urlWords[0].Substring(8) + "_" + urlWords[1] + "_" + urlWords[2] + "_" + lastPart[0] + "_" + lastPart[1];
|
|
|
|
// return result;
|
|
// }
|
|
}
|
|
}
|