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; 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 = "bluto://127.0.0.1:1976"; //myPickAServer(tag, myPubRoutes[myGetRouteIndex(tag, myPubRoutes, myPubRouteRegExps)].getAllPubSubs()); bts.Log.Gen.info($"PubSubRouter.getPubURLForTag tag: {tag} route: {rv}"); return rv; } public string getSubURLForTag(string tag) { var rv = "bluto://127.0.0.1:1976"; // myPickAServer(tag, mySubRoutes[myGetRouteIndex(tag, mySubRoutes, mySubRouteRegExps)].getAllPubSubs()); bts.Log.Gen.info($"PubSubRouter.getSubURLForTag tag: {tag} route: {rv}"); return rv; } public List getAllPubSubSubURLs() { List theURLs = new List(); foreach (PubSubRoute subRoute in mySubRoutes) theURLs.AddRange(subRoute.getAllPubSubs()); return theURLs; } public List getAllPubSubPubURLs() { List theURLs = new List(); foreach (PubSubRoute pubRoute in myPubRoutes) theURLs.AddRange(pubRoute.getAllPubSubs()); return theURLs; } 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; } } } }