Ejemplo n.º 1
0
        /// <summary>
        /// Asks the user which listener to add.
        /// </summary>
        protected void ShowAddListener()
        {
            Console.WriteLine(
                "Please enter the full class name of the Listener object you're trying to add:\r\n (ie. Org.Mentalis.Proxy.Http.HttpListener) or short name from table of Available Listeners:");
            ShowAvailableListeners();
            var classtype = Console.ReadLine();

            if (string.IsNullOrWhiteSpace(classtype))
            {
                return;
            }
            if (availableManager.ContainsKey(classtype))
            {
                classtype = availableManager.GetFullName(classtype);
            }
            var klass = Type.GetType(classtype);

            if (klass == null)
            {
                Console.WriteLine("The specified class does not exist!");
                return;
            }

            if (!klass.IsSubclassOf(typeof(Listener)))
            {
                Console.WriteLine("The specified object is not a valid Listener object.");
                return;
            }
            Console.WriteLine("Please enter the construction parameters:");
            ShowAvailableConstructors(classtype);
            string construct = Console.ReadLine();

            try
            {
                var pars = Tools.ParseStringToParams(construct);
                _proxy.AddListener(klass, pars);
            }
            catch (Exception ex)
            {
                if (ex is FormatException || ex is ArgumentException)
                {
                    Console.WriteLine("Invalid construction string.");
                    return;
                }
                if (ex is SocketException)
                {
                    Console.WriteLine(
                        "Error while staring the Listener.\r\n(Perhaps the specified port is already in use?)");
                    return;
                }
            }
            return;
        }