1
0

GridInfoService.cs 6.1 KB

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