From 097ce025e7ac9be37a7fcd5018f6b2643b577859 Mon Sep 17 00:00:00 2001 From: whitlocktech Date: Sat, 29 Nov 2025 00:14:10 -0600 Subject: [PATCH] Lesson 02 --- NetPulse.sln | 15 ++++++ src/NetPulse.Cli/NetPulse.Cli.csproj | 14 +++++ src/NetPulse.Cli/Program.cs | 71 ++++++++++++++++++++++++++ src/NetPulse.Core/Domain/PingResult.cs | 2 +- 4 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 src/NetPulse.Cli/NetPulse.Cli.csproj create mode 100644 src/NetPulse.Cli/Program.cs diff --git a/NetPulse.sln b/NetPulse.sln index db27ce3..beeee1e 100644 --- a/NetPulse.sln +++ b/NetPulse.sln @@ -7,6 +7,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{827E0CD3-B72 EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetPulse.Core", "src\NetPulse.Core\NetPulse.Core.csproj", "{B996D138-9747-4AE9-9FEB-586D6F9CD97E}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetPulse.Cli", "src\NetPulse.Cli\NetPulse.Cli.csproj", "{3E57F4B5-0012-43F7-A016-5B60A27A1D0A}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -29,11 +31,24 @@ Global {B996D138-9747-4AE9-9FEB-586D6F9CD97E}.Release|x64.Build.0 = Release|Any CPU {B996D138-9747-4AE9-9FEB-586D6F9CD97E}.Release|x86.ActiveCfg = Release|Any CPU {B996D138-9747-4AE9-9FEB-586D6F9CD97E}.Release|x86.Build.0 = Release|Any CPU + {3E57F4B5-0012-43F7-A016-5B60A27A1D0A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {3E57F4B5-0012-43F7-A016-5B60A27A1D0A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {3E57F4B5-0012-43F7-A016-5B60A27A1D0A}.Debug|x64.ActiveCfg = Debug|Any CPU + {3E57F4B5-0012-43F7-A016-5B60A27A1D0A}.Debug|x64.Build.0 = Debug|Any CPU + {3E57F4B5-0012-43F7-A016-5B60A27A1D0A}.Debug|x86.ActiveCfg = Debug|Any CPU + {3E57F4B5-0012-43F7-A016-5B60A27A1D0A}.Debug|x86.Build.0 = Debug|Any CPU + {3E57F4B5-0012-43F7-A016-5B60A27A1D0A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {3E57F4B5-0012-43F7-A016-5B60A27A1D0A}.Release|Any CPU.Build.0 = Release|Any CPU + {3E57F4B5-0012-43F7-A016-5B60A27A1D0A}.Release|x64.ActiveCfg = Release|Any CPU + {3E57F4B5-0012-43F7-A016-5B60A27A1D0A}.Release|x64.Build.0 = Release|Any CPU + {3E57F4B5-0012-43F7-A016-5B60A27A1D0A}.Release|x86.ActiveCfg = Release|Any CPU + {3E57F4B5-0012-43F7-A016-5B60A27A1D0A}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection GlobalSection(NestedProjects) = preSolution {B996D138-9747-4AE9-9FEB-586D6F9CD97E} = {827E0CD3-B72D-47B6-A68D-7590B98EB39B} + {3E57F4B5-0012-43F7-A016-5B60A27A1D0A} = {827E0CD3-B72D-47B6-A68D-7590B98EB39B} EndGlobalSection EndGlobal diff --git a/src/NetPulse.Cli/NetPulse.Cli.csproj b/src/NetPulse.Cli/NetPulse.Cli.csproj new file mode 100644 index 0000000..76f2650 --- /dev/null +++ b/src/NetPulse.Cli/NetPulse.Cli.csproj @@ -0,0 +1,14 @@ + + + + + + + + Exe + net9.0 + enable + enable + + + diff --git a/src/NetPulse.Cli/Program.cs b/src/NetPulse.Cli/Program.cs new file mode 100644 index 0000000..d1f4954 --- /dev/null +++ b/src/NetPulse.Cli/Program.cs @@ -0,0 +1,71 @@ +using System.Net.NetworkInformation; +using NetPulse.Core.Domain; + +if (args.Length == 0) +{ + Console.WriteLine("Usage: netpulse ping "); + return; +} + +var command = args[0].ToLowerInvariant(); + +switch (command) +{ + case "ping": + if (args.Length <2) + { + Console.WriteLine("Missing hostname/IP"); + return; + } + + var target = args[1]; + await RunPingAsync(target); + break; + + default: + Console.WriteLine($"Unkown command: {command}"); + break; +} + +static async Task RunPingAsync(string target) +{ + using var pinger = new Ping(); + + try + { + var reply = await pinger.SendPingAsync(target, 2000); + + var result = new PingResult + { + TargetId = Guid.Empty, + Timestamputc = DateTime.UtcNow, + Success = reply.Status == IPStatus.Success, + LatencyMs = reply.Status == IPStatus.Success ? reply.RoundtripTime : null, + ErrorMessage = reply.Status != IPStatus.Success ? reply.Status.ToString() : null + }; + + Console.WriteLine( + result.Success + ? $"Ping to {target} succeeded in {result.LatencyMs} ms" + : $"Ping to {target} failed: {result.ErrorMessage}"); + + SaveMockResult(result); + } + catch (Exception ex) + { + Console.WriteLine($"Ping failed: {ex.Message}"); + if (ex.InnerException is not null) + { + Console.WriteLine($"Innter: {ex.InnerException.Message}"); + } + } +} + +static void SaveMockResult(PingResult result) +{ + Console.WriteLine(" --- Saved Result ---"); + Console.WriteLine($"ID: {result.Id}"); + Console.WriteLine($"Success: {result.Success}"); + Console.WriteLine($"Latency: {result.LatencyMs}"); + Console.WriteLine($"Error: {result.ErrorMessage}"); +} diff --git a/src/NetPulse.Core/Domain/PingResult.cs b/src/NetPulse.Core/Domain/PingResult.cs index 5d796b7..ad0c729 100644 --- a/src/NetPulse.Core/Domain/PingResult.cs +++ b/src/NetPulse.Core/Domain/PingResult.cs @@ -15,7 +15,7 @@ public class PingResult /// /// When the check was performed (UTC). /// - public DateTime TimeStampUtc { get; init; } + public DateTime Timestamputc { get; init; } /// /// If the ping succeeded.