improvement: Polish clis in DataStreamSuite

Specifically PubSubServer, DataStreamPublisher and DSQA

* Use CommandLineParser (nuget pkg)
* Clean up csproj files a bit while we're at it
This commit is contained in:
John Colvin committed 2024-06-03 13:46:51 +00:00
1 parent 63306173b1
commit 306c572b53
13 files changed
+501 -229

No files matched your search

+71 -59
View File
@@ -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 {
/// <summary>
/// The main entry point for the application.
/// </summary>
[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<PubSubServerConfig>(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();
}
}