BaseOpenSimServer.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. m_log.Info("[SHUTDOWN]: Shutdown processing on main thread complete. Exiting...");
  101. RemovePIDFile();
  102. base.ShutdownSpecific();
  103. if (!SuppressExit)
  104. Environment.Exit(0);
  105. }
  106. /// <summary>
  107. /// Provides a list of help topics that are available. Overriding classes should append their topics to the
  108. /// information returned when the base method is called.
  109. /// </summary>
  110. ///
  111. /// <returns>
  112. /// A list of strings that represent different help topics on which more information is available
  113. /// </returns>
  114. protected virtual List<string> GetHelpTopics() { return new List<string>(); }
  115. /// <summary>
  116. /// Print statistics to the logfile, if they are active
  117. /// </summary>
  118. protected void LogDiagnostics(object source, ElapsedEventArgs e)
  119. {
  120. StringBuilder sb = new StringBuilder("DIAGNOSTICS\n\n");
  121. sb.Append(GetUptimeReport());
  122. sb.Append(StatsManager.SimExtraStats.Report());
  123. sb.Append(Environment.NewLine);
  124. sb.Append(GetThreadsReport());
  125. m_log.Debug(sb);
  126. }
  127. /// <summary>
  128. /// Performs initialisation of the scene, such as loading configuration from disk.
  129. /// </summary>
  130. public virtual void Startup()
  131. {
  132. m_log.Info("[STARTUP]: Beginning startup processing");
  133. m_log.Info("[STARTUP]: version: " + m_version + Environment.NewLine);
  134. // clr version potentially is more confusing than helpful, since it doesn't tell us if we're running under Mono/MS .NET and
  135. // the clr version number doesn't match the project version number under Mono.
  136. //m_log.Info("[STARTUP]: Virtual machine runtime version: " + Environment.Version + Environment.NewLine);
  137. m_log.InfoFormat(
  138. "[STARTUP]: Operating system version: {0}, .NET platform {1}, {2}-bit\n",
  139. Environment.OSVersion, Environment.OSVersion.Platform, Util.Is64BitProcess() ? "64" : "32");
  140. StartupSpecific();
  141. TimeSpan timeTaken = DateTime.Now - m_startuptime;
  142. // MainConsole.Instance.OutputFormat(
  143. // "PLEASE WAIT FOR LOGINS TO BE ENABLED ON REGIONS ONCE SCRIPTS HAVE STARTED. Non-script portion of startup took {0}m {1}s.",
  144. // timeTaken.Minutes, timeTaken.Seconds);
  145. }
  146. public string osSecret
  147. {
  148. // Secret uuid for the simulator
  149. get { return m_osSecret; }
  150. }
  151. public string StatReport(IOSHttpRequest httpRequest)
  152. {
  153. // If we catch a request for "callback", wrap the response in the value for jsonp
  154. if (httpRequest.Query.ContainsKey("callback"))
  155. {
  156. return httpRequest.Query["callback"].ToString() + "(" + StatsManager.SimExtraStats.XReport((DateTime.Now - m_startuptime).ToString() , m_version) + ");";
  157. }
  158. else
  159. {
  160. return StatsManager.SimExtraStats.XReport((DateTime.Now - m_startuptime).ToString() , m_version);
  161. }
  162. }
  163. }
  164. }