1
0

BaseOpenSimServer.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. namespace OpenSim.Framework.Servers
  48. {
  49. /// <summary>
  50. /// Common base for the main OpenSimServers (user, grid, inventory, region, etc)
  51. /// </summary>
  52. public abstract class BaseOpenSimServer : ServerBase
  53. {
  54. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  55. /// <summary>
  56. /// This will control a periodic log printout of the current 'show stats' (if they are active) for this
  57. /// server.
  58. /// </summary>
  59. private Timer m_periodicDiagnosticsTimer = new Timer(60 * 60 * 1000);
  60. /// <summary>
  61. /// Random uuid for private data
  62. /// </summary>
  63. protected string m_osSecret = String.Empty;
  64. protected BaseHttpServer m_httpServer;
  65. public BaseHttpServer HttpServer
  66. {
  67. get { return m_httpServer; }
  68. }
  69. public BaseOpenSimServer() : base()
  70. {
  71. // Random uuid for private data
  72. m_osSecret = UUID.Random().ToString();
  73. m_periodicDiagnosticsTimer.Elapsed += new ElapsedEventHandler(LogDiagnostics);
  74. m_periodicDiagnosticsTimer.Enabled = true;
  75. }
  76. /// <summary>
  77. /// Must be overriden by child classes for their own server specific startup behaviour.
  78. /// </summary>
  79. protected virtual void StartupSpecific()
  80. {
  81. if (m_console == null)
  82. return;
  83. RegisterCommonCommands();
  84. m_console.Commands.AddCommand("General", false, "quit",
  85. "quit",
  86. "Quit the application", HandleQuit);
  87. m_console.Commands.AddCommand("General", false, "shutdown",
  88. "shutdown",
  89. "Quit the application", HandleQuit);
  90. }
  91. /// <summary>
  92. /// Should be overriden and referenced by descendents if they need to perform extra shutdown processing
  93. /// </summary>
  94. public virtual void ShutdownSpecific() {}
  95. /// <summary>
  96. /// Provides a list of help topics that are available. Overriding classes should append their topics to the
  97. /// information returned when the base method is called.
  98. /// </summary>
  99. ///
  100. /// <returns>
  101. /// A list of strings that represent different help topics on which more information is available
  102. /// </returns>
  103. protected virtual List<string> GetHelpTopics() { return new List<string>(); }
  104. /// <summary>
  105. /// Print statistics to the logfile, if they are active
  106. /// </summary>
  107. protected void LogDiagnostics(object source, ElapsedEventArgs e)
  108. {
  109. StringBuilder sb = new StringBuilder("DIAGNOSTICS\n\n");
  110. sb.Append(GetUptimeReport());
  111. sb.Append(StatsManager.SimExtraStats.Report());
  112. sb.Append(Environment.NewLine);
  113. sb.Append(GetThreadsReport());
  114. m_log.Debug(sb);
  115. }
  116. /// <summary>
  117. /// Performs initialisation of the scene, such as loading configuration from disk.
  118. /// </summary>
  119. public virtual void Startup()
  120. {
  121. m_log.Info("[STARTUP]: Beginning startup processing");
  122. m_log.Info("[STARTUP]: OpenSimulator version: " + m_version + Environment.NewLine);
  123. // clr version potentially is more confusing than helpful, since it doesn't tell us if we're running under Mono/MS .NET and
  124. // the clr version number doesn't match the project version number under Mono.
  125. //m_log.Info("[STARTUP]: Virtual machine runtime version: " + Environment.Version + Environment.NewLine);
  126. m_log.InfoFormat(
  127. "[STARTUP]: Operating system version: {0}, .NET platform {1}, {2}-bit\n",
  128. Environment.OSVersion, Environment.OSVersion.Platform, Util.Is64BitProcess() ? "64" : "32");
  129. StartupSpecific();
  130. TimeSpan timeTaken = DateTime.Now - m_startuptime;
  131. m_log.InfoFormat(
  132. "[STARTUP]: Non-script portion of startup took {0}m {1}s. PLEASE WAIT FOR LOGINS TO BE ENABLED ON REGIONS ONCE SCRIPTS HAVE STARTED.",
  133. timeTaken.Minutes, timeTaken.Seconds);
  134. }
  135. /// <summary>
  136. /// Should be overriden and referenced by descendents if they need to perform extra shutdown processing
  137. /// </summary>
  138. public virtual void Shutdown()
  139. {
  140. ShutdownSpecific();
  141. m_log.Info("[SHUTDOWN]: Shutdown processing on main thread complete. Exiting...");
  142. RemovePIDFile();
  143. Environment.Exit(0);
  144. }
  145. private void HandleQuit(string module, string[] args)
  146. {
  147. Shutdown();
  148. }
  149. public string osSecret {
  150. // Secret uuid for the simulator
  151. get { return m_osSecret; }
  152. }
  153. public string StatReport(IOSHttpRequest httpRequest)
  154. {
  155. // If we catch a request for "callback", wrap the response in the value for jsonp
  156. if (httpRequest.Query.ContainsKey("callback"))
  157. {
  158. return httpRequest.Query["callback"].ToString() + "(" + StatsManager.SimExtraStats.XReport((DateTime.Now - m_startuptime).ToString() , m_version) + ");";
  159. }
  160. else
  161. {
  162. return StatsManager.SimExtraStats.XReport((DateTime.Now - m_startuptime).ToString() , m_version);
  163. }
  164. }
  165. }
  166. }