BaseOpenSimServer.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 OpenSim 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.IO;
  29. using System.Reflection;
  30. using System.Text;
  31. using System.Timers;
  32. using log4net;
  33. using OpenSim.Framework.Console;
  34. using OpenSim.Framework.Statistics;
  35. namespace OpenSim.Framework.Servers
  36. {
  37. /// <summary>
  38. /// Common base for the main OpenSimServers (user, grid, inventory, region, etc)
  39. /// </summary>
  40. public abstract class BaseOpenSimServer
  41. {
  42. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  43. /// <summary>
  44. /// This will control a periodic log printout of the current 'show stats' (if they are active) for this
  45. /// server.
  46. /// </summary>
  47. private Timer m_periodicDiagnosticsTimer = new Timer(60 * 60 * 1000);
  48. protected ConsoleBase m_console;
  49. /// <summary>
  50. /// Time at which this server was started
  51. /// </summary>
  52. protected DateTime m_startuptime;
  53. /// <summary>
  54. /// Record the initial startup directory for info purposes
  55. /// </summary>
  56. protected string m_startupDirectory = Environment.CurrentDirectory;
  57. /// <summary>
  58. /// Server version information. Usually VersionInfo + information about svn revision, operating system, etc.
  59. /// </summary>
  60. protected string m_version;
  61. protected BaseHttpServer m_httpServer;
  62. public BaseHttpServer HttpServer
  63. {
  64. get { return m_httpServer; }
  65. }
  66. /// <summary>
  67. /// Holds the non-viewer statistics collection object for this service/server
  68. /// </summary>
  69. protected IStatsCollector m_stats;
  70. public BaseOpenSimServer()
  71. {
  72. m_startuptime = DateTime.Now;
  73. m_version = VersionInfo.Version;
  74. m_periodicDiagnosticsTimer.Elapsed += new ElapsedEventHandler(LogDiagnostics);
  75. m_periodicDiagnosticsTimer.Enabled = true;
  76. }
  77. /// <summary>
  78. /// Print statistics to the logfile, if they are active
  79. /// </summary>
  80. protected void LogDiagnostics(object source, ElapsedEventArgs e)
  81. {
  82. StringBuilder sb = new StringBuilder("DIAGNOSTICS\n\n");
  83. sb.Append(GetUptimeReport());
  84. if (m_stats != null)
  85. {
  86. sb.Append(m_stats.Report());
  87. }
  88. m_log.Debug(sb);
  89. }
  90. /// <summary>
  91. /// Return a report about the uptime of this server
  92. /// </summary>
  93. /// <returns></returns>
  94. protected string GetUptimeReport()
  95. {
  96. StringBuilder sb = new StringBuilder(String.Format("Time now is {0}\n", DateTime.Now));
  97. sb.Append(String.Format("Server has been running since {0}, {1}\n", m_startuptime.DayOfWeek, m_startuptime));
  98. sb.Append(String.Format("That is an elapsed time of {0}\n", DateTime.Now - m_startuptime));
  99. return sb.ToString();
  100. }
  101. /// <summary>
  102. /// Performs initialisation of the scene, such as loading configuration from disk.
  103. /// </summary>
  104. public virtual void Startup()
  105. {
  106. m_log.Info("[STARTUP]: Beginning startup processing");
  107. EnhanceVersionInformation();
  108. m_log.Info("[STARTUP]: Version " + m_version + "\n");
  109. }
  110. /// <summary>
  111. /// Should be overriden and referenced by descendents if they need to perform extra shutdown processing
  112. /// </summary>
  113. public virtual void Shutdown()
  114. {
  115. m_log.Info("[SHUTDOWN]: Shutdown processing on main thread complete. Exiting...");
  116. Environment.Exit(0);
  117. }
  118. /// <summary>
  119. /// Runs commands issued by the server console from the operator
  120. /// </summary>
  121. /// <param name="command">The first argument of the parameter (the command)</param>
  122. /// <param name="cmdparams">Additional arguments passed to the command</param>
  123. public virtual void RunCmd(string command, string[] cmdparams)
  124. {
  125. switch (command)
  126. {
  127. case "help":
  128. Notice("quit - equivalent to shutdown.");
  129. Notice("show info - show server information (e.g. startup path).");
  130. if (m_stats != null)
  131. Notice("show stats - show statistical information for this server");
  132. Notice("show uptime - show server startup time and uptime.");
  133. Notice("show version - show server version.");
  134. Notice("shutdown - shutdown the server.\n");
  135. break;
  136. case "show":
  137. if (cmdparams.Length > 0)
  138. {
  139. Show(cmdparams[0]);
  140. }
  141. break;
  142. case "quit":
  143. case "shutdown":
  144. Shutdown();
  145. break;
  146. }
  147. }
  148. /// <summary>
  149. /// Outputs to the console information about the region
  150. /// </summary>
  151. /// <param name="ShowWhat">What information to display (valid arguments are "uptime", "users")</param>
  152. public virtual void Show(string ShowWhat)
  153. {
  154. switch (ShowWhat)
  155. {
  156. case "info":
  157. Notice("Version: " + m_version);
  158. Notice("Startup directory: " + m_startupDirectory);
  159. break;
  160. case "stats":
  161. if (m_stats != null)
  162. {
  163. Notice(m_stats.Report());
  164. }
  165. break;
  166. case "uptime":
  167. Notice(GetUptimeReport());
  168. /*
  169. Notice("Time now is " + DateTime.Now);
  170. Notice("Server has been running since " + m_startuptime.DayOfWeek + ", " + m_startuptime.ToString());
  171. Notice("That is an elapsed time of " + (DateTime.Now - m_startuptime).ToString());
  172. */
  173. break;
  174. case "version":
  175. Notice("Version: " + m_version);
  176. break;
  177. }
  178. }
  179. /// <summary>
  180. /// Console output is only possible if a console has been established.
  181. /// That is something that cannot be determined within this class. So
  182. /// all attempts to use the console MUST be verified.
  183. /// </summary>
  184. private void Notice(string msg)
  185. {
  186. if (m_console != null)
  187. {
  188. m_console.Notice(msg);
  189. }
  190. }
  191. /// <summary>
  192. /// Enhance the version string with extra information if it's available.
  193. /// </summary>
  194. protected void EnhanceVersionInformation()
  195. {
  196. string buildVersion = string.Empty;
  197. // Add subversion revision information if available
  198. // Try file "svn_revision" in the current directory first, then the .svn info.
  199. // This allows to make the revision available in simulators not running from the source tree.
  200. // FIXME: Making an assumption about the directory we're currently in - we do this all over the place
  201. // elsewhere as well
  202. string svnRevisionFileName = "svn_revision";
  203. string svnFileName = "../.svn/entries";
  204. string inputLine;
  205. int strcmp;
  206. if (File.Exists(svnRevisionFileName))
  207. {
  208. StreamReader RevisionFile = File.OpenText(svnRevisionFileName);
  209. buildVersion = RevisionFile.ReadLine();
  210. buildVersion.Trim();
  211. RevisionFile.Close();
  212. }
  213. if (string.IsNullOrEmpty(buildVersion) && File.Exists(svnFileName))
  214. {
  215. StreamReader EntriesFile = File.OpenText(svnFileName);
  216. inputLine = EntriesFile.ReadLine();
  217. while (inputLine != null)
  218. {
  219. // using the dir svn revision at the top of entries file
  220. strcmp = String.Compare(inputLine, "dir");
  221. if (strcmp == 0)
  222. {
  223. buildVersion = EntriesFile.ReadLine();
  224. break;
  225. }
  226. else
  227. {
  228. inputLine = EntriesFile.ReadLine();
  229. }
  230. }
  231. EntriesFile.Close();
  232. }
  233. if (!string.IsNullOrEmpty(buildVersion))
  234. {
  235. m_version += ", SVN build r" + buildVersion;
  236. }
  237. else
  238. {
  239. m_version += ", SVN build revision not available";
  240. }
  241. // Add operating system information if available
  242. string OSString = "";
  243. if (System.Environment.OSVersion.Platform != PlatformID.Unix)
  244. {
  245. OSString = System.Environment.OSVersion.ToString();
  246. }
  247. else
  248. {
  249. OSString = Util.ReadEtcIssue();
  250. }
  251. if (OSString.Length > 45)
  252. {
  253. OSString = OSString.Substring(0, 45);
  254. }
  255. m_version += ", OS " + OSString;
  256. }
  257. }
  258. }