1
0

IGridService.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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;
  29. using System.Collections.Generic;
  30. using System.Net;
  31. using System.Net.Sockets;
  32. using System.Reflection;
  33. using OpenSim.Framework;
  34. using OpenMetaverse;
  35. using log4net;
  36. namespace OpenSim.Services.Interfaces
  37. {
  38. public interface IGridService
  39. {
  40. /// <summary>
  41. /// Register a region with the grid service.
  42. /// </summary>
  43. /// <param name="regionInfos"> </param>
  44. /// <returns></returns>
  45. /// <exception cref="System.Exception">Thrown if region registration failed</exception>
  46. string RegisterRegion(UUID scopeID, GridRegion regionInfos);
  47. /// <summary>
  48. /// Deregister a region with the grid service.
  49. /// </summary>
  50. /// <param name="regionID"></param>
  51. /// <returns></returns>
  52. /// <exception cref="System.Exception">Thrown if region deregistration failed</exception>
  53. bool DeregisterRegion(UUID regionID);
  54. /// <summary>
  55. /// Get information about the regions neighbouring the given co-ordinates (in meters).
  56. /// </summary>
  57. /// <param name="x"></param>
  58. /// <param name="y"></param>
  59. /// <returns></returns>
  60. List<GridRegion> GetNeighbours(UUID scopeID, UUID regionID);
  61. GridRegion GetRegionByUUID(UUID scopeID, UUID regionID);
  62. GridRegion GetRegionByHandle(UUID scopeID, ulong regionhandle);
  63. /// <summary>
  64. /// Get the region at the given position (in meters)
  65. /// </summary>
  66. /// <param name="scopeID"></param>
  67. /// <param name="x"></param>
  68. /// <param name="y"></param>
  69. /// <returns></returns>
  70. GridRegion GetRegionByPosition(UUID scopeID, int x, int y);
  71. /// <summary>
  72. /// Get information about a region which exactly matches the name given.
  73. /// </summary>
  74. /// <param name="scopeID"></param>
  75. /// <param name="regionName"></param>
  76. /// <returns>Returns the region information if the name matched. Null otherwise.</returns>
  77. GridRegion GetRegionByName(UUID scopeID, string regionName);
  78. /// <summary>
  79. /// Get information about regions starting with the provided name.
  80. /// </summary>
  81. /// <param name="name">
  82. /// The name to match against.
  83. /// </param>
  84. /// <param name="maxNumber">
  85. /// The maximum number of results to return.
  86. /// </param>
  87. /// <returns>
  88. /// A list of <see cref="RegionInfo"/>s of regions with matching name. If the
  89. /// grid-server couldn't be contacted or returned an error, return null.
  90. /// </returns>
  91. List<GridRegion> GetRegionsByName(UUID scopeID, string name, int maxNumber);
  92. List<GridRegion> GetRegionRange(UUID scopeID, int xmin, int xmax, int ymin, int ymax);
  93. List<GridRegion> GetDefaultRegions(UUID scopeID);
  94. List<GridRegion> GetDefaultHypergridRegions(UUID scopeID);
  95. List<GridRegion> GetFallbackRegions(UUID scopeID, int x, int y);
  96. List<GridRegion> GetHyperlinks(UUID scopeID);
  97. /// <summary>
  98. /// Get internal OpenSimulator region flags.
  99. /// </summary>
  100. /// <remarks>
  101. /// See OpenSimulator.Framework.RegionFlags. These are not returned in the GridRegion structure -
  102. /// they currently need to be requested separately. Possibly this should change to avoid multiple service calls
  103. /// in some situations.
  104. /// </remarks>
  105. /// <returns>
  106. /// The region flags.
  107. /// </returns>
  108. /// <param name='scopeID'></param>
  109. /// <param name='regionID'></param>
  110. int GetRegionFlags(UUID scopeID, UUID regionID);
  111. Dictionary<string,object> GetExtraFeatures();
  112. }
  113. public interface IHypergridLinker
  114. {
  115. GridRegion TryLinkRegionToCoords(UUID scopeID, string mapName, int xloc, int yloc, UUID ownerID, out string reason);
  116. bool TryUnlinkRegion(string mapName);
  117. }
  118. public class GridRegion
  119. {
  120. // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  121. #pragma warning disable 414
  122. private static readonly string LogHeader = "[GRID REGION]";
  123. #pragma warning restore 414
  124. /// <summary>
  125. /// The port by which http communication occurs with the region
  126. /// </summary>
  127. public uint HttpPort { get; set; }
  128. /// <summary>
  129. /// A well-formed URI for the host region server (namely "http://" + ExternalHostName)
  130. /// </summary>
  131. public string ServerURI
  132. {
  133. get {
  134. if (!String.IsNullOrEmpty(m_serverURI)) {
  135. return m_serverURI;
  136. } else {
  137. if (HttpPort == 0)
  138. return "http://" + m_externalHostName + "/";
  139. else
  140. return "http://" + m_externalHostName + ":" + HttpPort + "/";
  141. }
  142. }
  143. set {
  144. if ( value == null)
  145. {
  146. m_serverURI = String.Empty;
  147. return;
  148. }
  149. if ( value.EndsWith("/") )
  150. {
  151. m_serverURI = value;
  152. }
  153. else
  154. {
  155. m_serverURI = value + '/';
  156. }
  157. }
  158. }
  159. protected string m_serverURI;
  160. /// <summary>
  161. /// Provides direct access to the 'm_serverURI' field, without returning a generated URL if m_serverURI is missing.
  162. /// </summary>
  163. public string RawServerURI
  164. {
  165. get { return m_serverURI; }
  166. set { m_serverURI = value; }
  167. }
  168. public string RegionName
  169. {
  170. get { return m_regionName; }
  171. set { m_regionName = value; }
  172. }
  173. protected string m_regionName = String.Empty;
  174. /// <summary>
  175. /// Region flags.
  176. /// </summary>
  177. /// <remarks>
  178. /// If not set (chiefly if a robust service is running code pre OpenSim 0.8.1) then this will be null and
  179. /// should be ignored. If you require flags information please use the separate IGridService.GetRegionFlags() call
  180. /// XXX: This field is currently ignored when used in RegisterRegion, but could potentially be
  181. /// used to set flags at this point.
  182. /// </remarks>
  183. public OpenSim.Framework.RegionFlags? RegionFlags { get; set; }
  184. protected string m_externalHostName;
  185. protected IPEndPoint m_internalEndPoint;
  186. /// <summary>
  187. /// The co-ordinate of this region in region units.
  188. /// </summary>
  189. public int RegionCoordX { get { return (int)Util.WorldToRegionLoc((uint)RegionLocX); } }
  190. /// <summary>
  191. /// The co-ordinate of this region in region units
  192. /// </summary>
  193. public int RegionCoordY { get { return (int)Util.WorldToRegionLoc((uint)RegionLocY); } }
  194. /// <summary>
  195. /// The location of this region in meters.
  196. /// DANGER DANGER! Note that this name means something different in RegionInfo.
  197. /// </summary>
  198. public int RegionLocX
  199. {
  200. get { return m_regionLocX; }
  201. set { m_regionLocX = value; }
  202. }
  203. protected int m_regionLocX;
  204. public int RegionSizeX { get; set; }
  205. public int RegionSizeY { get; set; }
  206. /// <summary>
  207. /// The location of this region in meters.
  208. /// DANGER DANGER! Note that this name means something different in RegionInfo.
  209. /// </summary>
  210. public int RegionLocY
  211. {
  212. get { return m_regionLocY; }
  213. set { m_regionLocY = value; }
  214. }
  215. protected int m_regionLocY;
  216. protected UUID m_estateOwner;
  217. public UUID EstateOwner
  218. {
  219. get { return m_estateOwner; }
  220. set { m_estateOwner = value; }
  221. }
  222. public UUID RegionID = UUID.Zero;
  223. public UUID ScopeID = UUID.Zero;
  224. public UUID TerrainImage = UUID.Zero;
  225. public UUID ParcelImage = UUID.Zero;
  226. public byte Access;
  227. public int Maturity;
  228. public string RegionSecret = string.Empty;
  229. public string Token = string.Empty;
  230. public GridRegion()
  231. {
  232. RegionSizeX = (int)Constants.RegionSize;
  233. RegionSizeY = (int)Constants.RegionSize;
  234. m_serverURI = string.Empty;
  235. }
  236. public GridRegion(uint xcell, uint ycell)
  237. {
  238. m_regionLocX = (int)Util.RegionToWorldLoc(xcell);
  239. m_regionLocY = (int)Util.RegionToWorldLoc(ycell);
  240. RegionSizeX = (int)Constants.RegionSize;
  241. RegionSizeY = (int)Constants.RegionSize;
  242. }
  243. public GridRegion(RegionInfo ConvertFrom)
  244. {
  245. m_regionName = ConvertFrom.RegionName;
  246. m_regionLocX = (int)(ConvertFrom.WorldLocX);
  247. m_regionLocY = (int)(ConvertFrom.WorldLocY);
  248. RegionSizeX = (int)ConvertFrom.RegionSizeX;
  249. RegionSizeY = (int)ConvertFrom.RegionSizeY;
  250. m_internalEndPoint = ConvertFrom.InternalEndPoint;
  251. m_externalHostName = ConvertFrom.ExternalHostName;
  252. HttpPort = ConvertFrom.HttpPort;
  253. RegionID = ConvertFrom.RegionID;
  254. ServerURI = ConvertFrom.ServerURI;
  255. TerrainImage = ConvertFrom.RegionSettings.TerrainImageID;
  256. ParcelImage = ConvertFrom.RegionSettings.ParcelImageID;
  257. Access = ConvertFrom.AccessLevel;
  258. Maturity = ConvertFrom.RegionSettings.Maturity;
  259. RegionSecret = ConvertFrom.regionSecret;
  260. EstateOwner = ConvertFrom.EstateSettings.EstateOwner;
  261. }
  262. public GridRegion(GridRegion ConvertFrom)
  263. {
  264. m_regionName = ConvertFrom.RegionName;
  265. RegionFlags = ConvertFrom.RegionFlags;
  266. m_regionLocX = ConvertFrom.RegionLocX;
  267. m_regionLocY = ConvertFrom.RegionLocY;
  268. RegionSizeX = ConvertFrom.RegionSizeX;
  269. RegionSizeY = ConvertFrom.RegionSizeY;
  270. m_internalEndPoint = ConvertFrom.InternalEndPoint;
  271. m_externalHostName = ConvertFrom.ExternalHostName;
  272. HttpPort = ConvertFrom.HttpPort;
  273. RegionID = ConvertFrom.RegionID;
  274. ServerURI = ConvertFrom.ServerURI;
  275. TerrainImage = ConvertFrom.TerrainImage;
  276. ParcelImage = ConvertFrom.ParcelImage;
  277. Access = ConvertFrom.Access;
  278. Maturity = ConvertFrom.Maturity;
  279. RegionSecret = ConvertFrom.RegionSecret;
  280. EstateOwner = ConvertFrom.EstateOwner;
  281. }
  282. public GridRegion(Dictionary<string, object> kvp)
  283. {
  284. if (kvp.ContainsKey("uuid"))
  285. RegionID = new UUID((string)kvp["uuid"]);
  286. if (kvp.ContainsKey("locX"))
  287. RegionLocX = Convert.ToInt32((string)kvp["locX"]);
  288. if (kvp.ContainsKey("locY"))
  289. RegionLocY = Convert.ToInt32((string)kvp["locY"]);
  290. if (kvp.ContainsKey("sizeX"))
  291. RegionSizeX = Convert.ToInt32((string)kvp["sizeX"]);
  292. else
  293. RegionSizeX = (int)Constants.RegionSize;
  294. if (kvp.ContainsKey("sizeY"))
  295. RegionSizeY = Convert.ToInt32((string)kvp["sizeY"]);
  296. else
  297. RegionSizeX = (int)Constants.RegionSize;
  298. if (kvp.ContainsKey("regionName"))
  299. RegionName = (string)kvp["regionName"];
  300. if (kvp.ContainsKey("access"))
  301. {
  302. byte access = Convert.ToByte((string)kvp["access"]);
  303. Access = access;
  304. Maturity = (int)Util.ConvertAccessLevelToMaturity(access);
  305. }
  306. if (kvp.ContainsKey("flags") && kvp["flags"] != null)
  307. RegionFlags = (OpenSim.Framework.RegionFlags?)Convert.ToInt32((string)kvp["flags"]);
  308. if (kvp.ContainsKey("serverIP"))
  309. {
  310. //int port = 0;
  311. //Int32.TryParse((string)kvp["serverPort"], out port);
  312. //IPEndPoint ep = new IPEndPoint(IPAddress.Parse((string)kvp["serverIP"]), port);
  313. ExternalHostName = (string)kvp["serverIP"];
  314. }
  315. else
  316. ExternalHostName = "127.0.0.1";
  317. if (kvp.ContainsKey("serverPort"))
  318. {
  319. Int32 port = 0;
  320. Int32.TryParse((string)kvp["serverPort"], out port);
  321. InternalEndPoint = new IPEndPoint(IPAddress.Parse("0.0.0.0"), port);
  322. }
  323. if (kvp.ContainsKey("serverHttpPort"))
  324. {
  325. UInt32 port = 0;
  326. UInt32.TryParse((string)kvp["serverHttpPort"], out port);
  327. HttpPort = port;
  328. }
  329. if (kvp.ContainsKey("serverURI"))
  330. ServerURI = (string)kvp["serverURI"];
  331. if (kvp.ContainsKey("regionMapTexture"))
  332. UUID.TryParse((string)kvp["regionMapTexture"], out TerrainImage);
  333. if (kvp.ContainsKey("parcelMapTexture"))
  334. UUID.TryParse((string)kvp["parcelMapTexture"], out ParcelImage);
  335. if (kvp.ContainsKey("regionSecret"))
  336. RegionSecret =(string)kvp["regionSecret"];
  337. if (kvp.ContainsKey("owner_uuid"))
  338. EstateOwner = new UUID(kvp["owner_uuid"].ToString());
  339. if (kvp.ContainsKey("Token"))
  340. Token = kvp["Token"].ToString();
  341. // m_log.DebugFormat("{0} New GridRegion. id={1}, loc=<{2},{3}>, size=<{4},{5}>",
  342. // LogHeader, RegionID, RegionLocX, RegionLocY, RegionSizeX, RegionSizeY);
  343. }
  344. public Dictionary<string, object> ToKeyValuePairs()
  345. {
  346. Dictionary<string, object> kvp = new Dictionary<string, object>();
  347. kvp["uuid"] = RegionID.ToString();
  348. kvp["locX"] = RegionLocX.ToString();
  349. kvp["locY"] = RegionLocY.ToString();
  350. kvp["sizeX"] = RegionSizeX.ToString();
  351. kvp["sizeY"] = RegionSizeY.ToString();
  352. kvp["regionName"] = RegionName;
  353. if (RegionFlags != null)
  354. kvp["flags"] = ((int)RegionFlags).ToString();
  355. kvp["serverIP"] = ExternalHostName; //ExternalEndPoint.Address.ToString();
  356. kvp["serverHttpPort"] = HttpPort.ToString();
  357. kvp["serverURI"] = ServerURI;
  358. kvp["serverPort"] = InternalEndPoint.Port.ToString();
  359. kvp["regionMapTexture"] = TerrainImage.ToString();
  360. kvp["parcelMapTexture"] = ParcelImage.ToString();
  361. kvp["access"] = Access.ToString();
  362. kvp["regionSecret"] = RegionSecret;
  363. kvp["owner_uuid"] = EstateOwner.ToString();
  364. kvp["Token"] = Token.ToString();
  365. // Maturity doesn't seem to exist in the DB
  366. return kvp;
  367. }
  368. #region Definition of equality
  369. /// <summary>
  370. /// Define equality as two regions having the same, non-zero UUID.
  371. /// </summary>
  372. public bool Equals(GridRegion region)
  373. {
  374. if ((object)region == null)
  375. return false;
  376. // Return true if the non-zero UUIDs are equal:
  377. return (RegionID != UUID.Zero) && RegionID.Equals(region.RegionID);
  378. }
  379. public override bool Equals(Object obj)
  380. {
  381. if (obj == null)
  382. return false;
  383. return Equals(obj as GridRegion);
  384. }
  385. public override int GetHashCode()
  386. {
  387. return RegionID.GetHashCode() ^ TerrainImage.GetHashCode() ^ ParcelImage.GetHashCode();
  388. }
  389. #endregion
  390. /// <value>
  391. /// This accessor can throw all the exceptions that Dns.GetHostAddresses can throw.
  392. ///
  393. /// XXX Isn't this really doing too much to be a simple getter, rather than an explict method?
  394. /// </value>
  395. public IPEndPoint ExternalEndPoint
  396. {
  397. get { return Util.getEndPoint(m_externalHostName, m_internalEndPoint.Port); }
  398. }
  399. public string ExternalHostName
  400. {
  401. get { return m_externalHostName; }
  402. set { m_externalHostName = value; }
  403. }
  404. public IPEndPoint InternalEndPoint
  405. {
  406. get { return m_internalEndPoint; }
  407. set { m_internalEndPoint = value; }
  408. }
  409. public ulong RegionHandle
  410. {
  411. get { return Util.UIntsToLong((uint)RegionLocX, (uint)RegionLocY); }
  412. }
  413. }
  414. }