1
0

BaseOpenSimServer.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. StatsManager.SimExtraStats = new SimExtraStatsCollector();
  82. RegisterCommonCommands();
  83. RegisterCommonComponents(Config);
  84. }
  85. protected override void ShutdownSpecific()
  86. {
  87. m_log.Info("[SHUTDOWN]: Shutdown processing on main thread complete. Exiting...");
  88. RemovePIDFile();
  89. base.ShutdownSpecific();
  90. Environment.Exit(0);
  91. }
  92. /// <summary>
  93. /// Provides a list of help topics that are available. Overriding classes should append their topics to the
  94. /// information returned when the base method is called.
  95. /// </summary>
  96. ///
  97. /// <returns>
  98. /// A list of strings that represent different help topics on which more information is available
  99. /// </returns>
  100. protected virtual List<string> GetHelpTopics() { return new List<string>(); }
  101. /// <summary>
  102. /// Print statistics to the logfile, if they are active
  103. /// </summary>
  104. protected void LogDiagnostics(object source, ElapsedEventArgs e)
  105. {
  106. StringBuilder sb = new StringBuilder("DIAGNOSTICS\n\n");
  107. sb.Append(GetUptimeReport());
  108. sb.Append(StatsManager.SimExtraStats.Report());
  109. sb.Append(Environment.NewLine);
  110. sb.Append(GetThreadsReport());
  111. m_log.Debug(sb);
  112. }
  113. /// <summary>
  114. /// Performs initialisation of the scene, such as loading configuration from disk.
  115. /// </summary>
  116. public virtual void Startup()
  117. {
  118. StartupSpecific();
  119. TimeSpan timeTaken = DateTime.Now - m_startuptime;
  120. m_log.InfoFormat(
  121. "[STARTUP]: Non-script portion of startup took {0}m {1}s. PLEASE WAIT FOR LOGINS TO BE ENABLED ON REGIONS ONCE SCRIPTS HAVE STARTED.",
  122. timeTaken.Minutes, timeTaken.Seconds);
  123. }
  124. public string osSecret
  125. {
  126. // Secret uuid for the simulator
  127. get { return m_osSecret; }
  128. }
  129. public string StatReport(IOSHttpRequest httpRequest)
  130. {
  131. // If we catch a request for "callback", wrap the response in the value for jsonp
  132. if (httpRequest.Query.ContainsKey("callback"))
  133. {
  134. return httpRequest.Query["callback"].ToString() + "(" + StatsManager.SimExtraStats.XReport((DateTime.Now - m_startuptime).ToString() , m_version) + ");";
  135. }
  136. else
  137. {
  138. return StatsManager.SimExtraStats.XReport((DateTime.Now - m_startuptime).ToString() , m_version);
  139. }
  140. }
  141. }
  142. }