aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/PPC/Tango.PPC.Common/WatchDog/WatchDogServer.cs
blob: 84ff68d08245ad8387015b76e7d729b7c938eac0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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 : 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()
        {
            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()
        {
            IsStarted = true;

            try
            {
                while (IsStarted)
                {
                    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 (Exception ex)
            {
                LogManager.Log(ex, "Error in watchdog server. Restarting watchdog...");
                IsStarted = false;

                try
                {
                    _server.Dispose();
                }
                catch { }

                Start();
                return;
            }
        }

        public void Stop()
        {
            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();
        }
    }
}