Mercurial > servermonitor
comparison ServerMonitor/Objects/PingCheck.cs @ 0:3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
author | Brad Greco <brad@bgreco.net> |
---|---|
date | Mon, 31 Dec 2018 18:32:14 -0500 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:3e1a2131f897 |
---|---|
1 using System; | |
2 using System.Collections.Generic; | |
3 using System.ComponentModel; | |
4 using System.Linq; | |
5 using System.Net.NetworkInformation; | |
6 using System.Text; | |
7 using System.Threading; | |
8 using System.Threading.Tasks; | |
9 | |
10 namespace ServerMonitorApp | |
11 { | |
12 [DisplayName("Ping check"), Description("Check if the server responds to a ping request"), DisplayWeight(0)] | |
13 public class PingCheck : Check | |
14 { | |
15 protected async override Task<CheckResult> ExecuteCheckAsync(CancellationToken token) | |
16 { | |
17 using (Ping ping = new Ping()) | |
18 { | |
19 try | |
20 { | |
21 PingReply reply = await ping.SendPingAsync(Server.Host, Timeout); | |
22 if (reply.Status == IPStatus.Success) | |
23 return Pass(string.Format("Reply received in {0} ms", reply.RoundtripTime)); | |
24 else | |
25 return Fail("Ping result: " + reply.Status); | |
26 } | |
27 catch (PingException e) | |
28 { | |
29 return Fail(e); | |
30 } | |
31 } | |
32 | |
33 // Cancellable version that doesn't actulaly work | |
34 // might be useful as a TaskCompletionSource example though | |
35 // | |
36 //TaskCompletionSource<CheckResult> tcs = new TaskCompletionSource<CheckResult | |
37 // | |
38 //using (Ping ping = new Ping()) | |
39 //{ | |
40 // token.Register(ping.SendAsyncCancel); | |
41 // ping.PingCompleted += (sender, e) => | |
42 // { | |
43 // if (e.Error != null) | |
44 // tcs.SetResult(Fail("Ping failed: " + e.Error.GetBaseException().Message)); | |
45 // else if (e.Reply.Status != IPStatus.Success) | |
46 // tcs.SetResult(Fail("Ping failed: " + e.Reply.Status.ToString())); | |
47 // else | |
48 // tcs.SetResult(Pass("Ping completed in " + e.Reply.RoundtripTime + "ms")); | |
49 // }; | |
50 // ping.SendAsync(Server.Host, Timeout, null); | |
51 //} | |
52 | |
53 //return tcs.Task; | |
54 } | |
55 } | |
56 } |