LocalGridServiceConnector.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 log4net;
  28. using Mono.Addins;
  29. using Nini.Config;
  30. using System;
  31. using System.Collections.Generic;
  32. using System.Reflection;
  33. using OpenSim.Framework;
  34. using OpenSim.Framework.Console;
  35. using OpenSim.Server.Base;
  36. using OpenSim.Region.Framework.Interfaces;
  37. using OpenSim.Region.Framework.Scenes;
  38. using OpenSim.Services.Interfaces;
  39. using GridRegion = OpenSim.Services.Interfaces.GridRegion;
  40. using OpenMetaverse;
  41. namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.Grid
  42. {
  43. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "LocalGridServicesConnector")]
  44. public class LocalGridServicesConnector : ISharedRegionModule, IGridService
  45. {
  46. private static readonly ILog m_log =
  47. LogManager.GetLogger(
  48. MethodBase.GetCurrentMethod().DeclaringType);
  49. private IGridService m_GridService;
  50. private Dictionary<UUID, RegionCache> m_LocalCache = new Dictionary<UUID, RegionCache>();
  51. private bool m_Enabled;
  52. public LocalGridServicesConnector()
  53. {
  54. m_log.Debug("[LOCAL GRID SERVICE CONNECTOR]: LocalGridServicesConnector no parms.");
  55. }
  56. public LocalGridServicesConnector(IConfigSource source)
  57. {
  58. m_log.Debug("[LOCAL GRID SERVICE CONNECTOR]: LocalGridServicesConnector instantiated directly.");
  59. InitialiseService(source);
  60. }
  61. #region ISharedRegionModule
  62. public Type ReplaceableInterface
  63. {
  64. get { return null; }
  65. }
  66. public string Name
  67. {
  68. get { return "LocalGridServicesConnector"; }
  69. }
  70. public void Initialise(IConfigSource source)
  71. {
  72. IConfig moduleConfig = source.Configs["Modules"];
  73. if (moduleConfig != null)
  74. {
  75. string name = moduleConfig.GetString("GridServices", "");
  76. if (name == Name)
  77. {
  78. InitialiseService(source);
  79. m_log.Info("[LOCAL GRID SERVICE CONNECTOR]: Local grid connector enabled");
  80. }
  81. }
  82. }
  83. private void InitialiseService(IConfigSource source)
  84. {
  85. IConfig assetConfig = source.Configs["GridService"];
  86. if (assetConfig == null)
  87. {
  88. m_log.Error("[LOCAL GRID SERVICE CONNECTOR]: GridService missing from OpenSim.ini");
  89. return;
  90. }
  91. string serviceDll = assetConfig.GetString("LocalServiceModule",
  92. String.Empty);
  93. if (serviceDll == String.Empty)
  94. {
  95. m_log.Error("[LOCAL GRID SERVICE CONNECTOR]: No LocalServiceModule named in section GridService");
  96. return;
  97. }
  98. Object[] args = new Object[] { source };
  99. m_GridService =
  100. ServerUtils.LoadPlugin<IGridService>(serviceDll,
  101. args);
  102. if (m_GridService == null)
  103. {
  104. m_log.Error("[LOCAL GRID SERVICE CONNECTOR]: Can't load grid service");
  105. return;
  106. }
  107. m_Enabled = true;
  108. }
  109. public void PostInitialise()
  110. {
  111. // FIXME: We will still add this command even if we aren't enabled since RemoteGridServiceConnector
  112. // will have instantiated us directly.
  113. MainConsole.Instance.Commands.AddCommand("Regions", false, "show neighbours",
  114. "show neighbours",
  115. "Shows the local regions' neighbours", HandleShowNeighboursCommand);
  116. }
  117. public void Close()
  118. {
  119. }
  120. public void AddRegion(Scene scene)
  121. {
  122. if (!m_Enabled)
  123. return;
  124. scene.RegisterModuleInterface<IGridService>(this);
  125. lock (m_LocalCache)
  126. {
  127. if (m_LocalCache.ContainsKey(scene.RegionInfo.RegionID))
  128. m_log.ErrorFormat("[LOCAL GRID SERVICE CONNECTOR]: simulator seems to have more than one region with the same UUID. Please correct this!");
  129. else
  130. m_LocalCache.Add(scene.RegionInfo.RegionID, new RegionCache(scene));
  131. }
  132. }
  133. public void RemoveRegion(Scene scene)
  134. {
  135. if (!m_Enabled)
  136. return;
  137. lock (m_LocalCache)
  138. {
  139. m_LocalCache[scene.RegionInfo.RegionID].Clear();
  140. m_LocalCache.Remove(scene.RegionInfo.RegionID);
  141. }
  142. }
  143. public void RegionLoaded(Scene scene)
  144. {
  145. }
  146. #endregion
  147. #region IGridService
  148. public string RegisterRegion(UUID scopeID, GridRegion regionInfo)
  149. {
  150. return m_GridService.RegisterRegion(scopeID, regionInfo);
  151. }
  152. public bool DeregisterRegion(UUID regionID)
  153. {
  154. return m_GridService.DeregisterRegion(regionID);
  155. }
  156. public List<GridRegion> GetNeighbours(UUID scopeID, UUID regionID)
  157. {
  158. return m_GridService.GetNeighbours(scopeID, regionID);
  159. }
  160. public GridRegion GetRegionByUUID(UUID scopeID, UUID regionID)
  161. {
  162. return m_GridService.GetRegionByUUID(scopeID, regionID);
  163. }
  164. public GridRegion GetRegionByPosition(UUID scopeID, int x, int y)
  165. {
  166. GridRegion region = null;
  167. // First see if it's a neighbour, even if it isn't on this sim.
  168. // Neighbour data is cached in memory, so this is fast
  169. lock (m_LocalCache)
  170. {
  171. foreach (RegionCache rcache in m_LocalCache.Values)
  172. {
  173. region = rcache.GetRegionByPosition(x, y);
  174. if (region != null)
  175. {
  176. return region;
  177. }
  178. }
  179. }
  180. // Then try on this sim (may be a lookup in DB if this is using MySql).
  181. return m_GridService.GetRegionByPosition(scopeID, x, y);
  182. }
  183. public GridRegion GetRegionByName(UUID scopeID, string regionName)
  184. {
  185. return m_GridService.GetRegionByName(scopeID, regionName);
  186. }
  187. public List<GridRegion> GetRegionsByName(UUID scopeID, string name, int maxNumber)
  188. {
  189. return m_GridService.GetRegionsByName(scopeID, name, maxNumber);
  190. }
  191. public List<GridRegion> GetRegionRange(UUID scopeID, int xmin, int xmax, int ymin, int ymax)
  192. {
  193. return m_GridService.GetRegionRange(scopeID, xmin, xmax, ymin, ymax);
  194. }
  195. public List<GridRegion> GetDefaultRegions(UUID scopeID)
  196. {
  197. return m_GridService.GetDefaultRegions(scopeID);
  198. }
  199. public List<GridRegion> GetFallbackRegions(UUID scopeID, int x, int y)
  200. {
  201. return m_GridService.GetFallbackRegions(scopeID, x, y);
  202. }
  203. public List<GridRegion> GetHyperlinks(UUID scopeID)
  204. {
  205. return m_GridService.GetHyperlinks(scopeID);
  206. }
  207. public int GetRegionFlags(UUID scopeID, UUID regionID)
  208. {
  209. return m_GridService.GetRegionFlags(scopeID, regionID);
  210. }
  211. #endregion
  212. public void HandleShowNeighboursCommand(string module, string[] cmdparams)
  213. {
  214. System.Text.StringBuilder caps = new System.Text.StringBuilder();
  215. lock (m_LocalCache)
  216. {
  217. foreach (KeyValuePair<UUID, RegionCache> kvp in m_LocalCache)
  218. {
  219. caps.AppendFormat("*** Neighbours of {0} ({1}) ***\n", kvp.Value.RegionName, kvp.Key);
  220. List<GridRegion> regions = kvp.Value.GetNeighbours();
  221. foreach (GridRegion r in regions)
  222. caps.AppendFormat(" {0} @ {1}-{2}\n", r.RegionName, r.RegionLocX / Constants.RegionSize, r.RegionLocY / Constants.RegionSize);
  223. }
  224. }
  225. MainConsole.Instance.Output(caps.ToString());
  226. }
  227. }
  228. }