Lesson 02
This commit is contained in:
15
NetPulse.sln
15
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
|
||||
|
||||
14
src/NetPulse.Cli/NetPulse.Cli.csproj
Normal file
14
src/NetPulse.Cli/NetPulse.Cli.csproj
Normal file
@@ -0,0 +1,14 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\NetPulse.Core\NetPulse.Core.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
71
src/NetPulse.Cli/Program.cs
Normal file
71
src/NetPulse.Cli/Program.cs
Normal file
@@ -0,0 +1,71 @@
|
||||
using System.Net.NetworkInformation;
|
||||
using NetPulse.Core.Domain;
|
||||
|
||||
if (args.Length == 0)
|
||||
{
|
||||
Console.WriteLine("Usage: netpulse ping <hostname>");
|
||||
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}");
|
||||
}
|
||||
@@ -15,7 +15,7 @@ public class PingResult
|
||||
/// <summary>
|
||||
/// When the check was performed (UTC).
|
||||
/// </summary>
|
||||
public DateTime TimeStampUtc { get; init; }
|
||||
public DateTime Timestamputc { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// If the ping succeeded.
|
||||
|
||||
Reference in New Issue
Block a user