Files
spark/src/DataStreamSuite/PubSub/Client/Routing/PubSubRouter.cs
T
2024-06-26 00:23:19 -04:00

84 lines
2.9 KiB
C#

using System;
using System.Text.RegularExpressions;
using System.Collections;
using System.Collections.Generic;
using BCG;
using Bts.PubSub;
using bts;
using System.Linq;
//using BCG.Utilities;
//using bts;
namespace BCG {
public class PubSubRouter : IPubSubRouter {
// private Regex[] mySubRouteRegExps;
private PubSubRoute[] _pub_routes;
private PubSubRoute[] _sub_routes;
public PubSubRouter() : this(
PubSubConfig.Instance.SubscriberRoutes?.ToArray()
?? throw new InvalidOperationException("Subscriber routes are not configured"),
PubSubConfig.Instance.PublisherRoutes?.ToArray()
?? throw new InvalidOperationException("Publisher routes are not configured")) { }
public PubSubRouter(params PubSubRoute[] routes) : this(routes, routes) { }
public PubSubRouter(PubSubRoute[] sub_routes, PubSubRoute[] pub_routes) {
string log_msg = String.Empty;
foreach (var route in sub_routes) {
log_msg += $" sub_route: {route}";
}
foreach (var route in pub_routes) {
log_msg += $" pub_route: {route}";
}
Log.Gen.info($"PubSubRouter routes: {log_msg}");
_sub_routes = sub_routes;
_pub_routes = pub_routes;
}
#region IPubSubRouter methods
public IEnumerable<PubSubRoute> sub_routes() {
return _sub_routes;
}
public RemoteConnectionInfo get_pub_route_for_tag(string tag) {
if (!_pub_routes.TryGetFirst(r => r?.IsMatch(tag) ?? false, out var route)) {
throw new ArgumentException($"Failed to find route for tag '{tag}'", nameof(tag));
}
Log.Gen.info($"PubSubRouter.get_sub_url_for_tag. tag: {tag} route: {route}");
return RemoteConnectionInfo.parse_url(route.server_address);
}
public RemoteConnectionInfo get_sub_route_for_tag(string tag) {
if (!_sub_routes.TryGetFirst(r => r?.IsMatch(tag) ?? false, out var route)) {
throw new ArgumentException($"Failed to find route for tag '{tag}'", nameof(tag));
}
Log.Gen.info($"PubSubRouter.get_sub_url_for_tag. tag: {tag} route: {route}");
return RemoteConnectionInfo.parse_url(route.server_address);
}
public List<string> get_all_pubsub_sub_urls() =>
_sub_routes.Select(r => r.server_address).ToList();
public List<string> get_all_pubsub_pub_urlss() =>
_pub_routes.Select(r => r.server_address).ToList();
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;
}
}
}
}