/// <summary> /// Loads the moose option from the settings XML into an represenative object /// </summary> private MinerOption LoadMinerOption(XElement optionElement, out string name) { MinerOption _option = new MinerOption() { Path = optionElement.Attribute("path").Value, ArgumentFormat = optionElement.Attribute("argumentformat").Value }; if (optionElement.Attribute("arguments") != null) { _option.Arguments = optionElement.Attribute("arguments").Value; } name = optionElement.Attribute("name").Value; return(_option); }
/// <summary> /// Loads the settings from the XML file. /// </summary> public void Load(string p_config_file_name) { string _localDirectory = Path.GetDirectoryName(p_config_file_name); if (Directory.Exists(_localDirectory)) { if (File.Exists(p_config_file_name)) { Dictionary <string, MinerOption> _options = new Dictionary <string, MinerOption>(); Dictionary <string, MinerSetting> _miners = new Dictionary <string, MinerSetting>(); XDocument _xdoc = null; try { _xdoc = XDocument.Load(p_config_file_name); } catch (Exception ex) { throw new ApplicationException("Failed to load settings from XML file. " + ex.Message, ex); } //load the defaults bool debugLog = false; if (_xdoc.Root.Element("DebugLogging") != null) { debugLog = Convert.ToBoolean(_xdoc.Root.Element("DebugLogging").Value); } string defUserName = (from n in _xdoc.Root.Descendants("DefaultUserName") select n.Value).FirstOrDefault(); string defPassword = (from n in _xdoc.Root.Descendants("DefaultPassword") select n.Value).FirstOrDefault(); string defMinerOpt = (from n in _xdoc.Root.Descendants("DefaultMinerOption") select n.Value).FirstOrDefault(); string defPool = (from n in _xdoc.Root.Descendants("DefaultPool") select n.Value).FirstOrDefault(); //load the moose options foreach (var element in _xdoc.Root.Element("MinerOptions").Descendants("MinerOption")) { string name = null; MinerOption opt = LoadMinerOption(element, out name); if (_options.ContainsKey(name)) { throw new ApplicationException(String.Format("Duplicate moose option name '{0}' detected. moose names must be unique.", name)); } _options.Add(name, opt); } //load the miners int minerIndex = 1; foreach (var element in _xdoc.Root.Element("Miners").Descendants("Miner")) { string name = null; MinerSetting m = LoadMiner(element, out name); name = name ?? String.Format("Miner #{0}", minerIndex); m.UserName = m.UserName ?? defUserName; m.Password = m.Password ?? defPassword; if (_miners.ContainsKey(name)) { throw new ApplicationException(String.Format("Duplicate moose name '{0}' detected. moose names must be unique.", name)); } _miners.Add(name, m); minerIndex++; } //load the pools List <MiningPool> pools = new List <MiningPool>(); foreach (var element in _xdoc.Root.Element("Pools").Descendants("Pool")) { MiningPool pool = new MiningPool() { Name = element.Attribute("name").Value, Host = element.Attribute("host").Value, Port = Convert.ToInt16(element.Attribute("port").Value) }; if (element.Attribute("website") != null) { pool.Website = element.Attribute("website").Value; } pools.Add(pool); } //validate if (String.IsNullOrEmpty(defMinerOpt)) { throw new ApplicationException("The default moose option is not specified in the settings file. It must be named as the value for the 'DefaultMinerOption' element."); } foreach (var option in _options) { if (File.Exists(option.Value.Path) == false) { if (File.Exists(Path.Combine(_localDirectory, option.Value.Path)) == false) { throw new ApplicationException(String.Format("Invalid path for moose option '{0}'. The file was not found or is inaccessible.", option.Key)); } else { option.Value.Path = Path.Combine(_localDirectory, option.Value.Path); //update path to use combined. } } if (String.IsNullOrEmpty(option.Key)) { throw new ApplicationException("Invalid name on moose option. All moose options must have a name attribute specified with a unique value."); } } foreach (var _miner in _miners) { if (_options.ContainsKey(_miner.Value.OptionKey) == false) { throw new ApplicationException(String.Format("Invalid option value '{1}' on moose '{0}'. The option value must be a name from the available moose options.", _miner.Key, _miner.Value.OptionKey)); } if (String.IsNullOrEmpty(_miner.Key)) { throw new ApplicationException("Invalid name on moose. All miners must have a name attribute specified with a unique value."); } } if (String.IsNullOrEmpty(defPool) == false && pools.Any(a => a.Name == defPool) == false) { throw new ApplicationException("The specified default pool does not exist."); } //set values this.BitMoosePath = _localDirectory; this.DebugLogging = debugLog; this.DefaultUserName = (defUserName ?? DefaultUserNameValue).Trim(); this.DefaultPassword = (defPassword ?? DefaultPasswordValue).Trim(); this.DefaultMinerOption = defMinerOpt; this.DefaultPool = defPool; this.MinerOptions = _options; this.Miners = _miners; this.Pools = pools.ToArray(); } else { throw new FileNotFoundException(String.Format("The settings file '{0}' was not found or is inaccessible.", p_config_file_name), p_config_file_name); } } else { throw new DirectoryNotFoundException(String.Format("The directory path '{0}' was not found or is inaccessible.", _localDirectory)); } }
/// <summary> /// Loads the settings about cpu-miner quark. /// </summary> public void LoadQuark(string p_localDirectory, bool p_debugLog, int p_priority, string p_defUserName, string p_defPassword, string p_defMinerOpt, string p_defPool, string p_defHost, string p_defArgs, int p_defPort) { //load the moose options Dictionary <string, MinerOption> _options = new Dictionary <string, MinerOption>(); { MinerOption _option = new MinerOption() { ArgumentFormat = " -a quark -q --url {host}:{port} -u {username} -p {password} {args}", Arguments = p_defArgs, Path = @"packages\qrk\quark.exe" }; _options.Add("quark", _option); } //load the miners Dictionary <string, MinerSetting> _miners = new Dictionary <string, MinerSetting>(); { MinerSetting _miner = new MinerSetting() { Pool = p_defPool, OptionKey = p_defMinerOpt, Arguments = p_defArgs, Host = p_defHost, Port = (short)p_defPort, UserName = p_defUserName, Password = p_defPassword, Enabled = true, CrashRecovery = new MinerCrashRecoverySetting() { AlternateMiner = "", Interval = 60, Retries = 3 }, Logging = new MinerLogSetting() { Enabled = false, MaxFileSize = 1048576 }, Process = new MinerProcessSetting() { Priority = (short)p_priority, CPUAffinity = "", LoadUserProfile = false, WinDomain = "", WinUserName = "", WinPassword = "" }, StatusUpdates = false }; _miners.Add("Default", _miner); } //load the pools List <MiningPool> _pools = new List <MiningPool>(); { MiningPool _pool = new MiningPool() { Name = "coinmine", Host = "qrk.coinmine.pl", Port = 6010, Website = "http://www2.coinmine.pl/qrk/" }; _pools.Add(_pool); } //set values this.BitMoosePath = p_localDirectory; this.DebugLogging = p_debugLog; this.DefaultUserName = (p_defUserName ?? DefaultUserNameValue).Trim(); this.DefaultPassword = (p_defPassword ?? DefaultPasswordValue).Trim(); this.DefaultMinerOption = p_defMinerOpt; this.DefaultPool = p_defPool; this.MinerOptions = _options; this.Miners = _miners; this.Pools = _pools.ToArray(); }