From 0d472dccb1125bf6df6def1bdcbe54a4a8c6eaf3 Mon Sep 17 00:00:00 2001 From: whitlocktech Date: Fri, 28 Nov 2025 23:33:51 -0600 Subject: [PATCH] Lesson 01: add host engine and core domain models --- src/NetPulse.Core/Domain/PingResult.cs | 34 ++++++++++++++++++++++++++ src/NetPulse.Core/Domain/PingTarget.cs | 27 ++++++++++++++++++++ src/NetPulse.Core/EngineLoop.cs | 33 +++++++++++++++++++------ src/NetPulse.Core/Program.cs | 11 +++++---- 4 files changed, 92 insertions(+), 13 deletions(-) create mode 100644 src/NetPulse.Core/Domain/PingResult.cs create mode 100644 src/NetPulse.Core/Domain/PingTarget.cs diff --git a/src/NetPulse.Core/Domain/PingResult.cs b/src/NetPulse.Core/Domain/PingResult.cs new file mode 100644 index 0000000..5d796b7 --- /dev/null +++ b/src/NetPulse.Core/Domain/PingResult.cs @@ -0,0 +1,34 @@ +namespace NetPulse.Core.Domain; + +/// +/// Result of a ping check against a Ping Target. +/// +public class PingResult +{ + public Guid Id { get; init; } = Guid.NewGuid(); + + /// + /// The Target this result belongs to. + /// + public Guid TargetId { get; init; } + + /// + /// When the check was performed (UTC). + /// + public DateTime TimeStampUtc { get; init; } + + /// + /// If the ping succeeded. + /// + public bool Success { get; init; } + + /// + /// Round-trip in milliseconds, if check succeeded. + /// + public double? LatencyMs { get; init; } + + /// + /// Error detail if the pin failed. + /// + public string? ErrorMessage { get; init; } +} \ No newline at end of file diff --git a/src/NetPulse.Core/Domain/PingTarget.cs b/src/NetPulse.Core/Domain/PingTarget.cs new file mode 100644 index 0000000..fdb4096 --- /dev/null +++ b/src/NetPulse.Core/Domain/PingTarget.cs @@ -0,0 +1,27 @@ +namespace NetPulse.Core.Domain; + +/// +/// A host or service that NetPulse should Monitor +/// +public class PingTarget +{ + /// + /// Internal Identifier (map to db key later) + /// + public Guid Id { get; init; } = Guid.NewGuid(); + + /// + /// Display name for the target + /// + public string Name { get; init; } = string.Empty; + + /// + /// Hostname or Ip address to ping. + /// + public string Host { get; init; } = string.Empty; + + /// + /// Whether this target is currently enabled for monitoring. + /// + public bool isEnabled { get; init; } = true; +} \ No newline at end of file diff --git a/src/NetPulse.Core/EngineLoop.cs b/src/NetPulse.Core/EngineLoop.cs index e087518..e08f13a 100644 --- a/src/NetPulse.Core/EngineLoop.cs +++ b/src/NetPulse.Core/EngineLoop.cs @@ -1,5 +1,6 @@ using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; +using NetPulse.Core.Domain; namespace NetPulse.Core; @@ -8,6 +9,12 @@ public class EngineLoop : BackgroundService private readonly ILogger _logger; private readonly TimeSpan _interval = TimeSpan.FromSeconds(2); + private readonly List _targets = new() + { + new PingTarget { Name = "Router", Host = "192.168.0.1" }, + new PingTarget { Name = "Cloudflare", Host = "1.1.1.1"} + }; + public EngineLoop(ILogger logger) { _logger = logger; @@ -17,16 +24,26 @@ public class EngineLoop : BackgroundService { _logger.LogInformation("EngineLoop started."); - while(!stoppingToken.IsCancellationRequested) + while (!stoppingToken.IsCancellationRequested) { try { - // ===== Core Heartbeat ===== - _logger.LogInformation("Hearbeat at {time}", DateTime.UtcNow); - // Future: metrics Collection - // Future: dispatch events - // Future: run modules + // ===== Core Heartbeat ===== + _logger.LogInformation("Heartbeat at {time}", DateTime.UtcNow); + + foreach (var target in _targets) + { + _logger.LogInformation( + "Target {name} at {host} is configured (enabled = {enabled})", + target.Name, + target.Host, + target.isEnabled); + } + + /// Future: metrics Collection + /// Future: dispatch events + /// Future: run modules await Task.Delay(_interval, stoppingToken); } @@ -36,10 +53,10 @@ public class EngineLoop : BackgroundService } catch (Exception ex) { - _logger.LogError(ex, "Unexpected Engine error."); + _logger.LogError(ex, "Unexpected engine error."); } } - _logger.LogInformation("EngineLoop Stopping"); + _logger.LogInformation("EngineLoop stopping."); } } \ No newline at end of file diff --git a/src/NetPulse.Core/Program.cs b/src/NetPulse.Core/Program.cs index 5e2eef8..9f64c7d 100644 --- a/src/NetPulse.Core/Program.cs +++ b/src/NetPulse.Core/Program.cs @@ -1,6 +1,7 @@ +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Configuration; namespace NetPulse.Core; @@ -11,23 +12,23 @@ public class Program using IHost host = Host.CreateDefaultBuilder(args) .ConfigureAppConfiguration(config => { - config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true); + config.AddJsonFile("appsettings.json", optional: true, reloadOnChange:true); }) - .ConfigureLogging(logging => + .ConfigureLogging(logging=> { logging.ClearProviders(); logging.AddSimpleConsole(options => { - options.TimestampFormat = "HH:MM:SS"; + options.TimestampFormat = "HH:mm:ss "; options.IncludeScopes = true; }); - }) .ConfigureServices((context, services) => { services.AddHostedService(); }) .Build(); + await host.RunAsync(); } } \ No newline at end of file