aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/PPC/Tango.PPC.Common/WatchDog
diff options
context:
space:
mode:
authorRoy Ben-Shabat <Roy@Twine-s.com>2019-02-17 15:52:11 +0200
committerRoy Ben-Shabat <Roy@Twine-s.com>2019-02-17 15:52:11 +0200
commite29d962c5602fc9fc3a54fa4da8957609de7eea4 (patch)
tree857c8448611b79c3d3834f37edb00965ad5435ba /Software/Visual_Studio/PPC/Tango.PPC.Common/WatchDog
parent0cd0b590f62b31a8874ea21f225ba75c7a37053c (diff)
downloadTango-e29d962c5602fc9fc3a54fa4da8957609de7eea4.tar.gz
Tango-e29d962c5602fc9fc3a54fa4da8957609de7eea4.zip
Completed PPC watchdog !
Diffstat (limited to 'Software/Visual_Studio/PPC/Tango.PPC.Common/WatchDog')
-rw-r--r--Software/Visual_Studio/PPC/Tango.PPC.Common/WatchDog/WatchDogClient.cs87
-rw-r--r--Software/Visual_Studio/PPC/Tango.PPC.Common/WatchDog/WatchDogServer.cs108
2 files changed, 180 insertions, 15 deletions
diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/WatchDog/WatchDogClient.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/WatchDog/WatchDogClient.cs
index 47a24ea85..bbdfbf7e2 100644
--- a/Software/Visual_Studio/PPC/Tango.PPC.Common/WatchDog/WatchDogClient.cs
+++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/WatchDog/WatchDogClient.cs
@@ -1,12 +1,97 @@
using System;
using System.Collections.Generic;
+using System.IO;
+using System.IO.Pipes;
using System.Linq;
using System.Text;
+using System.Threading;
using System.Threading.Tasks;
+using Tango.Core;
namespace Tango.PPC.Common.WatchDog
{
- public class WatchDogClient
+ public class WatchDogClient : ExtendedObject
{
+ public event EventHandler ServerNotResponding;
+
+ private NamedPipeClientStream _client;
+ private StreamReader _reader;
+ private StreamWriter _writer;
+ private Thread _thread;
+
+ public bool IsStarted { get; private set; }
+
+ public TimeSpan Interval { get; set; }
+
+ public WatchDogClient()
+ {
+ Interval = TimeSpan.FromSeconds(5);
+ }
+
+ public void Start()
+ {
+ if (!IsStarted)
+ {
+ LogManager.Log("Starting watchdog client...");
+
+ IsStarted = true;
+
+ _thread = new Thread(ThreadMethod);
+ _thread.IsBackground = true;
+ _thread.Start();
+ }
+ }
+
+ private void ThreadMethod()
+ {
+ try
+ {
+ LogManager.Log("Watchdog client started.");
+
+ while (IsStarted)
+ {
+ _client = new NamedPipeClientStream("Tango_Watch_Dog_Pipe");
+ _client.Connect(5000);
+
+ _reader = new StreamReader(_client);
+ _writer = new StreamWriter(_client);
+
+ _writer.WriteLine("ping");
+ _writer.Flush();
+
+ var line = _reader.ReadLine();
+
+ if (line != "ping")
+ {
+ LogManager.Log("Watchdog server not found. Raising notification...");
+ try
+ {
+ IsStarted = false;
+ _client.Dispose();
+ }
+ catch { }
+
+ ServerNotResponding?.Invoke(this, new EventArgs());
+ return;
+ }
+
+ _client.Dispose();
+
+ Thread.Sleep(Interval);
+ }
+ }
+ catch (Exception ex)
+ {
+ IsStarted = false;
+ LogManager.Log(ex, "Error in watchdog client.");
+ ServerNotResponding?.Invoke(this, new EventArgs());
+ }
+ }
+
+ public void Stop()
+ {
+ IsStarted = false;
+ _client.Dispose();
+ }
}
}
diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/WatchDog/WatchDogServer.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/WatchDog/WatchDogServer.cs
index 77bca2e30..84ff68d08 100644
--- a/Software/Visual_Studio/PPC/Tango.PPC.Common/WatchDog/WatchDogServer.cs
+++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/WatchDog/WatchDogServer.cs
@@ -1,26 +1,60 @@
using System;
using System.Collections.Generic;
+using System.Diagnostics;
using System.IO;
using System.IO.Pipes;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
+using System.Windows.Threading;
+using Tango.Core;
+using Tango.Core.Helpers;
namespace Tango.PPC.Common.WatchDog
{
- public class WatchDogServer
+ public class WatchDogServer : ExtendedObject, IDisposable
{
private NamedPipeServerStream _server;
+ private StreamReader _reader;
+ private StreamWriter _writer;
private Thread _thread;
+ private Dispatcher _dispatcher;
+ private String _watchdogAppPath;
+ private Process _watchdogProcess;
public bool IsStarted { get; private set; }
+ public WatchDogServer(Dispatcher uiDispatcher)
+ {
+ _watchdogAppPath = Path.Combine(AssemblyHelper.GetCurrentAssemblyFolder(), "Tango.PPC.WatchDog.exe");
+
+ _dispatcher = uiDispatcher;
+ }
+
public void Start()
{
- _thread = new Thread(ThreadMethod);
- _thread.IsBackground = true;
- _thread.Start();
+ LogManager.Log("Starting watchdog server...");
+
+ try
+ {
+ _watchdogProcess = Process.GetProcessesByName("Tango.PPC.WatchDog").FirstOrDefault();
+
+ _server = new NamedPipeServerStream("Tango_Watch_Dog_Pipe");
+ _reader = new StreamReader(_server);
+ _writer = new StreamWriter(_server);
+
+ IsStarted = true;
+ _thread = new Thread(ThreadMethod);
+ _thread.IsBackground = true;
+ _thread.Start();
+
+ LogManager.Log("Watchdog server started.");
+ }
+ catch (Exception ex)
+ {
+ LogManager.Log(ex, "Error starting watchdog server!");
+ }
}
private void ThreadMethod()
@@ -29,27 +63,73 @@ namespace Tango.PPC.Common.WatchDog
try
{
- _server = new NamedPipeServerStream("Tango_Watch_Dog_Pipe");
- _server.WaitForConnection();
- StreamReader reader = new StreamReader(_server);
- StreamWriter writer = new StreamWriter(_server);
-
while (IsStarted)
{
- var line = reader.ReadLine();
- writer.WriteLine(line);
- writer.Flush();
+ if (_watchdogProcess == null)
+ {
+ LogManager.Log($"Starting watchdog process at '{_watchdogAppPath}'...");
+
+ try
+ {
+ _watchdogProcess = Process.Start(_watchdogAppPath);
+ }
+ catch (Exception ex)
+ {
+ LogManager.Log(ex, "Error starting watchdog process!");
+ return;
+ }
+ }
+
+ _server.WaitForConnection();
+
+ var line = _reader.ReadLine();
+
+ _dispatcher.Invoke(() =>
+ {
+ _writer.WriteLine(line);
+ _writer.Flush();
+ });
+
+ _server.Disconnect();
}
}
- catch
+ catch (Exception ex)
{
+ LogManager.Log(ex, "Error in watchdog server. Restarting watchdog...");
IsStarted = false;
+
+ try
+ {
+ _server.Dispose();
+ }
+ catch { }
+
+ Start();
+ return;
}
}
public void Stop()
{
- _server.Dispose();
+ if (IsStarted)
+ {
+ try
+ {
+ _watchdogProcess.Kill();
+ _watchdogProcess = null;
+ }
+ catch (Exception ex)
+ {
+ LogManager.Log(ex, "Error killing watchdog process!");
+ }
+ IsStarted = false;
+ _server.Dispose();
+ }
+ }
+
+ public void Dispose()
+ {
+ Stop();
}
}
}