GridInfoService.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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;
  29. using System.IO;
  30. using System.Net;
  31. using System.Reflection;
  32. using System.Text;
  33. using log4net;
  34. using Nini.Config;
  35. using Nwc.XmlRpc;
  36. using OpenSim.Framework.Servers.HttpServer;
  37. namespace OpenSim.Framework.Communications.Services
  38. {
  39. public class GridInfoService
  40. {
  41. private static readonly ILog _log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  42. private Hashtable _info = new Hashtable();
  43. /// <summary>
  44. /// Instantiate a GridInfoService object.
  45. /// </summary>
  46. /// <param name="configPath">path to config path containing
  47. /// grid information</param>
  48. /// <remarks>
  49. /// GridInfoService uses the [GridInfo] section of the
  50. /// standard OpenSim.ini file --- which is not optimal, but
  51. /// anything else requires a general redesign of the config
  52. /// system.
  53. /// </remarks>
  54. public GridInfoService(IConfigSource configSource)
  55. {
  56. loadGridInfo(configSource);
  57. }
  58. /// <summary>
  59. /// Default constructor, uses OpenSim.ini.
  60. /// </summary>
  61. public GridInfoService()
  62. {
  63. try
  64. {
  65. IConfigSource configSource = new IniConfigSource(Path.Combine(Util.configDir(), "OpenSim.ini"));
  66. loadGridInfo(configSource);
  67. }
  68. catch (FileNotFoundException)
  69. {
  70. _log.Warn(
  71. "[GRID INFO SERVICE]: No OpenSim.ini file found --- GridInfoServices WILL NOT BE AVAILABLE to your users");
  72. }
  73. }
  74. private void loadGridInfo(IConfigSource configSource)
  75. {
  76. _info["platform"] = "OpenSim";
  77. try
  78. {
  79. IConfig startupCfg = configSource.Configs["Startup"];
  80. IConfig gridCfg = configSource.Configs["GridInfo"];
  81. IConfig netCfg = configSource.Configs["Network"];
  82. bool grid = startupCfg.GetBoolean("gridmode", false);
  83. if (grid)
  84. _info["mode"] = "grid";
  85. else
  86. _info["mode"] = "standalone";
  87. if (null != gridCfg)
  88. {
  89. foreach (string k in gridCfg.GetKeys())
  90. {
  91. _info[k] = gridCfg.GetString(k);
  92. }
  93. }
  94. else if (null != netCfg)
  95. {
  96. if (grid)
  97. _info["login"]
  98. = netCfg.GetString(
  99. "user_server_url", "http://127.0.0.1:" + ConfigSettings.DefaultUserServerHttpPort.ToString());
  100. else
  101. _info["login"]
  102. = String.Format(
  103. "http://127.0.0.1:{0}/",
  104. netCfg.GetString(
  105. "http_listener_port", ConfigSettings.DefaultRegionHttpPort.ToString()));
  106. IssueWarning();
  107. }
  108. else
  109. {
  110. _info["login"] = "http://127.0.0.1:9000/";
  111. IssueWarning();
  112. }
  113. }
  114. catch (Exception)
  115. {
  116. _log.Debug("[GRID INFO SERVICE]: Cannot get grid info from config source, using minimal defaults");
  117. }
  118. _log.DebugFormat("[GRID INFO SERVICE]: Grid info service initialized with {0} keys", _info.Count);
  119. }
  120. private void IssueWarning()
  121. {
  122. _log.Warn("[GRID INFO SERVICE]: found no [GridInfo] section in your OpenSim.ini");
  123. _log.Warn("[GRID INFO SERVICE]: trying to guess sensible defaults, you might want to provide better ones:");
  124. foreach (string k in _info.Keys)
  125. {
  126. _log.WarnFormat("[GRID INFO SERVICE]: {0}: {1}", k, _info[k]);
  127. }
  128. }
  129. public XmlRpcResponse XmlRpcGridInfoMethod(XmlRpcRequest request, IPEndPoint remoteClient)
  130. {
  131. XmlRpcResponse response = new XmlRpcResponse();
  132. Hashtable responseData = new Hashtable();
  133. _log.Info("[GRID INFO SERVICE]: Request for grid info");
  134. foreach (string k in _info.Keys)
  135. {
  136. responseData[k] = _info[k];
  137. }
  138. response.Value = responseData;
  139. return response;
  140. }
  141. public string RestGetGridInfoMethod(string request, string path, string param,
  142. OSHttpRequest httpRequest, OSHttpResponse httpResponse)
  143. {
  144. StringBuilder sb = new StringBuilder();
  145. sb.Append("<gridinfo>\n");
  146. foreach (string k in _info.Keys)
  147. {
  148. sb.AppendFormat("<{0}>{1}</{0}>\n", k, _info[k]);
  149. }
  150. sb.Append("</gridinfo>\n");
  151. return sb.ToString();
  152. }
  153. }
  154. }