aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/PPC/Tango.PPC.Common/WatchDog/WatchDogClient.cs
blob: bbdfbf7e2e5b2d97e320764040c3a92c5efd1065 (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
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 : 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();
        }
    }
}