EstateDataConnector.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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.Net;
  30. using System.Reflection;
  31. using log4net;
  32. using OpenMetaverse;
  33. using Nini.Config;
  34. using OpenSim.Framework;
  35. using OpenSim.Framework.ServiceAuth;
  36. using OpenSim.Services.Connectors;
  37. using OpenSim.Services.Interfaces;
  38. using OpenSim.Server.Base;
  39. namespace OpenSim.Services.Connectors
  40. {
  41. public class EstateDataRemoteConnector : BaseServiceConnector, IEstateDataService
  42. {
  43. private static readonly ILog m_log =
  44. LogManager.GetLogger(
  45. MethodBase.GetCurrentMethod().DeclaringType);
  46. private string m_ServerURI = String.Empty;
  47. private ExpiringCache<string, List<EstateSettings>> m_EstateCache = new ExpiringCache<string, List<EstateSettings>>();
  48. private const int EXPIRATION = 5 * 60; // 5 minutes in secs
  49. public EstateDataRemoteConnector(IConfigSource source)
  50. {
  51. Initialise(source);
  52. }
  53. public virtual void Initialise(IConfigSource source)
  54. {
  55. IConfig gridConfig = source.Configs["EstateService"];
  56. if (gridConfig == null)
  57. {
  58. m_log.Error("[ESTATE CONNECTOR]: EstateService missing from OpenSim.ini");
  59. throw new Exception("Estate connector init error");
  60. }
  61. string serviceURI = gridConfig.GetString("EstateServerURI",
  62. String.Empty);
  63. if (serviceURI == String.Empty)
  64. {
  65. m_log.Error("[ESTATE CONNECTOR]: No Server URI named in section EstateService");
  66. throw new Exception("Estate connector init error");
  67. }
  68. m_ServerURI = serviceURI;
  69. base.Initialise(source, "EstateService");
  70. }
  71. #region IEstateDataService
  72. public List<EstateSettings> LoadEstateSettingsAll()
  73. {
  74. string reply = string.Empty;
  75. string uri = m_ServerURI + "/estates";
  76. reply = MakeRequest("GET", uri, string.Empty);
  77. if (String.IsNullOrEmpty(reply))
  78. return new List<EstateSettings>();
  79. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  80. List<EstateSettings> estates = new List<EstateSettings>();
  81. if (replyData != null && replyData.Count > 0)
  82. {
  83. m_log.DebugFormat("[ESTATE CONNECTOR]: LoadEstateSettingsAll returned {0} elements", replyData.Count);
  84. Dictionary<string, object>.ValueCollection estateData = replyData.Values;
  85. foreach (object r in estateData)
  86. {
  87. if (r is Dictionary<string, object>)
  88. {
  89. EstateSettings es = new EstateSettings((Dictionary<string, object>)r);
  90. estates.Add(es);
  91. }
  92. }
  93. m_EstateCache.AddOrUpdate("estates", estates, EXPIRATION);
  94. }
  95. else
  96. m_log.DebugFormat("[ESTATE CONNECTOR]: LoadEstateSettingsAll from {0} received null or zero response", uri);
  97. return estates;
  98. }
  99. public List<int> GetEstatesAll()
  100. {
  101. List<int> eids = new List<int>();
  102. // If we don't have them, load them from the server
  103. List<EstateSettings> estates = null;
  104. if (!m_EstateCache.TryGetValue("estates", out estates))
  105. estates = LoadEstateSettingsAll();
  106. foreach (EstateSettings es in estates)
  107. eids.Add((int)es.EstateID);
  108. return eids;
  109. }
  110. public List<int> GetEstates(string search)
  111. {
  112. // If we don't have them, load them from the server
  113. List<EstateSettings> estates = null;
  114. if (!m_EstateCache.TryGetValue("estates", out estates))
  115. estates = LoadEstateSettingsAll();
  116. List<int> eids = new List<int>();
  117. foreach (EstateSettings es in estates)
  118. if (es.EstateName == search)
  119. eids.Add((int)es.EstateID);
  120. return eids;
  121. }
  122. public List<int> GetEstatesByOwner(UUID ownerID)
  123. {
  124. // If we don't have them, load them from the server
  125. List<EstateSettings> estates = null;
  126. if (!m_EstateCache.TryGetValue("estates", out estates))
  127. estates = LoadEstateSettingsAll();
  128. List<int> eids = new List<int>();
  129. foreach (EstateSettings es in estates)
  130. if (es.EstateOwner == ownerID)
  131. eids.Add((int)es.EstateID);
  132. return eids;
  133. }
  134. public List<UUID> GetRegions(int estateID)
  135. {
  136. string reply = string.Empty;
  137. // /estates/regions/?eid=int
  138. string uri = m_ServerURI + "/estates/regions/?eid=" + estateID.ToString();
  139. reply = MakeRequest("GET", uri, string.Empty);
  140. if (String.IsNullOrEmpty(reply))
  141. return new List<UUID>();
  142. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  143. List<UUID> regions = new List<UUID>();
  144. if (replyData != null && replyData.Count > 0)
  145. {
  146. m_log.DebugFormat("[ESTATE CONNECTOR]: GetRegions for estate {0} returned {1} elements", estateID, replyData.Count);
  147. Dictionary<string, object>.ValueCollection data = replyData.Values;
  148. foreach (object r in data)
  149. {
  150. UUID uuid = UUID.Zero;
  151. if (UUID.TryParse(r.ToString(), out uuid))
  152. regions.Add(uuid);
  153. }
  154. }
  155. else
  156. m_log.DebugFormat("[ESTATE CONNECTOR]: GetRegions from {0} received null or zero response", uri);
  157. return regions;
  158. }
  159. public EstateSettings LoadEstateSettings(UUID regionID, bool create)
  160. {
  161. string reply = string.Empty;
  162. // /estates/estate/?region=uuid&create=[t|f]
  163. string uri = m_ServerURI + string.Format("/estates/estate/?region={0}&create={1}", regionID, create);
  164. reply = MakeRequest("GET", uri, string.Empty);
  165. if(reply == null)
  166. {
  167. // this is a fatal error
  168. m_log.DebugFormat("[ESTATE CONNECTOR] connection to remote estates service failed");
  169. m_log.DebugFormat("[ESTATE CONNECTOR] simulator needs to terminate");
  170. Environment.Exit(-1);
  171. }
  172. if (String.IsNullOrEmpty(reply))
  173. return null;
  174. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  175. if (replyData != null && replyData.Count > 0)
  176. {
  177. m_log.DebugFormat("[ESTATE CONNECTOR]: LoadEstateSettings({0}) returned {1} elements", regionID, replyData.Count);
  178. EstateSettings es = new EstateSettings(replyData);
  179. return es;
  180. }
  181. else
  182. m_log.DebugFormat("[ESTATE CONNECTOR]: LoadEstateSettings(regionID) from {0} received null or zero response", uri);
  183. return null;
  184. }
  185. public EstateSettings LoadEstateSettings(int estateID)
  186. {
  187. string reply = string.Empty;
  188. // /estates/estate/?eid=int
  189. string uri = m_ServerURI + string.Format("/estates/estate/?eid={0}", estateID);
  190. reply = MakeRequest("GET", uri, string.Empty);
  191. if (String.IsNullOrEmpty(reply))
  192. return null;
  193. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  194. if (replyData != null && replyData.Count > 0)
  195. {
  196. m_log.DebugFormat("[ESTATE CONNECTOR]: LoadEstateSettings({0}) returned {1} elements", estateID, replyData.Count);
  197. EstateSettings es = new EstateSettings(replyData);
  198. return es;
  199. }
  200. else
  201. m_log.DebugFormat("[ESTATE CONNECTOR]: LoadEstateSettings(estateID) from {0} received null or zero response", uri);
  202. return null;
  203. }
  204. /// <summary>
  205. /// Forbidden operation
  206. /// </summary>
  207. /// <returns></returns>
  208. public EstateSettings CreateNewEstate()
  209. {
  210. // No can do
  211. return null;
  212. }
  213. public void StoreEstateSettings(EstateSettings es)
  214. {
  215. // /estates/estate/
  216. string uri = m_ServerURI + ("/estates/estate");
  217. Dictionary<string, object> formdata = es.ToMap();
  218. formdata["OP"] = "STORE";
  219. PostRequest(uri, formdata);
  220. }
  221. public bool LinkRegion(UUID regionID, int estateID)
  222. {
  223. // /estates/estate/?eid=int&region=uuid
  224. string uri = m_ServerURI + String.Format("/estates/estate/?eid={0}&region={1}", estateID, regionID);
  225. Dictionary<string, object> formdata = new Dictionary<string, object>();
  226. formdata["OP"] = "LINK";
  227. return PostRequest(uri, formdata);
  228. }
  229. private bool PostRequest(string uri, Dictionary<string, object> sendData)
  230. {
  231. string reqString = ServerUtils.BuildQueryString(sendData);
  232. string reply = MakeRequest("POST", uri, reqString);
  233. if (String.IsNullOrEmpty(reply))
  234. return false;
  235. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  236. bool result = false;
  237. if (replyData != null && replyData.Count > 0)
  238. {
  239. if (replyData.ContainsKey("Result"))
  240. {
  241. if (Boolean.TryParse(replyData["Result"].ToString(), out result))
  242. m_log.DebugFormat("[ESTATE CONNECTOR]: PostRequest {0} returned {1}", uri, result);
  243. }
  244. }
  245. else
  246. m_log.DebugFormat("[ESTATE CONNECTOR]: PostRequest {0} received null or zero response", uri);
  247. return result;
  248. }
  249. /// <summary>
  250. /// Forbidden operation
  251. /// </summary>
  252. /// <returns></returns>
  253. public bool DeleteEstate(int estateID)
  254. {
  255. return false;
  256. }
  257. #endregion
  258. private string MakeRequest(string verb, string uri, string formdata)
  259. {
  260. string reply = string.Empty;
  261. try
  262. {
  263. reply = SynchronousRestFormsRequester.MakeRequest(verb, uri, formdata, 30, m_Auth);
  264. return reply;
  265. }
  266. catch (WebException e)
  267. {
  268. using (HttpWebResponse hwr = (HttpWebResponse)e.Response)
  269. {
  270. if (hwr != null)
  271. {
  272. if (hwr.StatusCode == HttpStatusCode.NotFound)
  273. {
  274. m_log.Error(string.Format("[ESTATE CONNECTOR]: Resource {0} not found ", uri));
  275. return reply;
  276. }
  277. if (hwr.StatusCode == HttpStatusCode.Unauthorized)
  278. m_log.Error(string.Format("[ESTATE CONNECTOR]: Web request {0} requires authentication ", uri));
  279. }
  280. else
  281. m_log.Error(string.Format(
  282. "[ESTATE CONNECTOR]: WebException for {0} {1} {2} {3}",
  283. verb, uri, formdata, e.Message));
  284. }
  285. }
  286. catch (Exception e)
  287. {
  288. m_log.DebugFormat("[ESTATE CONNECTOR]: Exception when contacting estate server at {0}: {1}", uri, e.Message);
  289. }
  290. return null;
  291. }
  292. }
  293. }