annotate ServerMonitor/Objects/ServerMonitor.cs @ 4:3142e52cbe69

Lots more progress
author Brad Greco <brad@bgreco.net>
date Sun, 10 Feb 2019 20:51:26 -0500
parents 3e1a2131f897
children b6fe203af9d5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
1 using ServerMonitorApp.Properties;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
2 using System;
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
3 using System.Collections.Generic;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
4 using System.Diagnostics;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
5 using System.IO;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
6 using System.Linq;
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
7 using System.Net.NetworkInformation;
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
8 using System.Text;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
9 using System.Threading;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
10 using System.Threading.Tasks;
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
11 using System.Windows.Forms;
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
12 using System.Xml.Serialization;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
13
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
14 namespace ServerMonitorApp
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
15 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
16 public class ServerMonitor
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
17 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
18 private readonly string configFileDir;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
19 private readonly Logger logger;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
20 private readonly Dictionary<int, CancellationTokenSource> tokens = new Dictionary<int, CancellationTokenSource>();
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
21 private readonly List<int> pausedChecks = new List<int>();
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
22 private bool running, networkAvailable;
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
23 private Dictionary<Task<CheckResult>, int> tasks;
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
24 private ServerSummaryForm mainForm;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
25
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
26 //private List<Task<CheckResult>> tasks;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
27
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
28 public event EventHandler<CheckStatusChangedEventArgs> CheckStatusChanged;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
29
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
30 public List<Server> Servers { get; private set; } = new List<Server>();
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
31
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
32 public IEnumerable<Check> Checks => Servers.SelectMany(s => s.Checks);
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
33
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
34 public string ConfigFile { get; private set; }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
35
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
36 public ServerMonitor(ServerSummaryForm mainForm)
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
37 {
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
38 this.mainForm = mainForm;
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
39 configFileDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "ServerMonitor");
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
40 ConfigFile = Path.Combine(configFileDir, "servers.xml");
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
41 logger = new Logger(Path.Combine(configFileDir, "monitor.log"));
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
42 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
43
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
44 public void AddServer(Server server)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
45 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
46 Servers.Add(server);
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
47 SaveServers();
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
48 }
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
49
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
50 public void DeleteServer(Server server)
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
51 {
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
52 Servers.Remove(server);
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
53 SaveServers();
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
54 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
55
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
56 public void LoadServers()
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
57 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
58 TextReader reader = null;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
59 try
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
60 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
61 reader = new StreamReader(ConfigFile);
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
62 XmlSerializer serializer = CreateXmlSerializer();
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
63 Servers.Clear();
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
64 Servers.AddRange((List<Server>)serializer.Deserialize(reader));
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
65 // Update the Checks so they know what Server they belong to.
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
66 // Would rather do this in the Server object on deserialization, but
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
67 // that doesn't work when using the XML serializer for some reason.
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
68 foreach (Server server in Servers)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
69 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
70 foreach (Check check in server.Checks)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
71 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
72 check.Server = server;
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
73 if (check.Status == CheckStatus.Running)
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
74 check.Status = check.LastRunStatus;
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
75 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
76 server.CheckModified += Server_CheckModified;
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
77 server.EnabledChanged += Server_EnabledChanged;
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
78 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
79 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
80 // If the file doesn't exist, no special handling is needed. It will be created later.
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
81 catch (FileNotFoundException) { }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
82 catch (DirectoryNotFoundException) { }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
83 catch (InvalidOperationException)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
84 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
85 //TODO log
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
86 throw;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
87 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
88 finally
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
89 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
90 reader?.Close();
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
91 }
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
92 NetworkChange.NetworkAddressChanged += NetworkChange_NetworkAddressChanged;
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
93 Run();
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
94 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
95
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
96 public void SaveServers()
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
97 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
98 GenerateIds();
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
99 TextWriter writer = null;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
100 XmlSerializer serializer = null;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
101 try
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
102 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
103 writer = new StreamWriter(ConfigFile);
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
104 serializer = CreateXmlSerializer();
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
105 serializer.Serialize(writer, Servers);
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
106 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
107 catch (DirectoryNotFoundException)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
108 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
109 Directory.CreateDirectory(configFileDir);
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
110 writer = new StreamWriter(ConfigFile);
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
111 serializer = CreateXmlSerializer();
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
112 serializer.Serialize(writer, Servers);
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
113 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
114 catch (Exception)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
115 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
116 //TODO log
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
117 throw;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
118 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
119 finally
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
120 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
121 writer?.Close();
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
122 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
123 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
124
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
125 private async void Run()
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
126 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
127 if (running)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
128 return;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
129 running = true;
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
130 networkAvailable = Helpers.IsNetworkAvailable();
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
131 if (networkAvailable)
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
132 {
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
133 foreach (int id in pausedChecks)
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
134 {
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
135 await ExecuteCheckAsync(Checks.FirstOrDefault(c => c.Id == id));
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
136 }
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
137 pausedChecks.Clear();
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
138 }
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
139 //TODO subscribe to power events. Find any check's NextExecutionTime is in the past. Cancel waiting task and run immediately (or after short delay).
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
140 //tasks = Checks.Select(c => ScheduleExecuteCheckAsync(c)).ToList();
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
141 tasks = Checks.ToDictionary(c => ScheduleExecuteCheckAsync(c), c => c.Id);
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
142 while (tasks.Count > 0)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
143 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
144 Task<CheckResult> task = await Task.WhenAny(tasks.Keys);
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
145 tasks.Remove(task);
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
146 try
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
147 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
148 CheckResult result = await task;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
149 // Result will be null if a scheduled check was disabled
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
150 if (result != null && result.CheckStatus != CheckStatus.Disabled)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
151 tasks.Add(ScheduleExecuteCheckAsync(result.Check), result.Check.Id);
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
152 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
153 catch (OperationCanceledException)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
154 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
155
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
156 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
157 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
158 running = false;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
159 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
160
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
161 public async Task<CheckResult> ExecuteCheckAsync(Check check, CancellationToken token = default(CancellationToken))
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
162 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
163 check.Status = CheckStatus.Running;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
164 OnCheckStatusChanged(check);
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
165 CheckResult result = await check.ExecuteAsync(token);
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
166 OnCheckStatusChanged(check, result);
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
167 HandleResultAsync(result);
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
168 return result;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
169 }
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
170
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
171 private void HandleResultAsync(CheckResult result)
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
172 {
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
173 logger.Log(result);
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
174 if (result.FailAction == FailAction.FlashTaskbar)
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
175 mainForm.AlertServerForm(result.Check);
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
176 if (result.FailAction.In(FailAction.FlashTaskbar, FailAction.NotificationBalloon))
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
177 mainForm.ShowBalloon(result);
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
178 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
179
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
180 public IList<CheckResult> GetLog(Server server)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
181 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
182 return logger.Read(server);
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
183 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
184
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
185 private void OnCheckStatusChanged(Check check, CheckResult result = null)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
186 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
187 SaveServers();
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
188 CheckStatusChanged?.Invoke(check, new CheckStatusChangedEventArgs(check, result));
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
189 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
190
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
191 private void Server_CheckModified(object sender, EventArgs e)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
192 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
193 Check check = (Check)sender;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
194 Task<CheckResult> task = tasks.FirstOrDefault(kvp => kvp.Value == check.Id).Key;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
195 if (running)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
196 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
197 if (task == null)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
198 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
199 // No tasks associated with the check, so schedule a new one
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
200 tasks.Add(ScheduleExecuteCheckAsync(check), check.Id);
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
201 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
202 else
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
203 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
204 // Check was modified or deleted, so remove any waiting tasks
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
205 CancelCheck(check);
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
206 if (check.Server != null)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
207 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
208 // If the check was not deleted, schedule the new check.
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
209 // But only if it's still running, otherwise restarting the monitor below
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
210 // will create a duplicate run.
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
211 if (running)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
212 tasks.Add(ScheduleExecuteCheckAsync(check), check.Id);
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
213 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
214 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
215 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
216 // Run again in case removing a task above caused it to stop
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
217 Run();
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
218 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
219
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
220 private void Server_EnabledChanged(object sender, EventArgs e)
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
221 {
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
222 Server server = (Server)sender;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
223 if (server.Enabled)
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
224 {
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
225 Run();
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
226 }
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
227 else
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
228 {
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
229 foreach (Check check in server.Checks)
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
230 {
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
231 CancelCheck(check);
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
232 }
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
233 }
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
234 }
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
235
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
236 private void CancelCheck(Check check)
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
237 {
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
238 Task<CheckResult> task = tasks.FirstOrDefault(kvp => kvp.Value == check.Id).Key;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
239 tasks.Remove(task);
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
240 pausedChecks.RemoveAll(id => id == check.Id);
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
241 if (tokens.TryGetValue(check.Id, out CancellationTokenSource cts))
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
242 cts.Cancel();
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
243 }
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
244
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
245 private async Task<CheckResult> ScheduleExecuteCheckAsync(Check check)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
246 {
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
247 if (!check.Enabled || !check.Server.Enabled)
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
248 return await Task.FromResult(new CheckResult(check, CheckStatus.Disabled, null));
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
249
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
250 CancellationTokenSource cts = new CancellationTokenSource();
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
251 tokens[check.Id] = cts;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
252 check.NextRunTime = check.Schedule.GetNextTime(check.LastScheduledRunTime);
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
253 await Task.Delay(check.NextRunTime - DateTime.Now, cts.Token);
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
254 check.LastScheduledRunTime = check.NextRunTime;
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
255 if (networkAvailable)
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
256 return await ExecuteCheckAsync(check, cts.Token);
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
257 else
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
258 {
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
259 if (!pausedChecks.Contains(check.Id))
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
260 pausedChecks.Add(check.Id);
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
261 return await Task.FromResult(new CheckResult(check, CheckStatus.Disabled, null));
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
262 }
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
263 }
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
264
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
265 private void NetworkChange_NetworkAddressChanged(object sender, EventArgs e)
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
266 {
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
267 networkAvailable = Helpers.IsNetworkAvailable();
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
268 if (networkAvailable)
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
269 mainForm.Invoke((MethodInvoker)(() => Run()));
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
270 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
271
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
272 private void GenerateIds()
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
273 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
274 if (Servers.Any())
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
275 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
276 int id = Servers.Max(s => s.Id);
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
277 foreach (Server server in Servers)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
278 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
279 if (server.Id == 0)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
280 server.Id = ++id;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
281 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
282 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
283
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
284 if (Checks.Any())
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
285 {
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
286 int id = Math.Max(Settings.Default.MaxCheckId, Checks.Max(c => c.Id));
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
287 foreach (Check check in Checks)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
288 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
289 if (check.Id == 0)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
290 check.Id = ++id;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
291 }
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
292 Settings.Default.MaxCheckId = id;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
293 Settings.Default.Save();
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
294 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
295 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
296
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
297 private XmlSerializer CreateXmlSerializer()
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
298 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
299 return new XmlSerializer(typeof(List<Server>), Check.CheckTypes);
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
300 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
301 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
302
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
303 public class CheckStatusChangedEventArgs : EventArgs
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
304 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
305 public Check Check { get; private set; }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
306
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
307 public CheckResult CheckResult { get; private set; }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
308
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
309 public CheckStatusChangedEventArgs(Check check, CheckResult result)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
310 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
311 Check = check;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
312 CheckResult = result;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
313 }
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
314 }
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
315
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
316 public enum FailAction { FlashTaskbar = 0, NotificationBalloon = 1, None = 10 }
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
317 }