1
0

ServicesServerBase.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 =
  51. LogManager.GetLogger(
  52. MethodBase.GetCurrentMethod().DeclaringType);
  53. // Command line args
  54. //
  55. protected string[] m_Arguments;
  56. public string ConfigDirectory
  57. {
  58. get;
  59. private set;
  60. }
  61. // Run flag
  62. //
  63. private bool m_Running = true;
  64. // Handle all the automagical stuff
  65. //
  66. public ServicesServerBase(string prompt, string[] args) : base()
  67. {
  68. // Save raw arguments
  69. //
  70. m_Arguments = args;
  71. // Read command line
  72. //
  73. ArgvConfigSource argvConfig = new ArgvConfigSource(args);
  74. argvConfig.AddSwitch("Startup", "console", "c");
  75. argvConfig.AddSwitch("Startup", "logfile", "l");
  76. argvConfig.AddSwitch("Startup", "inifile", "i");
  77. argvConfig.AddSwitch("Startup", "prompt", "p");
  78. argvConfig.AddSwitch("Startup", "logconfig", "g");
  79. // Automagically create the ini file name
  80. //
  81. string fileName = Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().Location);
  82. string iniFile = fileName + ".ini";
  83. string logConfig = null;
  84. IConfig startupConfig = argvConfig.Configs["Startup"];
  85. if (startupConfig != null)
  86. {
  87. // Check if a file name was given on the command line
  88. //
  89. iniFile = startupConfig.GetString("inifile", iniFile);
  90. //
  91. // Check if a prompt was given on the command line
  92. prompt = startupConfig.GetString("prompt", prompt);
  93. //
  94. // Check for a Log4Net config file on the command line
  95. logConfig =startupConfig.GetString("logconfig",logConfig);
  96. }
  97. // Find out of the file name is a URI and remote load it
  98. // if it's possible. Load it as a local file otherwise.
  99. //
  100. Uri configUri;
  101. try
  102. {
  103. if (Uri.TryCreate(iniFile, UriKind.Absolute, out configUri) &&
  104. configUri.Scheme == Uri.UriSchemeHttp)
  105. {
  106. XmlReader r = XmlReader.Create(iniFile);
  107. Config = new XmlConfigSource(r);
  108. }
  109. else
  110. {
  111. Config = new IniConfigSource(iniFile);
  112. }
  113. }
  114. catch (Exception e)
  115. {
  116. System.Console.WriteLine("Error reading from config source. {0}", e.Message);
  117. Environment.Exit(1);
  118. }
  119. // Merge the configuration from the command line into the
  120. // loaded file
  121. //
  122. Config.Merge(argvConfig);
  123. // Refresh the startupConfig post merge
  124. //
  125. if (Config.Configs["Startup"] != null)
  126. {
  127. startupConfig = Config.Configs["Startup"];
  128. }
  129. ConfigDirectory = startupConfig.GetString("ConfigDirectory", ".");
  130. prompt = startupConfig.GetString("Prompt", prompt);
  131. // Allow derived classes to load config before the console is
  132. // opened.
  133. //
  134. ReadConfig();
  135. // Create main console
  136. //
  137. string consoleType = "local";
  138. if (startupConfig != null)
  139. consoleType = startupConfig.GetString("console", consoleType);
  140. if (consoleType == "basic")
  141. {
  142. MainConsole.Instance = new CommandConsole(prompt);
  143. }
  144. else if (consoleType == "rest")
  145. {
  146. MainConsole.Instance = new RemoteConsole(prompt);
  147. ((RemoteConsole)MainConsole.Instance).ReadConfig(Config);
  148. }
  149. else
  150. {
  151. MainConsole.Instance = new LocalConsole(prompt);
  152. }
  153. m_console = MainConsole.Instance;
  154. if (logConfig != null)
  155. {
  156. FileInfo cfg = new FileInfo(logConfig);
  157. XmlConfigurator.Configure(cfg);
  158. }
  159. else
  160. {
  161. XmlConfigurator.Configure();
  162. }
  163. LogEnvironmentInformation();
  164. RegisterCommonAppenders(startupConfig);
  165. if (startupConfig.GetString("PIDFile", String.Empty) != String.Empty)
  166. {
  167. CreatePIDFile(startupConfig.GetString("PIDFile"));
  168. }
  169. RegisterCommonCommands();
  170. RegisterCommonComponents(Config);
  171. // Allow derived classes to perform initialization that
  172. // needs to be done after the console has opened
  173. //
  174. Initialise();
  175. }
  176. public bool Running
  177. {
  178. get { return m_Running; }
  179. }
  180. public virtual int Run()
  181. {
  182. Watchdog.Enabled = true;
  183. MemoryWatchdog.Enabled = true;
  184. while (m_Running)
  185. {
  186. try
  187. {
  188. MainConsole.Instance.Prompt();
  189. }
  190. catch (Exception e)
  191. {
  192. m_log.ErrorFormat("Command error: {0}", e);
  193. }
  194. }
  195. RemovePIDFile();
  196. return 0;
  197. }
  198. protected override void ShutdownSpecific()
  199. {
  200. m_Running = false;
  201. m_log.Info("[CONSOLE] Quitting");
  202. base.ShutdownSpecific();
  203. }
  204. protected virtual void ReadConfig()
  205. {
  206. }
  207. protected virtual void Initialise()
  208. {
  209. }
  210. }
  211. }