aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/PPC/Modules/Tango.PPC.Events/App.xaml
blob: bd3cf16ee38b63b8fe359d1e2154ffc133fb25d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
<Application x:Class="Tango.PPC.Events.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/Tango.PPC.Common;component/Resources/Merged.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>
} /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
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();
        }
    }
}