RemoteGridServiceConnector.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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 System;
  30. using System.Collections;
  31. using System.Collections.Generic;
  32. using System.Reflection;
  33. using Nini.Config;
  34. using OpenMetaverse;
  35. using OpenSim.Framework;
  36. using OpenSim.Services.Connectors;
  37. using OpenSim.Region.Framework.Interfaces;
  38. using OpenSim.Region.Framework.Scenes;
  39. using OpenSim.Server.Base;
  40. using OpenSim.Services.Interfaces;
  41. using GridRegion = OpenSim.Services.Interfaces.GridRegion;
  42. namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.Grid
  43. {
  44. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "RemoteGridServicesConnector")]
  45. public class RemoteGridServicesConnector : ISharedRegionModule, IGridService
  46. {
  47. private static readonly ILog m_log =
  48. LogManager.GetLogger(
  49. MethodBase.GetCurrentMethod().DeclaringType);
  50. private bool m_Enabled = false;
  51. private string m_ThisGatekeeperURI = string.Empty;
  52. private string m_ThisGatekeeperHost = string.Empty;
  53. private string m_ThisGatekeeperIP = string.Empty;
  54. private HashSet<string> m_ThisGateKeeperAlias = new HashSet<string>();
  55. private IGridService m_LocalGridService;
  56. private IGridService m_RemoteGridService;
  57. private RegionInfoCache m_RegionInfoCache;
  58. public RemoteGridServicesConnector()
  59. {
  60. }
  61. public RemoteGridServicesConnector(IConfigSource source)
  62. {
  63. InitialiseServices(source);
  64. }
  65. #region ISharedRegionmodule
  66. public Type ReplaceableInterface
  67. {
  68. get { return null; }
  69. }
  70. public string Name
  71. {
  72. get { return "RemoteGridServicesConnector"; }
  73. }
  74. public void Initialise(IConfigSource source)
  75. {
  76. IConfig moduleConfig = source.Configs["Modules"];
  77. if (moduleConfig != null)
  78. {
  79. string name = moduleConfig.GetString("GridServices", "");
  80. if (name == Name)
  81. {
  82. if(InitialiseServices(source))
  83. {
  84. m_Enabled = true;
  85. m_log.Info("[REMOTE GRID CONNECTOR]: Remote grid enabled");
  86. }
  87. }
  88. }
  89. }
  90. private bool InitialiseServices(IConfigSource source)
  91. {
  92. IConfig gridConfig = source.Configs["GridService"];
  93. if (gridConfig == null)
  94. {
  95. m_log.Error("[REMOTE GRID CONNECTOR]: GridService missing from OpenSim.ini");
  96. return false;
  97. }
  98. string networkConnector = gridConfig.GetString("NetworkConnector", string.Empty);
  99. if (networkConnector == string.Empty)
  100. {
  101. m_log.Error("[REMOTE GRID CONNECTOR]: Please specify a network connector under [GridService]");
  102. return false;
  103. }
  104. Object[] args = new Object[] { source };
  105. m_RemoteGridService = ServerUtils.LoadPlugin<IGridService>(networkConnector, args);
  106. m_LocalGridService = new LocalGridServicesConnector(source, m_RegionInfoCache);
  107. if (m_LocalGridService == null)
  108. {
  109. m_log.Error("[REMOTE GRID CONNECTOR]: failed to load local connector");
  110. return false;
  111. }
  112. if(m_RegionInfoCache == null)
  113. m_RegionInfoCache = new RegionInfoCache();
  114. m_ThisGatekeeperURI = Util.GetConfigVarFromSections<string>(source, "GatekeeperURI",
  115. new string[] { "Startup", "Hypergrid", "GridService" }, String.Empty);
  116. // Legacy. Remove soon!
  117. m_ThisGatekeeperURI = gridConfig.GetString("Gatekeeper", m_ThisGatekeeperURI);
  118. Util.checkServiceURI(m_ThisGatekeeperURI, out m_ThisGatekeeperURI, out m_ThisGatekeeperHost, out m_ThisGatekeeperIP);
  119. string gatekeeperURIAlias = Util.GetConfigVarFromSections<string>(source, "GatekeeperURIAlias",
  120. new string[] { "Startup", "Hypergrid", "GridService" }, String.Empty);
  121. if (!string.IsNullOrWhiteSpace(gatekeeperURIAlias))
  122. {
  123. string[] alias = gatekeeperURIAlias.Split(',');
  124. for (int i = 0; i < alias.Length; ++i)
  125. {
  126. if (!string.IsNullOrWhiteSpace(alias[i]))
  127. m_ThisGateKeeperAlias.Add(alias[i].ToLower());
  128. }
  129. }
  130. return true;
  131. }
  132. public void PostInitialise()
  133. {
  134. if (m_Enabled)
  135. ((ISharedRegionModule)m_LocalGridService).PostInitialise();
  136. }
  137. public void Close()
  138. {
  139. }
  140. public void AddRegion(Scene scene)
  141. {
  142. if (m_Enabled)
  143. {
  144. scene.RegisterModuleInterface<IGridService>(this);
  145. ((ISharedRegionModule)m_LocalGridService).AddRegion(scene);
  146. }
  147. }
  148. public void RemoveRegion(Scene scene)
  149. {
  150. if (m_Enabled)
  151. ((ISharedRegionModule)m_LocalGridService).RemoveRegion(scene);
  152. }
  153. public void RegionLoaded(Scene scene)
  154. {
  155. }
  156. #endregion
  157. #region IGridService
  158. public string RegisterRegion(UUID scopeID, GridRegion regionInfo)
  159. {
  160. string msg = m_LocalGridService.RegisterRegion(scopeID, regionInfo);
  161. if (msg == String.Empty)
  162. return m_RemoteGridService.RegisterRegion(scopeID, regionInfo);
  163. return msg;
  164. }
  165. public bool DeregisterRegion(UUID regionID)
  166. {
  167. if (m_LocalGridService.DeregisterRegion(regionID))
  168. return m_RemoteGridService.DeregisterRegion(regionID);
  169. return false;
  170. }
  171. public List<GridRegion> GetNeighbours(UUID scopeID, UUID regionID)
  172. {
  173. return m_RemoteGridService.GetNeighbours(scopeID, regionID);
  174. }
  175. public GridRegion GetRegionByUUID(UUID scopeID, UUID regionID)
  176. {
  177. GridRegion rinfo = m_LocalGridService.GetRegionByUUID(scopeID, regionID);
  178. if (rinfo != null)
  179. return rinfo;
  180. rinfo = m_RemoteGridService.GetRegionByUUID(scopeID, regionID);
  181. m_RegionInfoCache.Cache(scopeID, rinfo);
  182. return rinfo;
  183. }
  184. // Get a region given its base world coordinates (in meters).
  185. // NOTE: this is NOT 'get a region by some point in the region'. The coordinate MUST
  186. // be the base coordinate of the region.
  187. // The coordinates are world coords (meters), NOT region units.
  188. public GridRegion GetRegionByPosition(UUID scopeID, int x, int y)
  189. {
  190. GridRegion rinfo = m_LocalGridService.GetRegionByPosition(scopeID, x, y);
  191. if (rinfo != null)
  192. {
  193. // m_log.DebugFormat("[REMOTE GRID CONNECTOR]: GetRegionByPosition. Found region {0} on local. Pos=<{1},{2}>, RegionHandle={3}",
  194. // rinfo.RegionName, rinfo.RegionCoordX, rinfo.RegionCoordY, rinfo.RegionHandle);
  195. return rinfo;
  196. }
  197. rinfo = m_RemoteGridService.GetRegionByPosition(scopeID, x, y);
  198. if (rinfo == null)
  199. {
  200. // uint regionX = Util.WorldToRegionLoc((uint)x);
  201. // uint regionY = Util.WorldToRegionLoc((uint)y);
  202. // m_log.WarnFormat("[REMOTE GRID CONNECTOR]: Requested region {0}-{1} not found", regionX, regionY);
  203. }
  204. else
  205. {
  206. m_RegionInfoCache.Cache(scopeID, rinfo);
  207. // m_log.DebugFormat("[REMOTE GRID CONNECTOR]: GetRegionByPosition. Added region {0} to the cache. Pos=<{1},{2}>, RegionHandle={3}",
  208. // rinfo.RegionName, rinfo.RegionCoordX, rinfo.RegionCoordY, rinfo.RegionHandle);
  209. }
  210. return rinfo;
  211. }
  212. public GridRegion GetRegionByName(UUID scopeID, string name)
  213. {
  214. GridRegion rinfo = m_LocalGridService.GetRegionByName(scopeID, name);
  215. if (rinfo != null)
  216. return rinfo;
  217. // HG urls should not get here, strip them
  218. // side effect is that local regions with same name as HG may also be found
  219. // this mb good or bad
  220. string regionName = name;
  221. if(name.Contains("."))
  222. {
  223. if(string.IsNullOrWhiteSpace(m_ThisGatekeeperIP))
  224. return rinfo; // no HG
  225. string regionURI = "";
  226. string regionHost = "";
  227. if (!Util.buildHGRegionURI(name, out regionURI, out regionHost, out regionName))
  228. return rinfo; // invalid
  229. if (!m_ThisGatekeeperHost.Equals(regionHost, StringComparison.InvariantCultureIgnoreCase)
  230. && !m_ThisGatekeeperIP.Equals(regionHost)
  231. && !m_ThisGateKeeperAlias.Contains(regionHost.ToLower()))
  232. return rinfo; // not local grid
  233. }
  234. if (String.IsNullOrEmpty(regionName))
  235. {
  236. rinfo = m_RemoteGridService.GetDefaultRegions(UUID.Zero)[0];
  237. if (rinfo == null)
  238. m_log.Warn("[REMOTE GRID CONNECTOR] returned null default region");
  239. else
  240. m_log.WarnFormat("[REMOTE GRID CONNECTOR] returned default region {0}", rinfo.RegionName);
  241. }
  242. else
  243. rinfo = m_RemoteGridService.GetRegionByName(scopeID, regionName);
  244. m_RegionInfoCache.Cache(scopeID, rinfo);
  245. return rinfo;
  246. }
  247. public List<GridRegion> GetRegionsByName(UUID scopeID, string name, int maxNumber)
  248. {
  249. List<GridRegion> rinfo = m_LocalGridService.GetRegionsByName(scopeID, name, maxNumber);
  250. //m_log.DebugFormat("[REMOTE GRID CONNECTOR]: Local GetRegionsByName {0} found {1} regions", name, rinfo.Count);
  251. // HG urls should not get here, strip them
  252. // side effect is that local regions with same name as HG may also be found
  253. // this mb good or bad
  254. string regionName = name;
  255. if(name.Contains("."))
  256. {
  257. if(string.IsNullOrWhiteSpace(m_ThisGatekeeperURI))
  258. return rinfo; // no HG
  259. string regionURI = "";
  260. string regionHost = "";
  261. if (!Util.buildHGRegionURI(name, out regionURI, out regionHost, out regionName))
  262. return rinfo; // invalid
  263. if (!m_ThisGatekeeperHost.Equals(regionHost, StringComparison.InvariantCultureIgnoreCase)
  264. && !m_ThisGatekeeperIP.Equals(regionHost)
  265. && !m_ThisGateKeeperAlias.Contains(regionHost.ToLower()))
  266. return rinfo; // not local grid
  267. }
  268. List<GridRegion> grinfo = null;
  269. if (String.IsNullOrEmpty(regionName))
  270. {
  271. List<GridRegion> grinfos = m_RemoteGridService.GetDefaultRegions(scopeID);
  272. if (grinfos == null || grinfos.Count == 0)
  273. m_log.Warn("[REMOTE GRID CONNECTOR] returned no default regions");
  274. else
  275. {
  276. m_log.WarnFormat("[REMOTE GRID CONNECTOR] returned default regions {0}, ...", grinfos[0].RegionName);
  277. // only return first
  278. grinfo = new List<GridRegion>(){grinfos[0]};
  279. }
  280. }
  281. else
  282. grinfo = m_RemoteGridService.GetRegionsByName(scopeID, regionName, maxNumber);
  283. if (grinfo != null)
  284. {
  285. //m_log.DebugFormat("[REMOTE GRID CONNECTOR]: Remote GetRegionsByName {0} found {1} regions", name, grinfo.Count);
  286. foreach (GridRegion r in grinfo)
  287. {
  288. m_RegionInfoCache.Cache(r);
  289. if (rinfo.Find(delegate(GridRegion gr) { return gr.RegionID == r.RegionID; }) == null)
  290. rinfo.Add(r);
  291. }
  292. }
  293. return rinfo;
  294. }
  295. public virtual List<GridRegion> GetRegionRange(UUID scopeID, int xmin, int xmax, int ymin, int ymax)
  296. {
  297. List<GridRegion> rinfo = m_LocalGridService.GetRegionRange(scopeID, xmin, xmax, ymin, ymax);
  298. //m_log.DebugFormat("[REMOTE GRID CONNECTOR]: Local GetRegionRange {0} found {1} regions", name, rinfo.Count);
  299. List<GridRegion> grinfo = m_RemoteGridService.GetRegionRange(scopeID, xmin, xmax, ymin, ymax);
  300. if (grinfo != null)
  301. {
  302. //m_log.DebugFormat("[REMOTE GRID CONNECTOR]: Remote GetRegionRange {0} found {1} regions", name, grinfo.Count);
  303. foreach (GridRegion r in grinfo)
  304. {
  305. m_RegionInfoCache.Cache(r);
  306. if (rinfo.Find(delegate(GridRegion gr) { return gr.RegionID == r.RegionID; }) == null)
  307. rinfo.Add(r);
  308. }
  309. }
  310. return rinfo;
  311. }
  312. public List<GridRegion> GetDefaultRegions(UUID scopeID)
  313. {
  314. List<GridRegion> rinfo = m_LocalGridService.GetDefaultRegions(scopeID);
  315. //m_log.DebugFormat("[REMOTE GRID CONNECTOR]: Local GetDefaultRegions {0} found {1} regions", name, rinfo.Count);
  316. List<GridRegion> grinfo = m_RemoteGridService.GetDefaultRegions(scopeID);
  317. if (grinfo != null)
  318. {
  319. //m_log.DebugFormat("[REMOTE GRID CONNECTOR]: Remote GetDefaultRegions {0} found {1} regions", name, grinfo.Count);
  320. foreach (GridRegion r in grinfo)
  321. {
  322. m_RegionInfoCache.Cache(r);
  323. if (rinfo.Find(delegate(GridRegion gr) { return gr.RegionID == r.RegionID; }) == null)
  324. rinfo.Add(r);
  325. }
  326. }
  327. return rinfo;
  328. }
  329. public List<GridRegion> GetDefaultHypergridRegions(UUID scopeID)
  330. {
  331. List<GridRegion> rinfo = m_LocalGridService.GetDefaultHypergridRegions(scopeID);
  332. //m_log.DebugFormat("[REMOTE GRID CONNECTOR]: Local GetDefaultHypergridRegions {0} found {1} regions", name, rinfo.Count);
  333. List<GridRegion> grinfo = m_RemoteGridService.GetDefaultHypergridRegions(scopeID);
  334. if (grinfo != null)
  335. {
  336. //m_log.DebugFormat("[REMOTE GRID CONNECTOR]: Remote GetDefaultHypergridRegions {0} found {1} regions", name, grinfo.Count);
  337. foreach (GridRegion r in grinfo)
  338. {
  339. m_RegionInfoCache.Cache(r);
  340. if (rinfo.Find(delegate(GridRegion gr) { return gr.RegionID == r.RegionID; }) == null)
  341. rinfo.Add(r);
  342. }
  343. }
  344. return rinfo;
  345. }
  346. public List<GridRegion> GetFallbackRegions(UUID scopeID, int x, int y)
  347. {
  348. List<GridRegion> rinfo = m_LocalGridService.GetFallbackRegions(scopeID, x, y);
  349. //m_log.DebugFormat("[REMOTE GRID CONNECTOR]: Local GetFallbackRegions {0} found {1} regions", name, rinfo.Count);
  350. List<GridRegion> grinfo = m_RemoteGridService.GetFallbackRegions(scopeID, x, y);
  351. if (grinfo != null)
  352. {
  353. //m_log.DebugFormat("[REMOTE GRID CONNECTOR]: Remote GetFallbackRegions {0} found {1} regions", name, grinfo.Count);
  354. foreach (GridRegion r in grinfo)
  355. {
  356. m_RegionInfoCache.Cache(r);
  357. if (rinfo.Find(delegate(GridRegion gr) { return gr.RegionID == r.RegionID; }) == null)
  358. rinfo.Add(r);
  359. }
  360. }
  361. return rinfo;
  362. }
  363. public List<GridRegion> GetHyperlinks(UUID scopeID)
  364. {
  365. List<GridRegion> rinfo = m_LocalGridService.GetHyperlinks(scopeID);
  366. //m_log.DebugFormat("[REMOTE GRID CONNECTOR]: Local GetHyperlinks {0} found {1} regions", name, rinfo.Count);
  367. List<GridRegion> grinfo = m_RemoteGridService.GetHyperlinks(scopeID);
  368. if (grinfo != null)
  369. {
  370. //m_log.DebugFormat("[REMOTE GRID CONNECTOR]: Remote GetHyperlinks {0} found {1} regions", name, grinfo.Count);
  371. foreach (GridRegion r in grinfo)
  372. {
  373. m_RegionInfoCache.Cache(r);
  374. if (rinfo.Find(delegate(GridRegion gr) { return gr.RegionID == r.RegionID; }) == null)
  375. rinfo.Add(r);
  376. }
  377. }
  378. return rinfo;
  379. }
  380. public int GetRegionFlags(UUID scopeID, UUID regionID)
  381. {
  382. int flags = m_LocalGridService.GetRegionFlags(scopeID, regionID);
  383. if (flags == -1)
  384. flags = m_RemoteGridService.GetRegionFlags(scopeID, regionID);
  385. return flags;
  386. }
  387. public Dictionary<string, object> GetExtraFeatures()
  388. {
  389. Dictionary<string, object> extraFeatures;
  390. extraFeatures = m_LocalGridService.GetExtraFeatures();
  391. if (extraFeatures.Count == 0)
  392. extraFeatures = m_RemoteGridService.GetExtraFeatures();
  393. return extraFeatures;
  394. }
  395. #endregion
  396. }
  397. }