ServicesServerBase.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.IO;
  30. using System.Reflection;
  31. using System.Threading;
  32. using System.Text;
  33. using System.Xml;
  34. using OpenSim.Framework;
  35. using OpenSim.Framework.Console;
  36. using OpenSim.Framework.Monitoring;
  37. using OpenSim.Framework.Servers;
  38. using log4net;
  39. using log4net.Config;
  40. using log4net.Appender;
  41. using log4net.Core;
  42. using log4net.Repository;
  43. using Nini.Config;
  44. namespace OpenSim.Server.Base
  45. {
  46. public class ServicesServerBase : ServerBase
  47. {
  48. // Logger
  49. //
  50. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  51. // Command line args
  52. //
  53. protected string[] m_Arguments;
  54. public string ConfigDirectory
  55. {
  56. get;
  57. private set;
  58. }
  59. // Run flag
  60. //
  61. private bool m_Running = true;
  62. // Handle all the automagical stuff
  63. //
  64. public ServicesServerBase(string prompt, string[] args) : base()
  65. {
  66. // Save raw arguments
  67. m_Arguments = args;
  68. // Read command line
  69. ArgvConfigSource argvConfig = new ArgvConfigSource(args);
  70. argvConfig.AddSwitch("Startup", "console", "c");
  71. argvConfig.AddSwitch("Startup", "logfile", "l");
  72. argvConfig.AddSwitch("Startup", "inifile", "i");
  73. argvConfig.AddSwitch("Startup", "prompt", "p");
  74. argvConfig.AddSwitch("Startup", "logconfig", "g");
  75. // Automagically create the ini file name
  76. string fileName = Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().Location);
  77. string iniFile = fileName + ".ini";
  78. string logConfig = null;
  79. IConfig startupConfig = argvConfig.Configs["Startup"];
  80. if (startupConfig != null)
  81. {
  82. // Check if a file name was given on the command line
  83. iniFile = startupConfig.GetString("inifile", iniFile);
  84. // Check if a prompt was given on the command line
  85. prompt = startupConfig.GetString("prompt", prompt);
  86. // Check for a Log4Net config file on the command line
  87. logConfig =startupConfig.GetString("logconfig", logConfig);
  88. }
  89. // Find out of the file name is a URI and remote load it if possible.
  90. // Load it as a local file otherwise.
  91. Uri configUri;
  92. try
  93. {
  94. if (Uri.TryCreate(iniFile, UriKind.Absolute, out configUri) &&
  95. configUri.Scheme == Uri.UriSchemeHttp)
  96. {
  97. XmlReader r = XmlReader.Create(iniFile);
  98. Config = new XmlConfigSource(r);
  99. }
  100. else
  101. {
  102. Config = new IniConfigSource(iniFile);
  103. }
  104. }
  105. catch (Exception e)
  106. {
  107. System.Console.WriteLine("Error reading from config source. {0}", e.Message);
  108. Environment.Exit(1);
  109. }
  110. // Merge OpSys env vars
  111. m_log.Info("[CONFIG]: Loading environment variables for Config");
  112. Util.MergeEnvironmentToConfig(Config);
  113. // Merge the configuration from the command line into the loaded file
  114. Config.Merge(argvConfig);
  115. // Refresh the startupConfig post merge
  116. if (Config.Configs["Startup"] != null)
  117. {
  118. startupConfig = Config.Configs["Startup"];
  119. }
  120. ConfigDirectory = startupConfig.GetString("ConfigDirectory", ".");
  121. prompt = startupConfig.GetString("Prompt", prompt);
  122. // Allow derived classes to load config before the console is opened.
  123. ReadConfig();
  124. // Create main console
  125. string consoleType = "local";
  126. if (startupConfig != null)
  127. consoleType = startupConfig.GetString("console", consoleType);
  128. if (consoleType == "basic")
  129. {
  130. MainConsole.Instance = new CommandConsole(prompt);
  131. }
  132. else if (consoleType == "rest")
  133. {
  134. MainConsole.Instance = new RemoteConsole(prompt);
  135. ((RemoteConsole)MainConsole.Instance).ReadConfig(Config);
  136. }
  137. else
  138. {
  139. MainConsole.Instance = new LocalConsole(prompt);
  140. }
  141. m_console = MainConsole.Instance;
  142. if (logConfig != null)
  143. {
  144. FileInfo cfg = new FileInfo(logConfig);
  145. XmlConfigurator.Configure(cfg);
  146. }
  147. else
  148. {
  149. XmlConfigurator.Configure();
  150. }
  151. LogEnvironmentInformation();
  152. RegisterCommonAppenders(startupConfig);
  153. if (startupConfig.GetString("PIDFile", String.Empty) != String.Empty)
  154. {
  155. CreatePIDFile(startupConfig.GetString("PIDFile"));
  156. }
  157. RegisterCommonCommands();
  158. RegisterCommonComponents(Config);
  159. // Allow derived classes to perform initialization that
  160. // needs to be done after the console has opened
  161. Initialise();
  162. }
  163. public bool Running
  164. {
  165. get { return m_Running; }
  166. }
  167. public virtual int Run()
  168. {
  169. Watchdog.Enabled = true;
  170. MemoryWatchdog.Enabled = true;
  171. while (m_Running)
  172. {
  173. try
  174. {
  175. MainConsole.Instance.Prompt();
  176. }
  177. catch (Exception e)
  178. {
  179. m_log.ErrorFormat("Command error: {0}", e);
  180. }
  181. }
  182. RemovePIDFile();
  183. return 0;
  184. }
  185. protected override void ShutdownSpecific()
  186. {
  187. m_Running = false;
  188. m_log.Info("[CONSOLE] Quitting");
  189. base.ShutdownSpecific();
  190. }
  191. protected virtual void ReadConfig()
  192. {
  193. }
  194. protected virtual void Initialise()
  195. {
  196. }
  197. }
  198. }