Clearing up PubSubMessageReactor

This commit is contained in:
Taha Afzal committed 2024-01-30 23:19:20 -05:00
1 parent d301d75305
commit b4d7bbcfcd
4 files changed
+304 -399

No files matched your search

@@ -8,6 +8,9 @@ using System.Threading;
using System.IO;
using System.Diagnostics;
using mbts;
using System.Linq;
using System.Collections;
using System.ComponentModel.DataAnnotations;
namespace BCG {
@@ -16,12 +19,20 @@ namespace BCG {
ITCPConnectionHandler, // interface to receive connections when calling ServerToClientManagedTCPConnectionAcceptor.accept_connections
ITCPMessageHandler { // interface to receive data from ITCPConnection
private int _port;
private bool myIsDisposed = false;
private bool _is_disposed = false;
public int port {
get { return _port; }
//property is read only
}
private class ConnectionStatusInfo {
public readonly string client_url;
public readonly PubSubCallbacks.ConnectionStatus status;
public ConnectionStatusInfo(string client_url_, PubSubCallbacks.ConnectionStatus status_) {
client_url = client_url_;
status = status_;
}
}
private bool _activated = false;
private PubSubCallbacks.PubSubServerConnectionStatusChange _connection_cb;
private PubSubServerDataDelegate _data_cb;
@@ -37,7 +48,9 @@ namespace BCG {
private SyncQueue<Pair<string, byte[]>> _received_data_queue;
/// <summary>
/// data to be sent to the client is stored here and is fetched by the
/// myDoItAll thread to be sent to the clients
/// myDoItAll thread to be sent to the clients. the send is not directly sent out
/// since there is some state management needed, so we do it on the main message
/// pump thread
/// </summary>
private SyncQueue<Pair<string, byte[]>> _send_data_queue = new SyncQueue<Pair<string, byte[]>>();
/// <summary>
@@ -45,11 +58,11 @@ namespace BCG {
/// are queued in the following queues respectively, and are
/// picked up by the doWork thread.
/// </summary>
private ThreadSafeQueue<string> _dead_connection_urls = new ThreadSafeQueue<string>(),
_new_connection_urls = new ThreadSafeQueue<string>();
// _connection_cb arguments are queued here
private ThreadSafeQueue<ConnectionStatusInfo> _conn_callback_queue = new ThreadSafeQueue<ConnectionStatusInfo>();
/// <summary>
/// new connections received (on the onConnection thread) is queued
/// in this and will be fetched by the doItAll thread
/// in this and will be fetched by the message_pump thread
/// </summary>
private ThreadSafeQueue<ITCPConnection> _new_connections = new ThreadSafeQueue<ITCPConnection>();
/// <summary>
@@ -64,6 +77,7 @@ namespace BCG {
private DateTime _last_HB_collection_time = DateTime.Now;
private DateTime _last_disposed_dead_collection_time = DateTime.Now;
// TODO, get
public ManagedTCPMessageHub(int port, TimeSpan hb_send_interval, int missing_HBs_for_gc, TimeSpan collect_disposed_connections_period) {
_port = port;
_hb_send_interval = hb_send_interval;
@@ -88,11 +102,9 @@ namespace BCG {
public void activate() {
_port = ServerToClientManagedTCPConnectionAcceptor.accept_connections(this, _port, _hb_send_interval);
string msg = String.Format("TCP Component is ready, listening on port {0} for incoming connections", _port);
Console.WriteLine(msg);
bts.Log.Gen.info($"TCP Component is ready, listening on port {_port} for incoming connections");
_activated = true;
Thread doItAllThread = new Thread(new ThreadStart(myDoItAll));
Thread doItAllThread = new Thread(new ThreadStart(_main_message_pump));
doItAllThread.IsBackground = true;
doItAllThread.Start();
}
@@ -123,17 +135,13 @@ namespace BCG {
/// <summary>
/// Reports back connected and disconnected connections
/// via the ConnectionStatucChange callback
/// via the ConnectionStatusChange callback
/// </summary>
public void doWork() {
if (_connection_cb != null) {
foreach (string nc in _new_connection_urls.dequeueAll()) {
//BlueEventLog.logEvent(String.Format("doWork : New client {0} connected, making connection callback", nc), PS3LogLevels.connection);
_connection_cb(nc, PubSubCallbacks.ConnectionStatus.CONNECTED);
}
foreach (string dc in _dead_connection_urls.dequeueAll()) {
//BlueEventLog.logEvent(String.Format("doWork : Client {0} disconnected, making disconnection callback", dc), PS3LogLevels.disconnection);
_connection_cb(dc, PubSubCallbacks.ConnectionStatus.DISCONNECTED);
var conns = _conn_callback_queue.dequeueAll();
foreach (var ci in conns) {
_connection_cb(ci.client_url, ci.status);
}
}
}
@@ -145,111 +153,59 @@ namespace BCG {
/// <summary>
/// Enqeues both the connection name (to be reported back
/// via ConnectionStatusChange) and connection (to be
/// stored in the hashtable and used for later communication)
/// stored in the hashtable and used for later communication).
/// This is called on an arbitrary thread
/// </summary>
/// <param name="connection"></param>
public void onConnection(ITCPConnection connection) {
// this is where we get new connections, dead connections are collected in _collect_disposed_connections
_new_connections.enqueue(connection);
}
#endregion
#region ITCPMessageHandler Members
// called by ITCPConnection on an arbitrary thread
public void onData(RemoteMessage message) {
Console.WriteLine("ManagedTCPMessgageHub.onData recv msg :" + message.remoteConnectionInfo.ToString());
bts.Log.Gen.info($"ManagedTCPMessgageHub.onData recv msg: {message}");
string sender = new BlutoSubAddress(message.remoteConnectionInfo.ip, message.remoteConnectionInfo.port).ToString();
if (message.messageType == RemoteMessageType.Data) {
if (_data_cb != null)
_data_cb(sender, message.message);
else
_received_data_queue.enqueue(new Pair<string, byte[]>(sender, message.message));
} else if (message.messageType == RemoteMessageType.Heartbeat)
_received_hbs.enqueue(sender);
else {
//BlueEventLog.logEvent("Unknown message type from the client", BlueEventLevel.Error);
switch (message.messageType) {
case RemoteMessageType.Data:
// is callback is registered fire it right away, else enqueue it
if (_data_cb != null)
_data_cb(sender, message.message);
else
_received_data_queue.enqueue(new Pair<string, byte[]>(sender, message.message));
break;
case RemoteMessageType.Connection:
// wont happen
break;
case RemoteMessageType.Disconnection:
break;
case RemoteMessageType.Heartbeat:
// dont care about, if it goes missing, we'll get a disconnection message
break;
default:
bts.Log.assert_d(false, "Unknown message type");
break;
}
}
#endregion
class TimmingInfo {
public TimmingInfo() {
myTimer = new Stopwatch();
reset();
}
public void start() {
if (PubSub3Config.instance.PS3_DoItAllSecondsBetweenLogs > 0) {
myTimer.Reset();
myTimer.Start();
}
}
public void stop() {
if (PubSub3Config.instance.PS3_DoItAllSecondsBetweenLogs > 0) {
myTimer.Stop();
myMin = Math.Min(myMin, myTimer.ElapsedMilliseconds);
myMax = Math.Max(myMax, myTimer.ElapsedMilliseconds);
myTotal += myTimer.ElapsedMilliseconds;
myNMeasurements++;
}
}
public void reset() {
myMin = Double.MaxValue;
myMax = Double.MinValue;
myTotal = 0;
myNMeasurements = 0;
}
public override string ToString() {
if (myNMeasurements > 0)
return String.Format("min={0} max={1} avg={2} nMeasurements={3}", myMin, myMax, myTotal / myNMeasurements, myNMeasurements);
else
return "No measurements";
}
private long myNMeasurements = 0;
private Stopwatch myTimer;
private double myMin;
private double myMax;
private double myTotal;
}
private void myDoItAll() {
Dictionary<string, TimmingInfo> times = new Dictionary<string, TimmingInfo>();
times.Add("myHandleNewConnections", new TimmingInfo());
times.Add("myCollectDisposedConnections", new TimmingInfo());
times.Add("myCollectMissingHeartbeatConnections", new TimmingInfo());
times.Add("send", new TimmingInfo());
DateTime lastTimmingLog = DateTime.Now;
private void _main_message_pump() {
while (true) {
try {
while (true) {
///new connections
times["myHandleNewConnections"].start();
myHandleNewConnections();
times["myHandleNewConnections"].stop();
// these are the connections that were received on onConnection
_handle_new_connections();
///dead connections that are disposed
///
times["myCollectDisposedConnections"].start();
myCollectDisposedConnections();
times["myCollectDisposedConnections"].stop();
_collect_disposed_connections();
///dead connections due to missing hbs
times["myCollectMissingHeartbeatConnections"].start();
myCollectMissingHeartbeatConnections();
times["myCollectMissingHeartbeatConnections"].stop();
///sending data - this is where the bulk of the time
///in this thread should be spend
Pair<string, byte[]> p = _send_data_queue.dequeue(_read_send_queue_timeout);
times["send"].start();
// times["send"].start();
if (p != null) {
ITCPConnection conn;
_connection_map.TryGetValue(p.first, out conn);
@@ -261,22 +217,16 @@ namespace BCG {
//BlueEventLog.logEvent(warningMsg, PS3LogLevels.disconnection);
}
}
times["send"].stop();
//times["send"].stop();
if (PubSub3Config.instance.PS3_DoItAllSecondsBetweenLogs > 0 &&
(DateTime.Now - lastTimmingLog).TotalSeconds > PubSub3Config.instance.PS3_DoItAllSecondsBetweenLogs) {
lastTimmingLog = DateTime.Now;
// if (PubSub3Config.instance.PS3_DoItAllSecondsBetweenLogs > 0 &&
// (DateTime.Now - lastTimmingLog).TotalSeconds > PubSub3Config.instance.PS3_DoItAllSecondsBetweenLogs) {
// lastTimmingLog = DateTime.Now;
StringBuilder b = new StringBuilder();
b.AppendFormat("myDoItAll stats:\n");
b.AppendFormat("queue size: {0}\n", _send_data_queue.getCount());
foreach (string timerName in times.Keys) {
b.AppendFormat("{0}: {1}\n", timerName, times[timerName]);
times[timerName].reset();
}
//BlueEventLog.logEvent(b.ToString(), PS3LogLevels.performance);
}
// StringBuilder b = new StringBuilder();
// b.AppendFormat("myDoItAll stats:\n");
// b.AppendFormat("queue size: {0}\n", _send_data_queue.getCount());
// }
}
} catch (Exception ex) {
//BlueEventLog.logEvent(ex);
@@ -286,79 +236,55 @@ namespace BCG {
}
}
private void myCollectDisposedConnections() {
private void _collect_disposed_connections() {
// since we are not listening to onData, connection status, this is used to collect connections
// that might be disposed
if (DateTime.Now - _last_disposed_dead_collection_time > _collect_disposed_connections_period) {
_last_disposed_dead_collection_time = DateTime.Now;
List<string> disposedURLs = new List<string>();
//var disposed_conns = _connection_map.Values.Where(c => c.isDisposed);
List<string> disposed_urls = new List<string>();
foreach (KeyValuePair<string, ITCPConnection> p in _connection_map) {
if (p.Value.isDisposed)
disposedURLs.Add(p.Key);
var url = p.Key;
var conn = p.Value;
if (conn.isDisposed)
disposed_urls.Add(url);
}
foreach (string url in disposedURLs) {
string disposeMsg = String.Format("Collecting disposed connections : {0} is disposed by the connection itself, " +
"connection count before deleting this = {1}", url, _connection_map.Count);
Console.WriteLine(disposeMsg);
//BlueEventLog.logEvent(disposeMsg, PS3LogLevels.disconnection);
foreach (string url in disposed_urls) {
string dispose_msg = $"Collecting disposed connections : {url} is disposed by the connection itself, " +
"connection count before deleting this: {_connection_map.Count}";
bts.Log.Gen.warn(dispose_msg);
_connection_map.Remove(url);
_dead_connection_urls.enqueue(url);
_conn_callback_queue.enqueue(new ConnectionStatusInfo(url, PubSubCallbacks.ConnectionStatus.DISCONNECTED));
}
}
}
private void myCollectMissingHeartbeatConnections() {
return; // disabling HBs for now to simplify things for the demo
if (DateTime.Now - _last_HB_collection_time > _subscriber_drop_threshold) {
_last_HB_collection_time = DateTime.Now;
///these are all the hbs received bw the last
///and current collectDeadConnection calls
///if a connection did not send a heartbeat during
///this interval (which is spanned over a few hb
///intervals, its considered dead)
IList<string> hbs = _received_hbs.dequeueAll();
Dictionary<string, bool> indexedHbs = new Dictionary<string, bool>();
List<string> deadClients = new List<string>();
foreach (string clientURL in hbs)
if (!indexedHbs.ContainsKey(clientURL))
indexedHbs.Add(clientURL, true);
foreach (string clientURL in _connection_map.Keys)
if (!indexedHbs.ContainsKey(clientURL))
deadClients.Add(clientURL);
foreach (string deadClient in deadClients) {
string dispose_msg = String.Format("Collecting missing HB connections : {0} is dead due to missing hbs, Disposing it now, " +
"connection count before deleting this = {1}", deadClient, _connection_map.Count);
bts.Log.Gen.error(dispose_msg);
_connection_map[deadClient].Dispose();
_connection_map.Remove(deadClient);
_dead_connection_urls.enqueue(deadClient);
}
}
}
private void myHandleNewConnections() {
IList<ITCPConnection> newConns = _new_connections.dequeueAll();
foreach (ITCPConnection conn in newConns) {
// these are the connections that were added to _new_connections, on on_connect
// (which gets called in another thread)
private void _handle_new_connections() {
var new_conns = _new_connections.dequeueAll();
foreach (ITCPConnection conn in new_conns) {
string ip;
int port;
conn.getPeerAddress(out ip, out port);
string clientURL = new BlutoSubAddress(ip, port).ToString();
// BlueEventLog.logEvent(String.Format("Handling new connections : {0} connnected, connection count before adding this = {1}",
// clientURL, myConnections.Count), PS3LogLevels.connection);
string client_url = new BlutoSubAddress(ip, port).ToString();
bts.Log.Gen.info("ManagedTCPMessageHub._handle_new_connections client_url: {client_url}");
///to stop it from being garbabge collected immediatly
_received_hbs.enqueue(clientURL);
_received_hbs.enqueue(client_url);
conn.register(this);
Console.WriteLine($"ManagedTCPMessageHub.myHandleNewConnections clientURL: {clientURL} type: {conn.GetType()}");
_connection_map.Add(clientURL, conn);
_new_connection_urls.enqueue(clientURL);
_connection_map.Add(client_url, conn);
// enqueued here to call the user sub in doWork
_conn_callback_queue.enqueue(new ConnectionStatusInfo(client_url, PubSubCallbacks.ConnectionStatus.CONNECTED));
}
}
#region IDisposable Memebers
public void Dispose() {
myIsDisposed = true;
_is_disposed = true;
}
public bool isDisposed {
get { return myIsDisposed; }
get { return _is_disposed; }
}
#endregion
@@ -48,6 +48,7 @@ namespace BCG {
public void doWork() {
if (myTagsReadyToPublish.Count > 0) {
// TODO: PubSubServerMsg
PubMetaData pub = new PubMetaData(tagType, tagName, 0, "local", ClientInfo.thisClient.processString);
MemoryStream str = new MemoryStream();
BinaryWriter bw = new BinaryWriter(str);
@@ -8,20 +8,21 @@ using mbts.spark;
using bts;
namespace BCG {
// This is the main PubSub Server class where it all starts. Its constructed and start is called and thats it.
// This is the main PubSub Server class where it all starts (called by Program.cs). Its constructed and start is called and thats it.
// The only outward facing members of this class is start
// It has an IPubsubMessageHub, that it gets messages from, and updates the PubSubState (which really is all the pubsub server state)
public class PubSubMessageReactor {
private IPubsubMessageHub _msg_hub;
private delegate void MyMsgHandler(string sender, Stream msg, MetaData m);
private Dictionary<string, MyMsgHandler> myOperationToHandler = new Dictionary<string, MyMsgHandler>();
// private delegate void MyMsgHandler(string sender, Stream msg, MetaData m);
// private Dictionary<string, MyMsgHandler> myOperationToHandler = new Dictionary<string, MyMsgHandler>();
private Thread _msg_polling_thread = null;
// all the data,list of subscribers etc are in the state
private PubSubState _state = new PubSubState();
private bool myStop = false;
private MasterWorker myMasterWorker = MasterWorker.getInstance(false);
private double myProcessingWarningThreshold =
Convert.ToDouble(PubSub3Config.instance.PS3_MessageProcessingWarningThreshold);
private HighPerformanceTimer myTimer = new HighPerformanceTimer();
private MasterWorker _mainloop_master_worker = MasterWorker.getInstance(false);
// private double myProcessingWarningThreshold =
// Convert.ToDouble(PubSub3Config.instance.PS3_MessageProcessingWarningThreshold);
// private HighPerformanceTimer myTimer = new HighPerformanceTimer();
private delegate void SingleTagTimeSliceDelegate(string tag, double processingTimeSeconds);
private SingleTagTimeSliceDelegate onSingleTagPub;
@@ -29,35 +30,26 @@ namespace BCG {
//this thing keeps track of whether a given tag is compressed or not. It is used for logging messages.
/* This delegate allows pubsub components to be notified when the identity ofe the pubsub is known*/
private delegate void IdentityFoundDelegate(string identity, bool isRoot);
private event IdentityFoundDelegate onIdentityFound;
private void fireIdentityFound(string identity, bool isRoot) {
if (onIdentityFound != null)
onIdentityFound(identity, isRoot);
}
private Dictionary<string, bool> myTagCompressionState = new Dictionary<string, bool>();
//public PubSubMessageReactor() : this(false, false, string.Empty, string.Empty) { }
//public PubSubMessageReactor(string q) : this(false, false, string.Empty, q) { }
public PubSubMessageReactor(/*bool clearQueue, bool bootEvent, string oldIP, string psq*/) {
myOperationToHandler.Add(typeof(PubMetaData).Name, new MyMsgHandler(myHandlePub));
myOperationToHandler.Add(typeof(SubMetaData).Name, new MyMsgHandler(myHandleSub));
myOperationToHandler.Add(typeof(UnSubMetaData).Name, new MyMsgHandler(myHandleUnsub));
myOperationToHandler.Add(typeof(TransactionMetaData).Name, new MyMsgHandler(myHandleTransaction));
// private delegate void IdentityFoundDelegate(string identity, bool isRoot);
// private event IdentityFoundDelegate onIdentityFound;
// private void fireIdentityFound(string identity, bool isRoot) {
// if (onIdentityFound != null)
// onIdentityFound(identity, isRoot);
// }
//private Dictionary<string, bool> myTagCompressionState = new Dictionary<string, bool>();
public PubSubMessageReactor(int port_no) {
bts.Log.Gen.info("PubSubMessageReactor.cons System Initialized");
_msg_polling_thread = new Thread(new ThreadStart(_polling_thread_func));
_msg_polling_thread.Priority = ThreadPriority.AboveNormal;
// TODO, fix it
var port_no = 12345;
//var port_no = 12345;
_msg_hub = new ManagedTCPMessageHub(port_no,
new TimeSpan(0, 0, 0, 0, PubSub3Config.instance.PS3_HeartBeatInterval),
PubSub3Config.instance.PS3_MissingHeartBeatsForGC,
new TimeSpan(0, 0, 0, 0, PubSub3Config.instance.PS3_DisposedConnectionCheckPeriod));
new TimeSpan(0, 0, 0, 0, PubSub3Config.instance.PS3_HeartBeatInterval),
PubSub3Config.instance.PS3_MissingHeartBeatsForGC,
new TimeSpan(0, 0, 0, 0, PubSub3Config.instance.PS3_DisposedConnectionCheckPeriod));
_msg_hub.register_connection_callback(new PubSubCallbacks.PubSubServerConnectionStatusChange(_handle_client_connection));
@@ -72,199 +64,199 @@ namespace BCG {
}
// all publishes to the pubsub end here
private void myHandlePub(string sender, Stream msg_str, MetaData meta_data) {
bts.Log.Gen.info($"PubSubMessageReactor.myHandlePub sender: {sender} metadata: {meta_data} str.pos: {msg_str.Position} str.len: {msg_str.Length}");
mySingleTagTimeSliceTimer.start();
///the position in the stream is set to where the data starts with,
///since the meta data was already read in the myProcessMsg
PubMetaData pub = new PubMetaData();
meta_data.copyTo(pub);
pub.connection = sender;
pub.warnIfLatencyExceedsThreshold();
var subs_for_tag = _state.getSubscriberConnectionsForTag(pub.tag);
bts.Log.Gen.info($"PubSubMessageReactor.myHandlePub {subs_for_tag.Count} subs for tag: {pub.tag}");
// private void myHandlePub(string sender, Stream msg_str, MetaData meta_data) {
// bts.Log.Gen.info($"PubSubMessageReactor.myHandlePub sender: {sender} metadata: {meta_data} str.pos: {msg_str.Position} str.len: {msg_str.Length}");
// mySingleTagTimeSliceTimer.start();
// ///the position in the stream is set to where the data starts with,
// ///since the meta data was already read in the myProcessMsg
// PubMetaData pub = new PubMetaData();
// meta_data.copyTo(pub);
// pub.connection = sender;
// pub.warnIfLatencyExceedsThreshold();
// var subs_for_tag = _state.getSubscriberConnectionsForTag(pub.tag);
// bts.Log.Gen.info($"PubSubMessageReactor.myHandlePub {subs_for_tag.Count} subs for tag: {pub.tag}");
///for all the subscribers, sends the messages over straightaway
foreach (string address in _state.getSubscriberConnectionsForTag(pub.tag)) {
///the position is adjusted to zero, so as to include
///the meta data as part of the message
msg_str.Position = 0;
bts.Log.Gen.info($"PubSubMessageReactor.myHandlePub sending tag: {pub.tag} to {address} len: {msg_str.Length - msg_str.Position}");
_msg_hub.send(address, msg_str);
}
// ///for all the subscribers, sends the messages over straightaway
// foreach (string address in _state.getSubscriberConnectionsForTag(pub.tag)) {
// ///the position is adjusted to zero, so as to include
// ///the meta data as part of the message
// msg_str.Position = 0;
// bts.Log.Gen.info($"PubSubMessageReactor.myHandlePub sending tag: {pub.tag} to {address} len: {msg_str.Length - msg_str.Position}");
// _msg_hub.send(address, msg_str);
// }
//reset the position of the message so we can decompress it appropriately
msg_str.Position = 0;
msg_str.Position = meta_data.size();
// //reset the position of the message so we can decompress it appropriately
// msg_str.Position = 0;
// msg_str.Position = meta_data.size();
if (pub.isCompressed) {
long mdLength = msg_str.Position;
long compressedSize = msg_str.Length - mdLength;
if (compressedSize == 0) {
string errorMsg = "Error: tag " + pub.tag + " had compressed size of 0 bytes. This shouldnt' happen. Not decompressing tag or sending out update.";
bts.Log.Gen.error(errorMsg);
return;
}
msg_str = StreamDecompressor.decompressStream(msg_str);
long decompressedSize = msg_str.Length - mdLength;
if (decompressedSize != pub.decompressedSize || compressedSize != pub.size) {
string compressionWarning = string.Format("Warning: Stream for tag {0} decompressed from {1} to {2} bytes, with {3} byes of metadata. Metadata expected {4} to {5}. Tag state not updated.",
pub.tag,
compressedSize,
decompressedSize,
mdLength,
pub.size,
pub.decompressedSize);
bts.Log.Gen.warn(compressionWarning);
return;
}
}
// if (pub.isCompressed) {
// long mdLength = msg_str.Position;
// long compressedSize = msg_str.Length - mdLength;
// if (compressedSize == 0) {
// string errorMsg = "Error: tag " + pub.tag + " had compressed size of 0 bytes. This shouldnt' happen. Not decompressing tag or sending out update.";
// bts.Log.Gen.error(errorMsg);
// return;
// }
// msg_str = StreamDecompressor.decompressStream(msg_str);
// long decompressedSize = msg_str.Length - mdLength;
// if (decompressedSize != pub.decompressedSize || compressedSize != pub.size) {
// string compressionWarning = string.Format("Warning: Stream for tag {0} decompressed from {1} to {2} bytes, with {3} byes of metadata. Metadata expected {4} to {5}. Tag state not updated.",
// pub.tag,
// compressedSize,
// decompressedSize,
// mdLength,
// pub.size,
// pub.decompressedSize);
// bts.Log.Gen.warn(compressionWarning);
// return;
// }
// }
//give the user a notification of the compression status of the tag has changed.
if ((!(myTagCompressionState.ContainsKey(pub.tag)) ||
myTagCompressionState[pub.tag] != pub.isCompressed)) {
string compressionMessage;
myTagCompressionState[pub.tag] = pub.isCompressed;
// //give the user a notification of the compression status of the tag has changed.
// if ((!(myTagCompressionState.ContainsKey(pub.tag)) ||
// myTagCompressionState[pub.tag] != pub.isCompressed)) {
// string compressionMessage;
// myTagCompressionState[pub.tag] = pub.isCompressed;
if (pub.isCompressed) {
compressionMessage = "Tag " + pub.tag + " is compressed.";
//BlueEventLog.logEvent(compressionMessage, PS3LogLevels.compressionRatio);
} else {
compressionMessage = "Tag " + pub.tag + " is not compressed.";
//BlueEventLog.logEvent(compressionMessage, PS3LogLevels.compressionRatio);
}
}
///updating state
msg_str.Position = 0;
msg_str.Position += meta_data.size();
_state.updatePubData(pub, msg_str);
mySingleTagTimeSliceTimer.stop();
double thisTagProcessingTime = mySingleTagTimeSliceTimer.lastIntervalTime;
if (onSingleTagPub != null)
onSingleTagPub(pub.tag, thisTagProcessingTime);
// if (pub.isCompressed) {
// compressionMessage = "Tag " + pub.tag + " is compressed.";
// //BlueEventLog.logEvent(compressionMessage, PS3LogLevels.compressionRatio);
// } else {
// compressionMessage = "Tag " + pub.tag + " is not compressed.";
// //BlueEventLog.logEvent(compressionMessage, PS3LogLevels.compressionRatio);
// }
// }
// ///updating state
// msg_str.Position = 0;
// msg_str.Position += meta_data.size();
// _state.updatePubData(pub, msg_str);
// mySingleTagTimeSliceTimer.stop();
// double thisTagProcessingTime = mySingleTagTimeSliceTimer.lastIntervalTime;
// if (onSingleTagPub != null)
// onSingleTagPub(pub.tag, thisTagProcessingTime);
}
// }
private void myHandleSub(string sender, Stream msg, MetaData m) {
bts.Log.Gen.info($"PubSubMessageReactor.myHandleSub sender: {sender} metadata: {m} str.pos: {msg.Position} str.len: {msg.Length}");
SubMetaData sub = new SubMetaData();
m.copyTo(sub);
sub.connection = sender;
// private void myHandleSub(string sender, Stream msg, MetaData m) {
// bts.Log.Gen.info($"PubSubMessageReactor.myHandleSub sender: {sender} metadata: {m} str.pos: {msg.Position} str.len: {msg.Length}");
// SubMetaData sub = new SubMetaData();
// m.copyTo(sub);
// sub.connection = sender;
///update the state
_state.sub(sub);
///to all these new subscriber s, send the existing data
foreach (string tag in sub.tags) {
byte[] data = _state.getContent(tag);
//ie data has been published once
if (data != null) {
string tagType = _state.getTagType(tag);
///passing "" as the 4th arg, as PubMeta is used both by the
///publisher to publish data to the server (where the publisher
///indicates its identification as the 4th arg) as well as by
///the server to send data to the client (like here). we can
///identify the publisher by filing in the 4th arg, but its not
///needed as of now, so its left black to save a few bytes
PubMetaData pub;
MemoryStream realContent;
if (PubSub3Config.instance.isTagCompressed(tag)) {
SharpZipCompressor Kompessor = new SharpZipCompressor();
realContent = new MemoryStream();
Kompessor.compress(new MemoryStream(data), realContent);
// ///update the state
// _state.sub(sub);
// ///to all these new subscriber s, send the existing data
// foreach (string tag in sub.tags) {
// byte[] data = _state.getContent(tag);
// //ie data has been published once
// if (data != null) {
// string tagType = _state.getTagType(tag);
// ///passing "" as the 4th arg, as PubMeta is used both by the
// ///publisher to publish data to the server (where the publisher
// ///indicates its identification as the 4th arg) as well as by
// ///the server to send data to the client (like here). we can
// ///identify the publisher by filing in the 4th arg, but its not
// ///needed as of now, so its left black to save a few bytes
// PubMetaData pub;
// MemoryStream realContent;
// if (PubSub3Config.instance.isTagCompressed(tag)) {
// SharpZipCompressor Kompessor = new SharpZipCompressor();
// realContent = new MemoryStream();
// Kompessor.compress(new MemoryStream(data), realContent);
pub = new PubMetaData(tagType, tag, (int)realContent.Length, (int)data.Length, "", "");
// pub = new PubMetaData(tagType, tag, (int)realContent.Length, (int)data.Length, "", "");
} else {
realContent = new MemoryStream(data);
pub = new PubMetaData(tagType, tag, data.Length, "", "");
}
// } else {
// realContent = new MemoryStream(data);
// pub = new PubMetaData(tagType, tag, data.Length, "", "");
// }
MemoryStream toSend = new MemoryStream();
new MetaData(pub).save(toSend);
toSend.Write(realContent.ToArray(), 0, (int)realContent.Length);
// MemoryStream toSend = new MemoryStream();
// new MetaData(pub).save(toSend);
// toSend.Write(realContent.ToArray(), 0, (int)realContent.Length);
toSend.Position = 0;
_msg_hub.send(sub.connection, toSend);
}
}
}
// toSend.Position = 0;
// _msg_hub.send(sub.connection, toSend);
// }
// }
// }
private void myHandleUnsub(string sender, Stream msg, MetaData m) {
UnSubMetaData unsub = new UnSubMetaData();
m.copyTo(unsub);
if (unsub.connection.Trim().Length == 0)
unsub.connection = sender;
_state.unsub(unsub);
}
// private void myHandleUnsub(string sender, Stream msg, MetaData m) {
// UnSubMetaData unsub = new UnSubMetaData();
// m.copyTo(unsub);
// if (unsub.connection.Trim().Length == 0)
// unsub.connection = sender;
// _state.unsub(unsub);
// }
private void myHandleTransaction(string sender, Stream msg, MetaData md) {
mySingleTagTimeSliceTimer.start();
TransactionMetaData t = new TransactionMetaData();
md.copyTo(t);
int nMessages = t.nMessages;
///form the structure tag->byte[]*
Dictionary<string, byte[]> tagToMessages = new Dictionary<string, byte[]>();
BinaryReader br = new BinaryReader(msg);
for (int i = 0; i < nMessages; i++) {
MetaData pubMd = new MetaData(msg);
PubMetaData pub = new PubMetaData();
pubMd.copyTo(pub);
pub.connection = sender;
byte[] tagData = new byte[pub.size];
msg.Read(tagData, 0, tagData.Length);
//report the fact that we have a publish on this tag, if it's a debug tag
if (PubSub3Config.instance.isTagDebugTag(pub.tag)) {
// BlueEventLog.logEvent(string.Concat(GetType().Name,
// " got publish on tag ",
// pub.tag,
// "\nfrom connection ",
// pub.connection, ".\n",
// pub.isCompressed ? "Data was compressed.\n" : "Data was not compressed.\n",
// "total size: ",
// tagData.Length,
// "\nMessage ",
// i,
// " of a transaction with ",
// nMessages,
// " total messages."),
// private void myHandleTransaction(string sender, Stream msg, MetaData md) {
// mySingleTagTimeSliceTimer.start();
// TransactionMetaData t = new TransactionMetaData();
// md.copyTo(t);
// int nMessages = t.nMessages;
// ///form the structure tag->byte[]*
// Dictionary<string, byte[]> tagToMessages = new Dictionary<string, byte[]>();
// BinaryReader br = new BinaryReader(msg);
// for (int i = 0; i < nMessages; i++) {
// MetaData pubMd = new MetaData(msg);
// PubMetaData pub = new PubMetaData();
// pubMd.copyTo(pub);
// pub.connection = sender;
// byte[] tagData = new byte[pub.size];
// msg.Read(tagData, 0, tagData.Length);
// //report the fact that we have a publish on this tag, if it's a debug tag
// if (PubSub3Config.instance.isTagDebugTag(pub.tag)) {
// // BlueEventLog.logEvent(string.Concat(GetType().Name,
// // " got publish on tag ",
// // pub.tag,
// // "\nfrom connection ",
// // pub.connection, ".\n",
// // pub.isCompressed ? "Data was compressed.\n" : "Data was not compressed.\n",
// // "total size: ",
// // tagData.Length,
// // "\nMessage ",
// // i,
// // " of a transaction with ",
// // nMessages,
// // " total messages."),
// PS3LogLevels.debug);
// // PS3LogLevels.debug);
}
// }
_state.updatePubData(pub, new MemoryStream(tagData));
tagToMessages.Add(pub.tag, tagData);
}
///this gives subscriber->tag*
Dictionary<string, List<string>> subscriberToTags = _state.getSubscriberConnectionsForTags(tagToMessages.Keys);
foreach (KeyValuePair<string, List<string>> kvp in subscriberToTags) {
MemoryStream subStream = new MemoryStream();
TransactionMetaData subTransaction = new TransactionMetaData(kvp.Value.Count);
new MetaData(subTransaction).save(subStream);
foreach (string tag in kvp.Value) {
byte[] tagData = tagToMessages[tag];
PubMetaData pub = new PubMetaData(_state.getTagType(tag), tag, tagData.Length, "", "");
new MetaData(pub).save(subStream);
subStream.Write(tagData, 0, tagData.Length);
}
subStream.Position = 0;
//myState.send(kvp.Key, subStream);
_msg_hub.send(kvp.Key, subStream);
}
// _state.updatePubData(pub, new MemoryStream(tagData));
// tagToMessages.Add(pub.tag, tagData);
// }
// ///this gives subscriber->tag*
// Dictionary<string, List<string>> subscriberToTags = _state.getSubscriberConnectionsForTags(tagToMessages.Keys);
// foreach (KeyValuePair<string, List<string>> kvp in subscriberToTags) {
// MemoryStream subStream = new MemoryStream();
// TransactionMetaData subTransaction = new TransactionMetaData(kvp.Value.Count);
// new MetaData(subTransaction).save(subStream);
// foreach (string tag in kvp.Value) {
// byte[] tagData = tagToMessages[tag];
// PubMetaData pub = new PubMetaData(_state.getTagType(tag), tag, tagData.Length, "", "");
// new MetaData(pub).save(subStream);
// subStream.Write(tagData, 0, tagData.Length);
// }
// subStream.Position = 0;
// //myState.send(kvp.Key, subStream);
// _msg_hub.send(kvp.Key, subStream);
// }
mySingleTagTimeSliceTimer.stop();
double timeExpended = mySingleTagTimeSliceTimer.lastIntervalTime;
//Don't try to divide by zero
int numTags = tagToMessages.Keys.Count;
if (numTags != 0) {
double timeExpendedPerTag = timeExpended / tagToMessages.Keys.Count;
if (onSingleTagPub != null)
foreach (string tag in tagToMessages.Keys)
onSingleTagPub(tag, timeExpendedPerTag);
}
}
// mySingleTagTimeSliceTimer.stop();
// double timeExpended = mySingleTagTimeSliceTimer.lastIntervalTime;
// //Don't try to divide by zero
// int numTags = tagToMessages.Keys.Count;
// if (numTags != 0) {
// double timeExpendedPerTag = timeExpended / tagToMessages.Keys.Count;
// if (onSingleTagPub != null)
// foreach (string tag in tagToMessages.Keys)
// onSingleTagPub(tag, timeExpendedPerTag);
// }
// }
/// <summary>
/// Extracts the meta data out, and based on the meta data calls
@@ -272,12 +264,12 @@ namespace BCG {
/// </summary>
/// <param name="msg"></param>
/// <returns></returns>
private void myProcessMsg(string sender, Stream msg, out MetaData md) {
md = new MetaData(msg); // msg's Posiiton moved to end of meta data
MyMsgHandler op = myOperationToHandler[md["op-code"]];
bts.Log.Gen.info($"PubSubMessageReactor.myProcessMsg sender: {sender} metadata: {md}");
op(sender, msg, md);
}
// private void myProcessMsg(string sender, Stream msg, out MetaData md) {
// md = new MetaData(msg); // msg's Posiiton moved to end of meta data
// MyMsgHandler op = myOperationToHandler[md["op-code"]];
// bts.Log.Gen.info($"PubSubMessageReactor.myProcessMsg sender: {sender} metadata: {md}");
// op(sender, msg, md);
// }
#endregion
@@ -320,22 +312,11 @@ namespace BCG {
// msg = null;
// meta = null;
try {
List<Pair<string, byte[]>> receivedData = _msg_hub.get_updates(poll_timeout, 1);
foreach (var kv in receivedData) {
var received_data = _msg_hub.get_updates(poll_timeout, 1);
foreach (var kv in received_data) {
_handle_client_msg(kv.first, kv.second);
}
// if (receivedData.Count > 0) {
// sender = receivedData[0].first;
// msg = new MemoryStream(receivedData[0].second);
// myTimer.start();
// myProcessMsg(sender, msg, out meta);
// myTimer.stop();
// if (myTimer.lastIntervalTime > myProcessingWarningThreshold) {
// string warningMsg = String.Format("processing metadata {0} took {1} msecs", meta, myTimer.lastIntervalTime);
// //BlueEventLog.logEvent(warningMsg, PS3LogLevels.performance);
// }
// }
myMasterWorker.doWork();
_mainloop_master_worker.doWork();
} catch (Exception ex) {
bts.Log.Gen.error($"PubSubMessage>reactor._polling_thread_func ex: {ex}");
if (ex is OutOfMemoryException)
@@ -343,7 +324,7 @@ namespace BCG {
}
}
}
// all messages sent to the client are handled here
private void _handle_client_msg(string sender, byte[] msg) {
// TODO: reduce object creation, C# has an ObjectPool
PubSubClientMessage ps_client_msg = new PubSubClientMessage();
@@ -367,14 +348,12 @@ namespace BCG {
private void _handle_sub(string sender, SubscribeMsg sub_msg) {
bts.Log.Gen.info($"PubSubMessageReactor._handle_sub sender: {sender} sub_msg: {sub_msg}");
// SubMetaData sub = new SubMetaData();
// m.copyTo(sub);
// sub.connection = sender;
///update the state
_state.sub(sub_msg); // state keeps the list of all subscribers
_state.handle_sub(sub_msg); // state keeps the list of all subscribers
///to all these new subscriber s, send the existing data
foreach (string tag in sub_msg.tags) {
byte[] data = _state.getContent(tag);
//ie data has been published once
if (data != null) {
@@ -408,23 +387,20 @@ namespace BCG {
bts.Log.Gen.info($"PubSubMessageReactor._handle_pub sending tag: {pub_msg.tag} to {client_url}");
_msg_hub.send(client_url, msg_to_send);
}
// if we had to persist the state, this is where we'll do it
}
private void _handle_transaction(string sender, TransactionMsg transaction_msg) {
bts.Log.assert_d(false, $"transactions are not supported yet msg: {transaction_msg}");
}
private void _handle_unsub(string sender, UnsubscribeMsg ubnsub_msg) {
// TODO: check ubnsub_msg.connection
// UnSubMetaData unsub = new UnSubMetaData();
// m.copyTo(unsub);
// if (unsub.connection.Trim().Length == 0)
// unsub.connection = sender;
_state.unsub(ubnsub_msg);
_state.handle_unsub(ubnsub_msg);
}
public static void internalSend(byte[] data) {
// TODO
//myReceivingQueue.enqueue(new Pair<string, byte[]>(internalPublisherConnection, data));
}
}
@@ -61,7 +61,7 @@ namespace BCG {
onTagMapChanged(sub.tags);
}
public void sub(bts.SubscribeMsg sub_msg) {
public void handle_sub(bts.SubscribeMsg sub_msg) {
// TODO
}
@@ -83,7 +83,7 @@ namespace BCG {
onTagMapChanged(unsub.tags);
}
public void unsub(bts.UnsubscribeMsg ubnsub_msg) {
public void handle_unsub(bts.UnsubscribeMsg ubnsub_msg) {
}
@@ -152,7 +152,9 @@ namespace BCG {
}
public void update_pub_data(bts.PublishMsg pub_msg) { }
public void update_pub_data(bts.PublishMsg pub_msg) {
// TODO
}
#endregion