SimianGridServiceConnector.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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.Collections.Specialized;
  30. using System.Net;
  31. using System.Reflection;
  32. using log4net;
  33. using Mono.Addins;
  34. using Nini.Config;
  35. using OpenSim.Framework;
  36. using OpenSim.Framework.Servers.HttpServer;
  37. using OpenSim.Region.Framework.Interfaces;
  38. using OpenSim.Region.Framework.Scenes;
  39. using OpenSim.Services.Interfaces;
  40. using OpenSim.Server.Base;
  41. using OpenMetaverse;
  42. using OpenMetaverse.StructuredData;
  43. using GridRegion = OpenSim.Services.Interfaces.GridRegion;
  44. namespace OpenSim.Services.Connectors.SimianGrid
  45. {
  46. /// <summary>
  47. /// Connects region registration and neighbor lookups to the SimianGrid
  48. /// backend
  49. /// </summary>
  50. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule")]
  51. public class SimianGridServiceConnector : IGridService, ISharedRegionModule
  52. {
  53. private static readonly ILog m_log =
  54. LogManager.GetLogger(
  55. MethodBase.GetCurrentMethod().DeclaringType);
  56. private string m_serverUrl = String.Empty;
  57. #region ISharedRegionModule
  58. public Type ReplaceableInterface { get { return null; } }
  59. public void RegionLoaded(Scene scene) { }
  60. public void PostInitialise() { }
  61. public void Close() { }
  62. public SimianGridServiceConnector() { }
  63. public string Name { get { return "SimianGridServiceConnector"; } }
  64. public void AddRegion(Scene scene) { if (!String.IsNullOrEmpty(m_serverUrl)) { scene.RegisterModuleInterface<IGridService>(this); } }
  65. public void RemoveRegion(Scene scene) { if (!String.IsNullOrEmpty(m_serverUrl)) { scene.UnregisterModuleInterface<IGridService>(this); } }
  66. #endregion ISharedRegionModule
  67. public SimianGridServiceConnector(IConfigSource source)
  68. {
  69. Initialise(source);
  70. }
  71. public void Initialise(IConfigSource source)
  72. {
  73. if (Simian.IsSimianEnabled(source, "GridServices", this.Name))
  74. {
  75. IConfig gridConfig = source.Configs["GridService"];
  76. if (gridConfig == null)
  77. {
  78. m_log.Error("[SIMIAN GRID CONNECTOR]: GridService missing from OpenSim.ini");
  79. throw new Exception("Grid connector init error");
  80. }
  81. string serviceUrl = gridConfig.GetString("GridServerURI");
  82. if (String.IsNullOrEmpty(serviceUrl))
  83. {
  84. m_log.Error("[SIMIAN GRID CONNECTOR]: No Server URI named in section GridService");
  85. throw new Exception("Grid connector init error");
  86. }
  87. m_serverUrl = serviceUrl;
  88. }
  89. }
  90. #region IGridService
  91. public string RegisterRegion(UUID scopeID, GridRegion regionInfo)
  92. {
  93. Vector3d minPosition = new Vector3d(regionInfo.RegionLocX, regionInfo.RegionLocY, 0.0);
  94. Vector3d maxPosition = minPosition + new Vector3d(Constants.RegionSize, Constants.RegionSize, 4096.0);
  95. string httpAddress = "http://" + regionInfo.ExternalHostName + ":" + regionInfo.HttpPort + "/";
  96. OSDMap extraData = new OSDMap
  97. {
  98. { "ServerURI", OSD.FromString(regionInfo.ServerURI) },
  99. { "InternalAddress", OSD.FromString(regionInfo.InternalEndPoint.Address.ToString()) },
  100. { "InternalPort", OSD.FromInteger(regionInfo.InternalEndPoint.Port) },
  101. { "ExternalAddress", OSD.FromString(regionInfo.ExternalEndPoint.Address.ToString()) },
  102. { "ExternalPort", OSD.FromInteger(regionInfo.ExternalEndPoint.Port) },
  103. { "MapTexture", OSD.FromUUID(regionInfo.TerrainImage) },
  104. { "Access", OSD.FromInteger(regionInfo.Access) },
  105. { "RegionSecret", OSD.FromString(regionInfo.RegionSecret) },
  106. { "EstateOwner", OSD.FromUUID(regionInfo.EstateOwner) },
  107. { "Token", OSD.FromString(regionInfo.Token) }
  108. };
  109. NameValueCollection requestArgs = new NameValueCollection
  110. {
  111. { "RequestMethod", "AddScene" },
  112. { "SceneID", regionInfo.RegionID.ToString() },
  113. { "Name", regionInfo.RegionName },
  114. { "MinPosition", minPosition.ToString() },
  115. { "MaxPosition", maxPosition.ToString() },
  116. { "Address", httpAddress },
  117. { "Enabled", "1" },
  118. { "ExtraData", OSDParser.SerializeJsonString(extraData) }
  119. };
  120. OSDMap response = WebUtil.PostToService(m_serverUrl, requestArgs);
  121. if (response["Success"].AsBoolean())
  122. return String.Empty;
  123. else
  124. return "Region registration for " + regionInfo.RegionName + " failed: " + response["Message"].AsString();
  125. }
  126. public bool DeregisterRegion(UUID regionID)
  127. {
  128. NameValueCollection requestArgs = new NameValueCollection
  129. {
  130. { "RequestMethod", "AddScene" },
  131. { "SceneID", regionID.ToString() },
  132. { "Enabled", "0" }
  133. };
  134. OSDMap response = WebUtil.PostToService(m_serverUrl, requestArgs);
  135. bool success = response["Success"].AsBoolean();
  136. if (!success)
  137. m_log.Warn("[SIMIAN GRID CONNECTOR]: Region deregistration for " + regionID + " failed: " + response["Message"].AsString());
  138. return success;
  139. }
  140. public List<GridRegion> GetNeighbours(UUID scopeID, UUID regionID)
  141. {
  142. const int NEIGHBOR_RADIUS = 128;
  143. GridRegion region = GetRegionByUUID(scopeID, regionID);
  144. if (region != null)
  145. {
  146. List<GridRegion> regions = GetRegionRange(scopeID,
  147. region.RegionLocX - NEIGHBOR_RADIUS, region.RegionLocX + (int)Constants.RegionSize + NEIGHBOR_RADIUS,
  148. region.RegionLocY - NEIGHBOR_RADIUS, region.RegionLocY + (int)Constants.RegionSize + NEIGHBOR_RADIUS);
  149. for (int i = 0; i < regions.Count; i++)
  150. {
  151. if (regions[i].RegionID == regionID)
  152. {
  153. regions.RemoveAt(i);
  154. break;
  155. }
  156. }
  157. m_log.Debug("[SIMIAN GRID CONNECTOR]: Found " + regions.Count + " neighbors for region " + regionID);
  158. return regions;
  159. }
  160. return new List<GridRegion>(0);
  161. }
  162. public GridRegion GetRegionByUUID(UUID scopeID, UUID regionID)
  163. {
  164. NameValueCollection requestArgs = new NameValueCollection
  165. {
  166. { "RequestMethod", "GetScene" },
  167. { "SceneID", regionID.ToString() }
  168. };
  169. OSDMap response = WebUtil.PostToService(m_serverUrl, requestArgs);
  170. if (response["Success"].AsBoolean())
  171. {
  172. return ResponseToGridRegion(response);
  173. }
  174. else
  175. {
  176. m_log.Warn("[SIMIAN GRID CONNECTOR]: Grid service did not find a match for region " + regionID);
  177. return null;
  178. }
  179. }
  180. public GridRegion GetRegionByPosition(UUID scopeID, int x, int y)
  181. {
  182. // Go one meter in from the requested x/y coords to avoid requesting a position
  183. // that falls on the border of two sims
  184. Vector3d position = new Vector3d(x + 1, y + 1, 0.0);
  185. NameValueCollection requestArgs = new NameValueCollection
  186. {
  187. { "RequestMethod", "GetScene" },
  188. { "Position", position.ToString() },
  189. { "Enabled", "1" }
  190. };
  191. OSDMap response = WebUtil.PostToService(m_serverUrl, requestArgs);
  192. if (response["Success"].AsBoolean())
  193. {
  194. return ResponseToGridRegion(response);
  195. }
  196. else
  197. {
  198. //m_log.InfoFormat("[SIMIAN GRID CONNECTOR]: Grid service did not find a match for region at {0},{1}",
  199. // x / Constants.RegionSize, y / Constants.RegionSize);
  200. return null;
  201. }
  202. }
  203. public GridRegion GetRegionByName(UUID scopeID, string regionName)
  204. {
  205. List<GridRegion> regions = GetRegionsByName(scopeID, regionName, 1);
  206. m_log.Debug("[SIMIAN GRID CONNECTOR]: Got " + regions.Count + " matches for region name " + regionName);
  207. if (regions.Count > 0)
  208. return regions[0];
  209. return null;
  210. }
  211. public List<GridRegion> GetRegionsByName(UUID scopeID, string name, int maxNumber)
  212. {
  213. List<GridRegion> foundRegions = new List<GridRegion>();
  214. NameValueCollection requestArgs = new NameValueCollection
  215. {
  216. { "RequestMethod", "GetScenes" },
  217. { "NameQuery", name },
  218. { "Enabled", "1" }
  219. };
  220. if (maxNumber > 0)
  221. requestArgs["MaxNumber"] = maxNumber.ToString();
  222. OSDMap response = WebUtil.PostToService(m_serverUrl, requestArgs);
  223. if (response["Success"].AsBoolean())
  224. {
  225. OSDArray array = response["Scenes"] as OSDArray;
  226. if (array != null)
  227. {
  228. for (int i = 0; i < array.Count; i++)
  229. {
  230. GridRegion region = ResponseToGridRegion(array[i] as OSDMap);
  231. if (region != null)
  232. foundRegions.Add(region);
  233. }
  234. }
  235. }
  236. return foundRegions;
  237. }
  238. public List<GridRegion> GetRegionRange(UUID scopeID, int xmin, int xmax, int ymin, int ymax)
  239. {
  240. List<GridRegion> foundRegions = new List<GridRegion>();
  241. Vector3d minPosition = new Vector3d(xmin, ymin, 0.0);
  242. Vector3d maxPosition = new Vector3d(xmax, ymax, 4096.0);
  243. NameValueCollection requestArgs = new NameValueCollection
  244. {
  245. { "RequestMethod", "GetScenes" },
  246. { "MinPosition", minPosition.ToString() },
  247. { "MaxPosition", maxPosition.ToString() },
  248. { "Enabled", "1" }
  249. };
  250. OSDMap response = WebUtil.PostToService(m_serverUrl, requestArgs);
  251. if (response["Success"].AsBoolean())
  252. {
  253. OSDArray array = response["Scenes"] as OSDArray;
  254. if (array != null)
  255. {
  256. for (int i = 0; i < array.Count; i++)
  257. {
  258. GridRegion region = ResponseToGridRegion(array[i] as OSDMap);
  259. if (region != null)
  260. foundRegions.Add(region);
  261. }
  262. }
  263. }
  264. return foundRegions;
  265. }
  266. public List<GridRegion> GetDefaultRegions(UUID scopeID)
  267. {
  268. // TODO: Allow specifying the default grid location
  269. const int DEFAULT_X = 1000 * 256;
  270. const int DEFAULT_Y = 1000 * 256;
  271. GridRegion defRegion = GetNearestRegion(new Vector3d(DEFAULT_X, DEFAULT_Y, 0.0), true);
  272. if (defRegion != null)
  273. return new List<GridRegion>(1) { defRegion };
  274. else
  275. return new List<GridRegion>(0);
  276. }
  277. public List<GridRegion> GetFallbackRegions(UUID scopeID, int x, int y)
  278. {
  279. GridRegion defRegion = GetNearestRegion(new Vector3d(x, y, 0.0), true);
  280. if (defRegion != null)
  281. return new List<GridRegion>(1) { defRegion };
  282. else
  283. return new List<GridRegion>(0);
  284. }
  285. public int GetRegionFlags(UUID scopeID, UUID regionID)
  286. {
  287. const int REGION_ONLINE = 4;
  288. NameValueCollection requestArgs = new NameValueCollection
  289. {
  290. { "RequestMethod", "GetScene" },
  291. { "SceneID", regionID.ToString() }
  292. };
  293. OSDMap response = WebUtil.PostToService(m_serverUrl, requestArgs);
  294. if (response["Success"].AsBoolean())
  295. {
  296. return response["Enabled"].AsBoolean() ? REGION_ONLINE : 0;
  297. }
  298. else
  299. {
  300. m_log.Warn("[SIMIAN GRID CONNECTOR]: Grid service did not find a match for region " + regionID + " during region flags check");
  301. return -1;
  302. }
  303. }
  304. #endregion IGridService
  305. private GridRegion GetNearestRegion(Vector3d position, bool onlyEnabled)
  306. {
  307. NameValueCollection requestArgs = new NameValueCollection
  308. {
  309. { "RequestMethod", "GetScene" },
  310. { "Position", position.ToString() },
  311. { "FindClosest", "1" }
  312. };
  313. if (onlyEnabled)
  314. requestArgs["Enabled"] = "1";
  315. OSDMap response = WebUtil.PostToService(m_serverUrl, requestArgs);
  316. if (response["Success"].AsBoolean())
  317. {
  318. return ResponseToGridRegion(response);
  319. }
  320. else
  321. {
  322. m_log.Warn("[SIMIAN GRID CONNECTOR]: Grid service did not find a match for region at " + position);
  323. return null;
  324. }
  325. }
  326. private GridRegion ResponseToGridRegion(OSDMap response)
  327. {
  328. if (response == null)
  329. return null;
  330. OSDMap extraData = response["ExtraData"] as OSDMap;
  331. if (extraData == null)
  332. return null;
  333. GridRegion region = new GridRegion();
  334. region.RegionID = response["SceneID"].AsUUID();
  335. region.RegionName = response["Name"].AsString();
  336. Vector3d minPosition = response["MinPosition"].AsVector3d();
  337. region.RegionLocX = (int)minPosition.X;
  338. region.RegionLocY = (int)minPosition.Y;
  339. Uri httpAddress = response["Address"].AsUri();
  340. region.ExternalHostName = httpAddress.Host;
  341. region.HttpPort = (uint)httpAddress.Port;
  342. region.ServerURI = extraData["ServerURI"].AsString();
  343. IPAddress internalAddress;
  344. IPAddress.TryParse(extraData["InternalAddress"].AsString(), out internalAddress);
  345. if (internalAddress == null)
  346. internalAddress = IPAddress.Any;
  347. region.InternalEndPoint = new IPEndPoint(internalAddress, extraData["InternalPort"].AsInteger());
  348. region.TerrainImage = extraData["MapTexture"].AsUUID();
  349. region.Access = (byte)extraData["Access"].AsInteger();
  350. region.RegionSecret = extraData["RegionSecret"].AsString();
  351. region.EstateOwner = extraData["EstateOwner"].AsUUID();
  352. region.Token = extraData["Token"].AsString();
  353. return region;
  354. }
  355. }
  356. }