diff --git a/src/DataStreamSuite/PubSubServer/Program.cs b/src/DataStreamSuite/PubSubServer/Program.cs index 1c3df22fb..4ab03b7a5 100644 --- a/src/DataStreamSuite/PubSubServer/Program.cs +++ b/src/DataStreamSuite/PubSubServer/Program.cs @@ -1,59 +1,70 @@ -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(); - - PubSubMessageReactor pubSubServer = new PubSubMessageReactor(); - 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(); + return cfg.SimpleEchoServer ?? false ? + RunEchoServer(cfg.Port ?? 12345) : + RunPubSubServer(cfg); + } 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(PubSubServerConfig cfg) { + 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().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 dbdcc1f7c..7dc029de4 100644 --- a/src/DataStreamSuite/PubSubServer/PubSubServer.csproj +++ b/src/DataStreamSuite/PubSubServer/PubSubServer.csproj @@ -6,6 +6,10 @@ enable + + + + diff --git a/src/DataStreamSuite/PubSubServer/PubSubServerConfig.cs b/src/DataStreamSuite/PubSubServer/PubSubServerConfig.cs new file mode 100644 index 000000000..b193a1881 --- /dev/null +++ b/src/DataStreamSuite/PubSubServer/PubSubServerConfig.cs @@ -0,0 +1,77 @@ +namespace Bts.PubSubServer; + +using CommandLine; + +using System; +using System.Collections.Generic; +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'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; + } +} +