From 330827deaf74acb4b7dc0e9b6581b107848a73d7 Mon Sep 17 00:00:00 2001 From: Dustin Updyke Date: Fri, 25 Oct 2024 12:38:09 -0400 Subject: [PATCH] dotnet format lite client --- .../src/Infrastructure/ApplicationDetails.cs | 27 +++++------ .../src/Infrastructure/Comms/CheckId.cs | 6 +-- .../Comms/ClientSocket/BackgroundTaskQueue.cs | 11 ++--- .../ClientSocket/ClientSocketConnection.cs | 48 +++++++++---------- .../Comms/ClientSocket/QueueEntry.cs | 2 +- .../src/Infrastructure/Comms/Updates.cs | 2 +- .../Infrastructure/Handlers/FileHandler.cs | 10 ++-- .../Infrastructure/Handlers/HttpHandler.cs | 4 +- .../src/Infrastructure/Orchestrator.cs | 2 +- .../src/Infrastructure/RandomFilename.cs | 4 +- .../src/Infrastructure/Services/LogWriter.cs | 4 +- .../src/Infrastructure/WebClientHeaders.cs | 2 +- src/Ghosts.Client.Lite/src/Program.cs | 6 +-- 13 files changed, 59 insertions(+), 69 deletions(-) diff --git a/src/Ghosts.Client.Lite/src/Infrastructure/ApplicationDetails.cs b/src/Ghosts.Client.Lite/src/Infrastructure/ApplicationDetails.cs index 3b7c71ba..c788f7c9 100644 --- a/src/Ghosts.Client.Lite/src/Infrastructure/ApplicationDetails.cs +++ b/src/Ghosts.Client.Lite/src/Infrastructure/ApplicationDetails.cs @@ -11,7 +11,7 @@ public static class ApplicationDetails { private static readonly Logger _log = LogManager.GetCurrentClassLogger(); - public static string Header => + public static string Header => @" ('-. .-. .-') .-') _ .-') ( OO ) / ( OO ). ( OO) ) ( OO ). ,----. ,--. ,--. .-'),-----. (_)---\_)/ '._ (_)---\_) @@ -65,7 +65,7 @@ public static string GetPath(string loc) { return Path.GetFullPath(Path.Combine(InstalledPath, loc)); } - + public static bool IsLinux() { return RuntimeInformation.IsOSPlatform(OSPlatform.Linux); @@ -172,20 +172,15 @@ public static class LogFiles public static string ClientUpdates => Clean(Path + "clientupdates.log"); } - public class ConfigurationUrls + public class ConfigurationUrls(string rootUrl) { - public ConfigurationUrls(string rootUrl) - { - this._root = rootUrl; - } - - private string _root; - public string Id => $"{this._root}/clientid"; - public string Timeline => $"{this._root}/clienttimeline"; - public string Results => $"{this._root}/clientresults"; - public string Updates => $"{this._root}/clientupdates"; - public string Survey => $"{this._root}/clientsurvey"; - public string Socket => $"{this._root.Replace("/api","")}/clientHub"; + private readonly string _root = rootUrl; + public string Id => $"{_root}/clientid"; + public string Timeline => $"{_root}/clienttimeline"; + public string Results => $"{_root}/clientresults"; + public string Updates => $"{_root}/clientupdates"; + public string Survey => $"{_root}/clientsurvey"; + public string Socket => $"{_root.Replace("/api", "")}/clientHub"; } } -} \ No newline at end of file +} diff --git a/src/Ghosts.Client.Lite/src/Infrastructure/Comms/CheckId.cs b/src/Ghosts.Client.Lite/src/Infrastructure/Comms/CheckId.cs index 80929958..564796db 100644 --- a/src/Ghosts.Client.Lite/src/Infrastructure/Comms/CheckId.cs +++ b/src/Ghosts.Client.Lite/src/Infrastructure/Comms/CheckId.cs @@ -78,7 +78,7 @@ private string Run() } var machine = new ResultMachine(); - + try { //call home @@ -132,7 +132,7 @@ private string Run() File.WriteAllText(IdFile, s); return s; } - + public static void WriteId(string id) { if (!string.IsNullOrEmpty(id)) @@ -149,4 +149,4 @@ public static void WriteId(string id) } } } -} \ No newline at end of file +} diff --git a/src/Ghosts.Client.Lite/src/Infrastructure/Comms/ClientSocket/BackgroundTaskQueue.cs b/src/Ghosts.Client.Lite/src/Infrastructure/Comms/ClientSocket/BackgroundTaskQueue.cs index 42bfbc8a..9ec08a15 100644 --- a/src/Ghosts.Client.Lite/src/Infrastructure/Comms/ClientSocket/BackgroundTaskQueue.cs +++ b/src/Ghosts.Client.Lite/src/Infrastructure/Comms/ClientSocket/BackgroundTaskQueue.cs @@ -6,20 +6,17 @@ namespace Ghosts.Client.Lite.Infrastructure.Comms.ClientSocket; public class BackgroundTaskQueue { - private ConcurrentQueue _workItems = new (); - private SemaphoreSlim _signal = new SemaphoreSlim(0); + private readonly ConcurrentQueue _workItems = new(); + private readonly SemaphoreSlim _signal = new(0); public IEnumerable GetAll() { return _workItems; } - + public void Enqueue(QueueEntry workItem) { - if (workItem == null) - { - throw new ArgumentNullException(nameof(workItem)); - } + ArgumentNullException.ThrowIfNull(workItem); _workItems.Enqueue(workItem); _signal.Release(); diff --git a/src/Ghosts.Client.Lite/src/Infrastructure/Comms/ClientSocket/ClientSocketConnection.cs b/src/Ghosts.Client.Lite/src/Infrastructure/Comms/ClientSocket/ClientSocketConnection.cs index d7206904..875c83c7 100644 --- a/src/Ghosts.Client.Lite/src/Infrastructure/Comms/ClientSocket/ClientSocketConnection.cs +++ b/src/Ghosts.Client.Lite/src/Infrastructure/Comms/ClientSocket/ClientSocketConnection.cs @@ -6,18 +6,13 @@ namespace Ghosts.Client.Lite.Infrastructure.Comms.ClientSocket; -public class Connection +public class Connection(ClientConfiguration.SocketsSettings options) { private int _attempts = 0; private HubConnection _connection; private readonly CancellationToken _ct = new(); public readonly BackgroundTaskQueue Queue = new(); - private readonly ClientConfiguration.SocketsSettings _options; - - public Connection(ClientConfiguration.SocketsSettings options) - { - this._options = options; - } + private readonly ClientConfiguration.SocketsSettings _options = options; public async Task Run() { @@ -34,10 +29,13 @@ public async Task Run() // Send a message to the server while (_connection.State == HubConnectionState.Connected) { - _ = new Timer(_ => { - Task.Run(async () => { + _ = new Timer(_ => + { + Task.Run(async () => + { await ClientHeartbeat(); - }, _ct).ContinueWith(task => { + }, _ct).ContinueWith(task => + { if (task.Exception != null) { // Log or handle the exception @@ -50,18 +48,18 @@ public async Task Run() { Console.WriteLine("Peeking into queue..."); - var item = await Queue.DequeueAsync(this._ct); + var item = await Queue.DequeueAsync(_ct); if (item != null) { Console.WriteLine($"There was a {item.Type} in the queue: {item.Payload}"); if (item.Type == QueueEntry.Types.Heartbeat) - await this.ClientHeartbeat(); + await ClientHeartbeat(); if (item.Type == QueueEntry.Types.Message) - await this.ClientMessage(item.Payload.ToString()); - + await ClientMessage(item.Payload.ToString()); + if (item.Type == QueueEntry.Types.MessageSpecific) - await this.ClientMessageSpecific(item.Payload.ToString()); + await ClientMessageSpecific(item.Payload.ToString()); } else { @@ -87,7 +85,7 @@ async Task EstablishConnection(string url) x.Headers = WebClientBuilder.GetHeaders(machine, true); }).WithAutomaticReconnect() .Build(); - + Console.WriteLine($"Connection state: {_connection.State}"); // Define how to handle incoming messages @@ -130,25 +128,25 @@ async Task EstablishConnection(string url) } catch (Exception ex) { - if(_attempts > 1) + if (_attempts > 1) Console.WriteLine($"An error occurred at {url} while connecting: {ex.Message}"); } } private async Task ClientHeartbeat() { - await _connection?.InvokeAsync("SendHeartbeat", $"Client heartbeat at {DateTime.UtcNow}", this._ct)!; + await _connection?.InvokeAsync("SendHeartbeat", $"Client heartbeat at {DateTime.UtcNow}", _ct)!; } - + private async Task ClientMessage(string message) { - if(!string.IsNullOrEmpty(message)) - await _connection?.InvokeAsync("SendMessage", $"Client message: {message}", this._ct)!; + if (!string.IsNullOrEmpty(message)) + await _connection?.InvokeAsync("SendMessage", $"Client message: {message}", _ct)!; } - + private async Task ClientMessageSpecific(string message) { - if(!string.IsNullOrEmpty(message)) - await _connection?.InvokeAsync("SendSpecificMessage", $"Client specific message: {message}", this._ct)!; + if (!string.IsNullOrEmpty(message)) + await _connection?.InvokeAsync("SendSpecificMessage", $"Client specific message: {message}", _ct)!; } -} \ No newline at end of file +} diff --git a/src/Ghosts.Client.Lite/src/Infrastructure/Comms/ClientSocket/QueueEntry.cs b/src/Ghosts.Client.Lite/src/Infrastructure/Comms/ClientSocket/QueueEntry.cs index ab8e0e1d..41899799 100644 --- a/src/Ghosts.Client.Lite/src/Infrastructure/Comms/ClientSocket/QueueEntry.cs +++ b/src/Ghosts.Client.Lite/src/Infrastructure/Comms/ClientSocket/QueueEntry.cs @@ -15,4 +15,4 @@ public enum Types public object Payload { get; set; } public Types Type { get; set; } -} \ No newline at end of file +} diff --git a/src/Ghosts.Client.Lite/src/Infrastructure/Comms/Updates.cs b/src/Ghosts.Client.Lite/src/Infrastructure/Comms/Updates.cs index b6762250..2117f9ff 100644 --- a/src/Ghosts.Client.Lite/src/Infrastructure/Comms/Updates.cs +++ b/src/Ghosts.Client.Lite/src/Infrastructure/Comms/Updates.cs @@ -430,4 +430,4 @@ internal static void PostSurvey() } } } -} \ No newline at end of file +} diff --git a/src/Ghosts.Client.Lite/src/Infrastructure/Handlers/FileHandler.cs b/src/Ghosts.Client.Lite/src/Infrastructure/Handlers/FileHandler.cs index 3a8f4979..e0e327dd 100644 --- a/src/Ghosts.Client.Lite/src/Infrastructure/Handlers/FileHandler.cs +++ b/src/Ghosts.Client.Lite/src/Infrastructure/Handlers/FileHandler.cs @@ -20,7 +20,7 @@ public async Task Execute(IJobExecutionContext context) var handler = JsonConvert.DeserializeObject(raw); if (handler == null) return; - + await FileHandler.Run(handler); } } @@ -36,7 +36,7 @@ public static async Task Run(TimelineHandler handler) await Run(handler.HandlerType, timelineEvent); } } - + public static async Task Run(HandlerType handler, TimelineEvent t) { var sizeMap = new Dictionary @@ -49,7 +49,7 @@ public static async Task Run(HandlerType handler, TimelineEvent t) var rand = RandomFilename.Generate(); var defaultSaveDirectory = t.CommandArgs[0].ToString(); - if (defaultSaveDirectory!.Contains("%")) + if (defaultSaveDirectory!.Contains('%')) { defaultSaveDirectory = Environment.ExpandEnvironmentVariables(defaultSaveDirectory); } @@ -64,7 +64,7 @@ public static async Task Run(HandlerType handler, TimelineEvent t) savePathString = savePathString.Replace("\\", "/"); // Can't deserialize Windows path var savePaths = JsonConvert.DeserializeObject(savePathString); defaultSaveDirectory = savePaths.PickRandom().Replace("/", "\\"); // Revert to Windows path - if (defaultSaveDirectory.Contains("%")) + if (defaultSaveDirectory.Contains('%')) { defaultSaveDirectory = Environment.ExpandEnvironmentVariables(defaultSaveDirectory); } @@ -101,7 +101,7 @@ public static async Task Run(HandlerType handler, TimelineEvent t) _log.Trace(File.Exists(path)); var bitLength = new Random().Next(1000, sizeMap[handler.ToString()]); var info = new UTF8Encoding(true).GetBytes(GenerateBits(bitLength)); - await fs.WriteAsync(info, 0, info.Length); + await fs.WriteAsync(info); } // Report on file creation success diff --git a/src/Ghosts.Client.Lite/src/Infrastructure/Handlers/HttpHandler.cs b/src/Ghosts.Client.Lite/src/Infrastructure/Handlers/HttpHandler.cs index 7092a9e9..8bb6415b 100644 --- a/src/Ghosts.Client.Lite/src/Infrastructure/Handlers/HttpHandler.cs +++ b/src/Ghosts.Client.Lite/src/Infrastructure/Handlers/HttpHandler.cs @@ -103,7 +103,7 @@ public async Task Run(HandlerType handler, TimelineEvent t, string? url) } } - private HttpClient CreateHttpClient(HandlerType handler, string url) + private static HttpClient CreateHttpClient(HandlerType handler, string url) { var clientHandler = new HttpClientHandler { @@ -138,7 +138,7 @@ private HttpClient CreateHttpClient(HandlerType handler, string url) return client; } - private async Task ReadContentAsync(HttpContent content) + private static async Task ReadContentAsync(HttpContent content) { var encoding = content.Headers.ContentEncoding; var contentStream = await content.ReadAsStreamAsync(); diff --git a/src/Ghosts.Client.Lite/src/Infrastructure/Orchestrator.cs b/src/Ghosts.Client.Lite/src/Infrastructure/Orchestrator.cs index 4ee02d61..b4195993 100644 --- a/src/Ghosts.Client.Lite/src/Infrastructure/Orchestrator.cs +++ b/src/Ghosts.Client.Lite/src/Infrastructure/Orchestrator.cs @@ -45,4 +45,4 @@ private static async Task ThreadLaunch(Timeline timeline, TimelineHandler handle break; } } -} \ No newline at end of file +} diff --git a/src/Ghosts.Client.Lite/src/Infrastructure/RandomFilename.cs b/src/Ghosts.Client.Lite/src/Infrastructure/RandomFilename.cs index a1270645..ae5d91a9 100644 --- a/src/Ghosts.Client.Lite/src/Infrastructure/RandomFilename.cs +++ b/src/Ghosts.Client.Lite/src/Infrastructure/RandomFilename.cs @@ -6,7 +6,7 @@ namespace Ghosts.Client.Lite.Infrastructure; public static class RandomFilename { - private static readonly Random _random = new (); + private static readonly Random _random = new(); public static string Generate() { @@ -61,4 +61,4 @@ public static string Generate() return fileName; } -} \ No newline at end of file +} diff --git a/src/Ghosts.Client.Lite/src/Infrastructure/Services/LogWriter.cs b/src/Ghosts.Client.Lite/src/Infrastructure/Services/LogWriter.cs index 79580ed1..f4a11379 100644 --- a/src/Ghosts.Client.Lite/src/Infrastructure/Services/LogWriter.cs +++ b/src/Ghosts.Client.Lite/src/Infrastructure/Services/LogWriter.cs @@ -9,7 +9,7 @@ namespace Ghosts.Client.Lite.Infrastructure.Services; public static class LogWriter { private static readonly Logger _timelineLog = LogManager.GetLogger("TIMELINE"); - + public static void Timeline(TimeLineRecord result) { var o = JsonConvert.SerializeObject(result, @@ -21,4 +21,4 @@ public static void Timeline(TimeLineRecord result) _timelineLog.Info($"TIMELINE|{DateTime.UtcNow}|{o}"); } -} \ No newline at end of file +} diff --git a/src/Ghosts.Client.Lite/src/Infrastructure/WebClientHeaders.cs b/src/Ghosts.Client.Lite/src/Infrastructure/WebClientHeaders.cs index e232da07..d97b681c 100644 --- a/src/Ghosts.Client.Lite/src/Infrastructure/WebClientHeaders.cs +++ b/src/Ghosts.Client.Lite/src/Infrastructure/WebClientHeaders.cs @@ -49,4 +49,4 @@ public static IDictionary GetHeaders(ResultMachine machine, bool return dict; } } -} \ No newline at end of file +} diff --git a/src/Ghosts.Client.Lite/src/Program.cs b/src/Ghosts.Client.Lite/src/Program.cs index 62633954..5ace6490 100644 --- a/src/Ghosts.Client.Lite/src/Program.cs +++ b/src/Ghosts.Client.Lite/src/Program.cs @@ -89,10 +89,10 @@ private static async Task Run(string[] args) var schedulerFactory = new StdSchedulerFactory(); var scheduler = await schedulerFactory.GetScheduler(); await scheduler.Start(); - + // Schedule timeline await ScheduleTimeline(scheduler, timeline); - + // Schedule memory cleanup job await ScheduleMemoryCleanup(scheduler); @@ -147,7 +147,7 @@ private static async Task ScheduleTimeline(IScheduler scheduler, Timeline timeli } } } - + private static async Task ScheduleMemoryCleanup(IScheduler scheduler) { var job = JobBuilder.Create()