1
0

SimianGridServiceConnector.cs 17 KB

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