LocalGridServiceConnector.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 static string LogHeader = "[LOCAL GRID SERVICE CONNECTOR]";
  50. private IGridService m_GridService;
  51. private RegionInfoCache m_RegionInfoCache;
  52. private HashSet<Scene> m_scenes = new HashSet<Scene>();
  53. private bool m_Enabled;
  54. public LocalGridServicesConnector()
  55. {
  56. m_log.DebugFormat("{0} LocalGridServicesConnector no parms.", LogHeader);
  57. }
  58. public LocalGridServicesConnector(IConfigSource source)
  59. {
  60. m_log.DebugFormat("{0} LocalGridServicesConnector instantiated directly.", LogHeader);
  61. InitialiseService(source, null);
  62. }
  63. public LocalGridServicesConnector(IConfigSource source, RegionInfoCache regionInfoCache)
  64. {
  65. m_log.DebugFormat("{0} LocalGridServicesConnector instantiated directly with cache.", LogHeader);
  66. InitialiseService(source, regionInfoCache);
  67. }
  68. #region ISharedRegionModule
  69. public Type ReplaceableInterface
  70. {
  71. get { return null; }
  72. }
  73. public string Name
  74. {
  75. get { return "LocalGridServicesConnector"; }
  76. }
  77. public void Initialise(IConfigSource source)
  78. {
  79. IConfig moduleConfig = source.Configs["Modules"];
  80. if (moduleConfig != null)
  81. {
  82. string name = moduleConfig.GetString("GridServices", "");
  83. if (name == Name)
  84. {
  85. if(InitialiseService(source, null))
  86. m_log.Info("[LOCAL GRID SERVICE CONNECTOR]: Local grid connector enabled");
  87. }
  88. }
  89. }
  90. private bool InitialiseService(IConfigSource source, RegionInfoCache ric)
  91. {
  92. if(ric == null && m_RegionInfoCache == null)
  93. m_RegionInfoCache = new RegionInfoCache();
  94. else
  95. m_RegionInfoCache = ric;
  96. IConfig config = source.Configs["GridService"];
  97. if (config == null)
  98. {
  99. m_log.Error("[LOCAL GRID SERVICE CONNECTOR]: GridService missing from OpenSim.ini");
  100. return false;
  101. }
  102. string serviceDll = config.GetString("LocalServiceModule", String.Empty);
  103. if (serviceDll == String.Empty)
  104. {
  105. m_log.Error("[LOCAL GRID SERVICE CONNECTOR]: No LocalServiceModule named in section GridService");
  106. return false;
  107. }
  108. Object[] args = new Object[] { source };
  109. m_GridService =
  110. ServerUtils.LoadPlugin<IGridService>(serviceDll,
  111. args);
  112. if (m_GridService == null)
  113. {
  114. m_log.Error("[LOCAL GRID SERVICE CONNECTOR]: Can't load grid service");
  115. return false;
  116. }
  117. m_Enabled = true;
  118. return true;
  119. }
  120. public void PostInitialise()
  121. {
  122. }
  123. public void Close()
  124. {
  125. }
  126. public void AddRegion(Scene scene)
  127. {
  128. if (!m_Enabled)
  129. return;
  130. lock(m_scenes)
  131. {
  132. if(!m_scenes.Contains(scene))
  133. m_scenes.Add(scene);
  134. }
  135. scene.RegisterModuleInterface<IGridService>(this);
  136. GridRegion r = new GridRegion(scene.RegionInfo);
  137. m_RegionInfoCache.CacheLocal(r);
  138. scene.EventManager.OnRegionUp += OnRegionUp;
  139. }
  140. public void RemoveRegion(Scene scene)
  141. {
  142. if (!m_Enabled)
  143. return;
  144. lock(m_scenes)
  145. {
  146. if(m_scenes.Contains(scene))
  147. m_scenes.Remove(scene);
  148. }
  149. m_RegionInfoCache.Remove(scene.RegionInfo.ScopeID, scene.RegionInfo.RegionHandle);
  150. scene.EventManager.OnRegionUp -= OnRegionUp;
  151. }
  152. public void RegionLoaded(Scene scene)
  153. {
  154. }
  155. #endregion
  156. #region IGridService
  157. private void OnRegionUp(GridRegion region)
  158. {
  159. // This shouldn't happen
  160. if (region == null)
  161. return;
  162. m_RegionInfoCache.CacheNearNeighbour(region.ScopeID, region);
  163. }
  164. public string RegisterRegion(UUID scopeID, GridRegion regionInfo)
  165. {
  166. return m_GridService.RegisterRegion(scopeID, regionInfo);
  167. }
  168. public bool DeregisterRegion(UUID regionID)
  169. {
  170. return m_GridService.DeregisterRegion(regionID);
  171. }
  172. public List<GridRegion> GetNeighbours(UUID scopeID, UUID regionID)
  173. {
  174. return m_GridService.GetNeighbours(scopeID, regionID);
  175. }
  176. public GridRegion GetRegionByUUID(UUID scopeID, UUID regionID)
  177. {
  178. bool inCache = false;
  179. GridRegion rinfo = m_RegionInfoCache.Get(scopeID,regionID,out inCache);
  180. if (inCache)
  181. return rinfo;
  182. rinfo = m_GridService.GetRegionByUUID(scopeID, regionID);
  183. if(rinfo != null)
  184. m_RegionInfoCache.Cache(scopeID, rinfo);
  185. return rinfo;
  186. }
  187. // Get a region given its base coordinates.
  188. // NOTE: this is NOT 'get a region by some point in the region'. The coordinate MUST
  189. // be the base coordinate of the region.
  190. public GridRegion GetRegionByPosition(UUID scopeID, int x, int y)
  191. {
  192. bool inCache = false;
  193. GridRegion rinfo = m_RegionInfoCache.Get(scopeID, (uint)x, (uint)y, out inCache);
  194. if (inCache)
  195. return rinfo;
  196. // Then try on this sim (may be a lookup in DB if this is using MySql).
  197. rinfo = m_GridService.GetRegionByPosition(scopeID, x, y);
  198. if(rinfo != null)
  199. m_RegionInfoCache.Cache(scopeID, rinfo);
  200. return rinfo;
  201. }
  202. public GridRegion GetRegionByName(UUID scopeID, string regionName)
  203. {
  204. bool inCache = false;
  205. GridRegion rinfo = m_RegionInfoCache.Get(scopeID, regionName, out inCache);
  206. if (inCache)
  207. return rinfo;
  208. rinfo = m_GridService.GetRegionByName(scopeID, regionName);
  209. if(rinfo != null)
  210. m_RegionInfoCache.Cache(scopeID, rinfo);
  211. return rinfo;
  212. }
  213. public List<GridRegion> GetRegionsByName(UUID scopeID, string name, int maxNumber)
  214. {
  215. return m_GridService.GetRegionsByName(scopeID, name, maxNumber);
  216. }
  217. public List<GridRegion> GetRegionRange(UUID scopeID, int xmin, int xmax, int ymin, int ymax)
  218. {
  219. return m_GridService.GetRegionRange(scopeID, xmin, xmax, ymin, ymax);
  220. }
  221. public List<GridRegion> GetDefaultRegions(UUID scopeID)
  222. {
  223. return m_GridService.GetDefaultRegions(scopeID);
  224. }
  225. public List<GridRegion> GetDefaultHypergridRegions(UUID scopeID)
  226. {
  227. return m_GridService.GetDefaultHypergridRegions(scopeID);
  228. }
  229. public List<GridRegion> GetFallbackRegions(UUID scopeID, int x, int y)
  230. {
  231. return m_GridService.GetFallbackRegions(scopeID, x, y);
  232. }
  233. public List<GridRegion> GetHyperlinks(UUID scopeID)
  234. {
  235. return m_GridService.GetHyperlinks(scopeID);
  236. }
  237. public int GetRegionFlags(UUID scopeID, UUID regionID)
  238. {
  239. return m_GridService.GetRegionFlags(scopeID, regionID);
  240. }
  241. public Dictionary<string, object> GetExtraFeatures()
  242. {
  243. return m_GridService.GetExtraFeatures();
  244. }
  245. #endregion
  246. }
  247. }