Example #1
0
        public void SetTopHeight(uint height)
        {
            TopHeight = height;

            if (LastHeight.HasValue)
            {
                if (FullBlockMesured)
                {
                    LastBlockTimeDuration = (DateTime.UtcNow - LastHeight.Value);
                    AverageBlocktime.Add((int)LastBlockTimeDuration.Value.TotalSeconds);
                }
                else
                {
                    FullBlockMesured = true;
                }
            }

            LastHeight = DateTime.UtcNow;
            NextHeight = LastHeight.Value.AddSeconds((int)BlockTimeSeconds);

            if (LateHeightCount > 0)
            {
                LateHeightCount = 0;
                Program.AlarmManager.Clear(NetworkAlarm, $"CLEARED: Network Stall Alarm {Name}");
                NetworkAlarm = null;
            }
        }
Example #2
0
        async public void PingHostAsync()
        {
            try
            {
                var pingTask = Task.Run(() =>
                {
                    Uri myUri = new Uri(Host);

                    clsRollingAverage pingLatency    = new clsRollingAverage(10);
                    clsRollingAverage pingPacketLoss = new clsRollingAverage(10);

                    try
                    {
                        using (var pinger = new Ping())
                        {
                            for (var f = 0; f < 10; f++)
                            {
                                PingReply reply = pinger.Send(myUri.Host, 2000);

                                if (reply.Status == IPStatus.Success)
                                {
                                    pingPacketLoss.Add((int)reply.RoundtripTime);
                                    pingPacketLoss.Add(0);
                                    Program.SendAlert($"Ping {myUri.Host} {reply.RoundtripTime:0} ms  {pingPacketLoss.CurrentAverage:0.0}%");
                                }
                                else
                                {
                                    pingPacketLoss.Add(100);
                                    Program.SendAlert($"Ping {myUri.Host} {reply.Status} ms  {pingPacketLoss.CurrentAverage:0.0}%");
                                }
                            }
                        }
                    }
                    catch (PingException ex)
                    {
                        Program.SendAlert($"Ping error {myUri.Host} {ex.Message}");
                    }
                });
            }
            catch (Exception ex)
            {
                Program.SendAlert($"Ping error {Name} {ex.Message}");
            }
        }
Example #3
0
        public void GetHeight(int timeout = 2000)
        {
            try
            {
                RestClient client;
                if (Host.Contains(":"))
                {
                    client = new RestClient($"http://{Host}");
                }
                else
                {
                    client = new RestClient($"http://{Host}:8088");
                }

                client.Timeout = timeout;

                var request = new RestRequest("v2", Method.POST);
                request.AddHeader("Content-type", "application/json");
                request.AddHeader("header", "value");
                request.AddJsonBody(
                    new { jsonrpc = "2.0", id = 0, method = "heights" }
                    );

                // execute the request
                var           sw       = Stopwatch.StartNew();
                IRestResponse response = client.Execute(request);
                sw.Stop();

                if (response.ResponseStatus == ResponseStatus.Completed)
                {
                    var content = response.Content; // raw content as string

                    var pos1 = 0;
                    var pos2 = 0;

                    if (!string.IsNullOrEmpty(content))
                    {
                        pos1  = content.IndexOf("leaderheight\":");
                        pos1 += 14;
                        pos2  = content.IndexOf(",", pos1);

                        uint msgheight = 0;
                        if (UInt32.TryParse(content.Substring(pos1, pos2 - pos1), out msgheight))
                        {
                            LeaderHeight = msgheight;
                        }
                        else
                        {
                            ErrorMsg = "Invalid data";
                        }
                    }
                    else
                    {
                        ErrorMsg = "Empty data";
                    }
                    LatencyList.Add((int)sw.ElapsedMilliseconds);
                    PacketLoss.Add(0);
                    RequestFailCount = 0;
                }
                else if (response.ResponseStatus == ResponseStatus.Error || response.ResponseStatus == ResponseStatus.TimedOut)
                {
                    PacketLoss.Add(100);
                    ErrorMsg = response.ErrorMessage;

                    RequestFailCount++;
                }
                else if (response.ResponseStatus == ResponseStatus.None)
                {
                    ErrorMsg = "Empty data";
                }

                // Console.WriteLine(ToString());
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                RequestFailCount++;
            }
        }