127 lines
4.5 KiB
C#
127 lines
4.5 KiB
C#
using bts;
|
|
|
|
using Bts.PubSub;
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace BCG {
|
|
|
|
/// <summary>
|
|
/// This class serves as the subscrition interface to the pubsub. The only methods
|
|
/// it exposes are getUpdates and subscribe. Internally, it manages a SingleServerSubscriberCore
|
|
/// foreach server. So this class does the routing part, and each SingleServerSubscriberCore
|
|
/// handles dealing sending (sub/unsub) and receiving (data) messages from the transport.
|
|
/// There is no sharing with respect to this class. Ie, it creates new SingleServerSubscriberCore
|
|
/// to the servers even though there might be so created by other instances of the SubscriberCore
|
|
///
|
|
/// </summary>
|
|
public class SubscriberCore : ISubscriberCore {
|
|
private Dictionary<RemoteConnectionInfo, ISingleServerSubscriberCore> _server_route_to_subcore_map =
|
|
new Dictionary<RemoteConnectionInfo, ISingleServerSubscriberCore>();
|
|
private IPubSubRouter _tag_router;
|
|
private PubSubServerConnectionFactory _connection_factory = new PubSubServerConnectionFactory(true);
|
|
|
|
public event PubSubConnectionChange onPubSubConnectionChange;
|
|
|
|
|
|
#region constructors
|
|
|
|
public SubscriberCore() {
|
|
//this defaults to a basic pubsubrouter if the config key is not filled out
|
|
_tag_router = new PubSubRouter(Log.notnull(PubSubConfig.Instance.SubscriberRoutes).ToArray());
|
|
}
|
|
public SubscriberCore(string site_name) {
|
|
_tag_router = new PubSubRouter(PubSubConfig.Instance.SubscriberRoutesFor(site_name).ToArray());
|
|
}
|
|
|
|
private IPubSubRouter _get_router() =>
|
|
new PubSubRouter(Log.notnull(PubSubConfig.Instance.SubscriberRoutes).ToArray());
|
|
|
|
public SubscriberCore(PubSubRoute[] routes) {
|
|
_tag_router = new PubSubRouter(routes);
|
|
}
|
|
|
|
public SubscriberCore(IPubSubRouter router) {
|
|
_tag_router = router;
|
|
}
|
|
|
|
#endregion
|
|
|
|
public IEnumerable<bts.PubSubServerMessage> get_updates(TimeSpan timeout) {
|
|
List<bts.PubSubServerMessage> rv = new List<bts.PubSubServerMessage>();
|
|
foreach (var kv in _server_route_to_subcore_map) {
|
|
var sub_core = kv.Value;
|
|
rv.AddRange(sub_core.get_updates(timeout));
|
|
}
|
|
return rv;
|
|
}
|
|
|
|
private IDictionary<string, int> _n_update_map = new Dictionary<string, int>();
|
|
private void _log_n_updates(IEnumerable<PubSubUpdate> updates) {
|
|
foreach (PubSubUpdate u in updates) {
|
|
if (!_n_update_map.ContainsKey(u.tag)) {
|
|
_n_update_map.Add(u.tag, 0);
|
|
}
|
|
_n_update_map[u.tag] = _n_update_map[u.tag] + 1;
|
|
}
|
|
// log it now
|
|
foreach (var kv in _n_update_map) {
|
|
bts.Log.Gen.info($"StaticSubscrberCore tag: {kv.Key} n_updates: {kv.Value}");
|
|
}
|
|
}
|
|
|
|
public void subscribe(params string[] tags) {
|
|
lock (_server_route_to_subcore_map) {
|
|
foreach (string tag in tags) {
|
|
RemoteConnectionInfo server_rci = _tag_router.get_sub_route_for_tag(tag);
|
|
var server_sub_core = _find_subscribercore(server_rci);
|
|
server_sub_core.subscribe(tag);
|
|
}
|
|
}
|
|
}
|
|
|
|
private ISingleServerSubscriberCore _find_subscribercore(RemoteConnectionInfo server_rci) {
|
|
ISingleServerSubscriberCore rv = null;
|
|
if (!_server_route_to_subcore_map.TryGetValue(server_rci, out rv)) {
|
|
rv = new SingleServerSubscriberCore(server_rci, _connection_factory);
|
|
_server_route_to_subcore_map.Add(server_rci, rv);
|
|
rv.onPubSubConnectionChange += _handle_pubsubserver_connection_change;
|
|
}
|
|
return rv;
|
|
}
|
|
|
|
|
|
public void _handle_pubsubserver_connection_change(bool is_connected, string server_url) {
|
|
if (onPubSubConnectionChange != null)
|
|
onPubSubConnectionChange(is_connected, server_url);
|
|
|
|
}
|
|
|
|
public void unsubscribe(params string[] tags) {
|
|
lock (_server_route_to_subcore_map) {
|
|
|
|
foreach (string tag in tags) {
|
|
var server_rci = _tag_router.get_sub_route_for_tag(tag);
|
|
var server_subcore = _find_subscribercore(server_rci);
|
|
server_subcore.unsubscribe(tag);
|
|
}
|
|
}
|
|
}
|
|
|
|
public IPubSubRouter get_router() {
|
|
return _tag_router;
|
|
}
|
|
|
|
#region IDisposable Members
|
|
|
|
public void Dispose() {
|
|
foreach (var sssc in _server_route_to_subcore_map.Values)
|
|
sssc.Dispose();
|
|
_connection_factory.Dispose();
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|