pubsub checkpt

This commit is contained in:
John Colvin committed 2024-07-14 13:17:29 -04:00
1 parent 18ee4724fe
commit ea2d9441cd
2 files changed
+57

No files matched your search

@@ -0,0 +1,6 @@
namespace Bts.PubSub.Services;
public class EchoServerConfig {
public bool Echo { get; set; }
public short Port { get; set; }
}
@@ -0,0 +1,51 @@
namespace Bts.PubSub.Services;
using BCG;
using bts;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using System;
using System.Threading;
using System.Threading.Tasks;
public class PubSubEchoServer : BackgroundService {
readonly EchoServerConfig config;
public PubSubEchoServer(IOptions<EchoServerConfig> config)
: this(config.Value) { }
public PubSubEchoServer(EchoServerConfig config) {
this.config = config;
}
protected override Task ExecuteAsync(CancellationToken cancellationToken) {
return Task.Factory.StartNew(
Run, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Default);
void Run() {
if (cancellationToken.IsCancellationRequested) {
return;
}
string log_msg = $"Running as TCP Echo server, port: {config.Port} echo: {config.Echo} Hit enter to continue:";
Console.WriteLine(log_msg);
Console.ReadLine();
var ready_msg = $"ready to listen to port: {config.Port}";
Log.Gen.info(ready_msg);
Console.WriteLine(ready_msg);
var sts = new SimpleTCPServer(config.Port, config.Echo);
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();
}
}
}
}