SimianGridServiceConnector.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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 GetRegionByName(UUID scopeID, string regionName)
  215. {
  216. List<GridRegion> regions = GetRegionsByName(scopeID, regionName, 1);
  217. m_log.Debug("[SIMIAN GRID CONNECTOR]: Got " + regions.Count + " matches for region name " + regionName);
  218. if (regions.Count > 0)
  219. return regions[0];
  220. return null;
  221. }
  222. public List<GridRegion> GetRegionsByName(UUID scopeID, string name, int maxNumber)
  223. {
  224. List<GridRegion> foundRegions = new List<GridRegion>();
  225. NameValueCollection requestArgs = new NameValueCollection
  226. {
  227. { "RequestMethod", "GetScenes" },
  228. { "NameQuery", name },
  229. { "Enabled", "1" }
  230. };
  231. if (maxNumber > 0)
  232. requestArgs["MaxNumber"] = maxNumber.ToString();
  233. // m_log.DebugFormat("[SIMIAN GRID CONNECTOR] request regions with name {0}",name);
  234. OSDMap response = SimianGrid.PostToService(m_ServerURI, requestArgs);
  235. if (response["Success"].AsBoolean())
  236. {
  237. // m_log.DebugFormat("[SIMIAN GRID CONNECTOR] found regions with name {0}",name);
  238. OSDArray array = response["Scenes"] as OSDArray;
  239. if (array != null)
  240. {
  241. for (int i = 0; i < array.Count; i++)
  242. {
  243. GridRegion region = ResponseToGridRegion(array[i] as OSDMap);
  244. if (region != null)
  245. foundRegions.Add(region);
  246. }
  247. }
  248. }
  249. return foundRegions;
  250. }
  251. public List<GridRegion> GetRegionRange(UUID scopeID, int xmin, int xmax, int ymin, int ymax)
  252. {
  253. List<GridRegion> foundRegions = new List<GridRegion>();
  254. Vector3d minPosition = new Vector3d(xmin, ymin, 0.0);
  255. Vector3d maxPosition = new Vector3d(xmax, ymax, Constants.RegionHeight);
  256. NameValueCollection requestArgs = new NameValueCollection
  257. {
  258. { "RequestMethod", "GetScenes" },
  259. { "MinPosition", minPosition.ToString() },
  260. { "MaxPosition", maxPosition.ToString() },
  261. { "Enabled", "1" }
  262. };
  263. //m_log.DebugFormat("[SIMIAN GRID CONNECTOR] request regions by range {0} to {1}",minPosition.ToString(),maxPosition.ToString());
  264. OSDMap response = SimianGrid.PostToService(m_ServerURI, requestArgs);
  265. if (response["Success"].AsBoolean())
  266. {
  267. OSDArray array = response["Scenes"] as OSDArray;
  268. if (array != null)
  269. {
  270. for (int i = 0; i < array.Count; i++)
  271. {
  272. GridRegion region = ResponseToGridRegion(array[i] as OSDMap);
  273. if (region != null)
  274. foundRegions.Add(region);
  275. }
  276. }
  277. }
  278. return foundRegions;
  279. }
  280. public List<GridRegion> GetDefaultRegions(UUID scopeID)
  281. {
  282. // TODO: Allow specifying the default grid location
  283. const int DEFAULT_X = 1000 * 256;
  284. const int DEFAULT_Y = 1000 * 256;
  285. GridRegion defRegion = GetNearestRegion(new Vector3d(DEFAULT_X, DEFAULT_Y, 0.0), true);
  286. if (defRegion != null)
  287. return new List<GridRegion>(1) { defRegion };
  288. else
  289. return new List<GridRegion>(0);
  290. }
  291. public List<GridRegion> GetDefaultHypergridRegions(UUID scopeID)
  292. {
  293. // TODO: Allow specifying the default grid location
  294. return GetDefaultRegions(scopeID);
  295. }
  296. public List<GridRegion> GetFallbackRegions(UUID scopeID, int x, int y)
  297. {
  298. GridRegion defRegion = GetNearestRegion(new Vector3d(x, y, 0.0), true);
  299. if (defRegion != null)
  300. return new List<GridRegion>(1) { defRegion };
  301. else
  302. return new List<GridRegion>(0);
  303. }
  304. public List<GridRegion> GetHyperlinks(UUID scopeID)
  305. {
  306. List<GridRegion> foundRegions = new List<GridRegion>();
  307. NameValueCollection requestArgs = new NameValueCollection
  308. {
  309. { "RequestMethod", "GetScenes" },
  310. { "HyperGrid", "true" },
  311. { "Enabled", "1" }
  312. };
  313. OSDMap response = SimianGrid.PostToService(m_ServerURI, requestArgs);
  314. if (response["Success"].AsBoolean())
  315. {
  316. // m_log.DebugFormat("[SIMIAN GRID CONNECTOR] found regions with name {0}",name);
  317. OSDArray array = response["Scenes"] as OSDArray;
  318. if (array != null)
  319. {
  320. for (int i = 0; i < array.Count; i++)
  321. {
  322. GridRegion region = ResponseToGridRegion(array[i] as OSDMap);
  323. if (region != null)
  324. foundRegions.Add(region);
  325. }
  326. }
  327. }
  328. return foundRegions;
  329. }
  330. public int GetRegionFlags(UUID scopeID, UUID regionID)
  331. {
  332. NameValueCollection requestArgs = new NameValueCollection
  333. {
  334. { "RequestMethod", "GetScene" },
  335. { "SceneID", regionID.ToString() }
  336. };
  337. m_log.DebugFormat("[SIMIAN GRID CONNECTOR] request region flags for {0}",regionID.ToString());
  338. OSDMap response = SimianGrid.PostToService(m_ServerURI, requestArgs);
  339. if (response["Success"].AsBoolean())
  340. {
  341. OSDMap extraData = response["ExtraData"] as OSDMap;
  342. int enabled = response["Enabled"].AsBoolean() ? (int)OpenSim.Framework.RegionFlags.RegionOnline : 0;
  343. int hypergrid = extraData["HyperGrid"].AsBoolean() ? (int)OpenSim.Framework.RegionFlags.Hyperlink : 0;
  344. int flags = enabled | hypergrid;
  345. m_log.DebugFormat("[SGGC] enabled - {0} hg - {1} flags - {2}", enabled, hypergrid, flags);
  346. return flags;
  347. }
  348. else
  349. {
  350. m_log.Warn("[SIMIAN GRID CONNECTOR]: Grid service did not find a match for region " + regionID + " during region flags check");
  351. return -1;
  352. }
  353. }
  354. public Dictionary<string, object> GetExtraFeatures()
  355. {
  356. /// See SimulatorFeaturesModule - Need to get map, search and destination guide
  357. Dictionary<string, object> extraFeatures = new Dictionary<string, object>();
  358. return extraFeatures;
  359. }
  360. #endregion IGridService
  361. private GridRegion GetNearestRegion(Vector3d position, bool onlyEnabled)
  362. {
  363. NameValueCollection requestArgs = new NameValueCollection
  364. {
  365. { "RequestMethod", "GetScene" },
  366. { "Position", position.ToString() },
  367. { "FindClosest", "1" }
  368. };
  369. if (onlyEnabled)
  370. requestArgs["Enabled"] = "1";
  371. OSDMap response = SimianGrid.PostToService(m_ServerURI, requestArgs);
  372. if (response["Success"].AsBoolean())
  373. {
  374. return ResponseToGridRegion(response);
  375. }
  376. else
  377. {
  378. m_log.Warn("[SIMIAN GRID CONNECTOR]: Grid service did not find a match for region at " + position);
  379. return null;
  380. }
  381. }
  382. private GridRegion ResponseToGridRegion(OSDMap response)
  383. {
  384. if (response == null)
  385. return null;
  386. OSDMap extraData = response["ExtraData"] as OSDMap;
  387. if (extraData == null)
  388. return null;
  389. GridRegion region = new GridRegion();
  390. region.RegionID = response["SceneID"].AsUUID();
  391. region.RegionName = response["Name"].AsString();
  392. Vector3d minPosition = response["MinPosition"].AsVector3d();
  393. Vector3d maxPosition = response["MaxPosition"].AsVector3d();
  394. region.RegionLocX = (int)minPosition.X;
  395. region.RegionLocY = (int)minPosition.Y;
  396. region.RegionSizeX = (int)maxPosition.X - (int)minPosition.X;
  397. region.RegionSizeY = (int)maxPosition.Y - (int)minPosition.Y;
  398. if ( ! extraData["HyperGrid"] ) {
  399. Uri httpAddress = response["Address"].AsUri();
  400. region.ExternalHostName = httpAddress.Host;
  401. region.HttpPort = (uint)httpAddress.Port;
  402. IPAddress internalAddress;
  403. IPAddress.TryParse(extraData["InternalAddress"].AsString(), out internalAddress);
  404. if (internalAddress == null)
  405. internalAddress = IPAddress.Any;
  406. region.InternalEndPoint = new IPEndPoint(internalAddress, extraData["InternalPort"].AsInteger());
  407. region.TerrainImage = extraData["MapTexture"].AsUUID();
  408. region.Access = (byte)extraData["Access"].AsInteger();
  409. region.RegionSecret = extraData["RegionSecret"].AsString();
  410. region.EstateOwner = extraData["EstateOwner"].AsUUID();
  411. region.Token = extraData["Token"].AsString();
  412. region.ServerURI = extraData["ServerURI"].AsString();
  413. } else {
  414. region.ServerURI = response["Address"];
  415. }
  416. return region;
  417. }
  418. }
  419. }