BaseOpenSimServer.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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.Text;
  32. using System.Text.RegularExpressions;
  33. using System.Threading;
  34. using System.Timers;
  35. using log4net;
  36. using log4net.Appender;
  37. using log4net.Core;
  38. using log4net.Repository;
  39. using OpenMetaverse;
  40. using OpenMetaverse.StructuredData;
  41. using OpenSim.Framework;
  42. using OpenSim.Framework.Console;
  43. using OpenSim.Framework.Monitoring;
  44. using OpenSim.Framework.Servers;
  45. using OpenSim.Framework.Servers.HttpServer;
  46. using Timer=System.Timers.Timer;
  47. using Nini.Config;
  48. namespace OpenSim.Framework.Servers
  49. {
  50. /// <summary>
  51. /// Common base for the main OpenSimServers (user, grid, inventory, region, etc)
  52. /// </summary>
  53. public abstract class BaseOpenSimServer : ServerBase
  54. {
  55. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  56. /// <summary>
  57. /// Used by tests to suppress Environment.Exit(0) so that post-run operations are possible.
  58. /// </summary>
  59. public bool SuppressExit { get; set; }
  60. /// <summary>
  61. /// This will control a periodic log printout of the current 'show stats' (if they are active) for this
  62. /// server.
  63. /// </summary>
  64. private int m_periodDiagnosticTimerMS = 60 * 60 * 1000;
  65. private Timer m_periodicDiagnosticsTimer = new Timer(60 * 60 * 1000);
  66. /// <summary>
  67. /// Random uuid for private data
  68. /// </summary>
  69. protected string m_osSecret = String.Empty;
  70. protected BaseHttpServer m_httpServer;
  71. public BaseHttpServer HttpServer
  72. {
  73. get { return m_httpServer; }
  74. }
  75. public BaseOpenSimServer() : base()
  76. {
  77. // Random uuid for private data
  78. m_osSecret = UUID.Random().ToString();
  79. }
  80. /// <summary>
  81. /// Must be overriden by child classes for their own server specific startup behaviour.
  82. /// </summary>
  83. protected virtual void StartupSpecific()
  84. {
  85. StatsManager.SimExtraStats = new SimExtraStatsCollector();
  86. RegisterCommonCommands();
  87. RegisterCommonComponents(Config);
  88. IConfig startupConfig = Config.Configs["Startup"];
  89. int logShowStatsSeconds = startupConfig.GetInt("LogShowStatsSeconds", m_periodDiagnosticTimerMS / 1000);
  90. m_periodDiagnosticTimerMS = logShowStatsSeconds * 1000;
  91. m_periodicDiagnosticsTimer.Elapsed += new ElapsedEventHandler(LogDiagnostics);
  92. if (m_periodDiagnosticTimerMS != 0)
  93. {
  94. m_periodicDiagnosticsTimer.Interval = m_periodDiagnosticTimerMS;
  95. m_periodicDiagnosticsTimer.Enabled = true;
  96. }
  97. }
  98. protected override void ShutdownSpecific()
  99. {
  100. Watchdog.Enabled = false;
  101. base.ShutdownSpecific();
  102. MainServer.Stop();
  103. Thread.Sleep(5000);
  104. Util.StopThreadPool();
  105. WorkManager.Stop();
  106. Thread.Sleep(1000);
  107. RemovePIDFile();
  108. m_log.Info("[SHUTDOWN]: Shutdown processing on main thread complete. Exiting...");
  109. if (!SuppressExit)
  110. Environment.Exit(0);
  111. }
  112. /// <summary>
  113. /// Provides a list of help topics that are available. Overriding classes should append their topics to the
  114. /// information returned when the base method is called.
  115. /// </summary>
  116. ///
  117. /// <returns>
  118. /// A list of strings that represent different help topics on which more information is available
  119. /// </returns>
  120. protected virtual List<string> GetHelpTopics() { return new List<string>(); }
  121. /// <summary>
  122. /// Print statistics to the logfile, if they are active
  123. /// </summary>
  124. protected void LogDiagnostics(object source, ElapsedEventArgs e)
  125. {
  126. StringBuilder sb = new StringBuilder("DIAGNOSTICS\n\n");
  127. sb.Append(GetUptimeReport());
  128. sb.Append(StatsManager.SimExtraStats.Report());
  129. sb.Append(Environment.NewLine);
  130. sb.Append(GetThreadsReport());
  131. m_log.Debug(sb);
  132. }
  133. /// <summary>
  134. /// Performs initialisation of the scene, such as loading configuration from disk.
  135. /// </summary>
  136. public virtual void Startup()
  137. {
  138. m_log.Info("[STARTUP]: Beginning startup processing");
  139. m_log.Info("[STARTUP]: version: " + m_version + Environment.NewLine);
  140. // clr version potentially is more confusing than helpful, since it doesn't tell us if we're running under Mono/MS .NET and
  141. // the clr version number doesn't match the project version number under Mono.
  142. //m_log.Info("[STARTUP]: Virtual machine runtime version: " + Environment.Version + Environment.NewLine);
  143. m_log.InfoFormat(
  144. "[STARTUP]: Operating system version: {0}, .NET platform {1}, {2}-bit\n",
  145. Environment.OSVersion, Environment.OSVersion.Platform, Util.Is64BitProcess() ? "64" : "32");
  146. try
  147. {
  148. StartupSpecific();
  149. }
  150. catch(Exception e)
  151. {
  152. m_log.Fatal("Fatal error: " + e.ToString());
  153. Environment.Exit(1);
  154. }
  155. TimeSpan timeTaken = DateTime.Now - m_startuptime;
  156. // MainConsole.Instance.OutputFormat(
  157. // "PLEASE WAIT FOR LOGINS TO BE ENABLED ON REGIONS ONCE SCRIPTS HAVE STARTED. Non-script portion of startup took {0}m {1}s.",
  158. // timeTaken.Minutes, timeTaken.Seconds);
  159. }
  160. public string osSecret
  161. {
  162. // Secret uuid for the simulator
  163. get { return m_osSecret; }
  164. }
  165. public string StatReport(IOSHttpRequest httpRequest)
  166. {
  167. // If we catch a request for "callback", wrap the response in the value for jsonp
  168. if (httpRequest.Query.ContainsKey("callback"))
  169. {
  170. return httpRequest.Query["callback"].ToString() + "(" + StatsManager.SimExtraStats.XReport((DateTime.Now - m_startuptime).ToString() , m_version) + ");";
  171. }
  172. else
  173. {
  174. return StatsManager.SimExtraStats.XReport((DateTime.Now - m_startuptime).ToString() , m_version);
  175. }
  176. }
  177. }
  178. }