/// <summary>
        /// Informs user that we are going to enter configuration phase and asks several questions regarding general configuration.
        /// </summary>
        /// <returns>true if the function succeeded, false otherwise.</returns>
        public static bool GeneralConfigurationPhase()
        {
            log.Trace("()");

            bool res = false;

            CUI.WriteRich("\nWe are going to configure all installed components now.\n");

            CUI.WriteRich("\nTrying to find out what is your external IP address... ");
            IPAddress externalIp    = GeneralConfiguration.FindExternalIpAddress();
            string    ipDefault     = "";
            string    externalIpStr = "";

            if (externalIp != null)
            {
                CUI.WriteOk();
                externalIpStr = externalIp.ToString();
                ipDefault     = string.Format("[{0}] ", externalIpStr);
            }
            else
            {
                CUI.WriteFailed();
            }

            bool done = false;

            while (!done)
            {
                CUI.WriteRich("\nEnter external IP address of this machine: {0}", ipDefault);
                string    answer = CUI.ReadStringAnswer(externalIpStr);
                IPAddress selectedIp;
                if (IPAddress.TryParse(answer, out selectedIp))
                {
                    GeneralConfiguration.ExternalIpAddress = selectedIp;
                    done = true;
                }
                else
                {
                    CUI.WriteRich("<red>ERROR:</red> <white>'{0}'</white> is not a valid IP address. Please try again.\n", answer);
                }
            }

            CUI.WriteLine();

            CUI.WriteRich("Trying to find out location of your IP address... ");
            GpsLocation location = GeneralConfiguration.FindIpLocation(GeneralConfiguration.ExternalIpAddress);

            string latitudeStr      = "";
            string longitudeStr     = "";
            string latitudeDefault  = "";
            string longitudeDefault = "";

            if (location != null)
            {
                CUI.WriteOk();
                CultureInfo enUs = new CultureInfo("en-US");
                latitudeStr      = location.Latitude.ToString("0.######", enUs);
                longitudeStr     = location.Longitude.ToString("0.######", enUs);
                latitudeDefault  = string.Format("[{0}] ", latitudeStr);
                longitudeDefault = string.Format("[{0}] ", longitudeStr);
            }
            else
            {
                CUI.WriteFailed();
            }

            decimal latitude  = 0;
            decimal longitude = 0;

            done = false;
            while (!done)
            {
                CUI.WriteRich("\nEnter latitude of GPS location of this machine: {0}", latitudeDefault);
                string answer = CUI.ReadStringAnswer(latitudeStr);

                if (decimal.TryParse(answer, NumberStyles.Any, CultureInfo.InvariantCulture, out latitude) && new GpsLocation(latitude, 0).IsValid())
                {
                    done = true;
                }
                else
                {
                    CUI.WriteRich("<red>ERROR:</red> <white>'{0}'</white> is not a valid GPS latitude. Please try again.\n", answer);
                }
            }

            done = false;
            while (!done)
            {
                CUI.WriteRich("Enter longitude of GPS location of this machine: {0}", longitudeDefault);
                string answer = CUI.ReadStringAnswer(longitudeStr);

                if (decimal.TryParse(answer, NumberStyles.Any, CultureInfo.InvariantCulture, out longitude) && new GpsLocation(latitude, longitude).IsValid())
                {
                    done = true;
                }
                else
                {
                    CUI.WriteRich("<red>ERROR:</red> <white>'{0}'</white> is not a valid GPS longitude. Please try again.\n", answer);
                }
            }

            GeneralConfiguration.Location = new GpsLocation(latitude, longitude);

            CUI.WriteLine();
            res = true;


            log.Trace("(-):{0}", res);
            return(res);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Tries to listen on a specific port and asks seed node to connect to it.
        /// </summary>
        /// <param name="Port">TCP port to check.</param>
        /// <returns>true if the port is open, false otherwise.</returns>
        public bool CheckPortOpen(int Port)
        {
            log.Trace("(Port:{0})", Port);

            bool res = false;

            if (Program.TestMode)
            {
                if (Port > 50000)
                {
                    CUI.WriteOk();
                    res = true;
                    log.Trace("(-)[TEST_MODE_ENABLED]:{0}", res);
                    return(res);
                }
            }

            try
            {
                portListenerThreadShutdownEvent.Reset();

                Thread portListenerThread = new Thread(new ParameterizedThreadStart(PortListenerThread));
                portListenerThread.Start(Port);

                if (portListenerReadyEvent.WaitOne(5000))
                {
                    res = GeneralConfiguration.CheckPort(GeneralConfiguration.ExternalIpAddress, Port);
                    if (res)
                    {
                        CUI.WriteOk();
                    }
                    else
                    {
                        CUI.WriteFailed();
                    }
                }
                else
                {
                    log.Error("Port listener thread did not get ready on time.");
                    CUI.WriteFailed();
                    CUI.WriteRich("<red>ERROR:</red> Unexpected error occurred, please try again.\n");
                }

                portListenerThreadShutdownEvent.Set();

                if (!portListenerThread.Join(10000))
                {
                    log.Error("Port listener thread failed to finish on time.");
                }
            }
            catch (Exception e)
            {
                log.Error("Exception occurred: {0}", e.ToString());
                CUI.WriteFailed();
                CUI.WriteRich("<red>ERROR:</red> Exception occurred: {0}\n", e.Message);
            }


            log.Trace("(-):{0}", res);
            return(res);
        }