Polish PubSubServer cli

This commit is contained in:
John Colvin committed 2024-02-09 22:27:57 -06:00
1 parent c98891f5e3
commit aaa3ff114b
3 files changed
+151 -59

No files matched your search

+70 -59
View File
@@ -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 {
/// <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();
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<PubSubServerConfig>(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();
}
}
@@ -6,6 +6,10 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.9.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\client\spark_core\spark_core.csproj" />
<ProjectReference Include="..\PubSub\PubSub.csproj" />
@@ -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 {
/// <summary>
/// The login user for sparkd
/// </summary>
[JsonPropertyName("user")]
[Option("user", HelpText = "The sparkd login user")]
public string? User { get; set; }
/// <summary>
/// The login password for sparkd
/// </summary>
[JsonPropertyName("password")]
[Option("pass", HelpText = "The sparkd login password")]
public string? Password { get; set; }
/// <summary>
/// The config server url for sparkd
/// </summary>
[JsonPropertyName("port")]
[Option("port", HelpText = "The port to run on")]
public short? Port { get; set; }
/// <summary>
/// The config server url for sparkd
/// </summary>
[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;
}
/// <summary>
/// Merges options with those defined in `ConfigFile` (does not overwrite
/// non-null existing values).
/// </summary>
/// <exception cref="InvalidOperationException">
/// JsonSerializer.Deserialize returns null
/// </exception>
public void MergeWithConfigFile() {
if (ConfigFile is null) {
return;
}
var cfgString = File.ReadAllText(ConfigFile);
var parsedCfg = JsonSerializer.Deserialize<PubSubServerConfig>(cfgString) ??
throw new InvalidOperationException($"Failed to parse json from {ConfigFile}");
MergeWith(parsedCfg);
ConfigFile = null;
}
}