コード例 #1
0
        /// <summary>
        /// Pings the host.
        /// </summary>
        /// <param name="nameOrAddress">The name or address to ping.</param>
        /// <param name="elapsed">The time in Timer.Ticks it took to ping the node.</param>
        /// <returns>true if node is pingable; Otherwise false. out value returns the Timer.Ticks taken to ping the host; Otherwise 10000000000000</returns>
        /// <example>
        /// This sample shows how to use the <see cref="PingHost"/> method.
        /// <code>
        /// class TestClass
        /// {
        ///     static void Main()
        ///     {
        ///         Connection connection = new Connection();
        ///
        ///         long ellapsed = 0;
        ///
        ///         connection.PingHost(connection.GetHost(), ellapsed);
        ///     }
        /// }
        /// </code>
        /// </example>


        public bool PingHost(string nameOrAddress, out long elapsed)
        {
            var pinger = new Ping();

            try
            {
                var sw = new Stopwatch();

                sw.Start();

                var reply = pinger.Send(nameOrAddress, RequestTimeout);

                sw.Stop();

                var pingable = reply?.Status == IPStatus.Success;

                if (pingable)
                {
                    var con = new Connection();

                    con.SetHost(nameOrAddress);

                    con.AutoHost = false;

                    var client = new NisClient(con);

                    client.BeginGetStatus(ar =>
                    {
                        if (ar?.Content?.Code != 6)
                        {
                            pingable = false;
                        }
                    });
                }

                elapsed = pingable ? sw.Elapsed.Ticks : 10000000000000;

                return(pingable);
            }
            catch (PingException pe)
            {
                elapsed = 10000000000000;

                return(false);
            }
        }
コード例 #2
0
        internal async void TestNodes()
        {
            while (true)
            {
                bot = new TelegramBot(accessToken: ConfigurationManager.AppSettings[name: "accessKey"]);

                var nodes = NodeUtils.GetAllNodes();

                foreach (var n in nodes)
                {
                    Thread.Sleep(500);

                    try
                    {
                        var con = new Connection();

                        con.SetHost(n.IP);

                        con.AutoHost = false;

                        var client = new NodeClient(con);

                        var info = client.EndGetExtendedNodeInfo(client.BeginGetExtendedNodeInfo());

                        if (n.WentOffLine != null)
                        {
                            var nis = new NisClient(con);

                            if (nis.EndGetStatus(nis.BeginGetStatus()).Code != 6)
                            {
                                continue;
                            }

                            await Nofity(
                                node : n,
                                msg : "Node: " + n.Alias + "\n" + " With IP: " + n.IP +
                                "\nis back online.");

                            n.WentOffLine = null;

                            NodeUtils.UpdateNode(snode: n, chatId: n.OwnedByUser);
                        }
                        if (info.Node.endpoint.Host == n.IP)
                        {
                            ScanTests(n: n);
                        }
                    }
                    catch (Exception e)
                    {
                        if (e.Message.Contains("blocked"))
                        {
                            AccountUtils.DeleteAccountsByUser(n.OwnedByUser);

                            NodeUtils.DeleteUserNodes(n.OwnedByUser);

                            UserUtils.DeleteUser(n.OwnedByUser);

                            break;
                        }

                        if (n.WentOffLine == null)
                        {
                            try
                            {
                                await Nofity(node : n,
                                             msg : "Node: " + n.Alias + "\n" + "With IP: " + n.IP +
                                             " \nis offline or otherwise unreachable. It will be removed from your list of registered nodes in 48 hours if it is not reachable in that time.");
                            }
                            catch (Exception ex)
                            {
                                await Nofity(node : n,
                                             msg : "Node: " + n.Alias + "\n" + "With IP: " + n.IP +
                                             " \nis offline or otherwise unreachable. It will be removed from your list of registered nodes in 48 hours if it is not reachable in that time.");
                            }

                            n.WentOffLine = DateTime.Now;

                            NodeUtils.UpdateNode(snode: n, chatId: n.OwnedByUser);
                        }
                        else if (n.WentOffLine < DateTime.Now.AddDays(value: -2))
                        {
                            await Nofity(node : n,
                                         msg : "Node: " + n.IP +
                                         " has been offline or otherwise unreachable for 48 hours. It will be removed from your list of registered nodes.");

                            NodeUtils.DeleteNode(
                                chatId: (long)n.OwnedByUser,
                                nodes: new List <string> {
                                n.IP
                            });

                            AccountUtils.DeleteAccount(
                                chatId: (long)n.OwnedByUser,
                                accounts: new List <string> {
                                AccountUtils.GetAccount(add: n.DepositAddress, user: (long)n.OwnedByUser).EncodedAddress
                            }
                                );
                        }
                    }
                }
            }
        }