Skip to content

Commit

Permalink
Added: Settings & About GUI. Updated: Main GUI, API, README.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ambratolm authored and Ambratolm committed Jun 10, 2021
1 parent 2f355da commit f60ec62
Show file tree
Hide file tree
Showing 46 changed files with 2,205 additions and 1,832 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,12 @@ public static void SetState(this ProgressBar progressBar, State state)
{
SendMessage(progressBar.Handle, 1040, (IntPtr)state, IntPtr.Zero);
}

public static void SetValue(this ProgressBar progressBar, int value)
{
if (value < progressBar.Minimum) value = progressBar.Minimum;
if (value > progressBar.Maximum) value = progressBar.Maximum;
progressBar.Value = value;
}
}
}
23 changes: 11 additions & 12 deletions Ping Tester Aluminium/API/Extensions/StringExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,26 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace PingTesterAluminium
{
public static class StringExtension
{
public static bool IsEmpty(this string str)
public static bool GetIsNullOrWhiteSpace(this string str)
{
return str == string.Empty;
return string.IsNullOrWhiteSpace(str);
}
public static bool IsValidHost(this string str)

public static bool GetIsValidHost(this string str)
{
//try
//{
// IPHostEntry ipHost = Dns.GetHostEntry(host);
// return true;
//}
//catch (SocketException)
//{
// return false;
//}
return Uri.CheckHostName(str) != UriHostNameType.Unknown;
}

public static string ToTitleCase(this string str)
{
Regex regex = new Regex(@"(?<=[A-Z])(?=[A-Z][a-z])|(?<=[^A-Z])(?=[A-Z])|(?<=[A-Za-z])(?=[^A-Za-z])");
return regex.Replace(str, " ");
}
}
}
20 changes: 20 additions & 0 deletions Ping Tester Aluminium/API/PingRating.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PingTesterAluminium
{
public enum PingRating
{
Unknown = 0,
Amazing = 1,
Excellent = 2,
Good = 3,
NotBad = 4,
Bad = 5,
Mediocre = 6,
Poor = 7,
Terrible = 8
}
}
76 changes: 65 additions & 11 deletions Ping Tester Aluminium/API/PingResult.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,78 @@
using System;
using System.Drawing;
using System.Net.NetworkInformation;
using System.Net;

namespace PingTesterAluminium
{
public class PingResult
{
public long Time { get; set; }
public string Rating { get; set; }
public Color Color { get; set; }
public string Message { get; set; }
public string Host { get; set; }
public IPAddress Address { get; private set; }
public Byte[] Buffer { get; private set; }
public long Time { get; private set; }
public IPStatus Status { get; private set; }
public int Ttl { get; set; }
public bool DontFragment { get; private set; }
public PingRating Rating { get; private set; }
public Color RatingColor { get; private set; }
public Color StatusColor { get; private set; }
public string ErrorMessage { get; private set; }
public bool HasError { get; private set; }

public PingResult(int time, string rating, Color color, string message, string host)
public PingResult(PingReply reply = null)
{
this.Time = time;
this.Rating = rating;
this.Color = color;
this.Message = message;
this.Host = host;
if (reply == null)
{
this.Time = -1;
this.Status = IPStatus.Unknown;
}
else
{
this.Address = reply.Address;
this.Buffer = reply.Buffer;
this.Time = reply.RoundtripTime;
this.Status = reply.Status;
this.Ttl = reply.Options.Ttl;
this.DontFragment = reply.Options.DontFragment;
}
this.Rating = GetRating(this.Time);
this.RatingColor = GetRatingColor(this.Time);
this.StatusColor = GetStatusColor(this.Status);
}

public PingResult(string errorMessage)
: this()
{
this.ErrorMessage = errorMessage;
this.HasError = true;
}

public static PingRating GetRating(long time = -1)
{
if (time < 0) return PingRating.Unknown;
if (time < 30) return PingRating.Amazing;
if (time < 60) return PingRating.Excellent;
if (time < 100) return PingRating.Good;
if (time < 150) return PingRating.NotBad;
if (time < 200) return PingRating.Bad;
if (time < 250) return PingRating.Mediocre;
if (time < 300) return PingRating.Poor;
return PingRating.Terrible;
}

public static Color GetRatingColor(long time = -1)
{
if (time < 0) return Color.Gray;
if (time <= 100) return Color.Green;
if (time <= 150) return Color.Yellow;
return Color.Red;
}

public static Color GetStatusColor(IPStatus status = IPStatus.Unknown)
{
if (status == IPStatus.Unknown) return Color.Gray;
if (status == IPStatus.Success) return Color.Blue;
else return Color.DarkRed;
}
}
}
52 changes: 14 additions & 38 deletions Ping Tester Aluminium/API/PingTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,51 +6,27 @@ namespace PingTesterAluminium
{
public static class PingTest
{
public static PingResult Ping(string host)
public static PingResult Run(string host, int timeout = 5000,
int bufferLength = 32, int ttl = 128, bool dontFragment = false)
{
if (host.IsEmpty() || !host.IsValidHost())
{
host = "google.com";
}
PingReply pingReply = null;
try
{
pingReply = new Ping().Send(host);
}
catch (Exception exception)
{
return new PingResult(-1, "Error", Color.DarkRed, exception.Message, host);
if (timeout < 0) timeout = 0;

if (bufferLength < 0) bufferLength = 0;
if (bufferLength > 65500) bufferLength = 65500;

if (ttl < 0) ttl = 0;
if (ttl > 255) ttl = 255;

PingReply reply = new Ping().Send(host, timeout, new byte[bufferLength],
new PingOptions(ttl, dontFragment));
return new PingResult(reply);
}
if (pingReply.Status != IPStatus.Success)
{
return new PingResult((int)pingReply.RoundtripTime, "No Response",
Color.Gray, pingReply.Status.ToString(), host);
}
else
catch (Exception exception)
{
return new PingResult((int)pingReply.RoundtripTime, GetRating(pingReply.RoundtripTime),
GetColor(pingReply.RoundtripTime), pingReply.Status.ToString(), host);
return new PingResult(exception.Message);
}
}

public static string GetRating(long time)
{
if (time <= 30) return "Amazing";
if (time <= 60) return "Excellent";
if (time <= 100) return "Good";
if (time <= 150) return "Not Bad";
if (time <= 200) return "Bad";
if (time <= 250) return "Mediocre";
if (time <= 300) return "Poor";
return "Terrible";
}

private static Color GetColor(long time)
{
if (time <= 100) return Color.Green;
if (time <= 150) return Color.Yellow;
return Color.Red;
}
}
}
101 changes: 101 additions & 0 deletions Ping Tester Aluminium/API/PingTestManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PingTesterAluminium.Properties;

namespace PingTesterAluminium
{
public static class PingTestManager
{
public static string Host
{
get
{
return Settings.Default.Host;
}
set
{
Settings.Default.Host = value;
Settings.Default.Save();
}
}
public static int Interval
{
get
{
return Settings.Default.Interval;
}
set
{
if (value < 100) value = 100;
Settings.Default.Interval = value;
Settings.Default.Save();
}
}
public static int Timeout
{
get
{
return Settings.Default.Timeout;
}
set
{
if (value < 0) value = 0;
Settings.Default.Timeout = value;
Settings.Default.Save();
}
}
public static int BufferLength
{
get
{
return Settings.Default.BufferLength;
}
set
{
if (value < 0) value = 0;
if (value > 65500) value = 65500;
Settings.Default.BufferLength = value;
Settings.Default.Save();
}
}
public static int Ttl
{
get
{
return Settings.Default.Ttl;
}
set
{
if (value < 0) value = 0;
if (value > 255) value = 255;
Settings.Default.Ttl = value;
Settings.Default.Save();
}
}
public static bool DontFragment
{
get
{
return Settings.Default.DontFragment;
}
set
{
Settings.Default.DontFragment = value;
Settings.Default.Save();
}
}

public static PingResult RunPingTest()
{
return PingTest.Run(
host: Host,
timeout: Timeout,
bufferLength: BufferLength,
ttl: Ttl,
dontFragment: DontFragment
);
}
}
}
Loading

0 comments on commit f60ec62

Please sign in to comment.