SimianGridServiceConnector.cs 20 KB

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