Esempio n. 1
0
        /// <summary>
        /// Sets the value of a configuration.
        /// </summary>
        /// <param name="name">The configuration value to set.</param>
        /// <param name="value">The value to assign to the configuration.</param>
        private void SetValue(ConfigurationNames name, object value)
        {
            ConfigurationAssocAttribute attribute = ReflectionHelper.GetAttribute <ConfigurationNames, ConfigurationAssocAttribute>(name);

            if (attribute == null)
            {
                return;
            }

            ConfigurationValidation validation = attribute.Validation;

            if (validation != ConfigurationValidation.None)
            {
                if (validation.HasFlag(ConfigurationValidation.NonNull) && value == null)
                {
                    throw new ArgumentNullException("value");
                }

                if (validation.HasFlag(ConfigurationValidation.NonZero) ||
                    validation.HasFlag(ConfigurationValidation.NonNegative) ||
                    validation.HasFlag(ConfigurationValidation.PortRange))
                {
                    int converted = Convert.ToInt32(value);

                    if (validation.HasFlag(ConfigurationValidation.NonZero) && converted == 0)
                    {
                        throw new ArgumentOutOfRangeException("value", "The value of this configuration cannot be zero");
                    }
                    if (validation.HasFlag(ConfigurationValidation.NonNegative) && converted < 0)
                    {
                        throw new ArgumentOutOfRangeException("value", "The value of this configuration cannot be below zero");
                    }
                    if (validation.HasFlag(ConfigurationValidation.PortRange) && (converted <= 0 || short.MaxValue < converted))
                    {
                        throw new ArgumentOutOfRangeException("value", "The value of this configuration must be within a valid port range");
                    }
                }

                if (validation.HasFlag(ConfigurationValidation.SizeDivision))
                {
                    double converted = Convert.ToDouble(value);

                    if (converted % 1024 != 0)
                    {
                        throw new ArgumentException("The value of this configuration must be divisible by 1024");
                    }
                }
            }

            values[name] = value;

            if (PropertyChanged != null)
            {
                PropertyChanged(client, new PropertyChangedEventArgs(attribute.Name));
            }

            SetConfCommand command  = new SetConfCommand(attribute.Name, ReflectionHelper.Convert(value, typeof(string)) as string);
            Response       response = command.Dispatch(client);
        }
Esempio n. 2
0
        /// <summary>
        /// Sets the value of a configuration supplied with the process arguments. Setting a configuration value within the create
        /// parameters overrides any values stored in the configuration file. The control port can also be set using this method.
        /// </summary>
        /// <param name="configuration">The configuration which should be overridden.</param>
        /// <param name="value">The value of the configuration. This should align with the type specified in the <see cref="Tor.Config.Configuration"/> class.</param>
        public void SetConfig(ConfigurationNames configuration, object value)
        {
            ConfigurationAssocAttribute attribute = ReflectionHelper.GetAttribute <ConfigurationNames, ConfigurationAssocAttribute>(configuration);

            if (attribute == null)
            {
                return;
            }

            if (value != null && !value.GetType().Equals(attribute.Type))
            {
                throw new ArgumentException("The value of this configuration should be of type " + attribute.Type, "value");
            }

            ConfigurationValidation validation = attribute.Validation;

            if (validation != ConfigurationValidation.None)
            {
                if (validation.HasFlag(ConfigurationValidation.NonNull) && value == null)
                {
                    throw new ArgumentNullException("value");
                }

                if (validation.HasFlag(ConfigurationValidation.NonZero) ||
                    validation.HasFlag(ConfigurationValidation.NonNegative) ||
                    validation.HasFlag(ConfigurationValidation.PortRange))
                {
                    int converted = Convert.ToInt32(value);

                    if (validation.HasFlag(ConfigurationValidation.NonZero) && converted == 0)
                    {
                        throw new ArgumentOutOfRangeException("value", "The value of this configuration cannot be zero");
                    }
                    if (validation.HasFlag(ConfigurationValidation.NonNegative) && converted < 0)
                    {
                        throw new ArgumentOutOfRangeException("value", "The value of this configuration cannot be below zero");
                    }
                    if (validation.HasFlag(ConfigurationValidation.PortRange) && (converted <= 0 || short.MaxValue < converted))
                    {
                        throw new ArgumentOutOfRangeException("value", "The value of this configuration must be within a valid port range");
                    }
                }

                if (validation.HasFlag(ConfigurationValidation.SizeDivision))
                {
                    double converted = Convert.ToDouble(value);

                    if (converted % 1024 != 0)
                    {
                        throw new ArgumentException("The value of this configuration must be divisible by 1024");
                    }
                }
            }

            if (configuration == ConfigurationNames.ControlPort)
            {
                controlPort = Convert.ToInt32(value);
            }
            else
            {
                overrides[configuration] = value;
            }
        }