1
0

GridInfoHandlers.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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.Security;
  33. using System.Text;
  34. using log4net;
  35. using Nini.Config;
  36. using Nwc.XmlRpc;
  37. using OpenSim.Framework;
  38. using OpenSim.Framework.Servers.HttpServer;
  39. using OpenMetaverse.StructuredData;
  40. namespace OpenSim.Server.Handlers.Grid
  41. {
  42. public class GridInfoHandlers
  43. {
  44. private static readonly ILog _log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  45. private IConfigSource m_Config;
  46. private Hashtable _info = new Hashtable();
  47. /// <summary>
  48. /// Instantiate a GridInfoService object.
  49. /// </summary>
  50. /// <param name="configPath">path to config path containing
  51. /// grid information</param>
  52. /// <remarks>
  53. /// GridInfoService uses the [GridInfo] section of the
  54. /// standard OpenSim.ini file --- which is not optimal, but
  55. /// anything else requires a general redesign of the config
  56. /// system.
  57. /// </remarks>
  58. public GridInfoHandlers(IConfigSource configSource)
  59. {
  60. m_Config = configSource;
  61. loadGridInfo(configSource);
  62. }
  63. private void loadGridInfo(IConfigSource configSource)
  64. {
  65. _info["platform"] = "OpenSim";
  66. try
  67. {
  68. IConfig gridCfg = configSource.Configs["GridInfoService"];
  69. IConfig netCfg = configSource.Configs["Network"];
  70. if (null != gridCfg)
  71. {
  72. foreach (string k in gridCfg.GetKeys())
  73. {
  74. _info[k] = gridCfg.GetString(k);
  75. }
  76. }
  77. else if (null != netCfg)
  78. {
  79. _info["login"]
  80. = String.Format(
  81. "http://127.0.0.1:{0}/",
  82. netCfg.GetString(
  83. "http_listener_port", ConfigSettings.DefaultRegionHttpPort.ToString()));
  84. IssueWarning();
  85. }
  86. else
  87. {
  88. _info["login"] = "http://127.0.0.1:9000/";
  89. IssueWarning();
  90. }
  91. }
  92. catch (Exception)
  93. {
  94. _log.Warn("[GRID INFO SERVICE]: Cannot get grid info from config source, using minimal defaults");
  95. }
  96. _log.DebugFormat("[GRID INFO SERVICE]: Grid info service initialized with {0} keys", _info.Count);
  97. }
  98. private void IssueWarning()
  99. {
  100. _log.Warn("[GRID INFO SERVICE]: found no [GridInfo] section in your configuration files");
  101. _log.Warn("[GRID INFO SERVICE]: trying to guess sensible defaults, you might want to provide better ones:");
  102. foreach (string k in _info.Keys)
  103. {
  104. _log.WarnFormat("[GRID INFO SERVICE]: {0}: {1}", k, _info[k]);
  105. }
  106. }
  107. public XmlRpcResponse XmlRpcGridInfoMethod(XmlRpcRequest request, IPEndPoint remoteClient)
  108. {
  109. XmlRpcResponse response = new XmlRpcResponse();
  110. Hashtable responseData = new Hashtable();
  111. _log.Debug("[GRID INFO SERVICE]: Request for grid info");
  112. foreach (string k in _info.Keys)
  113. {
  114. responseData[k] = _info[k];
  115. }
  116. response.Value = responseData;
  117. return response;
  118. }
  119. public string RestGetGridInfoMethod(string request, string path, string param,
  120. IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
  121. {
  122. StringBuilder sb = new StringBuilder();
  123. sb.Append("<gridinfo>\n");
  124. foreach (string k in _info.Keys)
  125. {
  126. sb.AppendFormat("<{0}>{1}</{0}>\n", k, SecurityElement.Escape(_info[k].ToString()));
  127. }
  128. sb.Append("</gridinfo>\n");
  129. return sb.ToString();
  130. }
  131. /// <summary>
  132. /// Get GridInfo in json format: Used bu the OSSL osGetGrid*
  133. /// Adding the SRV_HomeIRI to the kvp returned for use in scripts
  134. /// </summary>
  135. /// <returns>
  136. /// json string
  137. /// </returns>
  138. /// <param name='request'>
  139. /// Request.
  140. /// </param>
  141. /// <param name='path'>
  142. /// /json_grid_info
  143. /// </param>
  144. /// <param name='param'>
  145. /// Parameter.
  146. /// </param>
  147. /// <param name='httpRequest'>
  148. /// Http request.
  149. /// </param>
  150. /// <param name='httpResponse'>
  151. /// Http response.
  152. /// </param>
  153. public string JsonGetGridInfoMethod(string request, string path, string param,
  154. IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
  155. {
  156. OSDMap map = new OSDMap();
  157. foreach (string k in _info.Keys)
  158. {
  159. map[k] = OSD.FromString(_info[k].ToString());
  160. }
  161. string HomeURI = Util.GetConfigVarFromSections<string>(m_Config, "HomeURI",
  162. new string[] { "Startup", "Hypergrid" }, String.Empty);
  163. if (!String.IsNullOrEmpty(HomeURI))
  164. map["home"] = OSD.FromString(HomeURI);
  165. else // Legacy. Remove soon!
  166. {
  167. IConfig cfg = m_Config.Configs["LoginService"];
  168. if (null != cfg)
  169. HomeURI = cfg.GetString("SRV_HomeURI", HomeURI);
  170. if (!String.IsNullOrEmpty(HomeURI))
  171. map["home"] = OSD.FromString(HomeURI);
  172. }
  173. return OSDParser.SerializeJsonString(map).ToString();
  174. }
  175. }
  176. }