コード例 #1
0
        private async void AddServerButton_Click(object sender, RoutedEventArgs e)
        {
            var serverurl = NewServerTextBox.Text.ToLower().Trim();

            if (string.IsNullOrEmpty(serverurl))
            {
                return;
            }
            if (!serverurl.StartsWith("https://"))
            {
                var nd = new NoticeDialog("Server Address Error",
                                          "Error: Server must start with \"https://\".\r\nPlease correct the address and try again.");
                nd.ShowDialog();
                ;
                return;
            }
            var pingtime = AppHelpers.GetServerResponseTime(serverurl);

            if (pingtime <= 0 || pingtime > 120)
            {
                var nd = new NoticeDialog("Server Connection Error",
                                          "Error: Server connection failed or did not respond fast enough (" + pingtime +
                                          " ms).\r\nPlease try a different server.");
                nd.ShowDialog();
                return;
            }

            try
            {
                var api = new LiskAPI(serverurl);
                var ss  = await api.Loader_Status();

                if (!ss.loaded)
                {
                    var nd = new NoticeDialog("Server Sync Error",
                                              "Error: Server failed sync status test.\r\nPlease try a different server.");
                    nd.ShowDialog();
                    return;
                }
            }
            catch
            {
                var nd = new NoticeDialog("Server API Error",
                                          "Error: Server failed api test.\r\nPlease try a different server.");
                nd.ShowDialog();
                return;
            }

            Properties.Settings.Default.Servers.Add(serverurl);
            Properties.Settings.Default.Save();
            NewServerTextBox.Text = "";
            UpdateServersListView();
        }
コード例 #2
0
        internal static async Task SetupAPI()
        {
            WriteLine("Setting up API system");
            if (Settings.Default.Testnet)
            {
                Console.WriteLine("USING TESTNET");
            }
            // find the fastest server
            var fastserver     = "";
            var fastservertime = 0L;

            if (Settings.Default.AutoFindServer)
            {
                WriteLine("Finding best node");
                var servers = Settings.Default.Testnet ? Settings.Default.TestnetServers : Settings.Default.Servers;
                foreach (var s in servers)
                {
                    var rt = AppHelpers.GetServerResponseTime(s);
                    Console.WriteLine("tested server " + s + " response time " + rt + " ms");
                    if (rt < fastservertime || fastservertime <= 0)
                    {
                        try
                        {
                            Console.WriteLine("checking server " + s + " status");
                            // check that the server is synced
                            var _api = new LiskAPI(s);
                            var ls   = await _api.Loader_Status();

                            if (ls == null || !ls.success || !ls.loaded)
                            {
                                Console.WriteLine("server " + s + " is not synced, skipping");
                                continue;
                            }
                            var ss = await _api.Loader_SyncStatus();

                            if (ss == null || !ss.success || ss.syncing || ss.blocks != 0)
                            {
                                Console.WriteLine("server " + s + " is not synced, skipping");
                                continue;
                            }
                            fastservertime = rt;
                            fastserver     = s;
                        }
                        catch
                        {
                            Console.WriteLine("server " + s + " failed status query request, skipping");
                        }
                    }
                }

                if (fastservertime > 0 && !string.IsNullOrEmpty(fastserver))
                {
                    Console.WriteLine("Selected server " + fastserver + " response time " + fastservertime + " ms");
                    Globals.API = new LiskAPI(fastserver);
                    Globals.CurrentBlockHeight = (await Globals.API.Blocks_GetHeight()).height;
                    Console.WriteLine("API Server block height " + Globals.CurrentBlockHeight);
                    WriteLine("API system setup complete");
                }
                else
                {
                    WriteLine("API system setup failed, could not find a valid server.");
                    var mbr = MessageBox.Show("Error: Could not find a valid server node.\r\nWould you like retry?",
                                              "Server Error", MessageBoxButton.YesNo, MessageBoxImage.Question);
                    if (mbr == MessageBoxResult.Yes)
                    {
                        await SetupAPI();
                    }
                    else
                    {
                        ForceExit();
                    }
                }
            }
            else
            {
                fastservertime = AppHelpers.GetServerResponseTime(Settings.Default.Servers[0]);
                WriteLine("Selected first server " + Settings.Default.Servers[0] + " response time " +
                          fastservertime + " ms");
                if (fastservertime <= 0)
                {
                    WriteLine("API server did not respond to ping request, status unknown, attempting test call");
                }
                Globals.API = new LiskAPI(Settings.Default.Servers[0]);
                try
                {
                    var res = await Globals.API.Loader_Status();

                    if (res == null || !res.loaded)
                    {
                        WriteLine("API server failed to respond, fatal exiting");
                        Console.WriteLine("Press any key to quit");
                        Console.ReadLine();
                        ForceExit();
                    }
                }
                catch (Exception crap)
                {
                    WriteLine("Error: " + crap.Message);
                    Console.WriteLine("Press any key to quit");
                    Console.ReadLine();
                    ForceExit();
                }
                Globals.CurrentBlockHeight = (await Globals.API.Blocks_GetHeight()).height;
                Console.WriteLine("API Server block height " + Globals.CurrentBlockHeight);
                WriteLine("API system setup complete");
            }
        }