diff --git a/src/DataStreamSuite/DataStreamPublisher/Configurations/MarketDataPublisher.json b/src/DataStreamSuite/DataStreamPublisher/Configurations/MarketDataPublisher.json new file mode 100644 index 000000000..42cf76548 --- /dev/null +++ b/src/DataStreamSuite/DataStreamPublisher/Configurations/MarketDataPublisher.json @@ -0,0 +1,12 @@ +{ + "user": "bts", + "prepublish_tables": { + "bbo": "bbo()", + "instruments": "instruments()", + "timesales": "timesales(10000)", + "books": "books()", + "seconds_1_10": "second(1,10)", + "literal_420": "literal(\"x\", \"420.0\")", + "mongodb_testcollection": "mongodb(\"mongodb+srv://johnc-dev:DMhDhP!g-TKeK.5@atlas-online-archive-5e18ab3c77c74d1cffa64c03-m7jkr.a.query.mongodb.net/?ssl=true&authSource=admin\",\"admin\",\"test_collection\")" + } +} diff --git a/src/DataStreamSuite/DataStreamPublisher/Configurations/SimpleEchoClient.json b/src/DataStreamSuite/DataStreamPublisher/Configurations/SimpleEchoClient.json new file mode 100644 index 000000000..d4ef92277 --- /dev/null +++ b/src/DataStreamSuite/DataStreamPublisher/Configurations/SimpleEchoClient.json @@ -0,0 +1,4 @@ +{ + "user": "bts", + "simple_echo_client": true +} diff --git a/src/DataStreamSuite/DataStreamPublisher/DataStreamPublisher.csproj b/src/DataStreamSuite/DataStreamPublisher/DataStreamPublisher.csproj index c28c33ec4..7907b56a8 100644 --- a/src/DataStreamSuite/DataStreamPublisher/DataStreamPublisher.csproj +++ b/src/DataStreamSuite/DataStreamPublisher/DataStreamPublisher.csproj @@ -1,5 +1,16 @@ + + Exe + net7.0 + enable + true + + + + + + @@ -9,11 +20,4 @@ - - Exe - net7.0 - enable - true - - diff --git a/src/DataStreamSuite/DataStreamPublisher/DataStreamPublisherConfig.cs b/src/DataStreamSuite/DataStreamPublisher/DataStreamPublisherConfig.cs new file mode 100644 index 000000000..598889899 --- /dev/null +++ b/src/DataStreamSuite/DataStreamPublisher/DataStreamPublisherConfig.cs @@ -0,0 +1,76 @@ +namespace Bts.DataStreamPublisher; + +using CommandLine; + +using System; +using System.Collections.Generic; +using System.IO; +using System.Text.Json; +using System.Text.Json.Serialization; + +public class DataStreamPublisherConfig { + /// + /// The login user for sparkd + /// + [JsonPropertyName("user")] + [Option("user", HelpText = "The sparkd login user")] + public string? User { get; set; } + + /// + /// The login password for sparkd + /// + [JsonPropertyName("password")] + [Option("pass", HelpText = "The sparkd login password")] + public string? Password { get; set; } + + /// + /// The config server url for sparkd + /// + [JsonPropertyName("sparkd")] + [Option("sparkd", HelpText = "The sparkd config server")] + public string? Sparkd { get; set; } + + /// + /// The tables to publish on startup. Each key/value represents a table name/query to publish. + /// + [JsonPropertyName("prepublish_tables")] + public Dictionary? PrepublishTables { get; set; } + + [JsonPropertyName("simple_echo_client")] + [Option("simple-echo-client", HelpText = "Run a simple echo client (won't publish tables or connect to sparkd)")] + public bool? SimpleEchoClient { get; set; } + + [Option( + "config", + MetaValue = "PATH", + HelpText = "Path to json file representing these options (DataStreamPublisherConfig)")] + [JsonIgnore] + public string? ConfigFile { get; set; } + + public void MergeWith(DataStreamPublisherConfig other) { + User ??= other.User; + Password ??= other.Password; + Sparkd ??= other.Sparkd; + PrepublishTables ??= other.PrepublishTables; + SimpleEchoClient ??= other.SimpleEchoClient; + ConfigFile ??= other.ConfigFile; + } + + /// + /// Merges options with those defined in `ConfigFile` (does not overwrite + /// non-null existing values). + /// + /// + /// JsonSerializer.Deserialize returns null + /// + public void MergeWithConfigFile() { + if (ConfigFile is null) { + return; + } + var cfgString = File.ReadAllText(ConfigFile); + var parsedCfg = JsonSerializer.Deserialize(cfgString) ?? + throw new InvalidOperationException($"Failed to parse json from {ConfigFile}"); + MergeWith(parsedCfg); + ConfigFile = null; + } +} diff --git a/src/DataStreamSuite/DataStreamPublisher/Program.cs b/src/DataStreamSuite/DataStreamPublisher/Program.cs index a86fd2e2a..55609bbf0 100644 --- a/src/DataStreamSuite/DataStreamPublisher/Program.cs +++ b/src/DataStreamSuite/DataStreamPublisher/Program.cs @@ -1,130 +1,129 @@ -using System; -using mbts.config; -using bts.utils; -using System.Threading.Tasks; -using Bts.DataStreamExpressions; -using System.Collections.Generic; - -using BCG; -using System.IO; -using BCG.Utilities.General; -using bts; -using System.Linq; - - -bts.Log.Gen.info($"Starting DSPublisher"); -Console.WriteLine("Starting DSPublisher..."); -bool run_echo_tcp_client_only = false; -static void init(string[] args) { - Func login = async (l, p) => { - var login = l; - var passwd = p; - var service_ctx = bts.api.core.ServiceContext.create(); - await service_ctx.login(login, passwd); - }; - BTSNativeMethods.provide_command_line(args, args.Length); - Configuration.initialize(); - Task.Run(async () => await login("bts", String.Empty)); -} -init(args); -if (run_echo_tcp_client_only) - start_echo_client(); -else - _publish_streams(); - -static ViewStream build_stream(string stream_expression) { - try { - bts.Log.Gen.info($"build_stream stream_expression: {stream_expression}"); - var exp = DataStreamExpressionsInterpreter.Evaluate(stream_expression); - var rv = exp.getView(new Dictionary(0)); - return rv; - - } catch (Exception ex) { - bts.Log.assert(false, $"Could not build stream: {stream_expression} ex: {ex}"); - return null; - } -} - -static void start_echo_client() { - Console.WriteLine("Starting DSPublisher in TCP client echo mode, Hit key to continue"); - Console.ReadLine(); - SimpleTCPClient client = new SimpleTCPClient("127.0.0.1", 12345); - client.start(); - while (true) { - System.Threading.Thread.Sleep(1000); - } - // await SimpleTCPClient.start("127.0.0.1", 12345); - // Task.Delay(1000000000).Wait(); -} - -void _publish_streams() { - PubTableBroker pb_broker = new PubTableBroker(); - CloudStreamManager csm = new CloudStreamManager(); - string csv_folder = @"C:\Temp\csvs_topublish"; - CSVPublisher csv_publisher = new CSVPublisher(csv_folder, pb_broker); - // get it from config - Dictionary streams_to_publish = new Dictionary() { - //{"bbo","bbo()"}, - //{"instruments","instruments()"}, - //{"timesales","timesales(10000)"}, - //{"books","books()"}, - {"seconds_1_10","second(1,10)"}, - {"literal_420","literal(\"x\", \"420.0\")"}, - {"tradebar_30","tradebars(30)"}, - {"mongodb_testcollection", "mongodb(\"mongodb://192.168.1.80:27017\",\"local\",\"test_collection\")"} - }; - // {"bbo_ts","timeSeries(bbo())"} - - foreach (var kv in streams_to_publish) { - var name = kv.Key; - var exp = kv.Value; - BCG.ViewStream vs = build_stream(exp); - ViewPublisher vpub = new ViewPublisher(name, pb_broker, vs); - vs.onChange += on_change; - } - while (true) { - System.Threading.Thread.Sleep(TimeSpan.FromSeconds(1)); - ViewStreamDatabase.instance.doWork(); - csm.doWork(); - MasterWorker.getInstance(false).doWork(); - } -} - - - -void on_change(object obj) { - bts.Log.Gen.info($"DSPublisher on_change dp: {obj}"); -} - -public class CSVPublisher : IWorker { - private IDictionary _pubs = new Dictionary(); - // read from config - private readonly string _csv_directory = @"C:\Temp\csvs_topublish\"; - - private PubTableBroker _pub_table_broker; - public CSVPublisher(string csv_directory, PubTableBroker pub_table_broker) { - _csv_directory = csv_directory; - _pub_table_broker = pub_table_broker; - MasterWorker.getInstance(false).addWorker(this, TimeSpan.FromSeconds(1)); - } - public void doWork() { - var file_names = Directory.GetFiles(_csv_directory); - foreach (var fn in file_names) { - if (!_pubs.ContainsKey(fn)) { - _handle_new_file(fn); - } - } - } - - private void _handle_new_file(string file_name) { - if (file_name.EndsWith(".csv")) { - ViewStream vs = new CSVReaderStream(file_name); - vs = new SchemaGuesserStream(vs); - var toks = file_name.Split('\\'); - string table_name = toks.Last(); - table_name = table_name.Remove(table_name.Length - ".csv".Length); - ViewPublisher vp = new ViewPublisher(table_name, _pub_table_broker, vs); - _pubs.Add(file_name, vp); - } - } -} \ No newline at end of file +using BCG; +using BCG.Utilities.General; + +using bts; +using bts.api.core; +using bts.utils; + +using Bts.DataStreamExpressions; +using Bts.DataStreamPublisher; + +using CommandLine; + +using mbts.config; + +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Threading.Tasks; + +await Parser.Default.ParseArguments(args) + .MapResult(RunAsync, errs => Task.FromResult(1)); + +async Task RunAsync(DataStreamPublisherConfig cfg) { + try { + cfg.MergeWithConfigFile(); + Configuration.initialize(cfg.Sparkd); + Log.Gen.info("Starting DataStreamPublisher"); + var service_ctx = ServiceContext.create(cfg.Sparkd); + await service_ctx.login(cfg.User ?? "bts", cfg.Password ?? string.Empty); + if (cfg.SimpleEchoClient ?? false) { + start_echo_client(); + } else { + _publish_streams(cfg.PrepublishTables ?? new(0)); + } + } catch (Exception ex) { + // Write the error to console first, in case the logging system is compromised. + var error_msg = $"DataStreamPublisher threw exception:\n{ex}"; + Console.Error.WriteLine(error_msg); + Console.Error.Flush(); + bts.Log.Gen.error(error_msg); + return 1; + } + return 0; +} + +static ViewStream build_stream(string stream_expression) { + try { + bts.Log.Gen.info($"build_stream stream_expression: {stream_expression}"); + var exp = DataStreamExpressionsInterpreter.Evaluate(stream_expression); + var rv = exp.getView(new Dictionary(0)); + return rv; + + } catch (Exception ex) { + bts.Log.assert(false, $"Could not build stream: {stream_expression} ex: {ex}"); + return null; + } +} + +static void start_echo_client() { + Console.WriteLine("Starting DSPublisher in TCP client echo mode, Hit key to continue"); + Console.ReadLine(); + SimpleTCPClient client = new SimpleTCPClient("127.0.0.1", 12345); + client.start(); + while (true) { + System.Threading.Thread.Sleep(1000); + } + // await SimpleTCPClient.start("127.0.0.1", 12345); + // Task.Delay(1000000000).Wait(); +} + +void _publish_streams(IEnumerable> prepublish_streams) { + PubTableBroker pb_broker = new PubTableBroker(); + CloudStreamManager csm = new CloudStreamManager(); + string csv_folder = @"C:\Temp\csvs_topublish"; + CSVPublisher csv_publisher = new CSVPublisher(csv_folder, pb_broker); + foreach (var kv in prepublish_streams) { + var name = kv.Key; + var exp = kv.Value; + BCG.ViewStream vs = build_stream(exp); + ViewPublisher vpub = new ViewPublisher(name, pb_broker, vs); + vs.onChange += on_change; + } + while (true) { + System.Threading.Thread.Sleep(TimeSpan.FromSeconds(1)); + ViewStreamDatabase.instance.doWork(); + csm.doWork(); + MasterWorker.getInstance(false).doWork(); + } +} + + + +void on_change(object obj) { + bts.Log.Gen.info($"DSPublisher on_change dp: {obj}"); +} + +public class CSVPublisher : IWorker { + private IDictionary _pubs = new Dictionary(); + // read from config + private readonly string _csv_directory = @"C:\Temp\csvs_topublish\"; + + private PubTableBroker _pub_table_broker; + public CSVPublisher(string csv_directory, PubTableBroker pub_table_broker) { + _csv_directory = csv_directory; + _pub_table_broker = pub_table_broker; + MasterWorker.getInstance(false).addWorker(this, TimeSpan.FromSeconds(1)); + } + public void doWork() { + var file_names = Directory.GetFiles(_csv_directory); + foreach (var fn in file_names) { + if (!_pubs.ContainsKey(fn)) { + _handle_new_file(fn); + } + } + } + + private void _handle_new_file(string file_name) { + if (file_name.EndsWith(".csv")) { + ViewStream vs = new CSVReaderStream(file_name); + vs = new SchemaGuesserStream(vs); + var toks = file_name.Split('\\'); + string table_name = toks.Last(); + table_name = table_name.Remove(table_name.Length - ".csv".Length); + ViewPublisher vp = new ViewPublisher(table_name, _pub_table_broker, vs); + _pubs.Add(file_name, vp); + } + } +} diff --git a/src/DataStreamSuite/DataStreamQueryAnalyzer/Configurations/DataStreamQueryAnalyzer.json b/src/DataStreamSuite/DataStreamQueryAnalyzer/Configurations/DataStreamQueryAnalyzer.json new file mode 100644 index 000000000..ba490d24a --- /dev/null +++ b/src/DataStreamSuite/DataStreamQueryAnalyzer/Configurations/DataStreamQueryAnalyzer.json @@ -0,0 +1,3 @@ +{ + "user": "bts" +} diff --git a/src/DataStreamSuite/DataStreamQueryAnalyzer/DataStreamQueryAnalyzer.csproj b/src/DataStreamSuite/DataStreamQueryAnalyzer/DataStreamQueryAnalyzer.csproj index d0c622efd..f0a6a72da 100644 --- a/src/DataStreamSuite/DataStreamQueryAnalyzer/DataStreamQueryAnalyzer.csproj +++ b/src/DataStreamSuite/DataStreamQueryAnalyzer/DataStreamQueryAnalyzer.csproj @@ -7,6 +7,10 @@ true + + + + diff --git a/src/DataStreamSuite/DataStreamQueryAnalyzer/DataStreamQueryAnalyzerConfig.cs b/src/DataStreamSuite/DataStreamQueryAnalyzer/DataStreamQueryAnalyzerConfig.cs new file mode 100644 index 000000000..be679db83 --- /dev/null +++ b/src/DataStreamSuite/DataStreamQueryAnalyzer/DataStreamQueryAnalyzerConfig.cs @@ -0,0 +1,69 @@ +namespace Bts.DataStreamQueryAnalyzer; + +using CommandLine; + +using System; +using System.Collections.Generic; +using System.IO; +using System.Text.Json; +using System.Text.Json.Serialization; + +public class DataStreamQueryAnalyzerConfig { + /// + /// The login user for sparkd + /// + [JsonPropertyName("user")] + [Option("user", HelpText = "The sparkd login user")] + public string? User { get; set; } + + /// + /// The login password for sparkd + /// + [JsonPropertyName("password")] + [Option("pass", HelpText = "The sparkd login password")] + public string? Password { get; set; } + + /// + /// The config server url for sparkd + /// + [JsonPropertyName("sparkd")] + [Option("sparkd", HelpText = "The sparkd config server")] + public string? Sparkd { get; set; } + + [JsonPropertyName("stream_file")] + [Option("stream-file", HelpText = "Stream file to load")] + public string? StreamFile { get; set; } + + [Option( + "config", + MetaValue = "PATH", + HelpText = "Path to json file representing these options (DataStreamPublisherConfig)")] + [JsonIgnore] + public string? ConfigFile { get; set; } + + public void MergeWith(DataStreamQueryAnalyzerConfig other) { + User ??= other.User; + Password ??= other.Password; + Sparkd ??= other.Sparkd; + StreamFile ??= other.StreamFile; + ConfigFile ??= other.ConfigFile; + } + + /// + /// Merges options with those defined in `ConfigFile` (does not overwrite + /// non-null existing values). + /// + /// + /// JsonSerializer.Deserialize returns null + /// + public void MergeWithConfigFile() { + if (ConfigFile is null) { + return; + } + var cfgString = File.ReadAllText(ConfigFile); + var parsedCfg = JsonSerializer.Deserialize(cfgString) ?? + throw new InvalidOperationException($"Failed to parse json from {ConfigFile}"); + MergeWith(parsedCfg); + ConfigFile = null; + } +} diff --git a/src/DataStreamSuite/DataStreamQueryAnalyzer/Program.cs b/src/DataStreamSuite/DataStreamQueryAnalyzer/Program.cs index 41d7e9590..6c00ade0c 100644 --- a/src/DataStreamSuite/DataStreamQueryAnalyzer/Program.cs +++ b/src/DataStreamSuite/DataStreamQueryAnalyzer/Program.cs @@ -1,40 +1,46 @@ using BCG; -using bts.utils; +using bts; +using bts.api.core; + +using Bts.DataStreamQueryAnalyzer; + +using CommandLine; using mbts.config; using System; -using System.Linq; -using System.Windows.Forms; using System.Threading.Tasks; -using System.ComponentModel; +using System.Windows.Forms; static class Program { - static void init(string[] args) { - Func login = async (l, p) => { - var login = l; - var passwd = p; - var service_ctx = bts.api.core.ServiceContext.create(); - await service_ctx.login(login, passwd); - }; - BTSNativeMethods.provide_command_line(args, args.Length); - Configuration.initialize(); - Task.Run(async () => await login("bts", String.Empty)); - } [STAThread] - static void Main(string[] args) { - init(args); - string args_concat = String.Empty; - args.ToList().ForEach(t => args_concat += t); - var init_log = $"Starting DSQA args: {args_concat} len: {args.Length}"; - bts.Log.Gen.info(init_log); - Console.WriteLine(init_log); - Console.WriteLine("Launching Main Window"); - MainForm mainForm = new MainForm(); - if (args.Length > 0) { - mainForm.loadFile(args[0]); + static async Task Main(string[] args) { + await Parser.Default.ParseArguments(args) + .MapResult(RunAsync, errs => Task.FromResult(1)); + + async Task RunAsync(DataStreamQueryAnalyzerConfig cfg) { + try { + cfg.MergeWithConfigFile(); + Configuration.initialize(cfg.Sparkd); + Log.Gen.info("Starting DataStreamQueryAnalyzer"); + var service_ctx = ServiceContext.create(cfg.Sparkd); + await service_ctx.login(cfg.User ?? "bts", cfg.Password ?? string.Empty); + Log.Gen.info("Launching Main Window"); + var mainForm = new MainForm(); + if (cfg.StreamFile is not null) { + mainForm.loadFile(cfg.StreamFile); + } + Application.Run(mainForm); + } catch (Exception ex) { + // Write the error to console first, in case the logging system is compromised. + var error_msg = $"DataStreamPublisher threw exception:\n{ex}"; + Console.Error.WriteLine(error_msg); + Console.Error.Flush(); + bts.Log.Gen.error(error_msg); + return 1; + } + return 0; } - Application.Run(mainForm); } } diff --git a/src/DataStreamSuite/PubSubServer/Configurations/SimpleEchoServer.json b/src/DataStreamSuite/PubSubServer/Configurations/SimpleEchoServer.json new file mode 100644 index 000000000..8ec19d45e --- /dev/null +++ b/src/DataStreamSuite/PubSubServer/Configurations/SimpleEchoServer.json @@ -0,0 +1,4 @@ +{ + "user": "bts", + "simple_echo_server": true +} diff --git a/src/DataStreamSuite/PubSubServer/Program.cs b/src/DataStreamSuite/PubSubServer/Program.cs index e388f2771..f86edaa1e 100644 --- a/src/DataStreamSuite/PubSubServer/Program.cs +++ b/src/DataStreamSuite/PubSubServer/Program.cs @@ -1,59 +1,71 @@ -using System; -using System.Threading; -using System.Threading.Tasks; -using System.Linq; - -//using BCG.Utilities; -//using BCG.Utilities.General; - - -namespace BCG { - class PubSub2Main { - - /// - /// The main entry point for the application. - /// - [STAThread] - public static void Main(string[] args) { - bool run_echo_tcp_server_only = false; - bts.utils.BTSNativeMethods.provide_command_line(new string[] { }, 0); - mbts.config.Configuration.initialize(); - if (run_echo_tcp_server_only) { - Console.WriteLine("Running as TCP Echo server, Hit enter to continue"); - Console.ReadLine(); - var port = 12345; - Console.WriteLine($"ready to listen to port: {port}"); - SimpleTCPServer sts = new SimpleTCPServer(port); - sts.init(); - Task.Delay(1000000000).Wait(); - } - if (args.Length > 0 && args[0] == "daemon") - args = args.Skip(1).ToArray(); - - //CoreServicesInit.init(); - PubSub3Config config = PubSub3Config.instance; - - long currentMemory = 0; - int defaultWaitTime = 100; - bool gced = false; - new Thread(() => { - while (true) { - gced = false; - currentMemory = GC.GetTotalMemory(false); - if (config.PS3_GCActivationThreshold > 0 && currentMemory > config.PS3_GCActivationThreshold) { - string gcMsg = String.Format("GCing memory, mem before GC = {0}", currentMemory); - // BlueEventLog.logEvent(gcMsg, PS3LogLevels.info); - Console.WriteLine(gcMsg); - GC.Collect(); - gced = true; - } - Thread.Sleep(gced ? config.PS3_WaitPeriodAfterGC : defaultWaitTime); - } - }).Start(); - var port_no = 12345; - PubSubMessageReactor pubSubServer = new PubSubMessageReactor(port_no); - pubSubServer.start(); - - } - } -} +using BCG; + +using bts; + +using Bts.PubSubServer; + +using CommandLine; + +using mbts.config; + +using System; +using System.Threading; +using System.Threading.Tasks; + +Parser.Default.ParseArguments(args) + .MapResult(Run, errs => 1); + +int Run(PubSubServerConfig cfg) { + try { + Configuration.initialize(); + cfg.Port ??= 12345; + return cfg.SimpleEchoServer ?? false ? + RunEchoServer(cfg.Port.Value) : + RunPubSubServer(cfg.Port.Value); + } catch (Exception ex) { + // Write the error to console first, in case the logging system is compromised. + var error_msg = $"PubSubServer threw exception:\n{ex}"; + Console.Error.WriteLine(error_msg); + Console.Error.Flush(); + Log.Gen.error(error_msg); + return 1; + } +} + +int RunPubSubServer(short port) { + var config = PubSub3Config.instance; + long currentMemory = 0; + int defaultWaitTime = 100; + bool gced = false; + Task.Run(() => { + while (true) { + gced = false; + currentMemory = GC.GetTotalMemory(false); + if (config.PS3_GCActivationThreshold > 0 && currentMemory > config.PS3_GCActivationThreshold) { + Console.WriteLine($"GCing memory, mem before GC = {currentMemory}"); + GC.Collect(); + gced = true; + } + Thread.Sleep(gced ? config.PS3_WaitPeriodAfterGC : defaultWaitTime); + } + }); + new PubSubMessageReactor(port).start(); + return 0; +} + +int RunEchoServer(short port) { + Console.WriteLine("Running as TCP Echo server, Hit enter to continue"); + Console.ReadLine(); + Console.WriteLine($"ready to listen to port: {port}"); + var sts = new SimpleTCPServer(port); + sts.init(); + Console.CancelKeyPress += OnCancelKeyPress; + while (true) { + Thread.Sleep(TimeSpan.FromSeconds(3)); + } + + void OnCancelKeyPress(object? sender, ConsoleCancelEventArgs e) { + Log.Gen.error("Cancel key press. Disposing echo server"); + sts.Dispose(); + } +} diff --git a/src/DataStreamSuite/PubSubServer/PubSubServer.csproj b/src/DataStreamSuite/PubSubServer/PubSubServer.csproj index 8413ff9d1..e5c310564 100644 --- a/src/DataStreamSuite/PubSubServer/PubSubServer.csproj +++ b/src/DataStreamSuite/PubSubServer/PubSubServer.csproj @@ -1,15 +1,18 @@ - - - - - - Exe net7.0 enable + + + + + + + + + diff --git a/src/DataStreamSuite/PubSubServer/PubSubServerConfig.cs b/src/DataStreamSuite/PubSubServer/PubSubServerConfig.cs new file mode 100644 index 000000000..42c33636f --- /dev/null +++ b/src/DataStreamSuite/PubSubServer/PubSubServerConfig.cs @@ -0,0 +1,76 @@ +namespace Bts.PubSubServer; + +using CommandLine; + +using System; +using System.IO; +using System.Text.Json; +using System.Text.Json.Serialization; + +public class PubSubServerConfig { + /// + /// The login user for sparkd + /// + [JsonPropertyName("user")] + [Option("user", HelpText = "The sparkd login user")] + public string? User { get; set; } + + /// + /// The login password for sparkd + /// + [JsonPropertyName("password")] + [Option("pass", HelpText = "The sparkd login password")] + public string? Password { get; set; } + + /// + /// The config server url for sparkd + /// + [JsonPropertyName("port")] + [Option("port", HelpText = "The port to run on")] + public short? Port { get; set; } + + /// + /// The config server url for sparkd + /// + [JsonPropertyName("sparkd")] + [Option("sparkd", HelpText = "The sparkd config server")] + public string? Sparkd { get; set; } + + [JsonPropertyName("simple_echo_server")] + [Option("simple-echo-server", HelpText = "Run a simple echo server (won't connect to sparkd)")] + public bool? SimpleEchoServer { get; set; } + + [JsonIgnore] + [Option( + "config", + MetaValue = "PATH", + HelpText = "Path to json file representing these options (PubSubServerConfig)")] + public string? ConfigFile { get; set; } + + public void MergeWith(PubSubServerConfig other) { + User ??= other.User; + Password ??= other.Password; + Sparkd ??= other.Sparkd; + SimpleEchoServer ??= other.SimpleEchoServer; + ConfigFile ??= other.ConfigFile; + } + + /// + /// Merges options with those defined in `ConfigFile` (does not overwrite + /// non-null existing values). + /// + /// + /// JsonSerializer.Deserialize returns null + /// + public void MergeWithConfigFile() { + if (ConfigFile is null) { + return; + } + var cfgString = File.ReadAllText(ConfigFile); + var parsedCfg = JsonSerializer.Deserialize(cfgString) ?? + throw new InvalidOperationException($"Failed to parse json from {ConfigFile}"); + MergeWith(parsedCfg); + ConfigFile = null; + } +} +