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