Lesson 03

This commit is contained in:
2025-11-29 00:43:31 -06:00
parent 097ce025e7
commit 9e08ea251f
3 changed files with 95 additions and 6 deletions

View File

@@ -1,5 +1,6 @@
using System.Net.NetworkInformation; using System.Net.NetworkInformation;
using NetPulse.Core.Domain; using NetPulse.Core.Domain;
using NetPulse.Core.Services;
if (args.Length == 0) if (args.Length == 0)
{ {
@@ -18,15 +19,32 @@ switch (command)
return; return;
} }
var target = args[1]; var service = new PingService();
await RunPingAsync(target); var result = await service.RunAsync(Guid.Empty, args[1]);
break; Console.WriteLine(result.Success
? $"Success: {result.LatencyMs} ms"
: $"Failed: {result.ErrorMessage}");
if (!result.Success && result.InnerErrorMessage is not null)
{
Console.WriteLine($"Inner error: {result.InnerErrorMessage}");
}
default:
Console.WriteLine($"Unkown command: {command}");
break; break;
} }
/*
===========================================================
ARCHIVE NOTE — Old Ping Logic (Lesson 2 → Pre-Service Layer)
Keeping this for reference only. Not compiled. Not used.
This was the original inline/local-function ping approach
before we migrated to the proper PingService architecture.
Useful for:
- remembering how PingResult was initially populated
- comparing exception handling approaches
- seeing the progression of the codebase over lessons
- nostalgia (optional but valid)
===========================================================
static async Task RunPingAsync(string target) static async Task RunPingAsync(string target)
{ {
using var pinger = new Ping(); using var pinger = new Ping();
@@ -61,6 +79,7 @@ static async Task RunPingAsync(string target)
} }
} }
static void SaveMockResult(PingResult result) static void SaveMockResult(PingResult result)
{ {
Console.WriteLine(" --- Saved Result ---"); Console.WriteLine(" --- Saved Result ---");
@@ -69,3 +88,7 @@ static void SaveMockResult(PingResult result)
Console.WriteLine($"Latency: {result.LatencyMs}"); Console.WriteLine($"Latency: {result.LatencyMs}");
Console.WriteLine($"Error: {result.ErrorMessage}"); Console.WriteLine($"Error: {result.ErrorMessage}");
} }
===========================================================
End of Archive Block
===========================================================
*/

View File

@@ -31,4 +31,9 @@ public class PingResult
/// Error detail if the pin failed. /// Error detail if the pin failed.
/// </summary> /// </summary>
public string? ErrorMessage { get; init; } public string? ErrorMessage { get; init; }
/// <summary>
/// InnerErrorMessage for error handling
/// </summary>
public string? InnerErrorMessage { get; init; }
} }

View File

@@ -0,0 +1,61 @@
using System.Net.NetworkInformation;
using NetPulse.Core.Domain;
namespace NetPulse.Core.Services;
public class PingService
{
private readonly Ping _pinger = new();
public async Task<PingResult> RunAsync(Guid targetId, string host)
{
try
{
var reply = await _pinger.SendPingAsync(host, 2000);
if (reply.Status == IPStatus.Success)
{
return new PingResult
{
TargetId = targetId,
Timestamputc = DateTime.UtcNow,
Success = true,
LatencyMs = reply.RoundtripTime,
ErrorMessage = null
};
}
return new PingResult
{
TargetId = targetId,
Timestamputc = DateTime.UtcNow,
Success = false,
LatencyMs = null,
ErrorMessage = $"Ping failed with status: {reply.Status}"
};
}
catch (PingException ex)
{
return new PingResult
{
TargetId = targetId,
Timestamputc = DateTime.UtcNow,
Success = false,
LatencyMs = null,
ErrorMessage = ex.Message,
InnerErrorMessage = ex.InnerException?.Message
};
}
catch (Exception ex)
{
return new PingResult
{
TargetId = targetId,
Timestamputc = DateTime.UtcNow,
Success = false,
LatencyMs = null,
ErrorMessage = ex.Message
};
}
}
}