230 lines
11 KiB
C#
230 lines
11 KiB
C#
using System;
|
|
using System.Threading;
|
|
|
|
using bts;
|
|
|
|
using Bts.PubSub;
|
|
|
|
using Spark.Core;
|
|
using System.Text;
|
|
using System.Diagnostics;
|
|
|
|
namespace BCG {
|
|
// 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 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 _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;
|
|
private static SyncQueue<PublishMsg> _internal_pub_queue = new SyncQueue<PublishMsg>();
|
|
private Stopwatch _stop_watch = new Stopwatch();
|
|
//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(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;
|
|
_msg_hub = new ManagedTCPMessageHub(
|
|
port_no,
|
|
TimeSpan.FromMilliseconds(PubSubConfig.Instance.HeartbeatInterval.assert_value()),
|
|
PubSubConfig.Instance.MissingHeartbeatsBeforeGc.assert_value(),
|
|
TimeSpan.FromMilliseconds(PubSubConfig.Instance.DisposedConnectionCheckPeriod.assert_value()));
|
|
|
|
_msg_hub.register_connection_callback(new PubSubCallbacks.PubSubServerConnectionStatusChange(_handle_client_connection));
|
|
|
|
}
|
|
|
|
// the connection callback is the only event we get back from the message_hub.
|
|
private void _handle_client_connection(string client, PubSubCallbacks.ConnectionStatus conn_status) {
|
|
bts.Log.Gen.info($"PubsubMessageReactor._handle_client_connection client: {client} conn_status: {conn_status}");
|
|
///interested in disconnections only, as connections of no
|
|
///significance until they subscribe, in which case an entry
|
|
///will be made
|
|
if (conn_status == PubSubCallbacks.ConnectionStatus.DISCONNECTED)
|
|
_state.remove_connection(client);
|
|
}
|
|
|
|
private void stop() {
|
|
myStop = true;
|
|
}
|
|
|
|
public void start() {
|
|
_msg_polling_thread.Start();
|
|
}
|
|
|
|
/// <summary>
|
|
/// This is the main PubSubServer thread. It loops around and read messages from
|
|
/// the IPubsubMessageHub. It then deserializes the messages and calls the approrpriate
|
|
/// hanlder
|
|
/// </summary>
|
|
private void _polling_thread_func() {
|
|
Log.Gen.info("PubSubMessageReactor._polling_thread_func starting thread function _polling_thread_func");
|
|
TimeSpan poll_timeout = new TimeSpan(0, 0, 0, 0, 2500);
|
|
_msg_hub.activate();
|
|
Action handle_internal_pubs = () => {
|
|
Log.Gen.info($"PubSubMessageReactor._polling_thread_func handle_internal_pubs");
|
|
var internal_pubs = _internal_pub_queue.dequeueMultipleElements(TimeSpan.FromSeconds(1), 64000);
|
|
//Log.Gen.info($"PubSubMessageReactor._polling_thread_func internal_pubs count: {internal_pubs.Count}");
|
|
foreach (var msg in internal_pubs) {
|
|
try {
|
|
_handle_pub("Internal", msg, true);
|
|
} catch (Exception ex) {
|
|
bts.Log.Gen.error($"PubSubMessage>reactor._polling_thread_func ex: {ex}");
|
|
}
|
|
}
|
|
};
|
|
|
|
while (true) {
|
|
try {
|
|
//bts.Log.Gen.info($"PubSubMessageReactor._polling_thread_func pre get_updates");
|
|
var received_data = _msg_hub.get_all_updates();
|
|
var n_msgs = received_data.Count;
|
|
bts.Log.Gen.info($"PubSubMessageReactor._polling_thread_func post get_updates n_msgs: {n_msgs}");
|
|
foreach (var kv in received_data) {
|
|
// multiple pubs for the same tags should be batched here
|
|
_handle_client_msg(kv.first, kv.second);
|
|
}
|
|
handle_internal_pubs();
|
|
_mainloop_master_worker.doWork();
|
|
} catch (Exception ex) {
|
|
bts.Log.Gen.error($"PubSubMessageReactor._polling_thread_func ex: {ex}");
|
|
if (ex is OutOfMemoryException)
|
|
throw ex;
|
|
}
|
|
}
|
|
}
|
|
public static string to_hex(byte[] byte_arr, int start_pos, int end_pos) {
|
|
var len = end_pos - start_pos;
|
|
StringBuilder hex_builder = new StringBuilder(len * 2);
|
|
int i = 0;
|
|
|
|
foreach (byte b in byte_arr) {
|
|
if (i >= start_pos && i < end_pos)
|
|
hex_builder.Append($"{b:X2} ");
|
|
i++;
|
|
}
|
|
return hex_builder.ToString().Trim();
|
|
}
|
|
|
|
// all messages (polled from the message_hub) sent to the client are handled here
|
|
private void _handle_client_msg(string sender, byte[] msg) {
|
|
//_stop_watch.Restart();
|
|
var stop_watch = Stopwatch.StartNew();
|
|
//string msg_txt = to_hex(msg, 0, msg.Length);
|
|
Log.Gen.info($"PubSubMessageReactor._handle_client_msg sender: {sender} msg: {msg.Length}");
|
|
// TODO: reduce object creation, C# has an ObjectPool
|
|
PubSubClientMessage ps_client_msg = new PubSubClientMessage();
|
|
ps_client_msg.consruct_deserialize(msg);
|
|
bts.Log.Gen.info($"PubsubMessageReactor._handle_client_msg sender: {sender} ps_client_msg: {ps_client_msg}");
|
|
switch (ps_client_msg.msg_type) {
|
|
case PubSubMessageType.SUBSCRIBE:
|
|
_handle_sub(sender, ps_client_msg.sub_msg);
|
|
break;
|
|
case PubSubMessageType.UNSUBSCRIIBE:
|
|
_handle_unsub(sender, ps_client_msg.unsub_msg);
|
|
break;
|
|
case PubSubMessageType.TRANSACTION:
|
|
_handle_transaction(sender, ps_client_msg.transaction_msg);
|
|
break;
|
|
case PubSubMessageType.PUBLISH:
|
|
_handle_pub(sender, ps_client_msg.pub_msg, false);
|
|
break;
|
|
}
|
|
stop_watch.Stop();
|
|
Log.Gen.info($"PubSubMessageReactor._handle_client_msg processed ps_client_msg: {ps_client_msg} processing time: {stop_watch.ElapsedMilliseconds} msecs");
|
|
}
|
|
|
|
private void _handle_sub(string sender, SubscribeMsg sub_msg) {
|
|
//TODO: we make this correction here, since C++ clients are not sending this correctly
|
|
bts.Log.Gen.info($"PubSubMessageReactor._handle_sub sender: {sender} sub_msg: {sub_msg}");
|
|
if (String.IsNullOrEmpty(sub_msg.connection) || sub_msg.connection.Contains("unset")) {
|
|
sub_msg.connection = sender;
|
|
}
|
|
bts.Log.Gen.info($"PubSubMessageReactor._handle_sub(corrected) sender: {sender} sub_msg: {sub_msg}");
|
|
|
|
///update the state
|
|
_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.get_content(tag);
|
|
//ie data has been published once
|
|
if (data != null) {
|
|
bts.Log.Gen.info($"ManagedTCPMessgeHub._handle_sub get_content tag: {tag} data len: {data.Length}");
|
|
string tag_type_ = _state.getTagType(tag);
|
|
var msg_to_send = new PubSubServerMessage() {
|
|
tag = tag,
|
|
tag_type = tag_type_,
|
|
data = data
|
|
};
|
|
bts.Log.Gen.info($"ManagedTCPMessgeHub._handle_sub sending msg_to_send: {msg_to_send} connection: {sub_msg.connection}");
|
|
_msg_hub.send(sub_msg.connection, ps_msg: msg_to_send);
|
|
return;
|
|
} else {
|
|
bts.Log.Gen.error($"ManagedTCPMessgeHub._handle_sub could not retreive data for sub_msg: {sub_msg}");
|
|
}
|
|
}
|
|
|
|
}
|
|
private void _handle_pub(string sender, PublishMsg pub_msg, bool is_internal_pub) {
|
|
bts.Log.assert(!pub_msg.is_compressed);
|
|
//bts.Log.Gen.info($"PubSubMessageReactor._handle_pub sender: {sender} pub_msg: {pub_msg} is_internal_pub: {is_internal_pub}");
|
|
// this is where pubsub's data state is updated
|
|
_state.update_pub_data(pub_msg);
|
|
var sub_connections_for_tag = _state.get_subscriber_connections_for_tag(pub_msg.tag);
|
|
bts.Log.Gen.info($"PubSubMessageReactor._handle_pub {sub_connections_for_tag.Count} subs for tag: {pub_msg.tag}");
|
|
bts.PubSubServerMessage msg_to_send = new PubSubServerMessage() {
|
|
tag = pub_msg.tag,
|
|
tag_type = pub_msg.tag_type,
|
|
data = pub_msg.data_to_publish
|
|
};
|
|
|
|
///for all the subscribers, sends the messages over straightaway
|
|
foreach (string client_url in sub_connections_for_tag) {
|
|
//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
|
|
_state.handle_unsub(ubnsub_msg);
|
|
}
|
|
|
|
public static void internal_publish(PublishMsg msg) {
|
|
bts.Log.Gen.diag($"PubsubMessageReactor.internal_publish msg: {msg}");
|
|
bts.Log.Gen.debug_assert(!String.IsNullOrEmpty(msg.tag));
|
|
_internal_pub_queue.enqueue(msg);
|
|
}
|
|
}
|
|
}
|