173 lines
6.3 KiB
C#
173 lines
6.3 KiB
C#
using System;
|
|
//using System.Messaging;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Threading;
|
|
using System.Text.RegularExpressions;
|
|
|
|
using BCG;
|
|
using BCG.Utilities;
|
|
using BCG.Utilities.General;
|
|
using mbts;
|
|
using mbts.spark;
|
|
//using BCG.Utilities.BlueConfiguration;
|
|
|
|
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 StaticSubscriberCore : ISubscriberCore {
|
|
private Dictionary<string, ISingleServerSubscriberCore> myServerURLToSubCore = new Dictionary<string, ISingleServerSubscriberCore>();
|
|
private IPubSubRouter myRouter;
|
|
private HighPerformanceTimer myTimer = new HighPerformanceTimer();
|
|
private PubSubServerConnectionFactory myConnectionFactory = new PubSubServerConnectionFactory(true);
|
|
|
|
public event PubSubConnectionChange onPubSubConnectionChange;
|
|
|
|
|
|
#region constructors
|
|
|
|
public StaticSubscriberCore() {
|
|
//this defaults to a basic pubsubrouter if the config key is not filled out
|
|
myInit(myGetRouter());
|
|
}
|
|
public StaticSubscriberCore(string siteName) {
|
|
myInit(new PubSubRouter(PubSub3Config.instance.getRouting(siteName)));
|
|
}
|
|
|
|
private IPubSubRouter myGetRouter() { return new PubSubRouter(PubSub3Config.instance.getRouting()); }
|
|
|
|
public StaticSubscriberCore(PubSubRoute[] routes) {
|
|
myInit(new PubSubRouter(routes));
|
|
}
|
|
|
|
public StaticSubscriberCore(IPubSubRouter router) {
|
|
myInit(router);
|
|
}
|
|
|
|
#endregion
|
|
|
|
private void myInit(IPubSubRouter router) {
|
|
myRouter = router;
|
|
}
|
|
|
|
public ArrayList getUpdates() {
|
|
return getUpdates(10);
|
|
}
|
|
|
|
public ArrayList getUpdates(TimeSpan timeout) {
|
|
return getUpdates(Convert.ToInt32(timeout.TotalMilliseconds));
|
|
}
|
|
|
|
public ArrayList getUpdates(int timeout) {
|
|
//BCG.BlueTrace.WriteTraceMethodEnterW("StaticSubscriberCore.getUpdates");
|
|
lock (myServerURLToSubCore) {
|
|
|
|
if (myServerURLToSubCore.Count == 0) {
|
|
return new ArrayList();
|
|
} else {
|
|
///gets a time share for every subscriber and
|
|
///calls their updates
|
|
myTimer.start();
|
|
int timeShare = timeout / myServerURLToSubCore.Count;
|
|
List<PubSubUpdate> retVal = new List<PubSubUpdate>();
|
|
foreach (ISingleServerSubscriberCore subCore in myServerURLToSubCore.Values)
|
|
retVal.AddRange(subCore.getUpdates(timeShare));
|
|
_log_n_updates(retVal);
|
|
myTimer.stop();
|
|
//if (myTimer.lastIntervalTime > 10)
|
|
// BlueEventLog.logEvent("get updates taking " + myTimer.lastIntervalTime, BlueEventLevel.Error);
|
|
|
|
//BCG.BlueTrace.WriteTraceMethodExitW("StaticSubscriberCore.getUpdates");
|
|
return new ArrayList(retVal);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
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}");
|
|
}
|
|
}
|
|
|
|
private Dictionary<string, bool> myBlueCommTags = new Dictionary<string, bool>();
|
|
|
|
bool myIsBlueCommTag(string tag) {
|
|
return false;
|
|
}
|
|
|
|
public void subscribe(params string[] tags) {
|
|
lock (myServerURLToSubCore) {
|
|
foreach (string tag in tags) {
|
|
if (myIsBlueCommTag(tag)) {
|
|
} else {
|
|
string serverURL = myRouter.getSubURLForTag(tag);
|
|
if (!myServerURLToSubCore.ContainsKey(serverURL)) {
|
|
// BlueEventLog.logEvent("Creating a SingleServerSubscriberCore for " + serverURL, BlueEventLevel.Information);
|
|
myServerURLToSubCore.Add(serverURL, new SingleServerSubscriberCore(serverURL, myConnectionFactory));
|
|
myServerURLToSubCore[serverURL].onPubSubConnectionChange += myHandleConnectionChange;
|
|
}
|
|
myServerURLToSubCore[serverURL].subscribe(tag);
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
public void myHandleConnectionChange(bool isConnected, string connection) {
|
|
if (onPubSubConnectionChange != null)
|
|
onPubSubConnectionChange(isConnected, connection);
|
|
|
|
}
|
|
|
|
public void unsubscribe(params string[] tags) {
|
|
lock (myServerURLToSubCore) {
|
|
|
|
foreach (string tag in tags) {
|
|
if (myIsBlueCommTag(tag))
|
|
myServerURLToSubCore["bluecomm"].unsubscribe(tag);
|
|
else {
|
|
string serverURL = myRouter.getSubURLForTag(tag);
|
|
|
|
if (!myServerURLToSubCore.ContainsKey(serverURL)) {
|
|
// BlueEventLog.logEvent("Creating a SingleServerSubscriberCore for " + serverURL, BlueEventLevel.Information);
|
|
myServerURLToSubCore.Add(serverURL, new SingleServerSubscriberCore(serverURL, myConnectionFactory));
|
|
}
|
|
myServerURLToSubCore[serverURL].unsubscribe(tag);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
public IPubSubRouter getRouter() {
|
|
return myRouter;
|
|
}
|
|
|
|
#region IDisposable Members
|
|
|
|
public void Dispose() {
|
|
|
|
foreach (ISingleServerSubscriberCore sssc in myServerURLToSubCore.Values)
|
|
sssc.Dispose();
|
|
myConnectionFactory.Dispose();
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|