Skip to content

Commit d2cb34c

Browse files
committed
Added Ping page (#674)
1 parent 30f59eb commit d2cb34c

File tree

4 files changed

+399
-22
lines changed

4 files changed

+399
-22
lines changed

InternetTest/InternetTest/App.xaml.cs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,29 +26,28 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2626
using InternetTest.ViewModels;
2727
using System.Windows;
2828

29-
namespace InternetTest
29+
namespace InternetTest;
30+
31+
/// <summary>
32+
/// Interaction logic for App.xaml
33+
/// </summary>
34+
public partial class App : Application
3035
{
31-
/// <summary>
32-
/// Interaction logic for App.xaml
33-
/// </summary>
34-
public partial class App : Application
36+
protected override void OnStartup(StartupEventArgs e)
3537
{
36-
protected override void OnStartup(StartupEventArgs e)
37-
{
38-
// Load settings
39-
Settings settings = new();
40-
settings.Load();
38+
// Load settings
39+
Settings settings = new();
40+
settings.Load();
4141

42-
ThemeHelper.ChangeTheme(settings.Theme);
42+
ThemeHelper.ChangeTheme(settings.Theme);
4343

44-
// Set the main window
45-
MainWindow = new MainWindow();
46-
MainViewModel mvm = new(settings, MainWindow);
47-
MainWindow.DataContext = mvm;
44+
// Set the main window
45+
MainWindow = new MainWindow();
46+
MainViewModel mvm = new(settings, MainWindow);
47+
MainWindow.DataContext = mvm;
4848

49-
MainWindow.Show();
49+
MainWindow.Show();
5050

51-
base.OnStartup(e);
52-
}
51+
base.OnStartup(e);
5352
}
5453
}

InternetTest/InternetTest/ViewModels/Components/SidebarViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,6 @@ private void LocateIpPage(object? obj)
6969

7070
private void PingPage(object? obj)
7171
{
72-
_mainViewModel.CurrentViewModel = new PingPageViewModel();
72+
_mainViewModel.CurrentViewModel = new PingPageViewModel(_mainViewModel.Settings);
7373
}
7474
}

InternetTest/InternetTest/ViewModels/PingPageViewModel.cs

Lines changed: 105 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,116 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
2121
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2222
SOFTWARE.
2323
*/
24+
using InternetTest.Commands;
25+
using InternetTest.Models;
26+
using System.Net;
27+
using System.Net.NetworkInformation;
28+
using System.Threading.Tasks;
29+
using System.Windows;
30+
using System.Windows.Input;
31+
2432
namespace InternetTest.ViewModels;
2533

2634
public class PingPageViewModel : ViewModelBase
2735
{
28-
public PingPageViewModel()
36+
private string _min = string.Empty;
37+
public string Min { get => _min; set { _min = value; OnPropertyChanged(nameof(Min)); } }
38+
39+
private string _max = string.Empty;
40+
public string Max { get => _max; set { _max = value; OnPropertyChanged(nameof(Max)); } }
41+
42+
private string _avg = string.Empty;
43+
public string Avg { get => _avg; set { _avg = value; OnPropertyChanged(nameof(Avg)); } }
44+
45+
private string _startTime = string.Empty;
46+
public string StartTime { get => _startTime; set { _startTime = value; OnPropertyChanged(nameof(StartTime)); } }
47+
48+
private string _duration = string.Empty;
49+
public string Duration { get => _duration; set { _duration = value; OnPropertyChanged(nameof(Duration)); } }
50+
51+
private string _host = string.Empty;
52+
public string Host { get => _host; set { _host = value; OnPropertyChanged(nameof(Host)); } }
53+
54+
private string _query = string.Empty;
55+
public string Query { get => _query; set { _query = value; OnPropertyChanged(nameof(Query)); } }
56+
57+
private int _received;
58+
public int Received { get => _received; set { _received = value; OnPropertyChanged(nameof(Received)); } }
59+
60+
private int _sent;
61+
public int Sent { get => _sent; set { _sent = value; OnPropertyChanged(nameof(Sent)); } }
62+
63+
private int _lost;
64+
public int Lost { get => _lost; set { _lost = value; OnPropertyChanged(nameof(Lost)); } }
65+
66+
private string _lossPercentage = string.Empty;
67+
public string LossPercentage { get => _lossPercentage; set { _lossPercentage = value; OnPropertyChanged(nameof(LossPercentage)); } }
68+
69+
private int _requestAmount = 4;
70+
public int RequestAmount { get => _requestAmount; set { _requestAmount = value; OnPropertyChanged(nameof(RequestAmount)); } }
71+
72+
private bool _empty = true;
73+
74+
public bool Empty { get => _empty; set { _empty = value; OnPropertyChanged(nameof(Empty)); } }
75+
76+
public ICommand PingCommand => new RelayCommand(o => Ping());
77+
78+
private readonly Settings _settings;
79+
public PingPageViewModel(Settings settings)
80+
{
81+
_settings = settings;
82+
83+
Query = settings.TestSite ?? "google.com";
84+
}
85+
86+
private async Task Ping()
2987
{
88+
Query = Query.Replace("https://", "").Replace("http://", "");
89+
90+
if (Query is null or { Length: 0 } || string.IsNullOrWhiteSpace(Query))
91+
{
92+
MessageBox.Show(Properties.Resources.EnterIP, Properties.Resources.Error, MessageBoxButton.OK, MessageBoxImage.Information);
93+
return;
94+
}
95+
96+
Sent = 0;
97+
Received = 0;
98+
Lost = 0;
99+
StartTime = DateTime.Now.ToString("HH:mm:ss");
100+
Empty = false;
101+
102+
long[] times = new long[RequestAmount];
103+
for (int i = 0; i < RequestAmount; i++)
104+
{
105+
try
106+
{
107+
var ping = await new Ping().SendPingAsync(Query);
108+
times[i] = ping.RoundtripTime;
109+
Sent++;
110+
111+
if (ping.Status == IPStatus.Success)
112+
{
113+
Received++;
114+
Lost = Sent - Received;
115+
LossPercentage = $"{(double)Lost / Sent * 100:0.00}%";
116+
Min = $"{times.Min()} ms";
117+
Max = $"{times.Max()} ms";
118+
Avg = $"{times.Average()} ms";
119+
Duration = $"{times.Sum()} ms";
120+
}
121+
else
122+
{
123+
Lost++;
124+
LossPercentage = $"{(double)Lost / Sent * 100:0.00}%";
125+
}
126+
}
127+
catch (Exception ex)
128+
{
129+
MessageBox.Show(ex.Message, Properties.Resources.Error, MessageBoxButton.OK, MessageBoxImage.Error);
130+
return;
131+
}
132+
}
30133

134+
Host = Dns.GetHostEntry(Query).HostName;
31135
}
32136
}

0 commit comments

Comments
 (0)