Ping machine method
parent
1fcd2afdc5
commit
c8e2e3e1e4
|
@ -39,5 +39,12 @@ namespace NetworkResurrector.Api.Controllers
|
|||
var result = await _mediator.Send(wakeMachine);
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
[HttpPost("ping")]
|
||||
public async Task<IActionResult> PingMachine([FromBody] PingMachine pingMachine)
|
||||
{
|
||||
var result = await _mediator.Send(pingMachine);
|
||||
return Ok(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,40 @@
|
|||
using MediatR;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using NetworkResurrector.Application.Commands;
|
||||
using NetworkResurrector.Application.Events;
|
||||
using NetworkResurrector.Application.Services;
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace NetworkResurrector.Application.CommandHandlers
|
||||
{
|
||||
public class PingMachineHandler : IRequestHandler<PingMachine, MachinePinged>
|
||||
{
|
||||
private readonly ILogger<PingMachineHandler> _logger;
|
||||
private readonly IPingService _pingService;
|
||||
|
||||
public PingMachineHandler(ILogger<PingMachineHandler> logger, IPingService pingService)
|
||||
{
|
||||
_logger = logger;
|
||||
_pingService = pingService;
|
||||
}
|
||||
|
||||
public async Task<MachinePinged> Handle(PingMachine command, CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
_logger.LogDebug($"Start pinging '{command.IPAddress}'.");
|
||||
var (success, status) = await _pingService.PingMachine(command.IPAddress);
|
||||
_logger.LogDebug($"Pinging on '{command.IPAddress}' finished. Status: {status}");
|
||||
return new MachinePinged(success, status);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
var correlationIdMsg = $"CorrelationId: {command.Metadata.CorrelationId}";
|
||||
_logger.LogError(ex, $"An unexpected error has occurred. {correlationIdMsg}");
|
||||
return new MachinePinged(false, $"{ex.Message} {correlationIdMsg}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
using NetworkResurrector.Application.Events;
|
||||
using System;
|
||||
|
||||
namespace NetworkResurrector.Application.Commands
|
||||
{
|
||||
public class PingMachine : Command<MachinePinged>
|
||||
{
|
||||
public string IPAddress { get; set; }
|
||||
|
||||
public PingMachine(string ipAddress) : base(new Metadata() { CorrelationId = Guid.NewGuid() })
|
||||
{
|
||||
IPAddress = ipAddress;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -12,6 +12,7 @@ namespace NetworkResurrector.Application
|
|||
services.AddSingleton<IParamProvider, ParamProvider>();
|
||||
services.AddScoped<IUserService, UserService>();
|
||||
services.AddStores();
|
||||
services.AddSingleton<IPingService, PingService>();
|
||||
}
|
||||
|
||||
private static void AddStores(this IServiceCollection services)
|
||||
|
|
|
@ -0,0 +1,14 @@
|
|||
namespace NetworkResurrector.Application.Events
|
||||
{
|
||||
public class MachinePinged
|
||||
{
|
||||
public bool Success { get; }
|
||||
public string Status { get; }
|
||||
|
||||
public MachinePinged(bool success, string status)
|
||||
{
|
||||
Success = success;
|
||||
Status = status;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
using System.Threading.Tasks;
|
||||
|
||||
namespace NetworkResurrector.Application.Services
|
||||
{
|
||||
public interface IPingService
|
||||
{
|
||||
Task<(bool success, string status)> PingMachine(string ipAddress);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
using System.Net.NetworkInformation;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace NetworkResurrector.Application.Services
|
||||
{
|
||||
public class PingService : IPingService
|
||||
{
|
||||
/// <summary>
|
||||
/// Ping machine by IP
|
||||
/// https://docs.microsoft.com/en-us/dotnet/api/system.net.networkinformation.ping?redirectedfrom=MSDN&view=net-5.0
|
||||
/// </summary>
|
||||
/// <param name="ipAddress"></param>
|
||||
/// <returns>(bool success, string status)</returns>
|
||||
public async Task<(bool success, string status)> PingMachine(string ipAddress)
|
||||
{
|
||||
var ping = new Ping();
|
||||
|
||||
// Use the default Ttl value which is 128,
|
||||
// but change the fragmentation behavior.
|
||||
PingOptions options = new PingOptions
|
||||
{
|
||||
DontFragment = true
|
||||
};
|
||||
|
||||
// Create a buffer of 32 bytes of data to be transmitted.
|
||||
string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
|
||||
byte[] buffer = Encoding.ASCII.GetBytes(data);
|
||||
int timeout = 120;
|
||||
|
||||
PingReply reply = await ping.SendPingAsync(ipAddress, timeout, buffer, options);
|
||||
|
||||
if (reply.Status == IPStatus.Success)
|
||||
{
|
||||
var builder = new StringBuilder();
|
||||
builder.AppendLine($"Machine '{ipAddress}' has responded to ping.");
|
||||
|
||||
builder.AppendLine($"Address: {reply.Address.ToString()}");
|
||||
builder.AppendLine($"RoundTrip time: {reply.RoundtripTime}");
|
||||
builder.AppendLine($"Time to live: {reply.Options.Ttl}");
|
||||
builder.AppendLine($"Don't fragment: {reply.Options.DontFragment}");
|
||||
builder.AppendLine($"Buffer size: {reply.Buffer.Length}");
|
||||
|
||||
return (true, builder.ToString());
|
||||
}
|
||||
else
|
||||
return (false, $"Machine '{ipAddress}' does not respond to ping. Status: {reply.Status}");
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue