1
0

GridDBService.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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.Collections.Generic;
  30. using System.IO;
  31. using System.Reflection;
  32. using System.Xml;
  33. using log4net;
  34. using Nwc.XmlRpc;
  35. using OpenMetaverse;
  36. using OpenSim.Data;
  37. using OpenSim.Framework;
  38. using OpenSim.Framework.Communications;
  39. using OpenSim.Framework.Servers;
  40. namespace OpenSim.Grid.GridServer.Modules
  41. {
  42. public class GridDBService : IRegionProfileService
  43. {
  44. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  45. private List<IGridDataPlugin> _plugins = new List<IGridDataPlugin>();
  46. private List<ILogDataPlugin> _logplugins = new List<ILogDataPlugin>();
  47. /// <summary>
  48. /// Adds a list of grid and log data plugins, as described by
  49. /// `provider' and `connect', to `_plugins' and `_logplugins',
  50. /// respectively.
  51. /// </summary>
  52. /// <param name="provider">
  53. /// The filename of the inventory server plugin DLL.
  54. /// </param>
  55. /// <param name="connect">
  56. /// The connection string for the storage backend.
  57. /// </param>
  58. public void AddPlugin(string provider, string connect)
  59. {
  60. _plugins = DataPluginFactory.LoadDataPlugins<IGridDataPlugin>(provider, connect);
  61. _logplugins = DataPluginFactory.LoadDataPlugins<ILogDataPlugin>(provider, connect);
  62. }
  63. public int GetNumberOfPlugins()
  64. {
  65. return _plugins.Count;
  66. }
  67. /// <summary>
  68. /// Logs a piece of information to the database
  69. /// </summary>
  70. /// <param name="target">What you were operating on (in grid server, this will likely be the region UUIDs)</param>
  71. /// <param name="method">Which method is being called?</param>
  72. /// <param name="args">What arguments are being passed?</param>
  73. /// <param name="priority">How high priority is this? 1 = Max, 6 = Verbose</param>
  74. /// <param name="message">The message to log</param>
  75. private void logToDB(string target, string method, string args, int priority, string message)
  76. {
  77. foreach (ILogDataPlugin plugin in _logplugins)
  78. {
  79. try
  80. {
  81. plugin.saveLog("Gridserver", target, method, args, priority, message);
  82. }
  83. catch (Exception)
  84. {
  85. m_log.Warn("[storage]: Unable to write log via " + plugin.Name);
  86. }
  87. }
  88. }
  89. /// <summary>
  90. /// Returns a region by argument
  91. /// </summary>
  92. /// <param name="uuid">A UUID key of the region to return</param>
  93. /// <returns>A SimProfileData for the region</returns>
  94. public RegionProfileData GetRegion(UUID uuid)
  95. {
  96. foreach (IGridDataPlugin plugin in _plugins)
  97. {
  98. try
  99. {
  100. return plugin.GetProfileByUUID(uuid);
  101. }
  102. catch (Exception e)
  103. {
  104. m_log.Warn("[storage]: GetRegion - " + e.Message);
  105. }
  106. }
  107. return null;
  108. }
  109. /// <summary>
  110. /// Returns a region by argument
  111. /// </summary>
  112. /// <param name="uuid">A regionHandle of the region to return</param>
  113. /// <returns>A SimProfileData for the region</returns>
  114. public RegionProfileData GetRegion(ulong handle)
  115. {
  116. foreach (IGridDataPlugin plugin in _plugins)
  117. {
  118. try
  119. {
  120. return plugin.GetProfileByHandle(handle);
  121. }
  122. catch (Exception ex)
  123. {
  124. m_log.Debug("[storage]: " + ex.Message);
  125. m_log.Warn("[storage]: Unable to find region " + handle.ToString() + " via " + plugin.Name);
  126. }
  127. }
  128. return null;
  129. }
  130. /// <summary>
  131. /// Returns a region by argument
  132. /// </summary>
  133. /// <param name="regionName">A partial regionName of the region to return</param>
  134. /// <returns>A SimProfileData for the region</returns>
  135. public RegionProfileData GetRegion(string regionName)
  136. {
  137. foreach (IGridDataPlugin plugin in _plugins)
  138. {
  139. try
  140. {
  141. return plugin.GetProfileByString(regionName);
  142. }
  143. catch
  144. {
  145. m_log.Warn("[storage]: Unable to find region " + regionName + " via " + plugin.Name);
  146. }
  147. }
  148. return null;
  149. }
  150. public List<RegionProfileData> GetRegions(uint xmin, uint ymin, uint xmax, uint ymax)
  151. {
  152. List<RegionProfileData> regions = new List<RegionProfileData>();
  153. foreach (IGridDataPlugin plugin in _plugins)
  154. {
  155. try
  156. {
  157. regions.AddRange(plugin.GetProfilesInRange(xmin, ymin, xmax, ymax));
  158. }
  159. catch
  160. {
  161. m_log.Warn("[storage]: Unable to query regionblock via " + plugin.Name);
  162. }
  163. }
  164. return regions;
  165. }
  166. public List<RegionProfileData> GetRegions(string name, int maxNum)
  167. {
  168. List<RegionProfileData> regions = new List<RegionProfileData>();
  169. foreach (IGridDataPlugin plugin in _plugins)
  170. {
  171. try
  172. {
  173. int num = maxNum - regions.Count;
  174. List<RegionProfileData> profiles = plugin.GetRegionsByName(name, (uint)num);
  175. if (profiles != null) regions.AddRange(profiles);
  176. }
  177. catch
  178. {
  179. m_log.Warn("[storage]: Unable to query regionblock via " + plugin.Name);
  180. }
  181. }
  182. return regions;
  183. }
  184. public DataResponse AddUpdateRegion(RegionProfileData sim, RegionProfileData existingSim)
  185. {
  186. DataResponse insertResponse = DataResponse.RESPONSE_ERROR;
  187. foreach (IGridDataPlugin plugin in _plugins)
  188. {
  189. try
  190. {
  191. if (existingSim == null)
  192. {
  193. insertResponse = plugin.AddProfile(sim);
  194. }
  195. else
  196. {
  197. insertResponse = plugin.UpdateProfile(sim);
  198. }
  199. }
  200. catch (Exception e)
  201. {
  202. m_log.Warn("[LOGIN END]: " +
  203. "Unable to login region " + sim.ToString() + " via " + plugin.Name);
  204. m_log.Warn("[LOGIN END]: " + e.ToString());
  205. }
  206. }
  207. return insertResponse;
  208. }
  209. public DataResponse DeleteRegion(string uuid)
  210. {
  211. DataResponse insertResponse = DataResponse.RESPONSE_ERROR;
  212. foreach (IGridDataPlugin plugin in _plugins)
  213. {
  214. //OpenSim.Data.MySQL.MySQLGridData dbengine = new OpenSim.Data.MySQL.MySQLGridData();
  215. try
  216. {
  217. //Nice are we not using multiple databases?
  218. //MySQLGridData mysqldata = (MySQLGridData)(plugin);
  219. //DataResponse insertResponse = mysqldata.DeleteProfile(TheSim);
  220. insertResponse = plugin.DeleteProfile(uuid);
  221. }
  222. catch (Exception)
  223. {
  224. m_log.Error("storage Unable to delete region " + uuid + " via " + plugin.Name);
  225. //MainLog.Instance.Warn("storage", e.ToString());
  226. insertResponse = DataResponse.RESPONSE_ERROR;
  227. }
  228. }
  229. return insertResponse;
  230. }
  231. public string CheckReservations(RegionProfileData theSim, XmlNode authkeynode)
  232. {
  233. foreach (IGridDataPlugin plugin in _plugins)
  234. {
  235. try
  236. {
  237. //Check reservations
  238. ReservationData reserveData =
  239. plugin.GetReservationAtPoint(theSim.regionLocX, theSim.regionLocY);
  240. if ((reserveData != null && reserveData.gridRecvKey == theSim.regionRecvKey) ||
  241. (reserveData == null && authkeynode.InnerText != theSim.regionRecvKey))
  242. {
  243. plugin.AddProfile(theSim);
  244. m_log.Info("[grid]: New sim added to grid (" + theSim.regionName + ")");
  245. logToDB(theSim.ToString(), "RestSetSimMethod", String.Empty, 5,
  246. "Region successfully updated and connected to grid.");
  247. }
  248. else
  249. {
  250. m_log.Warn("[grid]: " +
  251. "Unable to update region (RestSetSimMethod): Incorrect reservation auth key.");
  252. // Wanted: " + reserveData.gridRecvKey + ", Got: " + theSim.regionRecvKey + ".");
  253. return "Unable to update region (RestSetSimMethod): Incorrect auth key.";
  254. }
  255. }
  256. catch (Exception e)
  257. {
  258. m_log.Warn("[GRID]: GetRegionPlugin Handle " + plugin.Name + " unable to add new sim: " +
  259. e.ToString());
  260. }
  261. }
  262. return "OK";
  263. }
  264. }
  265. }