/// <summary> /// Reads the configuration file and merges it with the command line arguments. /// </summary> private void ReadConfigurationFile() { Logger.LogDebug("Reading configuration file '{0}'.", ConfigurationFile); // Add the file configuration to the command-line configuration. TextFileConfiguration fileConfig = new TextFileConfiguration(File.ReadAllText(ConfigurationFile)); fileConfig.MergeInto(ConfigReader); }
/// <summary> /// Merges current instance of the configuration to the target instance. /// </summary> /// <param name="destination">Target instance to merge current instance into.</param> public void MergeInto(TextFileConfiguration destination) { foreach (KeyValuePair <string, List <string> > kv in args) { foreach (string v in kv.Value) { destination.Add(kv.Key, v); } } }
/// <summary> /// Initializes a new instance of the object. /// </summary> /// <param name="serverNode">The servernode the server runs on</param> /// <param name="protocolVersion">Supported protocol version for which to create the configuration.</param> /// <param name="agent">The servers user agent that will be shared with peers.</param> /// <param name="args">The command-line arguments.</param> /// <exception cref="ConfigurationException"> /// Thrown in case of any problems with the configuration file or command line /// arguments. /// </exception> /// <remarks> /// Processing depends on whether a configuration file is passed via the command line. /// There are two main scenarios here: /// - The configuration file is passed via the command line. In this case we need /// to read it earlier so that it can provide defaults for "testnet" and "regtest". /// - Alternatively, if the file name is not supplied then a servernode-specific file /// name would be determined. In this case we first need to determine the servernode. /// </remarks> public ServerSettings(ServerNodeBase serverNode, ProtocolVersion protocolVersion = ProtocolVersion.PROTOCOL_VERSION, string agent = "x42", string[] args = null) { // Create the default logger factory and logger. LoggerFactory = ExtendedLoggerFactory.Create(Log); Logger = LoggerFactory.CreateLogger(typeof(ServerSettings).FullName); ProtocolVersion = protocolVersion; Agent = agent; ConfigReader = new TextFileConfiguration(args ?? new string[] { }); // Log arguments. Logger.LogDebug("Arguments: servernode='{0}', protocolVersion='{1}', agent='{2}', args='{3}'.", string.IsNullOrEmpty(ServerName) ? "(None)" : ServerName, ProtocolVersion, Agent, args == null ? "(None)" : string.Join(" ", args)); // By default, we look for a file named '<servernode>.conf' in the servernode's data directory, // but both the data directory and the configuration file path may be changed using the -datadir and -conf command-line arguments. ConfigurationFile = ConfigReader.GetOrDefault <string>("conf", null, Logger)?.NormalizeDirectorySeparator(); DataDir = ConfigReader.GetOrDefault <string>("datadir", null, Logger)?.NormalizeDirectorySeparator(); DataDirRoot = ConfigReader.GetOrDefault("datadirroot", "xServerData", Logger); // If the configuration file is relative then assume it is relative to the data folder and combine the paths. if (DataDir != null && ConfigurationFile != null) { bool isRelativePath = Path.GetFullPath(ConfigurationFile).Length > ConfigurationFile.Length; if (isRelativePath) { ConfigurationFile = Path.Combine(DataDir, ConfigurationFile); } } // If the configuration file has been specified on the command line then read it now // so that it can provide the defaults for testnet and regtest. if (ConfigurationFile != null) { // If the configuration file was specified on the command line then it must exist. if (!File.Exists(ConfigurationFile)) { throw new ConfigurationException($"Configuration file does not exist at {ConfigurationFile}."); } // Sets the ConfigReader based on the arguments and the configuration file if it exists. ReadConfigurationFile(); } // Find out if we need to run on testnet from the config file. bool testNet = ConfigReader.GetOrDefault("testnet", false, Logger); if (testNet) { ServerNode = new X42TestServerNode(); } else { ServerNode = serverNode; } // Set the full data directory path. if (DataDir == null) { // Create the data directories if they don't exist. DataDir = CreateDefaultDataDirectories(Path.Combine($"{ServerName}Data", testNet ? "Test" : "Main")); } else { // Combine the data directory with the servernode's root folder and name. string directoryPath = Path.Combine(DataDir, ServerName); DataDir = Directory.CreateDirectory(directoryPath).FullName; Logger.LogDebug("Data directory initialized with path {0}.", DataDir); } // Set the data folder. DataFolder = new DataFolder(DataDir); // Get the configuration file name for the servernode if it was not specified on the command line. if (ConfigurationFile == null) { ConfigurationFile = Path.Combine(DataDir, ServerNode.DefaultConfigFilename); Logger.LogDebug("Configuration file set to '{0}'.", ConfigurationFile); if (File.Exists(ConfigurationFile)) { ReadConfigurationFile(); } } // Create the custom logger factory. Log = new LogSettings(); Log.Load(ConfigReader); LoggerFactory.AddNLog(); LoggerFactory.LoadNLogConfiguration(DataFolder); LoggerFactory.AddFilters(Log, DataFolder); // Load the configuration. LoadConfiguration(); }
/// <summary> /// Loads the server settings from the application configuration. /// </summary> private void LoadConfiguration() { TextFileConfiguration config = ConfigReader; }