GridService.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 Nini.Config;
  32. using log4net;
  33. using OpenSim.Framework;
  34. using OpenSim.Framework.Console;
  35. using OpenSim.Data;
  36. using OpenSim.Services.Interfaces;
  37. using GridRegion = OpenSim.Services.Interfaces.GridRegion;
  38. using OpenMetaverse;
  39. namespace OpenSim.Services.GridService
  40. {
  41. public class GridService : GridServiceBase, IGridService
  42. {
  43. private static readonly ILog m_log =
  44. LogManager.GetLogger(
  45. MethodBase.GetCurrentMethod().DeclaringType);
  46. public GridService(IConfigSource config)
  47. : base(config)
  48. {
  49. m_log.DebugFormat("[GRID SERVICE]: Starting...");
  50. }
  51. #region IGridService
  52. public bool RegisterRegion(UUID scopeID, GridRegion regionInfos)
  53. {
  54. // This needs better sanity testing. What if regionInfo is registering in
  55. // overlapping coords?
  56. RegionData region = m_Database.Get(regionInfos.RegionLocX, regionInfos.RegionLocY, scopeID);
  57. if ((region != null) && (region.RegionID != regionInfos.RegionID))
  58. {
  59. m_log.WarnFormat("[GRID SERVICE]: Region {0} tried to register in coordinates {1}, {2} which are already in use in scope {3}.",
  60. regionInfos.RegionID, regionInfos.RegionLocX, regionInfos.RegionLocY, scopeID);
  61. return false;
  62. }
  63. if ((region != null) && (region.RegionID == regionInfos.RegionID) &&
  64. ((region.posX != regionInfos.RegionLocX) || (region.posY != regionInfos.RegionLocY)))
  65. {
  66. // Region reregistering in other coordinates. Delete the old entry
  67. m_log.DebugFormat("[GRID SERVICE]: Region {0} ({1}) was previously registered at {2}-{3}. Deleting old entry.",
  68. regionInfos.RegionName, regionInfos.RegionID, regionInfos.RegionLocX, regionInfos.RegionLocY);
  69. try
  70. {
  71. m_Database.Delete(regionInfos.RegionID);
  72. }
  73. catch (Exception e)
  74. {
  75. m_log.DebugFormat("[GRID SERVICE]: Database exception: {0}", e);
  76. }
  77. }
  78. // Everything is ok, let's register
  79. RegionData rdata = RegionInfo2RegionData(regionInfos);
  80. rdata.ScopeID = scopeID;
  81. try
  82. {
  83. m_Database.Store(rdata);
  84. }
  85. catch (Exception e)
  86. {
  87. m_log.DebugFormat("[GRID SERVICE]: Database exception: {0}", e);
  88. }
  89. m_log.DebugFormat("[GRID SERVICE]: Region {0} ({1}) registered successfully at {2}-{3}",
  90. regionInfos.RegionName, regionInfos.RegionID, regionInfos.RegionLocX, regionInfos.RegionLocY);
  91. return true;
  92. }
  93. public bool DeregisterRegion(UUID regionID)
  94. {
  95. m_log.DebugFormat("[GRID SERVICE]: Region {0} deregistered", regionID);
  96. return m_Database.Delete(regionID);
  97. }
  98. public List<GridRegion> GetNeighbours(UUID scopeID, UUID regionID)
  99. {
  100. List<GridRegion> rinfos = new List<GridRegion>();
  101. RegionData region = m_Database.Get(regionID, scopeID);
  102. if (region != null)
  103. {
  104. // Not really? Maybe?
  105. List<RegionData> rdatas = m_Database.Get(region.posX - (int)Constants.RegionSize, region.posY - (int)Constants.RegionSize,
  106. region.posX + (int)Constants.RegionSize, region.posY + (int)Constants.RegionSize, scopeID);
  107. foreach (RegionData rdata in rdatas)
  108. if (rdata.RegionID != regionID)
  109. rinfos.Add(RegionData2RegionInfo(rdata));
  110. }
  111. return rinfos;
  112. }
  113. public GridRegion GetRegionByUUID(UUID scopeID, UUID regionID)
  114. {
  115. RegionData rdata = m_Database.Get(regionID, scopeID);
  116. if (rdata != null)
  117. return RegionData2RegionInfo(rdata);
  118. return null;
  119. }
  120. public GridRegion GetRegionByPosition(UUID scopeID, int x, int y)
  121. {
  122. int snapX = (int)(x / Constants.RegionSize) * (int)Constants.RegionSize;
  123. int snapY = (int)(y / Constants.RegionSize) * (int)Constants.RegionSize;
  124. RegionData rdata = m_Database.Get(snapX, snapY, scopeID);
  125. if (rdata != null)
  126. return RegionData2RegionInfo(rdata);
  127. return null;
  128. }
  129. public GridRegion GetRegionByName(UUID scopeID, string regionName)
  130. {
  131. List<RegionData> rdatas = m_Database.Get(regionName + "%", scopeID);
  132. if ((rdatas != null) && (rdatas.Count > 0))
  133. return RegionData2RegionInfo(rdatas[0]); // get the first
  134. return null;
  135. }
  136. public List<GridRegion> GetRegionsByName(UUID scopeID, string name, int maxNumber)
  137. {
  138. List<RegionData> rdatas = m_Database.Get("%" + name + "%", scopeID);
  139. int count = 0;
  140. List<GridRegion> rinfos = new List<GridRegion>();
  141. if (rdatas != null)
  142. {
  143. foreach (RegionData rdata in rdatas)
  144. {
  145. if (count++ < maxNumber)
  146. rinfos.Add(RegionData2RegionInfo(rdata));
  147. }
  148. }
  149. return rinfos;
  150. }
  151. public List<GridRegion> GetRegionRange(UUID scopeID, int xmin, int xmax, int ymin, int ymax)
  152. {
  153. int xminSnap = (int)(xmin / Constants.RegionSize) * (int)Constants.RegionSize;
  154. int xmaxSnap = (int)(xmax / Constants.RegionSize) * (int)Constants.RegionSize;
  155. int yminSnap = (int)(ymin / Constants.RegionSize) * (int)Constants.RegionSize;
  156. int ymaxSnap = (int)(ymax / Constants.RegionSize) * (int)Constants.RegionSize;
  157. List<RegionData> rdatas = m_Database.Get(xminSnap, yminSnap, xmaxSnap, ymaxSnap, scopeID);
  158. List<GridRegion> rinfos = new List<GridRegion>();
  159. foreach (RegionData rdata in rdatas)
  160. rinfos.Add(RegionData2RegionInfo(rdata));
  161. return rinfos;
  162. }
  163. #endregion
  164. #region Data structure conversions
  165. protected RegionData RegionInfo2RegionData(GridRegion rinfo)
  166. {
  167. RegionData rdata = new RegionData();
  168. rdata.posX = (int)rinfo.RegionLocX;
  169. rdata.posY = (int)rinfo.RegionLocY;
  170. rdata.RegionID = rinfo.RegionID;
  171. rdata.RegionName = rinfo.RegionName;
  172. rdata.Data = rinfo.ToKeyValuePairs();
  173. rdata.Data["regionHandle"] = Utils.UIntsToLong((uint)rdata.posX, (uint)rdata.posY);
  174. rdata.Data["owner_uuid"] = rinfo.EstateOwner.ToString();
  175. return rdata;
  176. }
  177. protected GridRegion RegionData2RegionInfo(RegionData rdata)
  178. {
  179. GridRegion rinfo = new GridRegion(rdata.Data);
  180. rinfo.RegionLocX = rdata.posX;
  181. rinfo.RegionLocY = rdata.posY;
  182. rinfo.RegionID = rdata.RegionID;
  183. rinfo.RegionName = rdata.RegionName;
  184. rinfo.ScopeID = rdata.ScopeID;
  185. return rinfo;
  186. }
  187. #endregion
  188. }
  189. }