Files
spark/src/DataStreamSuite/PubSub/Client/Routing/PubSubRouter.cs
T

102 lines
3.3 KiB
C#

using System;
using System.Text.RegularExpressions;
using System.Collections;
using System.Collections.Generic;
using BCG;
//using BCG.Utilities;
//using bts;
namespace BCG {
public class PubSubRouter : IPubSubRouter {
private Regex[] myPubRouteRegExps;
private Regex[] mySubRouteRegExps;
private PubSubRoute[] myPubRoutes;
private PubSubRoute[] mySubRoutes;
private readonly string _temp_only_pubsub = "bluto://127.0.0.1:1976";
public PubSubRouter() : this(
PubSub3Config.instance.getSubRouting(),
PubSub3Config.instance.getPubRouting()) { }
public PubSubRouter(params PubSubRoute[] routes) : this(routes, routes) { }
public PubSubRouter(PubSubRoute[] subRoutes, PubSubRoute[] pubRoutes) {
mySubRouteRegExps = new Regex[subRoutes.Length];
myPubRouteRegExps = new Regex[pubRoutes.Length];
mySubRoutes = subRoutes;
myPubRoutes = pubRoutes;
for (int i = 0; i < subRoutes.Length; ++i)
mySubRouteRegExps[i] = new Regex(subRoutes[i].tagRegExp);
for (int i = 0; i < pubRoutes.Length; ++i)
myPubRouteRegExps[i] = new Regex(pubRoutes[i].tagRegExp);
}
private int myGetRouteIndex(string forTag, PubSubRoute[] routes, Regex[] regexes) {
for (int i = 0; i < routes.Length; i++)
if (regexes[i].IsMatch(forTag))
return i;
throw new PubSubRouteNotFound(String.Format("No route found for {0}", forTag));
}
#region IPubSubRouter methods
private string myPickAServer(string tag, string[] urls) {
if (urls.Length == 1)
return urls[0];
else {
uint hc = (uint)tag.GetHashCode();
return urls[hc % urls.Length];
}
}
public string getPubURLForTag(string tag) {
var rv = _temp_only_pubsub;
bts.Log.Gen.info($"PubSubRouter.getPubURLForTag tag: {tag} route: {rv}");
return rv;
}
public string getSubURLForTag(string tag) {
var rv = _temp_only_pubsub; // myPickAServer(tag, mySubRoutes[myGetRouteIndex(tag, mySubRoutes, mySubRouteRegExps)].getAllPubSubs());
bts.Log.Gen.info($"PubSubRouter.getSubURLForTag tag: {tag} route: {rv}");
return rv;
}
public List<string> getAllPubSubSubURLs() {
// List<string> theURLs = new List<string>();
// foreach (PubSubRoute subRoute in mySubRoutes)
// theURLs.AddRange(subRoute.getAllPubSubs());
// return theURLs;
return new List<string>() { _temp_only_pubsub };
}
public List<string> getAllPubSubPubURLs() {
// List<string> theURLs = new List<string>();
// foreach (PubSubRoute pubRoute in myPubRoutes)
// theURLs.AddRange(pubRoute.getAllPubSubs());
// return theURLs;
return new List<string>() { _temp_only_pubsub };
}
public event PubSubTagURLChangeDelegate onSubURLTagRouteChange;
public event PubSubTagURLChangeDelegate onPubURLTagRouteChange;
#endregion
//this function is put here so that the events are used, in order to prevent build errors.
private void myNeverCallThisFunction() {
if (onPubURLTagRouteChange == null || onSubURLTagRouteChange == null) {
int aVar = 2;
aVar = aVar + 2;
}
}
}
}